Subject: Re: A short course in LISP, LAMBDA, QUOTE, MAPCAR...
Date: Mon, 04 Nov 1996 00:42:24 GMT
From: (Reini Urban)
Organization: TU Graz, Architecture & X-Ray
Newsgroups: comp.cad.autocad,
Message-ID: <>
References: 1,

Lu <> showed Vladimirs explanation (thanks !): But I've a minor error, that confuses me a bit:
>P.P.S. Have you got the great TRANSPOSE from Douglas Wilson's
> LISP PUZZLE? It's here one more time:
> (defun transpose(l)(apply 'mapcar (cons 'list l)))
> How does this works? (cons 'list l) returns
> (list 'list (car l)(cadr l) etc....), then apply
> calls mapcar with all that's in the list as arguments,
> (mapcar 'list (car l)(cadr l)...) !!!

This is the complete explanation:

If l is eg: ((1 2 3)(5 6 7)) than (cons 'list l) returns (list (1 2 3)(5 6 7)), it adds the list symbol to the list. first is the symbol list then the two points. list will be the function for mapcar.

(mapcar 'list '(1 2 3) '(5 6 7)) would return the required result => ((1 5)(2 6)(3 7)) mapcar applies the function to all the list entries starting with the first: (list 1 5) then (list 2 6) and so on...

but how to put mapcar to the front of the list?

You could do it like that: (eval (cons 'mapcar (list (1 2 3)(5 6 7))))

or with apply its the same: (apply 'mapcar (list (1 2 3)(5 6 7)))

because apply is defined as below (defun apply (fun lst) (eval (cons fun lst)))

okay: now you've got your function: (mapcar 'list '(1 2 3) '(5 6 7)) which returns the result:

BTW: In simple AutoLISP you dont need apply at all. Apply is a standard Lisp function because in standard lisp or common lisp functions are not symbols. Its easier for a compiler or optimizer if you know what's a function and what's a symbol (as argument). With apply its easy. The first argument is a function. With (eval (cons fun lst)) a very good optimizer could guess that after eval the first symbol in the next list is a function. but the list is created at run-time with cons. So its quite difficult. The optimizer would have to check the first argument of cons and mark this argument as function. Same with debugging. (serge, am I right?) So you can't precompile it, you have to evaluate it at run-time (slower) But as, I said in AutoLISP you dont really need apply.

Got it? :)

Reini Urban, TU Graz, Architecture & X-RAY