vendredi 21 octobre 2016

What's the difference between if and if...then in Lisp?

I don't know if my logic is good, but I think that when I use if...then, then execute the body source code if condition is false, but when I use if...then...else, then execute the body source code if condition is true.

For example

    (setq x 256)  
    (if (> x 300)  ;condition is false
        (format t "X is greater than 300")) ; block code isn't executed
    (format t "The condition is false because the value of X is ~a" x)  

The result returned is The condition is false because the value of X is 265

But when I use

    (setq x 265)
    (if (> x 300) ;condition is false
        then (format t "X is greater than 300")) ; code block is executed

The result returned is X is greater than 300

And with if...then...else

    (setq x 256)
    (if (> x 300) ; condition is false
        (format t "X is greater than 300") ;code block is not executed
        (format t "X is less than 300") ; code block is executed  

Why, if I use the optional then the code block is executed when the condition is false?

Aucun commentaire:

Enregistrer un commentaire