Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple LISP question (sequence of multiple statements)

1,042 views
Skip to first unread message

Timothy Miller

unread,
Aug 15, 2002, 12:00:24 AM8/15/02
to
I am familiar enough with LISP to know that in certain cases, you can
provide more than one discrete statement (function call?) in a
sequence, and the result evaluates to the last statement in the list.

For instance, you can have:
(defun myfun (x) (setf myglobal '(this is not returned))
(list 'We 'want 'to 'return 'this x))

Similarly, you can put multiple statements in a cond group:
(cond (cond1 expr1) (cond2 expr2) (cond3 expr3 expr4))

I can see how this would be useful in many contexts besides a
beginners LISP textbook.

However, there is one place where I can't determine from the book I
have how to list multiple statements, which is this:
(if (condition) (do something here) (do two somethings here))

How do I do two somethings there? Is there some function which, given
multiple arguments, executes them and returns the value of the last
one? I wanted to setf a global variable and then return something
else, but I ended up using cond instead which didn't result in the
most straight-forward solution.

Pardon my baby-talk. I program mostly C, C++, and Verilog, so I don't
know the proper terminology for LISP.

Any help will be appreciated.

Also, and I'm sure there's a FAQ, and I'm going to go look for it
right now, but I was wondering if anyone had any good suggestions for
teaching LISP to someone who is generally unfamiliar with programming.
I'm teaching it to my wife. Some may think it insane to teach LISP
to someone as a first language, but humor me. :)

The book I'm using, by Touretzki, is alright, but it really irritates
me sometimes. Some of the answers to exercises have required
functions that aren't introduced until later chapters, and some of the
answers in the answer key are just incorrect.

Thanks.

Marc Spitzer

unread,
Aug 15, 2002, 12:11:24 AM8/15/02
to
In article <80eae8c5.02081...@posting.google.com>,
Timothy Miller wrote:
> I am familiar enough with LISP to know that in certain cases, you can
> provide more than one discrete statement (function call?) in a
> sequence, and the result evaluates to the last statement in the list.
>
> For instance, you can have:
> (defun myfun (x) (setf myglobal '(this is not returned))
> (list 'We 'want 'to 'return 'this x))
>
> Similarly, you can put multiple statements in a cond group:
> (cond (cond1 expr1) (cond2 expr2) (cond3 expr3 expr4))
>
> I can see how this would be useful in many contexts besides a
> beginners LISP textbook.
>
> However, there is one place where I can't determine from the book I
> have how to list multiple statements, which is this:
> (if (condition) (do something here) (do two somethings here))
>
> How do I do two somethings there? Is there some function which, given
> multiple arguments, executes them and returns the value of the last
> one? I wanted to setf a global variable and then return something
> else, but I ended up using cond instead which didn't result in the
> most straight-forward solution.

look at progn and friends in the hyperspec.

>
> Pardon my baby-talk. I program mostly C, C++, and Verilog, so I don't
> know the proper terminology for LISP.
>
> Any help will be appreciated.
>
> Also, and I'm sure there's a FAQ, and I'm going to go look for it
> right now, but I was wondering if anyone had any good suggestions for
> teaching LISP to someone who is generally unfamiliar with programming.
> I'm teaching it to my wife. Some may think it insane to teach LISP
> to someone as a first language, but humor me. :)
>
> The book I'm using, by Touretzki, is alright, but it really irritates
> me sometimes. Some of the answers to exercises have required
> functions that aren't introduced until later chapters, and some of the
> answers in the answer key are just incorrect.
>
> Thanks.

OOCL by slade might be worth a look.

marc

Christopher C. Stacy

unread,
Aug 15, 2002, 12:57:31 AM8/15/02
to
>>>>> On 14 Aug 2002 21:00:24 -0700, Timothy Miller ("Timothy") writes:

Timothy> I am familiar enough with LISP to know that in certain
Timothy> cases, you can provide more than one discrete statement
Timothy> (function call?) in a sequence, and the result evaluates to
Timothy> the last statement in the list.

Timothy> For instance, you can have:
Timothy> (defun myfun (x) (setf myglobal '(this is not returned))
Timothy> (list 'We 'want 'to 'return 'this x))

Timothy> Similarly, you can put multiple statements in a cond group:
Timothy> (cond (cond1 expr1) (cond2 expr2) (cond3 expr3 expr4))

Timothy> I can see how this would be useful in many contexts besides a
Timothy> beginners LISP textbook.

Timothy> However, there is one place where I can't determine from the book I
Timothy> have how to list multiple statements, which is this:
Timothy> (if (condition) (do something here) (do two somethings here))

That's exactly what COND is for.
The IF form doesn't allow you to do that directly.

Timothy> How do I do two somethings there? Is there some function
Timothy> which, given multiple arguments, executes them and returns
Timothy> the value of the last one?

Yes, it's called PROGN and does just what you think:
encloses a bunch of other forms:

(IF (CONDITION)
(DO-SOMETHING-HERE)
(PROGN
(SOMETHING-1)
(SOMETHING-2)))

Timothy> I wanted to setf a global variable and then return something
Timothy> else, but I ended up using cond instead which didn't result
Timothy> in the most straight-forward solution.

You don't have to put a function in the predicate part of a COND clause.
You can put a variable there, too. What you want is something that
will always work as an "else" clause; something that you can put last
that would always be a "true" branch.

(COND ((YOUR-CONDITION)
(DO SOMETHING HERE))
(T
(SETF *X* 69)
42))


Timothy> Pardon my baby-talk. I program mostly C, C++, and Verilog,
Timothy> so I don't know the proper terminology for LISP.

You're doing fine, but you need a good Lisp book. I think Norvig's
"Paradigms of Artificial Intelligence" is probably a good one for you.

Erik Naggum

unread,
Aug 15, 2002, 2:38:57 AM8/15/02
to
* Timothy Miller

| However, there is one place where I can't determine from the book I
| have how to list multiple statements, which is this:
| (if (condition) (do something here) (do two somethings here))
|
| How do I do two somethings there?

I appreciate the intelligent way you arrive at the question. There are
multiple ways to do this. You have already seen what is called an "implicit
`progn´", where prog-n means that the nth value of a prog is returned.
[There is a lot of history here. You also have `prog1´ and `prog2´ which
return the first and second values, respectively, of a multi-form body.] An
implicit `progn´ occurs in function bodies as you have already seen. You
could write

(if condition
(do something here)
(progn
(do something-1 here)
(do something-2 here)
...
(do something-n here)))

to use `progn´ explicitly. [If you do this with both branches, some people
have invented `then´ and `else´ as local macros that are really `progn´.]
Notice how `prog1´, `prog2´ and `progn´ fall out from this example.

However, you already know about implicit `progn´s from elsewhere. You
already listed function bodies and `cond´ clauses, but also, e.g., in local
bindings with `let´, as in

(let ()
(do something-1 here)
(do something-2 here))

only the value of the last form is returned. [The empty list () would of
course be filled with bindings if you needed any.] The older `prog´ is
similar to `let´, except it takes labels. You need not worry about this; I
mention it for the sake of completeness and to illustrate how this problem
has been solved in many different ways over time. The canonical way these
days is with `progn´, but be forewarned that some people hate `progn´ with a
passion so have invented a disturbingly non-Lispy version of `if´ that gets
rid of it exchange for a lot of other random noise.



| Is there some function which, given multiple arguments, executes them and
| returns the value of the last one?

You could do it with a function, but Common Lisp has found the use of
special operators more convenient. Essentially, a `progn´ works just like a
function call would, evaluating each argument expression in turn, except
that all values are discarded instead of being collected for a function
call. The same is true of `let´, which is also a special operator. The
name "special operator" comes from the fact that its evaluation rule is
special, although in the case of `progn´ it is actually trivially normal.

| I wanted to setf a global variable and then return something else, but I
| ended up using cond instead which didn't result in the most straight-forward
| solution.

I tend to prefer `cond´ when the number of branches is indeterminate, `if´
when there are exactly two (such as because the `nil´ should be explicit
when the form is used for its value) , and `when´ and `unless´ when there is
only one branch that matters.

| Any help will be appreciated.

I hope the above suffices.

| Also, and I'm sure there's a FAQ, and I'm going to go look for it right now,
| but I was wondering if anyone had any good suggestions for teaching LISP to
| someone who is generally unfamiliar with programming. I'm teaching it to my
| wife. Some may think it insane to teach LISP to someone as a first
| language, but humor me. :)

I think it is the best choice for a first language. Eons ago, I found The
Little LISPer absorbingly fascinating.

| The book I'm using, by Touretzki, is alright, but it really irritates me
| sometimes.

It is the only book on Lisp I have declined to buy after looking at it.

--
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.

Paolo Amoroso

unread,
Aug 16, 2002, 9:12:39 AM8/16/02
to
On 14 Aug 2002 21:00:24 -0700, the...@hotmail.com (Timothy Miller) wrote:

> The book I'm using, by Touretzki, is alright, but it really irritates
> me sometimes. Some of the answers to exercises have required

Try this:

Successful Lisp
http://psg.com/~dlamkins/sl/contents.html


Paolo
--
EncyCMUCLopedia * Extensive collection of CMU Common Lisp documentation
http://www.paoloamoroso.it/ency/README

Marc Mertens

unread,
Aug 16, 2002, 3:54:47 PM8/16/02
to
On Thu, 15 Aug 2002 04:00:24 +0000, Timothy Miller wrote:

Try (progn do-something-1 ... do-something-n)

Marc

Christopher Browne

unread,
Aug 16, 2002, 2:07:06 PM8/16/02
to
Quoth the...@hotmail.com (Timothy Miller):

> I am familiar enough with LISP to know that in certain cases, you can
> provide more than one discrete statement (function call?) in a
> sequence, and the result evaluates to the last statement in the list.
>
> For instance, you can have:
> (defun myfun (x) (setf myglobal '(this is not returned))
> (list 'We 'want 'to 'return 'this x))
>
> Similarly, you can put multiple statements in a cond group:
> (cond (cond1 expr1) (cond2 expr2) (cond3 expr3 expr4))
>
> I can see how this would be useful in many contexts besides a
> beginners LISP textbook.
>
> However, there is one place where I can't determine from the book I
> have how to list multiple statements, which is this:
> (if (condition) (do something here) (do two somethings here))
>
> How do I do two somethings there? Is there some function which, given
> multiple arguments, executes them and returns the value of the last
> one? I wanted to setf a global variable and then return something
> else, but I ended up using cond instead which didn't result in the
> most straight-forward solution.
>
> Pardon my baby-talk. I program mostly C, C++, and Verilog, so I don't
> know the proper terminology for LISP.
>
> Any help will be appreciated.

The way to do this, and return the "nth" result, is to put the
multiple expressions into a PROGN.

(if (condition)
(progn
(do-something)
(do-something-else-as-well)))

It's sort of unfortunate that there's not much that lies in between
"IF-with-PROGN" and "COND".

COND is *certainly* indicated if you've got more than two conditions,
as you suggest above; I find I use COND more and more often over
time...
--
(reverse (concatenate 'string "gro.gultn@" "enworbbc"))
http://cbbrowne.com/info/x.html
Why do you need a driver's license to buy liquor when you can't drink
and drive?

Pascal Bourguignon

unread,
Aug 16, 2002, 10:05:23 PM8/16/02
to
ma...@oscar.eng.cv.net (Marc Spitzer) writes:

> In article <80eae8c5.02081...@posting.google.com>,
> Timothy Miller wrote:
> > I am familiar enough with LISP to know that in certain cases, you can
> > provide more than one discrete statement (function call?) in a
> > sequence, and the result evaluates to the last statement in the list.
> >
> > For instance, you can have:
> > (defun myfun (x) (setf myglobal '(this is not returned))
> > (list 'We 'want 'to 'return 'this x))
> >
> > Similarly, you can put multiple statements in a cond group:
> > (cond (cond1 expr1) (cond2 expr2) (cond3 expr3 expr4))
> >
> > I can see how this would be useful in many contexts besides a
> > beginners LISP textbook.
> >
> > However, there is one place where I can't determine from the book I
> > have how to list multiple statements, which is this:
> > (if (condition) (do something here) (do two somethings here))
> >
> > How do I do two somethings there? Is there some function which, given
> > multiple arguments, executes them and returns the value of the last
> > one? I wanted to setf a global variable and then return something
> > else, but I ended up using cond instead which didn't result in the
> > most straight-forward solution.
>
> look at progn and friends in the hyperspec.

Which to be really helpfull is at:

http://www.lispworks.com/reference/HyperSpec/index.html


> > Pardon my baby-talk. I program mostly C, C++, and Verilog, so I don't
> > know the proper terminology for LISP.
> >
> > Any help will be appreciated.
> >
> > Also, and I'm sure there's a FAQ, and I'm going to go look for it
> > right now, but I was wondering if anyone had any good suggestions for
> > teaching LISP to someone who is generally unfamiliar with programming.


> > I'm teaching it to my wife. Some may think it insane to teach LISP
> > to someone as a first language, but humor me. :)

On the contrary, this is probably the best language to learn first. A
lot of universities begin with Scheme, a lisp dialect.

> > The book I'm using, by Touretzki, is alright, but it really irritates
> > me sometimes. Some of the answers to exercises have required
> > functions that aren't introduced until later chapters, and some of the
> > answers in the answer key are just incorrect.

"The Little LISPer" http://www.cs.rice.edu/~matthias/TLS/



> > Thanks.
>
> OOCL by slade might be worth a look.
>
> marc

--
__Pascal_Bourguignon__ http://www.informatimago.com/
----------------------------------------------------------------------
The name is Baud,...... James Baud.

0 new messages