From: maitre Aliboron
Subject: sequence of instructions inside an if statement
Date: 
Message-ID: <f1pr8l$cvg$1@newsreader.mailgate.org>
How can I implement a sequence of instructions
inside an if statement? Tutorials I am reading do
not mention it but they shows only silly examples
involving single instructions such as:

(if  (odd n) (format t "odd") (format t "even"))

and so on... Doing a sequence of call inside
parentheses like this

(if (cond)
  ( (instr1a..)  (instr2a...) ...  )
  ( (instr1b...) (instr2b...) ...)
   )

is not good because interpreter
tell me it's not a lambda expression
(interpreter is right, I know...).

I found the macro BLOCK allowing to define
a block of instructions, but my tutorial do not
explain it and moreover it says that BLOCK is
for advanced applications, so I deduce that there should
be a *very_dumb_way_to_do it* (so dumb that tutorials
even do not explain).

OK, I'm dumber than tutorials and I did not find it,
so please, let me know how to implement this basic
construct...

(PS: my tutorial is the book: "Common Lisp, a gentle
introduction to symbolic computation"; maybe too
gentle...)

maitre Aliboron

From: dpapathanasiou
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <1178629656.165161.89920@u30g2000hsc.googlegroups.com>
> How can I implement a sequence of instructions
> inside an if statement? Tutorials I am reading do
> not mention it but they shows only silly examples
> involving single instructions such as:
>
> (if  (odd n) (format t "odd") (format t "even"))
>
> and so on... Doing a sequence of call inside
> parentheses like this
>
> (if (cond)
>   ( (instr1a..)  (instr2a...) ...  )
>   ( (instr1b...) (instr2b...) ...)
>    )

In the case of no "else" logic, you can use the (when) macro, and put
as many statements inside as you want:

(when (some condition is true)
  (statement 1)
  (statement 2)
  .
  .
  .
  (statement n))

Otherwise, use (progn) or (cond), as Rainer suggested.
From: Jeff
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <1178633998.603637.288990@w5g2000hsg.googlegroups.com>
Using progn:

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

Using cond:

(cond
  ((condition) (...) (...) (...))
  ((condition) (...) (...) (...)))

Or using when, as dpapathanasiou described above.

On May 8, 9:07 am, dpapathanasiou <···················@gmail.com>
wrote:
> > How can I implement a sequence of instructions
> > inside an if statement? Tutorials I am reading do
> > not mention it but they shows only silly examples
> > involving single instructions such as:
>
> > (if  (odd n) (format t "odd") (format t "even"))
>
> > and so on... Doing a sequence of call inside
> > parentheses like this
>
> > (if (cond)
> >   ( (instr1a..)  (instr2a...) ...  )
> >   ( (instr1b...) (instr2b...) ...)
> >    )
>
> In the case of no "else" logic, you can use the (when) macro, and put
> as many statements inside as you want:
>
> (when (some condition is true)
>   (statement 1)
>   (statement 2)
>   .
>   .
>   .
>   (statement n))
>
> Otherwise, use (progn) or (cond), as Rainer suggested.
From: Tamas Papp
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <87bqgvuzht.fsf@pu100877.student.princeton.edu>
"maitre Aliboron" <········@despammed.com> writes:

> OK, I'm dumber than tutorials and I did not find it,
> so please, let me know how to implement this basic
> construct...
>
> (PS: my tutorial is the book: "Common Lisp, a gentle
> introduction to symbolic computation"; maybe too
> gentle...)

Hi,

Others have answered your question, but I suggest that you try reading
a different tutorial, eg Practical Common Lisp, for example, has the
answer to your question:

http://www.gigamonkeys.com/book/macros-standard-control-constructs.html

has very nice explanations.  Paul Grahams ANSI Common Lisp is also
nice, with many examples.  I am a newbie too, and these two books help
a lot.

Best,

Tamas

-- 
Posted via a free Usenet account from http://www.teranews.com
From: maitre Aliboron
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <f1qbuj$2v7$1@newsreader.mailgate.org>
Many thanks to each one. I know
I'll be back very soon...

maitre Aliboron 
From: Ken Tilton
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <qu40i.17$S53.6@newsfe12.lga>
maitre Aliboron wrote:
> Many thanks to each one. I know
> I'll be back very soon...

Not so fast. The correct answer is to avoid multi-line branches. It will 
teach you lisp faster and make your code better, and on a good day 
restore hair growth.

kzo

-- 
http://www.theoryyalgebra.com/

"Algebra is the metaphysics of arithmetic." - John Ray

"As long as algebra is taught in school,
there will be prayer in school." - Cokie Roberts

"Stand firm in your refusal to remain conscious during algebra."
    - Fran Lebowitz

"I'm an algebra liar. I figure two good lies make a positive."
    - Tim Allen
From: ctnd
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <1178696644.472159.291500@o5g2000hsb.googlegroups.com>
On May 8, 8:48 pm, Ken Tilton <····@theoryyalgebra.com> wrote:
> teach you lisp faster and make your code better, and on a good day
> restore hair growth.

I LOL'd.
From: maitre Aliboron
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <f1se2e$n8e$1@newsreader.mailgate.org>
>> teach you lisp faster and make your code better, and on a good day
>> restore hair growth.
>
> I LOL'd.

whatever, I have no hair growth problems, at least... not
before starting to use LISP :-)) 
From: Ken Tilton
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <fJq0i.29$vz3.7@newsfe12.lga>
ctnd wrote:
> On May 8, 8:48 pm, Ken Tilton <····@theoryyalgebra.com> wrote:
> 
>>teach you lisp faster and make your code better, and on a good day
>>restore hair growth.
> 
> 
> I LOL'd.
> 

Credit where due: inspiration was Tom Waits "Step Right Up" off his 
classic "Small Change" album. Unfortunately the iTunes sample does not 
manifest the connection. Spend a buck, it's worth it. The $8.01 for the 
whole album is a better idea. Tom Traubert's Blues, The Piano Has Been 
Drinking (Not me), Bad Liver and A Broken Heart... great stuff.

Lyrics to "Step Right Up":

    http://www.lyricsdir.com/tom-waits-step-right-up-lyrics.html

kzo

-- 
http://www.theoryyalgebra.com/

"Algebra is the metaphysics of arithmetic." - John Ray

"As long as algebra is taught in school,
there will be prayer in school." - Cokie Roberts

"Stand firm in your refusal to remain conscious during algebra."
    - Fran Lebowitz

"I'm an algebra liar. I figure two good lies make a positive."
    - Tim Allen
From: Alan Crowe
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <864pmn11nl.fsf@cawtech.freeserve.co.uk>
"maitre Aliboron" <········@despammed.com> writes:

> Many thanks to each one. I know
> I'll be back very soon...
> 
> maitre Aliboron 
> 
> 

The use of progn to create blocks looks pretty ugly. For example

(if (> temperature maximum)
    (progn
      (setf maximum temperature)
      (setf time (now)))
    (setf last-check (now)))

One reason nothing is done to fix the language is that the
problem is less than it appears. For example the code above
can actually be written

(if (> temperature maximum)
    (setf maximum temperature
          time (now))
    (setf last-check (now)))

because 

(setf place1 value1
      place2 value2)

means exactly

(progn
  (setf place1 value1)
  (setf place2 value2))

Another artificial example.
This imperative code

         (let ((x (print (random 10)))
               (y (print (random 10)))
               (z '(-1)))
           (if (> x y)
               (progn 
                 (push y z)
                 (push x z))
               (progn
                 (push x z)
                 (push y z)))
           (assert (> (first z)
                      (second z)))
           z)
      

would probably be written in a more functional style
 
         (let ((x (print (random 10)))
               (y (print (random 10)))
               (z '(-1)))
           (setf z (if (> x y)
                       (cons x (cons y z))
                       (cons y (cons x z))))
           (assert (> (first z)
                      (second z)))


and

(if (endp list)
    (progn (clean up)(final value))
    (progn (do this)(do that))

might be written

(typecase list
  (null (clean up)(final value))
  (cons (do this)(so that)))

or 

(cond ((endp list)(clean up)(final value))
      (t (do this)(do that)))

With the sequencing implicit in nested function calls and
the variety of contructs with implicit blocks the ugly 

(if test
    (progn (this) (that))
    (progn (the) (other)))

is rare enough that lispers just shrug. (and write books
that don't cover the point all that well)

Alan Crowe
Edinburgh 
Scotland
From: maitre Aliboron
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <f1qpte$ks7$1@newsreader.mailgate.org>
> The use of progn to create blocks looks pretty ugly...

I got the lesson... I have changed my
iterative-with-huge-progn-sections function
into two compact-recursive-functions-without-progn.
It took me a couple of hours but they seem
work pretty well.

So no more need of progn for this time but
in the future, who knows? Many thanks

maitre Aliboron 
From: dpapathanasiou
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <1178659783.455077.305280@h2g2000hsg.googlegroups.com>
> I got the lesson... I have changed my
> iterative-with-huge-progn-sections function
> into two compact-recursive-functions-without-progn.
> It took me a couple of hours but they seem
> work pretty well.

Here's a good example contrasting imperative versus functional on the
same logic: http://www.bookshelf.jp/texi/onlisp/onlisp_4.html#SEC25
From: Rainer Joswig
Subject: Re: sequence of instructions inside an if statement
Date: 
Message-ID: <joswig-D79935.14555108052007@news-europe.giganews.com>
In article <············@newsreader.mailgate.org>,
 "maitre Aliboron" <········@despammed.com> wrote:

> How can I implement a sequence of instructions
> inside an if statement? Tutorials I am reading do
> not mention it but they shows only silly examples
> involving single instructions such as:
> 
> (if  (odd n) (format t "odd") (format t "even"))
> 
> and so on... Doing a sequence of call inside
> parentheses like this
> 
> (if (cond)
>   ( (instr1a..)  (instr2a...) ...  )
>   ( (instr1b...) (instr2b...) ...)
>    )
> 
> is not good because interpreter
> tell me it's not a lambda expression
> (interpreter is right, I know...).
> 
> I found the macro BLOCK allowing to define
> a block of instructions, but my tutorial do not
> explain it and moreover it says that BLOCK is
> for advanced applications, so I deduce that there should
> be a *very_dumb_way_to_do it* (so dumb that tutorials
> even do not explain).
> 
> OK, I'm dumber than tutorials and I did not find it,
> so please, let me know how to implement this basic
> construct...
> 
> (PS: my tutorial is the book: "Common Lisp, a gentle
> introduction to symbolic computation"; maybe too
> gentle...)
> 
> maitre Aliboron

Try PROGN.
http://www.lispworks.com/documentation/HyperSpec/Body/s_progn.htm#progn

Also look at COND.
http://www.lispworks.com/documentation/HyperSpec/Body/m_cond.htm#cond

-- 
http://lispm.dyndns.org