This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Conditional branching with more then one line of code

4 posts · 4495 views

Hi,

As far is I understand the "if" construct of common lisp (and that is not that far ;) you
can only put ONE expression in each branch like this:
(if (> x 1 )
 (print "true")
 (print "false))
I searched google and hyperpsec but only found examples like that above.

Is it possible to put more than one form (correct terminus???) into each branch of
the "if" (WARNING! WRONG SYNTAX AHEAD! ONLY AN EXAMPLE!):
(if (> x 1 )
 ((print "true")
 (print "also true"))
 ((print "false)
  (print "also false)))
?

Thanks you very much in advance for any help!
Best regards,
mcc

Re: Conditional branching with more then one line of code

See PROGN and use it like this:
(if (test-form ...)
    (progn
      (first-then-form ...)
      (second-then-form ...))
    (progn
      (first-else-form ...)
      (second-else-form ...)))

Re: Conditional branching with more then one line of code

Hi edgar-rft,

thanks again for your valuable help! :)
Best regards,
mcc

Re: Conditional branching with more then one line of code

And don't forget progn's lookalikes prog1 and prog2. I've found them useful once or twice. :o
blog.metalight.net