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