Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A full APPLY is difficult in a static language.</p> <p>In Lisp APPLY applies a function to a list of arguments. Both the function and the list of arguments are arguments to APPLY. </p> <ul> <li><p>APPLY can use any function. That means that this could be any result type and any argument types.</p></li> <li><p>APPLY takes arbitrary arguments in arbitrary length (in Common Lisp the length is restricted by an implementation specific constant value) with arbitrary and possibly different types.</p></li> <li><p>APPLY returns any type of value that is returned by the function it got as an argument.</p></li> </ul> <p>How would one type check that without subverting a static type system?</p> <p>Examples:</p> <pre class="lang-lisp prettyprint-override"><code>(apply #'+ '(1 1.4)) ; the result is a float. (apply #'open (list "/tmp/foo" :direction :input)) ; the result is an I/O stream (apply #'open (list name :direction direction)) ; the result is also an I/O stream (apply some-function some-arguments) ; the result is whatever the function bound to some-function returns (apply (read) (read)) ; neither the actual function nor the arguments are known before runtime. ; READ can return anything </code></pre> <p>Interaction example:</p> <pre class="lang-lisp prettyprint-override"><code>CL-USER 49 &gt; (apply (READ) (READ)) ; call APPLY open ; enter the symbol OPEN ("/tmp/foo" :direction :input :if-does-not-exist :create) ; enter a list #&lt;STREAM::LATIN-1-FILE-STREAM /tmp/foo&gt; ; the result </code></pre> <p>Now an example with the function REMOVE. We are going to remove the character a from a list of different things.</p> <pre class="lang-lisp prettyprint-override"><code>CL-USER 50 &gt; (apply (READ) (READ)) remove (#\a (1 "a" #\a 12.3 :foo)) (1 "a" 12.3 :FOO) </code></pre> <p>Note that you also can apply apply itself, since apply is a function.</p> <pre class="lang-lisp prettyprint-override"><code>CL-USER 56 &gt; (apply #'apply '(+ (1 2 3))) 6 </code></pre> <p>There is also a slight complication because the function APPLY takes an arbitrary number of arguments, where only the last argument needs to be a list:</p> <pre class="lang-lisp prettyprint-override"><code>CL-USER 57 &gt; (apply #'open "/tmp/foo1" :direction :input '(:if-does-not-exist :create)) #&lt;STREAM::LATIN-1-FILE-STREAM /tmp/foo1&gt; </code></pre> <p>How to deal with that?</p> <ul> <li><p>relax static type checking rules</p></li> <li><p>restrict APPLY</p></li> </ul> <p>One or both of above will have to be done in a typical statically type checked programming language. Neither will give you a fully statically checked and fully flexible APPLY.</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