Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First note that <code>'()</code> and <code>()</code> are equivalent, since empty list is self evaluating and equal to <code>NIL</code>, and in any case you don't need those in <a href="http://www.lispworks.com/documentation/HyperSpec/Body/s_let_l.htm" rel="noreferrer">LET</a> because <code>NIL</code> is implied by `(let (variable) ...) syntax, which is a reason why you need a parenthesis around each binding when giving the a starting value.</p> <p>Not that using <code>LET</code> is necessary for this case. Using <a href="http://www.lispworks.com/documentation/HyperSpec/Body/m_loop.htm" rel="noreferrer">LOOP</a> features more extensively this function can be written as:</p> <pre><code>(defun unzip (seq) "Takes an even-length list and breaks it apart by evens/odd index" (loop for n from 0 for element in seq if (oddp n) collect element into oddresult else collect element into evenresult finally (return (list oddresult evenresult)))) </code></pre> <p>Personally I prefer <a href="http://common-lisp.net/project/iterate/" rel="noreferrer">iterate</a> for most iteration, using which it can be written as:</p> <pre><code>(defun unzip (seq) "Takes an even-length list and breaks it apart by evens/odd index" (iter (for element in seq) (for n from 0) (if (oddp n) (collect element into oddresult) (collect element into evenresult)) (finally (return (list oddresult evenresult))))) </code></pre> <p>or even:</p> <pre><code>(defun unzip (seq) "Takes an even-length list and breaks it apart by evens/odd index" (iter (generate element in seq) (collect (next element) into evenresult) (collect (next element) into oddresult) (finally (return (list oddresult evenresult))))) </code></pre> <p>EDIT: Additional notes: The name <code>unzip</code> conventionally denotes a slightly different function. The argument name should really be <code>list</code>, since <code>seq</code> would suggest the the function also takes vectors. While it is possible to have functions operating on generalized sequences, it is usually not recommended, since lists and vectors have different performance characteristics. In particular, random access through <code>NTH</code> is linear time for lists, which means you should almost never use it. Even if the time cost is insignificant it usually indicates you should be using a different datastructure.</p>
 

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