From: synthespian
Subject: mapcar mistery
Date: 
Message-ID: <pan.2002.09.07.04.34.08.736497.12216@debian-rs.org>
Hi lispers --

 What is this mapcar mistery?


* (mapcar #'list '(1 2) '(3 4))
((1 3) (2 4))

* (mapcar #'append '((1 2)) '((3 4)))
((1 2 3 4))

* (append '(1 2) '(3 4))

(1 2 3 4)
* (mapcar #'append '(1 2) '(3 4))


Error in function APPEND:  1 is not a list.

Restarts:
  0: [ABORT] Return to Top-Level.

Debug  (type H for help)

(APPEND 1 3)
0] 0

That was in CLISP, same in CMUCL.
What explains mapcar's behavior with append?

Thanks

henry


_________________________________________________________________
Micro$oft-Free Human         100% Debian GNU/Linux
     KMFMS              "Bring the genome to the people!
		···········@debian-rs.org
www.debian.org - www.debian-br.cipsga.org.br - www.debian-rs.org

From: Jim White
Subject: Re: mapcar mistery
Date: 
Message-ID: <3D79B42A.2060400@pagesmiths.com>
synthespian wrote:
>  What is this mapcar mistery?

The CL spec is here:

http://www.lisp.org/HyperSpec/Body/fun_mapccm_ma_istcm_mapcon.html#mapcar

http://www.lisp.org/HyperSpec/Body/fun_append.html#append

> * (mapcar #'append '(1 2) '(3 4))
> 
> 
> Error in function APPEND:  1 is not a list.

As is plain from the definition of #mapcar, the first thing that is 
gonna happen is that the first element of each of the lists is gonna be 
applied to #append, that is:

(append 1 3)

Obviously that isn't gonna work because '1 is not a proper list.

You could do this:

(mapcar #'append '((1) (2)) '(3 4))

to get this:

((1 . 3) (2 . 4))

jim
From: Kaz Kylheku
Subject: Re: mapcar mistery
Date: 
Message-ID: <cf333042.0209071918.473cbda9@posting.google.com>
synthespian <···········@debian-rs.org> wrote in message news:<····································@debian-rs.org>...
> Hi lispers --
> 
>  What is this mapcar mistery?
> 
> 
> * (mapcar #'list '(1 2) '(3 4))
> ((1 3) (2 4))
> 
> * (mapcar #'append '((1 2)) '((3 4)))
> ((1 2 3 4))
> 
> * (append '(1 2) '(3 4))
> 
> (1 2 3 4)
> * (mapcar #'append '(1 2) '(3 4))

The reasons why this fails should be clear by contrasting
with the previous working example. The append function
requires lists. Here, the mapcar wants to effectively
do (funcall #'append 1 2) and (funcall #'append 3 4)
and return a list of these results. You can't append
1 and 2 because these are atoms, not lists.

In the previous example, mapcar calls append just once
with the arguments (1 2) (3 4). The result is the list
is (1 2 3 4), and mapcar makes a list which contains
this list as an element, hence ((1 2 3 4)).
From: Vijay L
Subject: Re: mapcar mistery
Date: 
Message-ID: <1eaf81aa.0209090134.68a9d38c@posting.google.com>
synthespian <···········@debian-rs.org> wrote in message news:<····································@debian-rs.org>...
> Hi lispers --
> 
>  What is this mapcar mistery?
> 
> 
> * (mapcar #'list '(1 2) '(3 4))
> ((1 3) (2 4))
> 
This is not really a reply to your question, since it has already been
answered by people for more proficient in Lisp than me. The example
you have taken above is given in "Lisp as an Alternative to Java" by
Erann Gat, Jet Propulsion Laboratory, California Institute of
Technology Pasadena, CA 91109 "g a t @ j p l . n a s a . g o v"
(remove spaces, I think he could be spammed by my printing his address
here).
In it he says:
"Lisp has powerful abstraction facilities like firstclass functions
that allow complex algorithms to be written in a very few lines of
code. A classic example is the following code for transposing
transposing a matrix represented as a list of lists:
(defun transpose (m) (apply 'mapcar 'list m))"

Just thought you should know :)

Thanks,

Vijay L
From: Frank A. Adrian
Subject: Re: mapcar mistery
Date: 
Message-ID: <Fy3h9.79$tz5.29356@news.uswest.net>
synthespian wrote:

> Hi lispers --
> 
>  What is this mapcar mistery?
> * (mapcar #'append '(1 2) '(3 4))
> 
> 
> Error in function APPEND:  1 is not a list.
> 
> That was in CLISP, same in CMUCL.
> What explains mapcar's behavior with append?

mapcar applies a function to the elements of a list (or if supplied with 
multiple lists, takes elements from each list and applies the function to 
them).  The function called in this way will first try to apply append to 
the atoms 1 and 3, and since 1 and 3 are atoms, append will puke.

I'd also recommend checking out the Common Lisp Hyperspec or reading Paul 
Graham's ANSI Common Lisp before proceeding further.  You seem to be trying 
to use the language in a proper fashion, but appear to have certain 
shortcomings in fundamental knowledge (such as the fact that there are real 
differences between ((1 2) (3 4)) and (1 2) followed in an argument list by 
(3 4).  Hope this helps...

faa
From: synthespian
Subject: Re: mapcar mistery
Date: 
Message-ID: <pan.2002.09.15.15.30.32.683586.8980@debian-rs.org>
On Sun, 15 Sep 2002 14:36:37 -0300, Frank A. Adrian wrote:

> synthespian wrote:
> 
>> Hi lispers --
>> 
>>  What is this mapcar mistery?
>> * (mapcar #'append '(1 2) '(3 4))
>> 
>> 
>> Error in function APPEND:  1 is not a list.
>> 
>> That was in CLISP, same in CMUCL.
>> What explains mapcar's behavior with append?
> 
> mapcar applies a function to the elements of a list (or if supplied with
> multiple lists, takes elements from each list and applies the function
> to them).  The function called in this way will first try to apply
> append to the atoms 1 and 3, and since 1 and 3 are atoms, append will
> puke.
> 
> I'd also recommend checking out the Common Lisp Hyperspec or reading
> Paul Graham's ANSI Common Lisp before proceeding further.  You seem to
> be trying to use the language in a proper fashion, but appear to have
> certain shortcomings in fundamental knowledge (such as the fact that
> there are real differences between ((1 2) (3 4)) and (1 2) followed in
> an argument list by (3 4).  Hope this helps...
> 
> faa

	In fact, I misread Paul Graham (p. 40): "(...) mapcar, which takes a
function and on or more lists"...But then, of course, he says "and
returns the function to elements taken from each list". To elements taken
from each list is the key to understanding my mystake and your
explanation. A pitfall.
	Thank you for your explanation. It was very clear.

	Regs
	Henry

_________________________________________________________________
Micro$oft-Free Human         100% Debian GNU/Linux
     KMFMS              "Bring the genome to the people!
www.debian.org - www.debian-br.cipsga.org.br - www.debian-rs.org
From: synthespian
Subject: Re: mapcar mistery
Date: 
Message-ID: <pan.2002.09.15.18.32.29.784156.22488@debian-rs.org>
On Sun, 15 Sep 2002 15:30:35 -0300, synthespian wrote:

> On Sun, 15 Sep 2002 14:36:37 -0300, Frank A. Adrian wrote:
> 
>> synthespian wrote:
>> 
>>> Hi lispers --
>>> 
>>>  What is this mapcar mistery?
>>> * (mapcar #'append '(1 2) '(3 4))
>>> 
>>> 
>>> Error in function APPEND:  1 is not a list.
>>> 
>>> That was in CLISP, same in CMUCL.
>>> What explains mapcar's behavior with append?
>> 
>> mapcar applies a function to the elements of a list (or if supplied
>> with multiple lists, takes elements from each list and applies the
>> function to them).  The function called in this way will first try to
>> apply append to the atoms 1 and 3, and since 1 and 3 are atoms, append
>> will puke.
>> 
>> I'd also recommend checking out the Common Lisp Hyperspec or reading
>> Paul Graham's ANSI Common Lisp before proceeding further.  You seem to
>> be trying to use the language in a proper fashion, but appear to have
>> certain shortcomings in fundamental knowledge (such as the fact that
>> there are real differences between ((1 2) (3 4)) and (1 2) followed in
>> an argument list by (3 4).  Hope this helps...
>> 
>> faa
> 
> 	In fact, I misread Paul Graham (p. 40): "(...) mapcar, which takes a
> function and on or more lists"...

	In fact, I also mistyped him "on one or more lists"

But then, of course, he says "and
> returns the function to elements taken from each list". To elements
> taken from each list is the key to understanding my mystake and your
> explanation. A pitfall.
> 	Thank you for your explanation. It was very clear.
> 
> 	Regs
> 	Henry
> 
_________________________________________________________________
Micro$oft-Free Human         100% Debian GNU/Linux
     KMFMS              "Bring the genome to the people!
www.debian.org - www.debian-br.cipsga.org.br - www.debian-rs.org