Page 1 of 1

Conditional branching with more then one line of code

Posted: Fri Apr 10, 2015 10:05 pm
by mcc
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:

Code: Select all

(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!):

Code: Select all

(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

Posted: Sat Apr 11, 2015 2:11 am
by edgar-rft
See PROGN and use it like this:

Code: Select all

(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

Posted: Sat Apr 11, 2015 7:37 am
by mcc
Hi edgar-rft,

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

Re: Conditional branching with more then one line of code

Posted: Fri Apr 17, 2015 5:33 am
by Kohath
And don't forget progn's lookalikes prog1 and prog2. I've found them useful once or twice. :o