Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Lisp Rocks

by hding (Chaplain)
on May 25, 2001 at 20:11 UTC ( [id://83407]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re: Lisp Rocks
in thread Lisp Rocks

Just some (hopefully) constructive criticism. If I understand correctly, this is supposed to take a list of numbers and multiply only the negative ones together (or return 1 if there are none)? (Which to be fair, it does.) The intent is a little obscured - the second clause in the cond could be more clearly written - as is, you have the result you want as the third condition of the "and" rather than the result that is returned when the "and" clause tests to true - since "and" returns the value of the last argument if all of them are true, and since a cond clause returns the value of the test if it is true but no result clause is specified, this works, but is unclear. Also, the first comment is misleading. You might want to avoid using the name "alist" here - an alist is a specific type of data structure that Lisp programmers will probably expect to see here if you use that name (that threw me off a little at first!). You can just say "list". Finally, Lisp style generally tells us to avoid having parentheses alone on lines. A cleaned up version might be (oh, I slipped minusp in there too :-):

(defun multi-negative (list) "Return the product of the negative numbers in list." (cond ((null list) 1) ((and (numberp (car list)) (minusp (car list))) (* (multi-negative (cdr list)) (car list))) (t (multi-negative (cdr list)))))

There are lots of other fun ways to write this in Lisp, too. Have fun discovering them!

Update: Added a little clarification w.r.t. the "and" stuff. Also, it'd be better to use realp than numberp, as minusp will choke on a complex number.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://83407]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.