Re: novice: mapcan use?
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Sun, 28 Aug 2005 21:18:34 +0200
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 ++++ .
- Follow-Ups:
- Re: novice: mapcan use?
- From: Kenny Tilton
- Re: novice: mapcan use?
- From: David Steuber
- Re: novice: mapcan use?
- From: Bernd Schmitt
- Re: novice: mapcan use?
- References:
- novice: mapcan use?
- From: Bernd Schmitt
- Re: novice: mapcan use?
- From: Pascal Costanza
- Re: novice: mapcan use?
- From: Bernd Schmitt
- novice: mapcan use?
- Prev by Date: Re: Beyond CL?
- Next by Date: Re: novice: mapcan use?
- Previous by thread: Re: novice: mapcan use?
- Next by thread: Re: novice: mapcan use?
- Index(es):
Relevant Pages
|
|