summaryrefslogtreecommitdiff
path: root/docsrc/xlisp/xlisp-doc/tutorials/lisp-faq.htm
diff options
context:
space:
mode:
Diffstat (limited to 'docsrc/xlisp/xlisp-doc/tutorials/lisp-faq.htm')
-rw-r--r--docsrc/xlisp/xlisp-doc/tutorials/lisp-faq.htm199
1 files changed, 199 insertions, 0 deletions
diff --git a/docsrc/xlisp/xlisp-doc/tutorials/lisp-faq.htm b/docsrc/xlisp/xlisp-doc/tutorials/lisp-faq.htm
new file mode 100644
index 0000000..a00e621
--- /dev/null
+++ b/docsrc/xlisp/xlisp-doc/tutorials/lisp-faq.htm
@@ -0,0 +1,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>&nbsp; -&nbsp;
+<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>&nbsp;&nbsp;</code></nobr></td>
+ <td class="button"><nobr><code>(apply #'and <font color="#0000CC">list</font>)</code></nobr></td>
+ <td><nobr>&nbsp;&rarr;&nbsp;</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>&nbsp;&nbsp;</code></nobr></td>
+ <td class="button"><nobr><code>(apply #'or&nbsp; <font color="#0000CC">list</font>)</code></nobr></td>
+ <td><nobr>&nbsp;&rarr;&nbsp;</nobr></td>
+ <td class="button"><nobr><code>(some&nbsp; #'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> (&amp;rest args)
+ (eval (cons 'and args)))
+
+(defun <font color="#0000CC">or-fn</font> (&amp;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> (&amp;rest args)
+ (dolist (x args t)
+ (and (not x) (return nil))))
+
+(defun <font color="#0000CC">or-fn</font> (&amp;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>&nbsp;&nbsp;<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>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
+
+<hr>
+
+<a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
+<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>