git.m455.casa

m455.casa

clone url: git://git.m455.casa/m455.casa


html/archive/2020/having-fun-with-lisps.html

1 <!DOCTYPE html>
2 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
3 <head>
4 <meta charset="utf-8" />
5 <meta name="generator" content="pandoc" />
6 <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
7 <title>Having fun with Lisp(s)</title>
8 <style>
9 code{white-space: pre-wrap;}
10 span.smallcaps{font-variant: small-caps;}
11 span.underline{text-decoration: underline;}
12 div.column{display: inline-block; vertical-align: top; width: 50%;}
13 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
14 ul.task-list{list-style: none;}
15 </style>
16 <style>
17 body {
18 line-height: 1.5;
19 font-family: sans-serif;
20 font-size: 18px;
21 margin: 20px auto;
22 max-width: 630px;
23 }
24
25 a {
26 color: blue;
27 }
28
29 code, pre {
30 background-color: #fddee3;
31 font-size: 14px;
32 }
33
34 pre {
35 padding: 25px 25px;
36 overflow: auto;
37 }
38
39 pre code {
40 white-space: pre;
41 }
42
43 img {
44 max-width: 100%;
45 }
46
47 table {
48 border-collapse: collapse;
49 }
50
51 table caption {
52 font-weight: bold;
53 margin: 10px 0px;
54 text-align: left;
55 }
56
57 th, td {
58 border: 1px solid #000;
59 padding: 4px;
60 }
61
62 blockquote {
63 border-left: 3px solid #000;
64 padding-left: 10px;
65 }
66
67 .border {
68 border: 1px solid #000;
69 margin: 25px 0px;
70 padding: 5px 25px;
71 }
72
73 @media only screen and (max-width: 700px) {
74 body {
75 margin: 10px;
76 }
77 }
78
79 @media (prefers-color-scheme: dark) {
80 body {
81 background-color: #111;
82 color: #eee;
83 }
84 a {
85 color: #009fff;
86 }
87 code, pre {
88 background-color: #111;
89 color: #fd6363;
90 }
91 pre {
92 padding: 15px 25px;
93 }
94 blockquote {
95 border-left: 3px solid #666;
96 }
97 .border, th, td {
98 border: 1px solid #666;
99 }
100 }
101 </style>
102 </head>
103 <body>
104 <main>
105 <h2 id="having-fun-with-lisps">Having fun with Lisp(s)</h2>
106 <p>2020-06-07 00:00</p>
107 <p>A while back, I was commuting across the city to work. It took about an hour and a half to commute one way, so I had a lot of free time on the bus if I wasn’t falling asleep from the same monotonous view every day.</p>
108 <p>During this time, if I wasn’t sleeping, I was either reading programming books or doing programming puzzles in my notebook. Sometimes the bumps would get the best of me, but what can you do?</p>
109 <p>There were a few books that I really enjoyed, and I didn’t finish any of them. I kind of just hopped through different pages learning about different things, because these were reference books, not novels, and that’s what do you do with these right? Okay, that was an excuse. Sometimes, my attention span gets the best of me, and I need to move onto something else, so I would always have at least two books with me to jump between.</p>
110 <p>Before leaving in the morning, I would choose two books, from the four books I was studying, to read on the bus:</p>
111 <ul>
112 <li>The Structure and Interpretation of Programming Languages (SICP)</li>
113 <li>The Little Schemer</li>
114 <li>Living Clojure</li>
115 <li>How to Design Programs (HtDP)</li>
116 </ul>
117 <h3 id="reading-sicp">Reading SICP</h3>
118 <p>SICP’s language felt like I was reading either a really long computer science riddle, or an ancient wizard’s spell book, so it was hard to focus on that, especially since I don’t have a background in computer science. I think I restarted SICP several times, worrying that I missed something that required me to understand something further in the book haha. I also learned that all of that “useless” math I learned in grade school was suddenly useful to the hobbyist programmer in me as well haha.</p>
119 <h3 id="reading-the-little-schemer">Reading The Little Schemer</h3>
120 <p>The Little Schemer is still one of my favourite books. <a href="https://www.insom.me.uk/">A friend</a> knew I was learning Racket/Scheme, and was kind enough to lend me a hard copy for a few years. I really think this book changed my way of thinking. It teaches you how to solve programming problems using recursion. I had no clue you could solve division problems just with recursion and a <code>(- 1 ...)</code> procedure. That blew my mind!</p>
121 <p>While I was writing my <a href="https://%7B%7Bgit-domain%7D%7D/pancake">static website generator</a>, I ended up coming back into The Little Schemer mindset: I wrote a little procedure that recursively checks if all elements in one list exist in another list, and if there are any missing, it returns the missing elements in a new list.</p>
122 <p>Here’s the code for that procedure below:</p>
123 <pre><code>;; lst1 is a list of required elements
124 ;; lst2 is a list of elements to check
125
126 (define (get-missing lst1 lst2)
127 (if (null? lst1)
128 null
129 (if (member (car lst1) lst2)
130 (get-missing (cdr lst1) lst2)
131 (cons (car lst1)
132 (get-missing (cdr lst1) lst2)))))</code></pre>
133 <p>I ended up using this procedure to check if all directories or files existed, and if not, my program would offer to “repair” the files or directories for the user.</p>
134 <h3 id="reading-living-clojure">Reading Living Clojure</h3>
135 <p><a href="https://selfsamegames.com">Another friend</a> recommended me the Living Clojure book, and had mentioned that, at the time, it was available on <a href="https://www.humblebundle.com/">HumbleBundle</a>, so I bought it as an ebook. Even though I only got three quarters of the way through it, I feel like it really helped kick-start my Lisp-learning adventure.</p>
136 <p>Before Clojure, I had started learning Lisp-like languages, such as <a href="https://docs.hylang.org/en/stable/">Hy</a>, but after getting into Clojure, everything felt so great programming-wise. With Hy, it felt like I was trying to force Python habits into Lisp, which didn’t feel right.</p>
137 <p>Don’t get me wrong, Hy is super cool, and I still want to get back into it eventually. It was just that, at the time, I was really enjoying Clojure (and still do).</p>
138 <p>One thing I really like about Clojure is its choice on syntax in comparison to other lisps. It’s a lot less verbose.</p>
139 <p>For example, here’s Clojure’s <code>let</code> statement:</p>
140 <pre><code>(let [x value] body ...)</code></pre>
141 <p>in comparison to Racket/Scheme’s <code>let</code> statement:</p>
142 <pre><code>(let ([x value]) body ...)</code></pre>
143 <p>and Clojure’s anonymous function:</p>
144 <pre><code>(fn [x] body ...)</code></pre>
145 <p>in comparison to Racket/Scheme’s anonymous procedure:</p>
146 <pre><code>(lambda (x) ...)</code></pre>
147 <p>It’s just way less typing!</p>
148 <p>… Okay, the lambda syntax in the last example is only a few letters less, but you get the idea!</p>
149 <h3 id="reading-how-to-design-programs">Reading How to Design Programs</h3>
150 <p>With this book, I had a similar experience to SICP in that I had kept restarting it, or had just used it as a reference book by jumping between sections. Also, not having a REPL on the bus probably contributed to some of the struggles too, haha.</p>
151 <p>I had started somewhere in the middle of the book, because some of the beginning problems seemed too easy. I was wrong in doing that, because I missed a lot of the great knowledge HtDP gives you in the beginning of the book, so I’m starting it over by keeping a notebook-like git repository, and working through the exercises properly, just like a school book. It feels like a good compromise between SICP and The Little Schemer, though I still stand in saying that The Little Schemer is still one of my favourite programming books.</p>
152 <p>After I finish HtDP, if I ever do, it’s a huge book, I plan on tackling SICP, in hopes that I’m better prepared to take on some of its trickier math and mind exercises.</p>
153 <h3 id="having-fun-with-recursion">Having fun with recursion</h3>
154 <p>Even after having gone through most of The Little Scheme pretty thoroughly, I still catch myself “doodling” with code once in a while.</p>
155 <p>One night I was in the mood to fiddle with some recursive programming, but nothing that involved a project or anything serious, just something I could play with and have fun. I decided to make a procedure that “ate” a list of food, and displayed each food item that it ate. After it finished “eating” all the food items in the list, it would “burp”:</p>
156 <pre><code>##lang racket
157 (define (eat-everything listof-food)
158 (if (null? listof-food)
159 (displayln &quot;burp&quot;)
160 (begin (displayln (string-append &quot;ate &quot; (car listof-food)))
161 (eat-everything (cdr listof-food)))))</code></pre>
162 <p>This is more or less a for loop implemented using recursion, but how it works is it first checks if the list you provide is empty. In racket <code>null</code> or <code>'()</code> both mean the same thing: “empty”. If the list is empty, then print <code>"burp"</code> to the screen; however, if the list is not empty, then the first item of the list is joined with the word <code>"ate "</code> and printed to the user. The procedure then calls the remaining items of the list as its new parameter. The process the repeats, but the first item of the list is now the next item in the list, each time the procedure calls itself.</p>
163 <p>I still wasn’t done though! I wanted to play more! So I made a picky procedure that eats all items in a list, except for one item of food, and then returns the same list with that item removed:</p>
164 <pre><code>(define (eat-everything-but food listof-food)
165 (if (null? listof-food)
166 &#39;()
167 (if (equal? (car listof-food) food)
168 (eat-everything-but food (cdr listof-food))
169 (cons (car listof-food)
170 (eat-everything-but food (cdr listof-food))))))</code></pre>
171 <p>The way this procedure works is similar, but the way it handles the first item in the list it is given is different. In this procedure the first item is checked to see if it equals the <code>food</code> parameter value, which is the item we don’t want our procedure to “eat”, if it equals that item, then the procedure calls itself with the rest of the items in the list as its new parameter.</p>
172 <p>As an example, the first time around, the parameter could be something like:</p>
173 <pre><code>&#39;(&quot;apple&quot; &quot;orange&quot; &quot;banana&quot;)</code></pre>
174 <p>Then, when the procedure calls itself, it only takes in the rest of the items in the list using the <code>(cdr ..)</code> procedure as a parameter like so:</p>
175 <pre><code>(cdr &#39;(&quot;apple&quot; &quot;orange&quot; &quot;banana&quot;))</code></pre>
176 <p>which would result in:</p>
177 <pre><code>&#39;(&quot;orange&quot; &quot;banana&quot;)</code></pre>
178 <p>When checking the rest of the items, it, again, checks the first item in the list, which is the next item in the list of the original list we fed the procedure. If the item is not equal to the <code>food</code> parameter value, it adds that item to the list, because it’s an item we want to keep.</p>
179 <p>The <code>(cons (car listof-food) (eat-everything-but ...)</code> creates a little waiting line of items to be added to a list. When the procedure recursively reaches the end of the list, our procedure returns <code>'()</code>. This is important, because all of the items that were “waiting in line” can now be “<code>cons</code>ed” onto <code>'()</code> to create a new list like so:</p>
180 <pre><code>(cons &quot;apples&quot; (cons &quot;bananas&quot; (cons &quot;oranges&quot; &#39;())))</code></pre>
181 <p>which would return <code>'("apples" "bananas" "oranges")</code>.</p>
182 <h3 id="wrapping-up">Wrapping up</h3>
183 <p>Okay, this blog post started to get unstructured really quickly, but I had a lot of fun writing it, and I don’t get to do a lot of unstructured writing as a technical writer! haha.</p>
184 <p>P.S. Thanks to everyone who has ever helped me understand anything. Programming is such an adventure for me!</p>
185 </main>
186 </body>
187 </html>