Lisp HUG Maillist Archive

[help] sum of list

Hi, Everyone,
 
I am starting to learn Lisp. Could you please bother to help me on the following question? Thank you!
 
I plan to write a function that returns the sum of all non-nil numbers in a list. However, it does not work as intended. Could you please tell me what is the problem and how to correct it? Thank you so much .
The function I wrote is as follows:
(defun (summit ( lst) (remove nil lst) (apply #' + lst))
 
Thanks! :-)
 
 

Re: [help] sum of list

Remember that remove returns a copy of the list. It doesn't change the  
original list. So (apply #'+ lst) is still referring to the original  
list.

Try

(defun summit (lst)
	(apply #'+
		(remove nil lst)))

Happy learning. It's an enlightening journey.

Laughing Water


On Jan 19, 2008, at 12:35 PM, He Jibo wrote:

> Hi, Everyone,
>
> I am starting to learn Lisp. Could you please bother to help me on  
> the following question? Thank you!
>
> I plan to write a function that returns the sum of all non-nil  
> numbers in a list. However, it does not work as intended. Could you  
> please tell me what is the problem and how to correct it? Thank you  
> so much .
> The function I wrote is as follows:
> (defun (summit ( lst) (remove nil lst) (apply #' + lst))
>
> Thanks! :-)
>
>


Re: [help] sum of list

Firstly, the syntax of defun is

(defun <name> <args>
   <body>)

So you want

(defun summit (lst)
   ...)

Secondly, remove is a function. It doesn't alter its arguments, it  
returns a new list. Consider typing these forms in the listener:

 > (setf list '(1 2 3))
==> (1 2 3)
 > (remove 2 list)
==> (1 3)
 > list
==> (1 2 3)

The call to remove returned what I want but my list is the same after  
the call.

Lastly, you may want to look up the function reduce.

Regards.


On Jan 19, 2008, at 2:35 PM, He Jibo wrote:

> Hi, Everyone,
>
> I am starting to learn Lisp. Could you please bother to help me on  
> the following question? Thank you!
>
> I plan to write a function that returns the sum of all non-nil  
> numbers in a list. However, it does not work as intended. Could you  
> please tell me what is the problem and how to correct it? Thank you  
> so much .
> The function I wrote is as follows:
> (defun (summit ( lst) (remove nil lst) (apply #' + lst))
>
> Thanks! :-)
>
>

--
Gary Warren King, metabang.com
Cell: (413) 559 8738
Fax: (206) 338-4052
gwkkwg on Skype * garethsan on AIM





Re: [help] sum of list

He Jibo wrote:

> I plan to write a function that returns the sum of all non-nil numbers 
> in a list. However, it does not work as intended. Could you please tell 
> me what is the problem and how to correct it? Thank you so much .
> The function I wrote is as follows:
> (defun (summit ( lst) (remove nil lst) (apply #' + lst))

1. No parenthesis between defun and summit.
2. (remove nil lst) doesn't change lst, it returns a list which you can 
subsequently use.

So: (defun summit (lst) (apply #'+ (remove nil lst)))

reduce could be used instead of apply, but I don't know if that is an 
improvement.

It is likely that someone will mention that this list is primarily, if 
not exclusively for Lispworks-specific topics. But I enjoy the 
infrequent occasion when I can offer an answer.

Mitch


Re: [help] sum of list


On 19-Jan-08, at 12:35 PM, He Jibo wrote:

> Hi, Everyone,
>
> I am starting to learn Lisp. Could you please bother to help me on  
> the following question? Thank you!
>
> I plan to write a function that returns the sum of all non-nil  
> numbers in a list. However, it does not work as intended. Could you  
> please tell me what is the problem and how to correct it? Thank you  
> so much .
> The function I wrote is as follows:
> (defun (summit ( lst) (remove nil lst) (apply #' + lst))
>
> Thanks! :-)
>
>


For something like that I would not use remove.  Just iterate through  
the
list and sum the non nils.


(defun sum-non-nil (list)
   (loop for n in list when n sum n))

Wade


Re: [help] sum of list

To ensure that only numbers are processed, replace the "when n" by "when (numberp n), i.e.,

(loop for n in *list*
          when (numberp n)
          sum n)

By the way, from one newbie to another, I do suggest that you learn to use loop (see ). Lots of details yes, but definitely grounds for an aha. It is powerful.

Alex

Re: [help] sum of list

By the way, from one newbie to another, I do suggest that you learn to use loop (see ). Lots of details yes, but definitely grounds for an aha. It is powerful.

Oops. I forgot the URL (for a great chapter on Loop, from a great book by Peter Seibel): http://gigamonkeys.com/book/loop-for-black-belts.html .

Cheers
A.

Re: [help] sum of list

Unable to parse email body. Email id is 7477

Re: [help] sum of list

Unable to parse email body. Email id is 7480

Updated at: 2020-12-10 08:44 UTC