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

usage of (if.....

6 views
Skip to first unread message

mur...@crystal.cirrus.com

unread,
Sep 25, 1998, 3:00:00 AM9/25/98
to
Hi all,

A very elementary q about the usage of "if" in lisp.

How can do multiple commands under one if? I have seen that the following does
not work

(if (= numb1 numb2)
(
(+ numb3 numb4)
(message "message1")
))

I want to be able to do 2 things ie do the addition and display the message
when the above cond is true? Putting 2 brackets also didnt help. any clues?
thanks murli

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

Erik Naggum

unread,
Sep 25, 1998, 3:00:00 AM9/25/98
to
* mur...@crystal.cirrus.com

| A very elementary q about the usage of "if" in lisp.

see WHEN, UNLESS and PROGN.

#:Erik

Mike McDonald

unread,
Sep 25, 1998, 3:00:00 AM9/25/98
to
[Posted and mailed]

In article <6ugspr$md2$1...@nnrp1.dejanews.com>,
mur...@crystal.cirrus.com writes:
> Hi all,


>
> A very elementary q about the usage of "if" in lisp.
>

> How can do multiple commands under one if? I have seen that the following does
> not work
>
> (if (= numb1 numb2)

> ( PROGN

^^^^^ (or in this case, you could use WHEN instead of if)

> (+ numb3 numb4)
> (message "message1")
> ))
>
> I want to be able to do 2 things ie do the addition and display the message
> when the above cond is true? Putting 2 brackets also didnt help. any clues?
> thanks murli
>
> -----== Posted via Deja News, The Leader in Internet Discussion ==-----
> http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

--

Mike McDonald
mik...@mikemac.com


Hrvoje Niksic

unread,
Sep 25, 1998, 3:00:00 AM9/25/98
to mur...@crystal.cirrus.com
mur...@crystal.cirrus.com writes:

> Hi all,
>
> A very elementary q about the usage of "if" in lisp.
>
> How can do multiple commands under one if? I have seen that the following does
> not work
>
> (if (= numb1 numb2)
> (

> (+ numb3 numb4)
> (message "message1")
> ))

Use `progn' or `when'. Examples:

(if (= numb1 numb2)
(progn
(+ numb3 numb4) ; an oop, btw.
(message "message1")))

Or, nicer:

(when (= numb1 numb2)
(+ numb3 numb4) ; an oop, btw.
(message "message1"))

--
Hrvoje Niksic <hni...@srce.hr> | Student at FER Zagreb, Croatia
--------------------------------+--------------------------------
"Listen, kid, we're all in it together!" -- Harry Tuttle in _Brazil_

See sig for reply

unread,
Sep 27, 1998, 3:00:00 AM9/27/98
to
On Fri, 25 Sep 1998 mur...@crystal.cirrus.com wrote:

> A very elementary q about the usage of "if" in lisp.
>
> How can do multiple commands under one if? I have seen that the following does
> not work
>
> (if (= numb1 numb2)
> (
> (+ numb3 numb4)
> (message "message1")
> ))

You have to use "progn":

if (...)
(
progn
(
(...)
(...)
)
)

I would prefer your method too ...


Cheers,
Detlef.


+++
no archive: yes

--
Detlef Marxsen at 53 27 N 09 57 E.
Due to bad spam experience, my header is faked. To send email to me
remove the ".no" from de...@astrax.snafu.de.no
Sorry for the inconvenience.
May the bugs and computer-viruses eat the spammers alive!


Chuck Fry

unread,
Sep 28, 1998, 3:00:00 AM9/28/98
to

I'm surprised no one mentioned COND as an alternative. Am I showing my
age? Or has COND been deprecated somehow?

E.g.:

(if (foo-p x)
(progn
(do-foo-1 x)
(do-foo-2 x))
(progn
(do-non-foo-1 x)
(do-non-foo-2 x)))

can also be done as:

(cond
((foo-p x)
(do-foo-1 x)
(do-foo-2 x))
(t
(do-non-foo-1 x)
(do-non-foo-2 x)))

COND also extends nicely into an if-then-elseif construct.

For the record, I do sometimes use FIRST/REST in preference to
CAR/CDR. :-)

-- Chuck, from the Old Lispers' Home
--
Chuck Fry -- Jack of all trades, master of none
chu...@chucko.com (text only please) chuc...@home.com (MIME enabled)
Lisp bigot, mountain biker, car nut, sometime guitarist and photographer

Kelly Murray

unread,
Sep 28, 1998, 3:00:00 AM9/28/98
to
mur...@crystal.cirrus.com wrote:
> How can do multiple commands under one if? I have seen that the following does
> not work
>
> (if (= numb1 numb2)
> (
> (+ numb3 numb4)
> (message "message1")
> ))
>

May I suggest you use the Franz ACL if syntax:

(if* (= numb1 numb2)
then


(+ numb3 numb4)
(message "message1")
)

This makes WHEN obsolete, as well as COND.

-Kelly Murray k...@intellimarket.com

(defmacro if (cond &rest args)
(let* ((then (first args)))
(cond
((or (not (symbolp then)) ;; probably using CL syntax
(not (eqs then 'then)))
(loop for a in args
do
(cond ((or (eqs a 'then)
(eqs a 'else)
(eqs a 'elseif))
(error "Syntax error in IF: missing THEN ~s" args))))
;; passed the test, expand into a CommonLisp IF
`(cond (,cond ,then)
(t ,(second args))) ;; returns nil if no then
)
;; otherwise, they have a THEN after the conditional,
;: parse the extended IF
(t
(pop args) ;; pop off THEN
;; collect all the ELSEIF cases
(loop with cases = nil
with case = (list cond)
for arg = (pop args)
do
(cond
((null args) ;; ran off end of THEN
(push arg case)
(return
`(cond
,@(reverse cases)
(,@(reverse case))
(t nil))))
;; have the ELSE case, were done
((eqs arg 'else)
(return
`(cond
,@(reverse cases)
(,@(reverse case))
(t ,@args))))
;; start of another case
((eqs arg 'elseif)
(push (reverse case) cases) ;; done with this case
(when (null args)
(error "Syntax error in IF: missing condition after ELSEIF ~s"
args))
(setf cond (pop args)) ;; get the conditional
(setf then (pop args))
(when (or (not (symbolp then))
(not (equalp (symbol-name then) "THEN")))
(error "Syntax error in IF: missing THEN after ELSEIF ~s" args))
(setf case (list cond))
)
;; otherwise, we're still accumulating THEN forms
(t (push arg case)
)
))))))

Felix Winkelmann

unread,
Sep 30, 1998, 3:00:00 AM9/30/98
to
k...@IntelliMarket.Com wrote:
> mur...@crystal.cirrus.com wrote:
> > How can do multiple commands under one if? I have seen that the
> > following does not work
> >
> > (if (= numb1 numb2)
> > (
> > (+ numb3 numb4)
> > (message "message1")
> > ))
> >
>
> May I suggest you use the Franz ACL if syntax:
>
> (if* (= numb1 numb2)
> then
> (+ numb3 numb4)
> (message "message1")
> )
>
> This makes WHEN obsolete, as well as COND.
>
> -Kelly Murray k...@intellimarket.com
>
> (defmacro if (cond &rest args) ...

yuck!

felix


James A. Crippen

unread,
Oct 1, 1998, 3:00:00 AM10/1/98
to
Felix Winkelmann wrote:
>
> k...@IntelliMarket.Com wrote:
> > mur...@crystal.cirrus.com wrote:
> > > How can do multiple commands under one if? I have seen that the
> > > following does not work
> > >
> > > (if (= numb1 numb2)
> > > (
> > > (+ numb3 numb4)
> > > (message "message1")
> > > ))

The best way is to use progn.
(if (= n1 n2)
(progn
(+ n3 n4)
(message "message1"))
...)

BTW, indenting properly is really the only way to make sense out of an
if
stmt as it's usually difficult to tell which is the 'then' and which is
the
'else'. If you don't have a decent editor or a good interactive system
then
you can at least kluge the indentation with spaces:
(if (= n1 n2)
(progn
(+ n3 n4)
(message "foo!"))
(message "bar!"))
Oddly enough this actually works correctly for many functions.

cheers,
james

Kent M Pitman

unread,
Oct 1, 1998, 3:00:00 AM10/1/98
to
"James A. Crippen" <crip...@saturn.math.uaa.alaska.edu> writes:

> If you don't have a decent editor or a good interactive system
> then you can at least kluge the indentation with spaces:
> (if (= n1 n2)
> (progn
> (+ n3 n4)
> (message "foo!"))
> (message "bar!"))
> Oddly enough this actually works correctly for many functions.

Personally, I prefer to use COND whenever I find an IF that requires a
sub-PROGN. IF is about expressing a simple concept; when the
simplicity is gone, its value is expired, at least to me.

(cond ((= n1 n2)
;; ( This next code's value isn't used, btw.
;; Is it being called for effect to do type-checking
;; in high-safety code? ;-)


(+ n3 n4)
(message "foo!"))

(t
(message "bar!")))

Also, when multiple branches of an IF do the same thing, consider
consolidating the IF into the function call. That may or may not
be the right thing in this case, but in general it's an option
worth considering. Notice how doing such a transformation
in this case eliminates the need for an overcomplex IF:

(let ((same-p (= n1 n2)))
(when same-p (+ n3 n4))
(message (if same-p "foo!" "bar!")))

This sometimes makes it easier to tell that a message is
unconditionally typed, too. It's easy in an IF or COND to sometimes
end up not doing a piece of common code in one clause or another.

James A. Crippen

unread,
Oct 5, 1998, 3:00:00 AM10/5/98
to
Kent M Pitman wrote:
>
> Personally, I prefer to use COND whenever I find an IF that requires a
> sub-PROGN. IF is about expressing a simple concept; when the
> simplicity is gone, its value is expired, at least to me.

I agree completely. The original question seemed to imply that they
didn't want to use COND, but it certainly is the way to go if there's
more than a single simple concept being expressed.

I've always tried to think of an IF as a McCarthy conditional (which it
is), and tried to write code that could look good if expressed in McC
cond form.

CL: (if foo bar baz)
McC cond: foo -> bar, baz

So if I can't make something clean like this:
(n = 1) -> 1,
(n - 1)
then it ought to be in a COND instead.



> (cond ((= n1 n2)
> ;; ( This next code's value isn't used, btw.
> ;; Is it being called for effect to do type-checking
> ;; in high-safety code? ;-)
> (+ n3 n4)
> (message "foo!"))
> (t
> (message "bar!")))

Hmm. Why would someone want to add n3 and n4 in the COND when they
don't have any real reference to the values in question, namely n1 and
n2? I suppose more context would be necessary to determine that line's
usefulness, but it still smells funny to me. Now if it was (+ n1 n3) or
(+ n2 n4) then it might make sense, perhaps checking that the value of
the CADR is the same type as the CADDR, but the entire process would be
better done with a DECLARE. Of course, that might be MacLISP (or some
other archaic Lisp), in which people did all kinds of crazy things that
I can't understand.



> Also, when multiple branches of an IF do the same thing, consider
> consolidating the IF into the function call. That may or may not
> be the right thing in this case, but in general it's an option
> worth considering. Notice how doing such a transformation
> in this case eliminates the need for an overcomplex IF:
>
> (let ((same-p (= n1 n2)))
> (when same-p (+ n3 n4))
> (message (if same-p "foo!" "bar!")))
>
> This sometimes makes it easier to tell that a message is
> unconditionally typed, too. It's easy in an IF or COND to sometimes
> end up not doing a piece of common code in one clause or another.

That's a neat trick. I'll have to remember that. Tho I still think
that the
(+ n3 n4) is bogus. The only real problem with your example is that it
creates another context which would potentially cost more since we need
another frame on the stack to accurately represent the LET. But if one
was translating to an iterative language like most assemblers the LET
would be easily represented as an assignment that was cleaned when the
LET exited. The original IF would be more efficient since there's
really no assignment going on.

Bah, never mind. I shouldn't be optimizing a nonexistent implementation
anyway.

BTW, does anyone know of a good Lisp->Scheme translator? It can't be
*too* terribly difficult. Me and a friend are working on a Scheme based
OS and I'd like to steal some code from CMUCL and friends, but I don't
want to spend uncountably finite hours in XEmacs retyping crud.

cheers,
james

0 new messages