Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I saw several opinions about using <code>apply</code> is a bad style. But actually that would be great if somebody will explain me why <code>apply</code> is considered to be bad. </p> <p>What do you mean using a word "lispy". Common lisp allows to program in any style you want. </p> <p>If "lispy" means functional programming style, then the first code is written in more functional programming style. A function is passed to a function <code>mapcar</code> and another function is passed to <code>apply</code> and all the job is done by passing the results of one function to another. In you code you don't pass functions as arguments to other functions. But recursion can be considered as functional programming style sign. And code in the book is shorter than yours.</p> <p>If you don't like <code>apply</code> because of apply determines the argument count in runtime, you can use <code>reduce</code> in this situation (if I understood the data structures correctly): (Thanks to Joshua Taylor for pointing a huge resource overhead without <code>:from-end t</code> key argument) </p> <pre><code>(defun describe-paths (location edges) (reduce #'append (mapcar #'describe-path (rest (assoc location edges))) :from-end t)) </code></pre> <p>Anyway I'm pretty sure that the purpose of the code in the book is the education reason. It's an example of <code>mapcar</code> and <code>apply</code> that shows how lists are treated as data and code in lisp.</p> <p>p.s. Actually I figured why <code>apply</code> can be bad (stack is used for function calls). </p> <pre><code>&gt; (apply #'+ (make-list 500000 :initial-element 1)) *** - Lisp stack overflow. RESET </code></pre> <p>So as Rainer Joswig told it's lispy to avoid stack overflows. Reduce fix the problem.</p> <pre><code>&gt; (reduce #'+ (make-list 50000000 :initial-element 1)) 50000000 </code></pre>
 

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