clone url: git://


1 <!DOCTYPE html>
2 <html xmlns="" 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 <link rel="stylesheet" href="/assets/archive.css">
17 </head>
18 <body>
19 <main>
20 <h2 id="having-fun-with-lisps">Having fun with Lisp(s)</h2>
21 <p>2020-06-07 00:00</p>
22 <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>
23 <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>
24 <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>
25 <p>Before leaving in the morning, I would choose two books, from the four books I was studying, to read on the bus:</p>
26 <ul>
27 <li>The Structure and Interpretation of Programming Languages (SICP)</li>
28 <li>The Little Schemer</li>
29 <li>Living Clojure</li>
30 <li>How to Design Programs (HtDP)</li>
31 </ul>
32 <h3 id="reading-sicp">Reading SICP</h3>
33 <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>
34 <h3 id="reading-the-little-schemer">Reading The Little Schemer</h3>
35 <p>The Little Schemer is still one of my favourite books. <a href="">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>
36 <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>
37 <p>Here’s the code for that procedure below:</p>
38 <pre><code>;; lst1 is a list of required elements
39 ;; lst2 is a list of elements to check
41 (define (get-missing lst1 lst2)
42 (if (null? lst1)
43 null
44 (if (member (car lst1) lst2)
45 (get-missing (cdr lst1) lst2)
46 (cons (car lst1)
47 (get-missing (cdr lst1) lst2)))))</code></pre>
48 <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>
49 <h3 id="reading-living-clojure">Reading Living Clojure</h3>
50 <p><a href="">Another friend</a> recommended me the Living Clojure book, and had mentioned that, at the time, it was available on <a href="">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>
51 <p>Before Clojure, I had started learning Lisp-like languages, such as <a href="">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>
52 <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>
53 <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>
54 <p>For example, here’s Clojure’s <code>let</code> statement:</p>
55 <pre><code>(let [x value] body ...)</code></pre>
56 <p>in comparison to Racket/Scheme’s <code>let</code> statement:</p>
57 <pre><code>(let ([x value]) body ...)</code></pre>
58 <p>and Clojure’s anonymous function:</p>
59 <pre><code>(fn [x] body ...)</code></pre>
60 <p>in comparison to Racket/Scheme’s anonymous procedure:</p>
61 <pre><code>(lambda (x) ...)</code></pre>
62 <p>It’s just way less typing!</p>
63 <p>… Okay, the lambda syntax in the last example is only a few letters less, but you get the idea!</p>
64 <h3 id="reading-how-to-design-programs">Reading How to Design Programs</h3>
65 <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>
66 <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>
67 <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>
68 <h3 id="having-fun-with-recursion">Having fun with recursion</h3>
69 <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>
70 <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>
71 <pre><code>##lang racket
72 (define (eat-everything listof-food)
73 (if (null? listof-food)
74 (displayln &quot;burp&quot;)
75 (begin (displayln (string-append &quot;ate &quot; (car listof-food)))
76 (eat-everything (cdr listof-food)))))</code></pre>
77 <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>
78 <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>
79 <pre><code>(define (eat-everything-but food listof-food)
80 (if (null? listof-food)
81 &#39;()
82 (if (equal? (car listof-food) food)
83 (eat-everything-but food (cdr listof-food))
84 (cons (car listof-food)
85 (eat-everything-but food (cdr listof-food))))))</code></pre>
86 <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>
87 <p>As an example, the first time around, the parameter could be something like:</p>
88 <pre><code>&#39;(&quot;apple&quot; &quot;orange&quot; &quot;banana&quot;)</code></pre>
89 <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>
90 <pre><code>(cdr &#39;(&quot;apple&quot; &quot;orange&quot; &quot;banana&quot;))</code></pre>
91 <p>which would result in:</p>
92 <pre><code>&#39;(&quot;orange&quot; &quot;banana&quot;)</code></pre>
93 <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>
94 <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>
95 <pre><code>(cons &quot;apples&quot; (cons &quot;bananas&quot; (cons &quot;oranges&quot; &#39;())))</code></pre>
96 <p>which would return <code>'("apples" "bananas" "oranges")</code>.</p>
97 <h3 id="wrapping-up">Wrapping up</h3>
98 <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>
99 <p>P.S. Thanks to everyone who has ever helped me understand anything. Programming is such an adventure for me!</p>
100 </main>
101 </body>
102 </html>