Re: novice: mapcan use?



Bernd Schmitt wrote:
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?

IIUC - If I understand correctly...

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)?

It works just accidentally in your example. You should _always_ say this:

(setq x (nconc x y))

....as you would say...

(setq x (append x y))

For example, try this:

(setq x '() y '(1 2))
(nconc x y)

And, additionally, don't use nconc on literal lists, as you did in your example. Instead, generate the lists at runtime:

(setq x (list 1 2) y (list 3 4))
(setq x (nconc x y))

Otherwise, you'll get undefined behavior.

It cannot be stressed enough: Don't use the destructive functions as your regular tools, and better forget completely about them in the beginning. They should be used exactly like the non-destructive versions, and only ever for optimizations. There's no other advantage in using them, and you should only optimize those parts of your code that you have measured before so that you know that optimizations actually buys you anything. Everything else is a waste of your (valuable!) development time.

[...] 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?

It should be regarded as intermediate, and needs to be bound or assigned to something if you want to get hold of it.


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.

OK, I have checked that book at amazon.de, and it seems to be indeed somewhat old-fashioned. There are seemingly not enough good books about Common Lisp in German. I am aware of a German translation of Paul Grahamn's "ANSI Common Lisp" - maybe you can find a copy at ebay.


Somewhat should translate Peter's book into German... ;)


Viele Gruesse, Pascal

--
OOPSLA'05 tutorial on generic functions & the CLOS Metaobject Protocol
++++ see http://p-cos.net/oopsla05-tutorial.html for more details ++++
.



Relevant Pages

  • Re: novice: mapcan use?
    ... destructive functions in Lisp are allowed _not_ to update the corresponding variables. ... filtering function has to return the elements wrapped in lists. ... 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: novice: mapcan use?
    ... Understanding when one can get away with it is not so hard, and requires no more than the same understanding one needs anyway to program in Lisp. ... It's better to get the whole picture that Lisp isn't "just" about list processing, but also has other data structures that are probably more important for creating efficient programs than destructive functions. ...
    (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: very newbie
    ... >>(defun aggiungi (x L) ... > lisp is difficult but with a community like this become simpler. ... to aggiungi a variable containing the address of the first link in a ... destructive functions, one must often capture the result returned by the ...
    (comp.lang.lisp)