Re: Lisp Syntax - functions versus data
- From: Rainer Joswig <joswig@xxxxxxx>
- Date: Tue, 27 Feb 2007 08:50:40 +0100
In article <1172557054.945971.84460@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"S. Robert James" <srobertjames@xxxxxxxxx> wrote:
I'm still struggling to master some of the finer points of Lisp
syntax. Not the S-expressions, mind you, but the rules for forms
being either evaluated, referring to data, or referring to
functions...
I'm trying:
(defun mapcar. (func lst)
(if (null lst)
()
(append (list (funcall func (car lst))) (mapcar. func (cdr
lst)))))
(= '(3 4 5 6) (mapcar. '(lambda (x) (+ 3 x)) '(0 1 2 3)))
but can't get it to work... how should this be quoted? Should I
switch to Scheme, which seems to have simpler syntax?
What kind of error do you get?
I see:
(LAMBDA (X)
(+ 3 X)) is not of type (OR SYMBOL FUNCTION), and can't be FUNCALLed
or APPLYed
A function needs ro be quoted by #' .
So:
(= '(3 4 5 6) (mapcar. #'(lambda (x) (+ 3 x)) '(0 1 2 3)))
Then you get:
value (3 4 5 6) is not of the expected type NUMBER.
Because = is only for comparing numbers.
See:
http://www.lisp.org/HyperSpec/Body/fun_eqcm_sleq__lteqcm_gteq.html
So:
(equal '(3 4 5 6) (mapcar. #'(lambda (x) (+ 3 x)) '(0 1 2 3)))
Also:
Try to write MAPCAR. without APPEND. Using APPEND is often inefficient.
.
- References:
- Lisp Syntax - functions versus data
- From: S. Robert James
- Lisp Syntax - functions versus data
- Prev by Date: Re: Lisp Syntax - functions versus data
- Next by Date: Re: nested backquoting (newbie) question in Clisp
- Previous by thread: Re: Lisp Syntax - functions versus data
- Next by thread: Re: Lisp Syntax - functions versus data
- Index(es):
Relevant Pages
|