Re: a noob question
From: Fred Gilham (gilham_at_snapdragon.csl.sri.com)
Date: 01/25/05
- Next message: Fred Gilham: "Re: a noob question"
- Previous message: David Sletten: "Re: FLET/LABELS question"
- In reply to: Michael Beattie: "a noob question"
- Next in thread: Fred Gilham: "Re: a noob question"
- Reply: Fred Gilham: "Re: a noob question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 24 Jan 2005 17:12:12 -0800
Michael Beattie <mbeattie@alumni.rutgers.edu> writes:
> Why is it that some macros or whatever have double parens around them?
> Like...
>
> (let ((x 2))) ; this has 2 parens around the x=2 assignment
>
> or
>
> (do ((i 0 (+ i 1))) ; there's 2 parens around the i=0 and the increment
> ((> i 10) 'done) ; 2 parens around the "end" condition
> (print i))
>
> I guess part of the question is "what exactly is the template for the do
> or let macro?"
You've received a number of answers so far, but here's a
"list-oriented" way to think about it. (After all, Lisp is a
"list-processing" language, right?)
The basic unit of Lisp is the form. A form is either some atomic
object or a list. Both LET and DO forms are lists.
LET forms take a list of bindings and a sequence of zero or more "body
forms" that it evaluates in the current lexical environment extended
by the bindings. (The whole purpose of LET forms are to extend the
current lexical environment with new bindings. If that is
gobbledy-gook to you, you should try to find out what it means.) The
value of the last of the sequence of body-forms is the return value of
the LET form.
(let binding-list body-forms)
A binding list is a list of zero or more bindings.
(let (binding binding binding ....) body-forms)
A binding is either
1) a symbol
or
2) a list of a symbol and a form to evaluate to get the value of the
variable the symbol names.
(let (symbol1
(symbol2 form)
...)
body-forms)
For example:
(let (symbol1 ; No value, defaults to NIL.
(symbol2 3) ; Initial value 3.
(symbol3 'foo) ; Initial value 'foo.
(symbol4 (sqrt 10000))) ; Initial value (sqrt 10000).
(setf symbol2 (* symbol2 symbol4)) ; Body form 1.
symbol2) ; Body form 2, returned as value.
A DO form is like a LET form with a few differences.
(do binding-list end-list body-forms)
or
(do (binding binding binding ...) (end-test result-forms) body-forms)
A binding in a DO form is like a binding in a LET form but it has one
extra possibility. It is
1) a symbol,
or
2) a list containing a symbol and a form to get the symbol's initial
value,
*or*
3) a list of a symbol, an initial value form, and a form to give the
symbol a new value upon each iteration of the do.
The end-list is a list consisting of an end test and a sequence of
forms to evaluate, the last of which is the return value of the DO
form. The end test is simply any form that returns a value. If it
returns nil, the end-test is false; if it returns anything else the
end-test is true and the DO loop terminates.
(do (symbol1 ; No initial value (default NIL). No iteration form.
(symbol2 0) ; Initial value but no iteration form.
(symbol3 0 (1+ symbol3))) ; Both initial value and iteration form.
((> symbol3 10) (incf symbol1) (values symbol1 symbol2)); End test
; and two return forms.
(incf symbol2) ; Body form 1.
(setf symbol1 (+ symbol2 symbol3)) ; Body form 2.
)
It helps me at least to think of things this way.
-- Fred Gilham gilham@csl.sri.com I mean what's Stanford doing teaching Java? That's not education, it's vocational training. -- Bruce Stephens
- Next message: Fred Gilham: "Re: a noob question"
- Previous message: David Sletten: "Re: FLET/LABELS question"
- In reply to: Michael Beattie: "a noob question"
- Next in thread: Fred Gilham: "Re: a noob question"
- Reply: Fred Gilham: "Re: a noob question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|