Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, make sure you have <code>lexical-binding</code> enabled. The simplest way to do so is to evaluate <code>(setq lexical-binding t)</code> in your current buffer. More information on the topic can be found <a href="http://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html" rel="nofollow">here</a>.</p> <p>Your definition of <code>divisible-by-z</code> is basically correct except that you have a mistype (naming both parameters <code>z</code>; the lambda's parameter should be <code>x</code>). Also, it would be more idiomatic to introduce the binding for <code>remainder</code> with <code>let</code> - <code>setq</code> is generally reserved for mutating bindings that already exist. Here's the result:</p> <pre><code>(defun divisible-by-z (z) (lambda (x) (let ((remainder (% x z))) (if (= remainder 0) 1 0)))) </code></pre> <p>You can't use <code>defun</code> to create <code>divisible-by-3</code> in quite the way you've tried - it's expecting the argument list for a new function to be where you have the call to <code>divisible-by-z</code>.</p> <p>You could either create a global, dynamic binding with</p> <pre><code>(defvar divisible-by-3 (divisible-by-z 3)) </code></pre> <p>Or a local, lexical binding with</p> <pre><code>(let ((divisible-by-3 (divisible-by-z 3))) ...) </code></pre> <p>Either way, you'll then need to use <code>funcall</code> to call the function</p> <pre><code>(funcall divisible-by-3 9) ; =&gt; 1 </code></pre> <p>Of course, you could also skip giving it its own name entirely and simply</p> <pre><code>(funcall (divisible-by-z 3) 10) ; =&gt; 0 </code></pre> <p><code>funcall</code> is necessary because Emacs Lisp is (basically) a Lisp-2, meaning it can attach both a function and a value to a given symbol. So when you're treating functions as values (returning one from a function or passing one in to a function as a parameter) you essentially have to tell it to look in that value "cell" rather than the usual function cell. If you search for "Lisp-1 vs Lisp-2" you'll find more than you want to know about this.</p>
    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. 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.
    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