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.


In reply to Re: Re: Lisp Rocks by hding
in thread Lisp Rocks by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.