a collection of notes from reading the little schemer but using racket instead
I’ve decided to dust off my copy of The Little Schemer by Daniel P Friedman and Matthias Felleisen and run through it with racket 8. I’ve had a project on the back-burner for ten years with a touch every couple years since I started it and and I thought-why not start over and use a totally different language for it!
I also caught myself taking some notes and thought–why not blog about it, some of this is bound to be useful to someone else, at the very least, future-me.
chapter 00 – preliminaries
I had to setup emacs again. It has been too long since I’ve written code outside of RStudio or excel and I hadn’t set up my preferred IDE on new devices.
I always start a new emacs installation with technomancy’s collection of better-defaults.
I use org-mode to collect my notes, and org-babel provides a means to insert code blocks:
#+begin_src racket
...
#+end_src
emacs comes with racket-mode so that will properly indent and do the syntax highlight stuff for you right there in your org file. However, C-c C-c does not execute racket source blocks by default. You need to install an ob-racket. There are a few, and I used hasu’s. After cloning the git repo, add the path to your ob-racket to your emacs load-path by adding this to your emacs init file:
(add-to-list 'load-path "path/to/emacs-ob-racket")
Once you have ob-racket in your load-path, you can insert and run racket source blocks (assuming racket is in your path).
I don’t love DrRacket, but I love the C-\ default keybind for λ, so to add that to emacs, I added this to the emacs init file:
(global-set-key (kbd "C-\\") "λ")
So emacs is set up to work with racket relatively painlessly, and I can now embed and run code snippets inside org-mode. Ready to get down to work.
chapter 00 – intro
The little schemer requires three procedures that are not in the scheme spec: atom?, add1, and sub1. The first of these returns #t the single argument is atomic, and the other two increment and decrement numeric arguments respectively. These are also not in the racket spec, so we add them to our “the-little-racketeer.rkt” file.
I’m using λ instead of directly defining procedures because I don’t want to hide it behind syntactic sugar while learning, and I don’t want to think about the syntax when using anonymous functions later. Also using the Greek letter rather than the lambda keyword because it’s massively quicker and just as easy to read.
(define atom?
(λ (x)
(and
(not (null? x))
(not (pair? x)))))
(define add1
(λ (x)
(+ x 1)))
(define sub1
(λ (x)
(- x 1)))
chapter 01 – toys
My first higher level observation with the text is that the writing style is probably cringe to younger people. The method aims to teach the reader how to construct the right questions. New concepts are introduced with a series of questions of the form “what is f(x) when x is value.” From a pedagogical perspective, I like this design for the expected reader of this book (highly motivated, possibly independent learner). It reminds me of three math texts by R. P. Burn (A Pathway to Number Theory, Groups, and Numbers and Functions). As an introduction, it may seem to go slower than it must, but encouraging careful and precise thinking is the point–it’s not a book about programming scheme, it is a book about thinking about how to precisely define ideas that uses scheme as a tool for exploration.
the Socratic method design of the “little” books could be used by an instructor to prime the mind of a new programmer to think with a test driven development mindset. It occasionally converts questions to code, but it “feels” like being nudged in the direction of doing that yourself.
📌I started converting some of the book into basic unit test functions, and building a little test suite for my procedures set up some mental scaffolding for how I would start a racket project. It might be plausible to convert the book into an interactive notebook with a little effort and creativity for the procedures that are not formally defined.
this chapter introduces atoms and lists but not pairs. you can introduce yourself to pairs by violating “the law of cons” (p. 9).
The only quibble I have with this chapter is the introduction of the term S-expression with implicit definition, but I suppose it will be formally defined by the end of this book.
Next up: Chapter 02 – do it, do it again, and again, and again