Skip to content

Latest commit

 

History

History
27 lines (16 loc) · 3.71 KB

28.md

File metadata and controls

27 lines (16 loc) · 3.71 KB

Day 1

Today I decided to do (most of) Chapter 3 of Practical Common Lisp. I figured it would be a good next move since I'm liking the book so far and this chapter is dedicated to getting people familiar with what it feels like to program in Lisp. To do this, we build a simple database system that's meant to keep track of CDs (lol).

First thing I noticed pretty soon after I began to read was that I was wrong yesterday about everything in Lisp being a list. Instead, all those things in parentheses are called forms. I realized this when the author was going over how to create lists and it turns out you do so by creating a new form with the list operator (I'm going to use the terms operator, macro, and function interchangeably here since I haven't learned what the differences are yet) followed by the elements you want the list to contain.

Seibel also introduced "property lists" which seem to basically be what dictionaries are in Python and what hashtables are in basically every other language. The last time I heard "property list" was when I was doing some iOS development in Objective-C a number of years ago. The syntax for property lists is similar to that of Python dictionaries but with one major difference. In Python you could have a dictionary like {"cat" : "meow"}. You can't do that with CL property lists. This is because the keys in the key-value pairs of a property list must be symbols (aka variable names). So in CL you couldn't do (list :"cat" "meow") because that would be setting the value of a literal to another literal which makes no sense. If you wanted to have a symbol called "cat" you could do (list :cat "meow") though. So a property list really is just a list of properties (hence the name), not a dictionary/hashtable. That makes sense.

Something else I've noticed: I really enjoy how Lisp always puts the operator as the first entry in a form. It kind of forces you to see basic things like addition in a way you don't think about them every day. Like (+ 1 2 3) makes you think "oh yeah I guess 1 + 2 + 3 really is just applying the addition operator to a series of numbers." Which if someone told you that you might say "yeah, obviously." It's just not a way you see it in everyday life. I can see how this might open new avenues of thinking when programming. One downside though is that this approach decreases the readibility. I'm sure this will be something I get used to though and don't even think about eventually.

CL also seems to have quite a few useful built-in operators/macros like remove-if-not which creates a new list of elements from a list passed in that excludes all elements that don't match the condition.

Although the fact that everything in Lisp is a form and is kind of hard to read for me (currently anyway), I really do like this idea. It feels like it makes the thinking behind programming so much clearer. How beautiful. I'm sure this pattern has showed up in other programming languages I've used, it's just the first time I've seen it so explicitly obvious.

"Keyword parameters" were also briefly introduced. These basically allow you to have optional parameters for functions you build and supply default values for the ones not given. With this concept I imagine writing the list function used earlier might look something like this:

(defun list (&key ele1 ele2) 
    ;; creation of list with passed in elements
)

Except of course this would be very limiting, I'm guessing some other feature is used for referring to "as many parameters as you want." It's similar in spirit though.

I also learned that you can write anonymous functions using the lambda operator.

Anyway, that's all for today. Code I wrote for today can be found in code/Nov2018/28 from the top-level of this repo.