Hi, all, do you guys know how to do "and" and "or" in lisp?

Discussion of Common Lisp

Hi, all, do you guys know how to do "and" and "or" in lisp?

Postby akatsuki521 » Sun Nov 03, 2013 9:27 am

I mean in the "if" and "cond". How to evaluate multiple conditions? Thx.
akatsuki521
 
Posts: 1
Joined: Sun Nov 03, 2013 9:24 am

Re: Hi, all, do you guys know how to do "and" and "or" in li

Postby edgar-rft » Sun Nov 03, 2013 5:10 pm

See Peter Seibel's Practical Common Lisp, Chapter 7: Standard Control Constructs
edgar-rft
 
Posts: 147
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Hi, all, do you guys know how to do "and" and "or" in li

Postby sylwester » Mon Nov 04, 2013 11:52 am

cond is just a macro that ends up being nested ifs. It has implicit progn so a if where the consequent or alternative needs a progn or if it's more than one predicate simply looks better written as a cond.

Code: Select all
(macroexpand
 '(cond ((test-predicate-function arg) (consequent1-with-sideeffect)
                             (consequent2-tail-call))
        (test-predicate-value consequent-value1)
        (t (alternative-expression))))

; ==>

(if (test-predicate-function arg)
    (progn (consequent1-with-sideeffect)
           (consequent2-tail-call))
    (if test-predicate-value
        consequent-value1
        (alternative-expression)))


and and or are macros that end up being nested ifs as well. Just use macroexpand to see the maging. (don't forget to quote the code or else it will try to expand the result)
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
sylwester
 
Posts: 81
Joined: Mon Jul 11, 2011 2:53 pm


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 3 guests