1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
<html><head>
<title>Lisp FAQ</title>
<style type="text/css">
.example {
color: #000000;
background-color: #F5F5F5;
padding: 8px;
border: #808080;
border-style: solid;
border-width: 1px;
width:auto;
}
.button {
color: #000000;
background-color: #F5F5F5;
padding-top: 1px;
padding-bottom: 1px;
padding-left: 4px;
padding-right: 8px;
border: #808080;
border-style: solid;
border-width: 1px;
white-space: pre;
}
.box {
color: #000000;
padding-top: 4px;
padding-bottom: 4px;
padding-left: 16px;
padding-right: 16px;
border: #808080;
border-style: solid;
border-width: 1px;
}
</style>
</head>
<body>
<a href="../start.htm">Nyquist / XLISP 2.0</a> -
<a href="../manual/contents.htm">Contents</a> |
<a href="tutorials.htm">Tutorials</a> |
<a href="../examples/examples.htm">Examples</a> |
<a href="../reference/reference-index.htm">Reference</a>
<hr>
<h1>Lisp FAQ</h1>
<hr>
<ol>
<li><nobr><a href="#and-or-with-apply-funcall">Why do #'and and #'or not work with apply and funcall?</a></nobr></li>
<li><nobr><a href="#more-faqs">Where can I find more Lisp FAQs in the Internet?</a></nobr></li>
</ol>
<a name="and-or-with-apply-funcall"></a>
<hr>
<h2>Why do #'and and #'or not work with apply and funcall?</h2>
<hr>
<p><nobr>From
<a href="http://www.cs.cmu.edu/Groups/AI/html/faqs/lang/lisp/part3/faq-doc-3.html"
>http://www.cs.cmu.edu/Groups/AI/html/faqs/lang/lisp/part3/faq-doc-3.html</a></nobr></p>
<p>The short answer is that <a href="../reference/and.htm">and</a> and
<a href="../reference/or.htm">or</a> are special operators, not functions,
while <a href="../reference/apply.htm">apply</a> and
<a href="../reference/funcall.htm">funcall</a> can only be used to invoke
functions, not macros and special operators.</p>
<p>The reason why <a href="../reference/and.htm">and</a> and <a
href="../reference/or.htm">or</a> are special operators is because they
implement control structure in addition to computing a boolean value. They
evaluate their subforms sequentially from left to right, and stop evaluating
subforms as soon as the result can be determined. This is referred to as
'<nobr>short circuiting</nobr>'. <a href="../reference/apply.htm">apply</a>
and <a href="../reference/funcall.htm">funcall</a>, however, are ordinary
functions. Therefore, their arguments are evaluated before they are called.
Thus, were <a href="../reference/apply.htm">apply</a> able to be used with
#'<a href="../reference/and.htm">and</a> and
<a href="../reference/or.htm">or</a>, the <nobr>short-circuiting</nobr>
would be defeated.</p>
<p>Perhaps you don't really care about the <nobr>short-circuiting</nobr>,
and simply want the functional, boolean interpretation. While this may be a
reasonable interpretation of trying to
<a href="../reference/apply.htm">apply</a>
<a href="../reference/and.htm">and</a>
<nobr>or <a href="../reference/or.htm">or</a></nobr>, it doesn't generalize
to other macros well, so there's no obvious way to have the Lisp system 'do
the right thing' when trying to apply macros. <nobr>The only</nobr> function
associated with a macro is its expander function. This function accepts and
returns and form, so it cannot be used to compute the value.</p>
<p>The <nobr>Common Lisp</nobr> functions EVERY, SOME and IDENTITY can be
used to get the functionality you intend when trying to
<a href="../reference/apply.htm">apply</a>
#'<a href="../reference/and.htm">and</a>
<nobr>and #'<a href="../reference/or.htm">or</a></nobr>:</p>
<p><table cellpadding="0" cellspacing="0"><tbody>
<tr>
<td><nobr><code> </code></nobr></td>
<td class="button"><nobr><code>(apply #'and <font color="#0000CC">list</font>)</code></nobr></td>
<td><nobr> → </nobr></td>
<td class="button"><nobr><code>(every #'identity <font color="#0000CC">list</font>)</code></nobr></td>
</tr>
<tr>
<td height="2px"></td>
</tr>
<tr>
<td><nobr><code> </code></nobr></td>
<td class="button"><nobr><code>(apply #'or <font color="#0000CC">list</font>)</code></nobr></td>
<td><nobr> → </nobr></td>
<td class="button"><nobr><code>(some #'identity <font color="#0000CC">list</font>)</code></nobr></td>
</tr>
</tbody></table></p>
<p>Defining <a href="../reference/and.htm">and</a> and
<a href="../reference/or.htm">or</a> as functions using
<a href="../reference/cons.htm">cons</a> and
<a href="../reference/eval.htm">eval</a>:</p>
<pre class="example">
(defun <font color="#0000CC">and-fn</font> (&rest args)
(eval (cons 'and args)))
(defun <font color="#0000CC">or-fn</font> (&rest args)
(eval (cons 'or args)))
</pre>
<p>Defining <a href="../reference/and.htm">and</a> and
<a href="../reference/or.htm">or</a> as functions using
<a href="../reference/dolist.htm">dolist</a>:</p>
<pre class="example">
(defun <font color="#0000CC">and-fn</font> (&rest args)
(dolist (x args t)
(and (not x) (return nil))))
(defun <font color="#0000CC">or-fn</font> (&rest args)
(dolist (x args nil)
(and x (return t))))
</pre>
<pre class="example">
(defun <font color="#0000CC">apply*</font> (function args)
(if (eq (type-of function) 'fsubr)
(eval (cons function args))
(apply function args)))
</pre>
<p><nobr> <a href="#top">Back to top</a></nobr></p>
<a name="more-faqs"></a>
<hr>
<h2>Where can I find more Lisp FAQs in the Internet?</h2>
<hr>
<ul>
<li><nobr><a href="http://www.cs.cmu.edu/Groups/AI/html/faqs/lang/lisp/top.html"
>http://www.cs.cmu.edu/Groups/AI/html/faqs/lang/lisp/top.html</a>
- comp.lang.lisp FAQ from the CMU AI Repository</nobr></li>
</ul>
<p><nobr> <a href="#top">Back to top</a></nobr></p>
<hr>
<a href="../start.htm">Nyquist / XLISP 2.0</a> -
<a href="../manual/contents.htm">Contents</a> |
<a href="tutorials.htm">Tutorials</a> |
<a href="../examples/examples.htm">Examples</a> |
<a href="../reference/reference-index.htm">Reference</a>
</body></html>
|