Page 1 of 1

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

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• 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))
)
)
)

```

### #3 Raynes

• D.I.C Lover

Reputation: 614
• 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

### #4 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• 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)))))

```

### #5 Raynes

• D.I.C Lover

Reputation: 614
• 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.

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.