{"id":4536,"date":"2016-01-10T10:18:27","date_gmt":"2016-01-10T10:18:27","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=4536"},"modified":"2019-05-07T09:13:02","modified_gmt":"2019-05-07T09:13:02","slug":"common-lisp","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=4536","title":{"rendered":"Common LISP"},"content":{"rendered":"<p><a href=\"http:\/\/dublin.zhaw.ch\/~bkrt\/pspp_6dc3\/\">Unterlagen PSPP HS15<\/a><\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/20568684\/converting-number-to-base-2-binary-string-representation\">Converting number to base-2 (binary) string representation [duplicate]<br \/>\n<\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n(defun int-to-binary-string (i)\r\n  &quot;convert an integer into it's binary representation in string format&quot;\r\n  (let ((res &quot;&quot;))\r\n    (while (not (= i 0))\r\n      (setq res (concat (if (= 1 (logand i 1)) &quot;1&quot; &quot;0&quot;) res))\r\n      (setq i (lsh i -1)))\r\n    (if (string= res &quot;&quot;)\r\n        (setq res &quot;0&quot;))\r\n    res))\r\n<\/pre>\n<h3>Documentation<\/h3>\n<p><a href=\"http:\/\/clqr.boundp.org\/\">Common Lisp Quick Reference<\/a><br \/>\n<a href=\"http:\/\/www.gigamonkeys.com\/book\/\">Practical Common Lisp<\/a><br \/>\n<a href=\"http:\/\/cl-cookbook.sourceforge.net\/index.html\">The Common Lisp Cookbook<\/a><br \/>\n<a href=\"https:\/\/www.cs.cmu.edu\/~dst\/LispBook\/book.pdf\">COMMON LISP: A Gentle Introduction to Symbolic Computation<\/a> (PDF)<br \/>\n<a href=\"http:\/\/landoflisp.com\/\">Land of LISP<\/a>, No Starch Press <a href=\"https:\/\/github.com\/ardumont\/land-of-lisp-lab\">github Source Code<\/a><br \/>\n<a href=\"http:\/\/www.cs.sfu.ca\/CourseCentral\/310\/pwfong\/Lisp\/\">Learning Lisp for CMPT 310<\/a><br \/>\n<a href=\"http:\/\/www.clisp.org\/impnotes\/index.html\">Implementation Notes for GNU CLISP<\/a><br \/>\n<a href=\"http:\/\/www.cliki.net\/\">CLiki Common Lisp wiki<\/a><br \/>\n<a href=\"http:\/\/www.cs.ubbcluj.ro\/~gabis\/plf\/Lab\/ResurseLab\/CLisp\/Using%20and%20Installing%20CLISP.htm\">Using and Installing CLISP <\/a><\/p>\n<h3>Editors<\/h3>\n<p><a href=\"https:\/\/github.com\/blakemcbride\/eclipse-lisp\">Eclipse Common Lisp<\/a><br \/>\n<a href=\"http:\/\/www.cliki.net\/Development\">CLiki: Development &#8211; Editor\/IDE support<\/a><br \/>\n<a href=\"http:\/\/www.cliki.net\/Dandelion\">CLiki: Dandelion<\/a><br \/>\n<a href=\"http:\/\/www.cliki.net\/cusp\">CLiki: Cusp Eclipse IDE plugin<\/a><br \/>\n<a href=\"http:\/\/www.cliki.net\/SBCL\">CLiki: Steel Bank Common Lisp (SBCL)<\/a><br \/>\n<a href=\"http:\/\/sourceforge.net\/projects\/dandelion-ecl\/\">Dandelion &#8211; Eclipse Lisp Plugin<\/a> <a href=\"http:\/\/dandelion-ecl.sourceforge.net\/update\/\">Eclipse Update<\/a><br \/>\n<a href=\"http:\/\/www.ibm.com\/developerworks\/library\/os-eclipse-lispcusp\/\">Develop Lisp applications using the Cusp Eclipse plug-in<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/31637415\/does-anyone-have-a-cusp-plugin-for-eclipse-common-lisp\">Does anyone have a CUSP plugin for Eclipse (Common Lisp)<\/a><br \/>\n<a href=\"https:\/\/code.google.com\/p\/cusp\/wiki\/DevStart\">Getting Cusp running from source<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/1997838\/what-are-the-good-rich-ides-for-lisp\">What are the good \u201crich\u201d IDEs for Lisp?<\/a><br \/>\n<a href=\"http:\/\/www.lispworks.com\/downloads\/index.html\">LispWorks Personal Edition<\/a> (free)<\/p>\n<h3>Common LISP<\/h3>\n<p><a href=\"http:\/\/www.clisp.org\/\">CLISP<\/a><br \/>\n<a href=\"http:\/\/ccl.clozure.com\/\">Clozure Common Lisp<\/a><\/p>\n<p><a href=\"http:\/\/www.lispworks.com\/documentation\/HyperSpec\/Body\/c_data_a.htm\">5.3 The Data and Control Flow Dictionary<\/a><br \/>\n<a href=\"http:\/\/www.lispworks.com\/documentation\/HyperSpec\/Body\/c_conses.htm\">14.2 The Conses Dictionary<\/a><\/p>\n<p><a href=\"http:\/\/www.lispworks.com\/documentation\/HyperSpec\/Body\/t_list.htm\">LIST (System Class)<\/a><br \/>\n<a href=\"http:\/\/clhs.lisp.se\/Body\/f_values.htm\">VALUES (Accessor)<\/a><br \/>\n<a href=\"http:\/\/clhs.lisp.se\/Body\/s_progn.htm\">PROGN (Special Operator)<\/a><br \/>\n<a href=\"http:\/\/clhs.lisp.se\/Body\/f_kwdp.htm\">KEYWORDP (Function)<\/a><br \/>\n<a href=\"http:\/\/www.lispworks.com\/documentation\/HyperSpec\/Body\/s_let_l.htm\">LET (Special Operator)<\/a><br \/>\n<a href=\"http:\/\/www.lispworks.com\/documentation\/HyperSpec\/Body\/f_null.htm\">NULL (Function)<\/a><br \/>\n<a href=\"http:\/\/www.lispworks.com\/documentation\/HyperSpec\/Body\/f_floorc.htm\">FLOOR, CEILING, TRUNCATE, ROUND (Function)<\/a><br \/>\n<a href=\"http:\/\/www.lispworks.com\/documentation\/lw50\/CLHS\/Body\/m_cond.htm\">COND (Macro)<\/a><br \/>\n<a href=\"http:\/\/www.lispworks.com\/documentation\/lw50\/CLHS\/Body\/f_reduce.htm\">REDUCE (Function)<\/a><br \/>\n<a href=\"http:\/\/www.lispworks.com\/documentation\/HyperSpec\/Body\/f_mapc_.htm\">MAPCAR, MAPLIST (Function)<\/a><\/p>\n<h4>Debugger<\/h4>\n<p><a href=\"http:\/\/www.clisp.org\/impnotes\/repl.html\">Top Level Loop<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/11651818\/lisp-how-to-eliminate-restarts\">lisp, how to eliminate restarts<\/a><\/p>\n<pre class=\"brush: plain; title: Quit the debugger; notranslate\" title=\"Quit the debugger\">\r\nThe following restarts are available:\r\nUSE-VALUE      :R1      Input a value to be used instead.\r\nABORT          :R2      Abort debug loop\r\nABORT          :R3      Abort debug loop\r\nABORT          :R4      Abort debug loop\r\nABORT          :R5      Abort main loop\r\nBreak 4 &#x5B;6]&gt; :q\r\n<\/pre>\n<pre class=\"brush: plain; title: Quit CLISP; notranslate\" title=\"Quit CLISP\">\r\n&#x5B;1]&gt; (bye)\r\n&#x5B;1]&gt; (exit)\r\n&#x5B;1]&gt; (quit)\r\n<\/pre>\n<p><strong>value<\/strong><\/p>\n<ol>\n<li>\n<ul>\n<li>a. one of possibly several objects that are the result of an evaluation.<\/li>\n<li>b. (in a situation where exactly one value is expected from the evaluation of a form) the primary value returned by the form.<\/li>\n<li>c. (of forms in an implicit progn) one of possibly several objects that result from the evaluation of the last form, or nil if there are no forms.<\/li>\n<\/ul>\n<\/li>\n<li>an object associated with a name in a binding.<\/li>\n<li>(of a symbol) the value of the dynamic variable named by that symbol.<\/li>\n<li>an object associated with a key in an association list, a property list, or a hash table.<\/li>\n<\/ol>\n<p><strong>multiple values<\/strong><\/p>\n<ol>\n<li>more than one value.\n<ul>\n<li>&#8220;The function truncate returns multiple values.&#8221;<\/li>\n<\/ul>\n<\/li>\n<li>a variable number of values, possibly including zero or one.\n<ul>\n<li>&#8220;The function values returns multiple values.&#8221;<\/li>\n<\/ul>\n<\/li>\n<li>a fixed number of values other than one.\n<ul>\n<li>&#8220;The macro multiple-value-bind is among the few operators in Common Lisp which can detect and manipulate multiple values.&#8221;<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h4>form\/forms vs. datum<\/h4>\n<p><a href=\"http:\/\/www.n-a-n-o.com\/lisp\/cmucl-tutorials\/LISP-tutorial-9.html\">Forms and the Top-Level Loop<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/2877371\/definition-of-lisp-form\">Definition of \u201clisp form\u201d?<\/a><\/p>\n<blockquote><p>\nThe things which you type to the LISP interpreter are called forms; the LISP interpreter repeatedly reads a form, evaluates it, and prints the result. This procedure is called the read-eval-print loop.<\/p>\n<p>Some forms will cause errors. After an error, LISP will put you into the debugger so you can try to figure out what caused the error. LISP debuggers are all different; but most will respond to the command &#8220;help&#8221; or &#8220;:help&#8221; by giving some form of help.<\/p>\n<p>In general, a form is either an atom (for example, a symbol, an integer, or a string) or a list. If the form is an atom, LISP evaluates it immediately. Symbols evaluate to their value; integers and strings evaluate to themselves. If the form is a list, LISP treats its first element as the name of a function; it evaluates the remaining elements recursively, and then calls the function with the values of the remaining elements as arguments.<\/p>\n<p>For example, if LISP sees the form (+ 3 4), it treats + as the name of a function. It then evaluates 3 to get 3 and 4 to get 4; finally it calls + with 3 and 4 as the arguments. The + function returns 7, which LISP prints.<\/p><\/blockquote>\n<p>form = datum + program, that can be evaluated without an error.<br \/>\nThis is a lisp datum, it&#8217;s a list of 3, 4 and 1. This is not a form however as trying to evaluate it does not result into another datum. But rather an error:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n(3 4 1)\r\n<\/pre>\n<p>This is a datum, and a form, also called a &#8216;normal form&#8217; or a &#8216;self-evaluating datum&#8217;, it evaluates to itself:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n3\r\n<\/pre>\n<p>This is a compound form, evaluating it results into the normal form 8:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n(+ 3 4 1)\r\n<\/pre>\n<p>Apart from normal forms and compound forms, compound forms can be subdivided into procedure calls and special forms (also called syntax) but more properly, the head of a special form is the syntax, as in:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n(if (oddp 2) (print &quot;me&quot;) (print &quot;or me&quot;))\r\n<\/pre>\n<p>This is a special form because it&#8217;s head is syntax, and not a procedure, the only difference between procedure calls and special forms is that procedure calls see all of the arguments of the form as forms in itself and try to evaluate it first and special forms not necessarily do that. As we understand, only the second and fourth member of this compound form get evaluated, the first member is syntax, and the third is discarded in this case.<br \/>\nAs we know for instance this code. But it&#8217;s not a form in Common Lisp:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n((a 1) (b 2))\r\n<\/pre>\n<p>So:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n(let ((a 1) (b 2)) (+ a b))\r\n<\/pre>\n<p>Is a special form, it does not evaluate its second member, and evaluates its third member in a different fashion than what would be expected if it was not a special form. That is, a and b as subforms of its third form have a different binding. let in this case is a syntactic keyword that signals the special form.<\/p>\n<h4>arguments &#038;optional, &#038;rest and &#038;key<\/h4>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&amp;optional = optional argument(s)\r\n            Optionale Parameter\r\n\r\n&amp;rest     = rest arguments(s)\r\n            Restparameter\r\n            Werden in eine eigene Liste zusammengefasst.\r\n            &amp;rest steht an letzter Stelle, nach notwendigen\r\n            und optionalen Parametern\r\n\r\n&amp;key      = key arguments\r\n            Statt Parameter-Reihenfolge \u00fcber Key\/Value\r\n&#x5B;code]\r\n(defun foo (&amp;key a b c) (list a b c))\r\n\r\n(foo)                ==&gt; (NIL NIL NIL)\r\n(foo :a 1)           ==&gt; (1 NIL NIL)\r\n(foo :b 1)           ==&gt; (NIL 1 NIL)\r\n(foo :c 1)           ==&gt; (NIL NIL 1)\r\n(foo :a 1 :c 3)      ==&gt; (1 NIL 3)\r\n(foo :a 1 :b 2 :c 3) ==&gt; (1 2 3)\r\n(foo :a 1 :c 3 :b 2) ==&gt; (1 2 3)\r\n<\/pre>\n<h4>keyword or keys in arguments (colons \ud83d\ude42<\/h4>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/25921643\/what-is-the-colon-in-emacs-lisp\">What is the colon (:) in Emacs lisp?<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/8567155\/why-colons-precede-variables-in-common-lisp\">Why colons precede variables in Common Lisp<\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;1] :yellow\r\n:YELLOW\r\n<\/pre>\n<h4>(list) vs. (values)<\/h4>\n<blockquote><p>Most forms create only one value<br \/>\nA form typically returns only one value. Lisp has only a small number of forms which create or receive multiple values.<\/p><\/blockquote>\n<blockquote><p>Normally multiple values are not used. Special forms are required both to produce multiple values and to receive them. If the caller of a function does not request multiple values, but the called function produces multiple values, then the first value is given to the caller and all others are discarded; if the called function produces zero values, then the caller gets nil as a value.<\/p><\/blockquote>\n<p><a href=\"https:\/\/www.cs.cmu.edu\/Groups\/AI\/html\/cltl\/clm\/node94.html\">Constructs for Handling Multiple Values<\/a><br \/>\n<a href=\"https:\/\/psg.com\/~dlamkins\/sl\/chapter03-09.html\">Essential Multiple Values<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/22795608\/values-function-in-common-lisp\/22796346\">alues function in Common Lisp<\/a><br \/>\n<a href=\"http:\/\/programmers.stackexchange.com\/questions\/268107\/values-vs-list-for-returning-multiple-values-from-lisp-form\">`values` vs `list` for returning multiple values from Lisp form<\/a><\/p>\n<h4>(cons) vs. Consing<\/h4>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/2256261\/why-is-consing-in-lisp-slow\">Why is consing in Lisp slow?<\/a><\/p>\n<h4>(progn)<\/h4>\n<p><a href=\"http:\/\/programmers.stackexchange.com\/questions\/243143\/object-oriented-equivalent-of-lisps-progn-function\">Object-Oriented equivalent of LISP&#8217;s progn function?<\/a><\/p>\n<h4>(setq) vs. (setf)<\/h4>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/971734\/lisp-style-setq-vs-setf\">Lisp style: setq vs. setf<\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n(set symbol  foo)   = set symbol to foo (Deprecated!)\r\n(setf place  form)  = set place to primary values of forms (Macro to setq)\r\n(setq symbol form)  = set symbol to primary values of form\r\n<\/pre>\n<h3>EMACS Lisp<\/h3>\n<p><a href=\"http:\/\/www.gnu.org\/software\/emacs\/manual\/html_node\/elisp\/Comment-Tips.html\">Tips on Writing Comments<\/a><\/p>\n<h3>Wikibooks<\/h3>\n<h4>Basic topics<\/h4>\n<p><a href=\"https:\/\/en.wikibooks.org\/wiki\/Common_Lisp\/Basic_topics\/Lists\">Lists<\/a><br \/>\n<a href=\"https:\/\/en.wikibooks.org\/wiki\/Common_Lisp\/Basic_topics\/Functions\">Functions<\/a><\/p>\n<h3>Examples<\/h3>\n<pre class=\"brush: plain; title: Try some simple things; notranslate\" title=\"Try some simple things\">\r\n&#x5B;1]&gt; (+ 2 3)\r\n5\r\n\r\n&#x5B;2]&gt; (print &quot;hello world&quot;)\r\n&quot;hello world&quot;\r\n&quot;hello world&quot;\r\n<\/pre>\n<pre class=\"brush: plain; title: Define a function; notranslate\" title=\"Define a function\">\r\n&#x5B;3]&gt; (defun hello-name (name) (format nil &quot;Hello ~A&quot; name))\r\nHELLO-NAME\r\n\r\n&#x5B;4]&gt; (hello-name 'matt)\r\n&quot;Hello MATT&quot;\r\n<\/pre>\n<pre class=\"brush: plain; title: Load external LISP source; notranslate\" title=\"Load external LISP source\">\r\n&#x5B;5]&gt; (load &quot;power.lisp&quot;)\r\n;; Loading file power.lisp\r\n;; Loaded file power.lisp\r\nT\r\n\r\n&#x5B;6]&gt; (power 3 4)\r\n81\r\n<\/pre>\n<h3>FAQ<\/h3>\n<h4>Question<\/h4>\n<p>Why &#8216;x&#8217; doesn&#8217;t be recognized?<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;13]&gt; (defun beispiel (a b c) (list a b c))\r\nBEISPIEL\r\n\r\n&#x5B;14]&gt; (beispiel 1 2 3)\r\n(1 2 3)\r\n\r\n&#x5B;15]&gt; (beispiel x y z)\r\n\r\n*** - SYSTEM::READ-EVAL-PRINT: variable X has no value\r\n<\/pre>\n<h4>Question<\/h4>\n<p>How to return a value?<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;1]&gt; (defun test123 ()\r\n(print &quot;hallo&quot;)\r\n(print &quot;welt&quot;)\r\n(print &quot;!&quot;))\r\nTEST123\r\n\r\n&#x5B;2]&gt; (test123)\r\n&quot;hallo&quot; \r\n&quot;welt&quot; \r\n&quot;!&quot; \r\n&quot;!&quot;\r\n<\/pre>\n<h4>Answer<\/h4>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;3]&gt; (defun test123 ()\r\n(print &quot;hallo&quot;)\r\n(print &quot;welt&quot;)\r\n(print &quot;!&quot;)\r\nNIL)\r\nTEST123\r\n\r\n&#x5B;4]&gt; (test123)\r\n&quot;hallo&quot; \r\n&quot;welt&quot; \r\n&quot;!&quot; \r\nNIL\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Unterlagen PSPP HS15 Converting number to base-2 (binary) string representation [duplicate] (defun int-to-binary-string (i) &quot;convert an integer into it&#8217;s binary representation in string format&quot; (let ((res &quot;&quot;)) (while (not (= i 0)) (setq res (concat (if (= 1 (logand i 1)) &quot;1&quot; &quot;0&quot;) res)) (setq i (lsh i -1))) (if (string= res &quot;&quot;) (setq res [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4536","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/4536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4536"}],"version-history":[{"count":40,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/4536\/revisions"}],"predecessor-version":[{"id":9598,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/4536\/revisions\/9598"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}