4 Replies - 5408 Views - Last Post: 07 June 2010 - 07:36 PM

#1 Guest_Phillip*


Reputation:

"(7) is not a real number" Lisp list sorting program

Posted 06 June 2010 - 06:53 PM

Hi, I am trying to teach myself LISP and decided on making a max number program. After thinking and coding I made this:

(Defun max2 (x) (if (null (cdr x))
(car x)




	(if (> (car x) (max2(cdr x)))

	(car x)

	(cdr x)

	)

)	

)


When I input (max2 (list 4 9 )) I get (9).
When I input (max2 (list 4 1 )) I get 4(no parentheses).
When I input (max2 (list 4 1 9)) I get (9) is not a real number.
The error is in the first if statement it is not getting the cdr out correctly, I think.
Any help would be appreciated

Is This A Good Question/Topic? 0

Replies To: "(7) is not a real number" Lisp list sorting program

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2019
  • View blog
  • Posts: 5,417
  • Joined: 27-December 05

Re: "(7) is not a real number" Lisp list sorting program

Posted 07 June 2010 - 08:25 AM

In order to evaluate the expression
(> (car x) (max2(cdr x)))


(max2(cdr x)) must always be a number, because > requires two operands both of which are numbers.

The problem is that your max2 function sometimes returns a list (cdr x) because of this expression:
(if (> (car x) (max2(cdr x))) (car x)
            (cdr x)
        )



The solution is a simple modification so that it always returns a number:
(Defun max2 (x)
    (if (null (cdr x)) (car x)
        (if (> (car x) (max2(cdr x))) (car x)
            (max2 (cdr x))
        )
    )       
)


Was This Post Helpful? 1
  • +
  • -

#3 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: "(7) is not a real number" Lisp list sorting program

Posted 07 June 2010 - 07:01 PM

Also, that is probably the most horrid formatting of Lisp code I've ever seen.

Please, use an editor that indents Lisp code properly (two spaces). Don't put closing parens on different lines. When you call a function, put a space between the function's name and the first argument. Don't do this: (max(car x)), do this instead (max (car x)). Also, there is no reason to put that many newlines in your code. Lisp code is meant to be terse and small. Putting 100 newlines in every function does nothing but clutter your code with whitespace and make it hard to read. Remember, you're writing code in a Lisp, not Java or C++.

(defun max2 (x)
  (if (null (cdr x)) (car x)
    (if (> (car x) (max2 (cdr x))) (car x) 
      (max2 (cdr x)))))


That is what it should look like, not abusing Common Lisp's case insensitivity, anyway.

This post has been edited by Raynes: 07 June 2010 - 07:04 PM

Was This Post Helpful? 1
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2019
  • View blog
  • Posts: 5,417
  • Joined: 27-December 05

Re: "(7) is not a real number" Lisp list sorting program

Posted 07 June 2010 - 07:15 PM

Yeah, my preoccupation with C++ got the better of me.

Actually, in Lisp I prefer this:
(defun max2 (x)
  (cond ((null (cdr x)) (car x))
        ((> (car x) (max2 (cdr x))) (car x) )
        (t (max2 (cdr x)))))


Was This Post Helpful? 0
  • +
  • -

#5 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: "(7) is not a real number" Lisp list sorting program

Posted 07 June 2010 - 07:36 PM

Yeah. cond is better in this situation. I didn't pay much attention to the code. You got to this one before I did. :P

I need to install SBCL so that I can be more helpful on CL questions. I can usually read CL code, but I don't trust myself to answer CL questions without testing out the code in my answers, since I'm a Clojure programmer, and while they are both Lisps, there are some major differences.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1