954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
 

I would have suggested the following:

(defun enlarge ( x ) 
  (cond
    ( (< x 0) (- x 1) )
    ( (> x 0) (+ x 1) )
    ( T nil )
  )
)
Ghost_Buster
Newbie Poster
9 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 
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
Team Colleague
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!

Ghost_Buster
Newbie Poster
9 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 
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
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Just for your information, I've been using LISP for 26 years now and I'm still convinced that I prefer my method.... Lisp is one of the oldest programming language available in AutoCAD and I'm a CAD programmer ... full time (using LISP that is)!

I'm even worst (to your stand point) than that!
I put comments after every close parentheses....

Ghost_Buster
Newbie Poster
9 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You