From: phal
Subject: whether error with let or what??, I new...
Date: 
Message-ID: <1129775870.091645.175160@g43g2000cwa.googlegroups.com>
Dear friend

This code get error while compile
------------------------------
(defun summit(lst)
  (if (null lst)
     nil
   (let ((x (car lst)))
       (+ x(summit(cdr lst))))))

-----------------------------

If you have ANSI Common Lisp Book by Paul Granham
you can see the exercise on Page 30 session 9.B,

As I new to Lisp, but I think that
 (let ((x (car lst))) is make the error
it should be (let (setf x (car lst))), I not sure whether this is the
corrct one or not, but it still get error.

Please help for newbie

Thank

From: ·············@gmail.com
Subject: Re: whether error with let or what??, I new...
Date: 
Message-ID: <1129778304.706022.302940@f14g2000cwb.googlegroups.com>
phal wrote:
> Dear friend
>
> This code get error while compile
> ------------------------------
> (defun summit(lst)
>   (if (null lst)
>      nil
>    (let ((x (car lst)))
>        (+ x(summit(cdr lst))))))
>
> -----------------------------
>
> If you have ANSI Common Lisp Book by Paul Granham
> you can see the exercise on Page 30 session 9.B,
>
> As I new to Lisp, but I think that
>  (let ((x (car lst))) is make the error
> it should be (let (setf x (car lst))), I not sure whether this is the
> corrct one or not, but it still get error.
>
> Please help for newbie
>
> Thank

Setf is not the problem.

This is what the code does:
(summit (cons 1 nil))  -> (+ 1 (summit nil)) -> (+ 1 nil)

What is the value of (+ 1 nil)?  What would you rather (summit nil)
return?
From: Pedro Kröger
Subject: Re: whether error with let or what??, I new...
Date: 
Message-ID: <1129810312.479692.212470@f14g2000cwb.googlegroups.com>
You should have 0 instead of nill, or you'll get an error like
"Argument Y is not a NUMBER: NIL". this should work:

(defun summit (lst)
  (if (null lst)
      0
      (let ((x (car lst)))
        (+ x (summit (cdr lst))))))

A stylistic suggestion, don't do things like:

 (defun summit(lst)

a space between elements is important to help to distinguish them

 (defun summit (lst)

You say you're new to lisp, so even if you're an experienced programmer
in other languages, this page will give very helpful tips

http://www.lisp.org/table/style.htm