{"id":304,"date":"2018-11-04T11:39:58","date_gmt":"2018-11-04T11:39:58","guid":{"rendered":"https:\/\/books.compclassnotes.com\/elementarycomputing\/?page_id=304"},"modified":"2018-11-15T04:15:45","modified_gmt":"2018-11-15T04:15:45","slug":"lists","status":"publish","type":"page","link":"https:\/\/books.compclassnotes.com\/elementarycomputing\/lists\/","title":{"rendered":"Lists"},"content":{"rendered":"<p>A list is basically a list of\u00a0<em>things<\/em>:<\/p>\n<p>(1 2 3 4)<\/p>\n<p>We call the head of the list the\u00a0<strong>car<\/strong> and the tail of the list (which is everything except the head) is called the\u00a0<strong>cdr<\/strong> (pronounced &#8220;coulder&#8221;).<\/p>\n<p>Let&#8217;s try a few examples with the list (a b c d). Find the car and the cdr, and then find the cdr of the cdr. (That will be\u00a0<strong>(cdr (cdr (a b c d))),\u00a0<\/strong>because cdr is just the same as any other prefix operator and follows the same rules, that is, we execute the most deeply nested on first.<\/p>\n\n<!-- iframe plugin v.6.0 wordpress.org\/plugins\/iframe\/ -->\n<iframe loading=\"lazy\" src=\"https:\/\/my.compclassnotes.com\/canonical\/48606644-ad2b-44ae-bbd6-09e63ce6e47c\" width=\"100%\" height=\"400\" marginwidth=\"0\" marginheight=\"0\" 0=\"scrolling=&quot;yes\u201d\" scrolling=\"yes\" class=\"iframe-class\" frameborder=\"0\"><\/iframe>\n\n<p>Now try to get the cdr of the list\u00a0<strong>(+ 1 2)<\/strong>.<\/p>\n<p>Hm, that seemed to go horribly wrong, didn&#8217;t it? Let&#8217;s go through it step by step:<\/p>\n<p><strong>(cdr (+ 1 2))\u00a0<\/strong><\/p>\n<p>Because we execute the most deeply nested thing first, we evaluate\u00a0<strong>(+ 1 2)<\/strong> which gives us this:<\/p>\n<p><strong>(cdr 3)<\/strong><\/p>\n<p>But\u00a0<strong>3<\/strong> doesn&#8217;t have a tail, so the cdr is the empty list. Some implementations of Lisp will cause an error here because cdr is really only defined to operate on lists and we&#8217;re giving it what&#8217;s called an\u00a0<em>atom<\/em>, which is basically the smallest unit in Lisp and something that doesn&#8217;t have a tail.<\/p>\n<p>Similarly, what would happen if we had an operator called\u00a0<strong>a <\/strong>for the earlier examples? Wouldn&#8217;t that cause the same sort of problem? It turns out that this is a common issue with Lisp, so it is possible to precede a list with a single quote (&#8216;) to tell Lisp not to evaluate the list, even though it may be the most deeply nested thing in brackets.<\/p>\n<p>Try evaluating this:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">(cdr '(+ 1 2))<\/pre>\n\n<!-- iframe plugin v.6.0 wordpress.org\/plugins\/iframe\/ -->\n<iframe loading=\"lazy\" src=\"https:\/\/my.compclassnotes.com\/canonical\/48606644-ad2b-44ae-bbd6-09e63ce6e47c\" width=\"100%\" height=\"400\" marginwidth=\"0\" marginheight=\"0\" 0=\"scrolling=&quot;yes\u201d\" scrolling=\"yes\" class=\"iframe-class\" frameborder=\"0\"><\/iframe>\n\n<p>So we now have very subtle distinction between an expression\u00a0<strong>(+ 1 2)\u00a0<\/strong> and a list &#8216;<strong>(+ 1 2)<\/strong>. Play around with them for a while and when you&#8217;re satisfied with how to manipulate them, let&#8217;s look at how we can use them to evaluate arbitrarily long expressions like we saw in <a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/functional-programming\/\">the last section which introduced Functional Programming.<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A list is basically a list of\u00a0things: (1 2 3 4) We call the head of the list the\u00a0car and the tail of the list (which is everything except the head) is called the\u00a0cdr (pronounced &#8220;coulder&#8221;). Let&#8217;s try a few <span class=\"readmore\"><a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/lists\/\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":4,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-304","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/comments?post=304"}],"version-history":[{"count":11,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/304\/revisions"}],"predecessor-version":[{"id":493,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/304\/revisions\/493"}],"wp:attachment":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/media?parent=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}