Lisp question

Started by
9 comments, last by kordova 20 years, 11 months ago
What exactly is wrong with this function...
  
    (defun sumlist (lst)
       (remove nil lst)
       (mapcar #''+ lst))
  
I''m really at a loss in terms of using the provided debugger/tracing etc as yet in the LispWorks Personal software so I thought I''d toy about a bit with the simpler things first and get comfortable with it. In the event it doesn''t make much sense I''m trying to return the sum of numeric elements in a list. Thanks much in advance.
Advertisement
You're trying to program imperatively. Don't. Very few Lisp functions actually modify their arguments, because that is a no-no in functional programming.

      (defun sumlist (lst)    (mapcar #'+ (remove nil lst)))      

Just so you know, that code only removes nils, not all non-numerical elements in the list.

You can get the results you need with remove-if, numberp, and complement. Have fun. ^_^

[edited by - micepick on May 26, 2003 5:45:03 PM]
quote:Original post by micepick
You''re trying to program imperatively. Don''t. Very few Lisp functions actually modify their arguments, because that is a no-no in functional programming.

I think my biggest stumbling block so far, as I''ve only been doing this for a few hours now is trying to equate things in a C(++)/Java etc fashion which really just doesn''t work.

quote:Original post by micepick
(defun sumlist (lst)
(mapcar #''+ (remove nil lst)))

Thanks!
quote:Original post by micepick
Just so you know, that code only removes nils, not all non-numerical elements in the list.

Yeah I realized as much. I''m just working with available code snippets from ANSI Common Lisp to get a better feel for this "lisp" mystery. Thanks again and I''m sure I''ll have some more obvious questions soon enough. Cheers.
Hmmm...

CL-USER 1 > (defun sumlist (lst)    (mapcar #''+ (remove nil lst)))SUMLISTCL-USER 2 > (sumlist ''(1 2))(1 2)CL-USER 3 > 


I could''ve sworn that worked two days ago...
... Don't know what to tell you. Try making one that actually sums numerical elements. It might work better.

EDIT: Wait, what am I thinking, of course that won't work! I must have been high when I was helping you.

Explanation: mapcar applies a function that takes one argument (try evaluating (+ 1)) to each element of the list consecutively. That's obviously not what you need: You need a function that takes elements of the list two at a time and feeds them to a function that collapses them to one. The function you need is reduce.

[edited by - micepick on May 29, 2003 10:23:42 PM]
But I recall getting a single answer (6) from (1 nil 2 nil 3) from that...
That shouldn''t happen...
Anyway, sorry for the confusion! In case you're not clear on what reduce does, I wrote a simple implementation for you:
(defun reduce (func lst)    (if (= (length lst) 1)                            ; if there's only one element        (car lst)                                     ; just return it    (funcall func (car lst) (reduce func (cdr lst))))) ; otherwise, recurse and reduce

Is that clear?

The great thing about Lisp is, you can implement most of its functions yourself in a couple of lines of code.

[edited by - micepick on May 29, 2003 10:33:21 PM]
Hey look at me I'm smart. Anyway, apply works Sorry for wasting your time once again!
Edit: ah, and thanks for that as well, I'm trying to still get over the c mentality which doesn't seem to want to go away.
quote:Original post by micepick
The great thing about Lisp is, you can implement most of its functions yourself in a couple of lines of code.

I have noticed that and I like the possibilities it offers.

[edited by - kordova on May 29, 2003 10:36:08 PM]
Yeah, apply is fine too, since + takes any number of arguments.

(apply #'+ '(1 2 3))

is equivalent to

(+ 1 2 3)

is equivalent to

(funcall #'+ 1 2 3)

Enjoying yourself?

[edited by - micepick on May 29, 2003 10:36:54 PM]

This topic is closed to new replies.

Advertisement