Re: novice: mapcan use?



Bernd Schmitt wrote:

Ok, I will do so (even if I do not understand the difference between '(1 2) and (list 1 2) right now).

All programming languages have literal values. It's so common that it's hard to understand why that's something special. But it really is.


For example, when you bind 42 to a variable, the "42" is a value that you have literally written as part of your source code:

(setq x 42)

Whereas when you increment x afterwards you will have a number stored in x that you have never written down:

(setq x (+ x 1))
x => 43

The latter output ("42") was not part of your program source code.

Lisp is special in many ways, including the fact that anything can be made part of some source code (as Kent already explained). When you type in '(1 2), the parser (called reader in Lisp terminology) parses the list consisting of the elements 1 and 2, and makes it part of your source code. The quote ("'") takes care that this will be treated as a literal datum in your program.

Now think about it: Assume you would change the contents of that list at runtime - this would theoretically mean that you would change the source code, since that list is part of that source code! That doesn't make sense, so ANSI Common Lisp states that the result is undefined when you try to do this.

On the other hand, list is a function that takes some elements and creates a list at runtime. Since that list is not part of some source code, it is perfectly legal to change its contents. So, as an analogue, (setq l '(1 2)) is similar to (setq x 42) whereas (setq l (list 1 2)) is similar to (setq x (+ x 1)), that is, the former value is taken from the source code whereas the latter value is generated at runtime.

I hope this helps.


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: Parsing s-expression trees
    ... expressions --> lisp. ... the irregularity in its often cited regular syntax. ... programers can pretty much tell what piece of source code ... Lisp relies on a regular nested syntax. ...
    (comp.lang.lisp)
  • Re: Regarding Lisp/Scheme
    ... I would say if you're planning on selling a game written in lisp then ... games without the source code. ... You can compile without it, but the problem is, the Cygwin toolchain has many stacked libraries that are built with it by default. ... MinGW environments are damn difficult to reproduce, there's so much build labor involved because it's the *Minimalist* GNU for Windows. ...
    (comp.lang.lisp)
  • Re: When is Xah going to ease up on Emacs...
    ... Fundamental Problems of Lisp ... However, the lisp syntax has ... it allows transformation of the source code by a simple ... what this means is that lisp's lists is ...
    (comp.lang.lisp)
  • Re: Purpose of the LAMBDA keyword?
    ... Functions are not lists which encode ... source code (i.e. a list structure beginning with LAMBDA in the case ... is closer to the spirit of Lisp, and that it's true in lambda calculus. ...
    (comp.lang.scheme)
  • Re: Lisp Optimised Workstation
    ... > for Lisp were the Lisp machines of the early 1990's, ... the unsurpassed efficacy of their environments... ... source code to enable all kinds of experimentations. ... For developers willing to dedicate some time I think it is best ...
    (comp.lang.lisp)