Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the most polite sense, your code is a bit off. You're learning Lisp this week, aren't you? That's OK! It's a fun language and can really do some awesome things.</p> <p>So I'm going to walk through the creation of the routine, and take you along the tour.</p> <p>Your basic case is - </p> <pre><code>(defun negate (n) (if (&gt; n 0) (- 0 n))) (map #'negate '(1 2 3 4)) </code></pre> <p>Walking the tree is more complex, but let's walk through the ideas.</p> <p>Essentially, you have three cases to answer: is the current element nil, a list or an atom?</p> <pre><code>(if (not (car seq)) (if (listp (car seq)) ;;Recurse ;;Otherwise negate the current element and append it to the recursed. </code></pre> <p>Let's try a first cut at this:</p> <pre><code>(defun negate-seq (seq) (if (not seq) (return-from negate-seq)) (if (listp (car seq)) (negate-seq seq) (list (negate (car seq)) (negate-seq (cdr seq))))) </code></pre> <p>That's great! Except...</p> <pre><code>(negate-seq '(1 2)) ==&gt; (-1 (-2 NIL)) </code></pre> <p>And...</p> <pre><code> (negate-seq '(1 (1 2 -3))) ==&gt; STACK OVERFLOW! </code></pre> <p>Oh boy. We're in trouble now.</p> <p>First, let's just try a <code>cons</code> instead of a <code>list</code>. That cleans up the weird nested list problem.</p> <p>It's obvious that we're gotten into a loop of infinite recursion. That shouldn't be possible, because we've got the <code>not seq</code> guard. Okay, so let's try an debug. I'm using CLISP, and I can trace arguments with:</p> <pre><code>(trace 'negate-seq) </code></pre> <p>then,</p> <pre><code>(negate-seq '(1 (1 2 -3))) </code></pre> <p>Suddenly I see an explosion of</p> <pre><code>1621. Trace: (NEGATE-SEQ '((1 2 -3))) 1622. Trace: (NEGATE-SEQ '((1 2 -3))) 1623. Trace: (NEGATE-SEQ '((1 2 -3))) 1624. Trace: (NEGATE-SEQ '((1 2 -3))) </code></pre> <p>Crikey, I forgot my cdr and to cons up the list case! Hmmmm.</p> <p>Let's try this:</p> <pre><code>(defun negate-seq (seq) (if (not seq) (return-from negate-seq)) (if (listp (car seq)) (cons (negate-seq (car seq)) (negate-seq (cdr seq))) (cons (negate (car seq)) (negate-seq (cdr seq))))) </code></pre> <p>Recurse for the car, recuse on the car, cons them together, we might be on to something.</p> <pre><code> (negate-seq '(1 (1 2 -3))) =&gt; (-1 (-1 -2 NIL) </code></pre> <p>Hmmmm. Let's take a look at the trace.</p> <blockquote> <ol> <li>Trace: (NEGATE-SEQ '(1 (1 2 -3)))</li> <li>Trace: (NEGATE-SEQ '((1 2 -3)))</li> <li>Trace: (NEGATE-SEQ '(1 2 -3))</li> <li>Trace: (NEGATE-SEQ '(2 -3))</li> <li>Trace: (NEGATE-SEQ '(-3))</li> <li>Trace: (NEGATE-SEQ 'NIL)</li> <li>Trace: NEGATE-SEQ ==> NIL</li> <li>Trace: NEGATE-SEQ ==> (NIL)</li> <li>Trace: NEGATE-SEQ ==> (-2 NIL)</li> <li>Trace: NEGATE-SEQ ==> (-1 -2 NIL)</li> <li>Trace: (NEGATE-SEQ 'NIL)</li> <li>Trace: NEGATE-SEQ ==> NIL</li> <li>Trace: NEGATE-SEQ ==> ((-1 -2 NIL))</li> <li>Trace: NEGATE-SEQ ==> (-1 (-1 -2 NIL))</li> </ol> </blockquote> <p>So I recurse until the -3, then.... it falls off? Odd. Ah! I'm continually grabbing the CDR of things. A CDR is always a list. (cdr '(-3)) is nil!</p> <p>Let's see here....</p> <p>(much rummaging around)</p> <p>Negate returns nil on positive. D'oh. </p> <pre><code>(defun negate (n) (if ( &gt; n 0) (- 0 n) n)) (defun negate-seq (seq) "Written by Paul Nathan" (if (not seq) (return-from negate-seq)) (if (listp (car seq)) (cons (negate-seq (car seq)) (negate-seq (cdr seq))) (cons (negate (car seq)) (negate-seq (cdr seq))))) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload