joe dirt
joe dirt

Reputation: 75

If statement with then clause (LISP)

So I'm trying to teach myself lisp, I'm currently using this site as a reference: https://www.tutorialspoint.com/lisp/lisp_if_construct.htm

I don't quite understand why the then clause is executed, despite the fact that the if clause is false?

(setq a 10)
(if (> a 20)
   then (format t "~% a is less than 20"))
(format t "~% value of a is ~d " a)

The output is:

a is less than 20
value of a is 10 

Does the then clause just always execute, even when the if statement is false? (which in this case it is).

Any help would be appreciated, also sorry if my terminology is totally incorrect I'm still new to Lisp!

Upvotes: 0

Views: 2270

Answers (1)

Sylwester
Sylwester

Reputation: 48745

According to the documentation an if has 3 elements. test-expression eg (> a 10), a then-expression eg (- a 10) and an else-expression eg. a:

(if (> a 10) ; if a is larger than 10
    (- a 10) ; then return the value of a minus 10
    a)       ; else return the value of a

Look at your code:

(if (> a 20)                          ; if a is larger than 20
    then                              ; then return the value of "then"
    (format t "~% a is less than 20")); else print a is less than 20 to screen

In this example they provided the then clause as the single variable then. Since the test is false the else-expression is always printed.

Upvotes: 5

Related Questions