Common Lisp Help...
Merry Christmas everyone!
I'm using Lisp in a Box (emacs + GC Lisp)
And i tried making a function which takes a number as argument, adds 1 to it if it's negative, and substracts 1 from it if it's positive.
(look)
(defun enlarge(x)
(if (< x 0) (- x 1) nil)
(if (> x 0) (+ x 1) nil)
)
Yes, i know, should have made everythin in one IF, but i wanted to have 2 separate IF's for strictly positive and strictly negative numbers (too see what happens if the user enters 0)
Nevertheless:
CL-USER> (enlarge 5)
6
CL-USER> (enlarge -3)
NIL
This is what i get. Why??
Thank you.
;;;Btw, is it worth learning LISP? I've started learning little bits from a book, and i've already found it to be absolutely charming !!!
(Even though i've been 'growing up' with C/C++ syntax)
TotoTitus
Junior Poster in Training
58 posts since Dec 2009
Reputation Points: 16
Solved Threads: 1
Never mind, someone helped me figure out the problem - it seems the second IF was never evaluated, and i should have instead used the ELSE part of the first IF.
TotoTitus
Junior Poster in Training
58 posts since Dec 2009
Reputation Points: 16
Solved Threads: 1
Never mind, someone helped me figure out the problem - it seems the second IF was never evaluated, and i should have instead used the ELSE part of the first IF.
No, both IFs were evaluated. The result of the first was discarded.I would have suggested the following:
(defun enlarge ( x )
(cond
( (< x 0) (- x 1) )
( (> x 0) (+ x 1) )
( T nil )
)
)
You should stop being a special snowflake and put your parentheses where everybody else puts them.
(defun enlarge (x)
(cond
((< x 0) (- x 1))
((> x 0) (+ x 1))
(t nil)))
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
First I'm not everybody
Then I don't like the way you put your parentheses because one cannot match opening and closing parentheses at a glance
Finally, you do as you wish and I'll do as I wish!
It doesn't matter. After using Lisp for a while you'll end up changing your mind and deciding that the way everybody else does it is better.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177