Re: novice: mapcan use?



Hello Pascal,

Pascal Costanza wrote:
Bernd Schmitt wrote:


What you have in mind is that variables are updated to see new values. However, destructive functions in Lisp are allowed _not_ to update the corresponding variables. (There are good reasons for this.) So in general, you still need to use assignment via setq or setf to store the results in the right places. That's why it in general also doesn't really matter whether you use the destructive or non-destructive functions, except for optimization purposes.

As far as i understand (is there no apprev. for this AFAIU? I have the feeling I will use this term frequently ...), nconc is a destructive version of append, isnt't it? Nconc does change variables, if i understand the following in the right way:


CL-USER> (setq x '(1 2) y '(3 4))
(3 4)
CL-USER> (nconc x y)
(1 2 3 4)
CL-USER> x
(1 2 3 4)

So, when do I know, whether a variable can be used to catch the result of a destructive function (or was my example implementation dependent)?



[...] As mapcan iterates over the input list, it generates an intermediate list for the result, and that list is destructively changed.

Hm. So the result of CL-USER> (nconc '(1 2) '(3 4)) (1 2 3 4) is intermediate as well, right?



However, mapcan is somewhat old-fashioned. It's especially odd that the filtering function has to return the elements wrapped in lists.

It's clearer (IMHO) to use, for example, the LOOP macro instead:

(loop for x in '(a 2 b c 3 4 d 5)
      when (numberp x)
      collect x)

Ok. Loop is 90 pages away ... But as i see in the index, there is no collect ...




> It seems to me that you are using an "old-fashioned" book. It may be
> better to go for something more recent, like Peter Seibel's "Practical
> Common Lisp", David Lamkins's "Successful Lisp" or Peter Norvig's
> "Paradigms of Artificial Intelligence Programming".

The reason why i read this book ("Programmieren in Common Lisp" O. Mayer) is, that I need a basic vocabulary build up in my mother language (german) to understand english computer texts (i am no computer science student, just a hobby coder in c/c++ tcl/tk). Otherwise things like "side-effect", "slot" ... would have no/wrong meaning to me. That is the main reason why somehow can not read pcl now.




Thank you for your reply, Bernd






-- https://gna.org/projects/mipisti - (microscope) picture stitching T_a_k_e__c_a_r_e__o_f__y_o_u_r__R_I_G_H_T_S. P_r_e_v_e_n_t__L_O_G_I_C--P_A_T_E_N_T_S http://www.ffii.org, http://www.nosoftwarepatents.org .



Relevant Pages

  • Re: novice: mapcan use?
    ... destructive functions in Lisp are allowed _not_ to update the corresponding variables. ... Nconc does change variables, if i ... Don't use the destructive functions as your regular tools, and better forget completely about them in the beginning. ... The reason why i read this book is, that I need a basic vocabulary build up in my mother language to understand english computer texts. ...
    (comp.lang.lisp)
  • Re: Do people dislike parentheses or the conceptual mismatch with trees?
    ... "Could this be the reason why people dislike s-expressions". ... Lisp feel as they do. ... most people is that I frankly don't think most people use trees. ... for example compared to lists. ...
    (comp.lang.lisp)
  • Re: novice: mapcan use?
    ... In the beginning, it's better to learn about the higher level ways to express solutions in Lisp, and only care about optimization when you detect overheads in your programs that you want to avoid. ... However, destructive functions are "destructive" to the actual data structures, which means that they are allowed to side effect them - for example delete elements in lists. ...
    (comp.lang.lisp)
  • Re: Question about the sort function
    ... I am very familiar with lisp and I ... detail about sort from now on. ... That is the reason I posted this question on the user group. ... can pass variables differently or lists are implemented differently. ...
    (comp.lang.lisp)
  • Re: Are we close to a Lisp boom ?
    ... As a newcomer lisp is quite confusing: ... another reason to not go back to java. ... The IDE for those is usually SLIME/Emacs. ... write either Windows software or you want a 'cheap' ...
    (comp.lang.lisp)