Conditional branching with more then one line of code

Discussion of Common Lisp
Post Reply
mcc
Posts: 18
Joined: Fri Mar 27, 2015 10:47 pm

Conditional branching with more then one line of code

Post by mcc » Fri Apr 10, 2015 10:05 pm

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

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Conditional branching with more then one line of code

Post by edgar-rft » Sat Apr 11, 2015 2:11 am

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 ...)))

mcc
Posts: 18
Joined: Fri Mar 27, 2015 10:47 pm

Re: Conditional branching with more then one line of code

Post by mcc » Sat Apr 11, 2015 7:37 am

Hi edgar-rft,

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

Kohath
Posts: 61
Joined: Mon Jul 07, 2008 8:06 pm
Location: Toowoomba, Queensland, Australia
Contact:

Re: Conditional branching with more then one line of code

Post by Kohath » Fri Apr 17, 2015 5:33 am

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

Post Reply