Use Dr. Racket for Reading The Little Schemer (or the missing setup instructions for The Little Schemer)

jamesliudotcc

James Liu

Posted on July 26, 2020

Use Dr. Racket for Reading The Little Schemer (or the missing setup instructions for The Little Schemer)

TL;DR

  • Mostly, note cards and pencil is fine.
  • Use Racket, and in fact, use the Dr. Racket IDE.
  • Wrap strings in ""
  • Quote list literals, so '(a b c)
  • Use Dr. Racket's testing capabilities to save yourself work

Use Dr. Racket for Reading The Little Schemer (the missing setup instructions)

Why The Little Schemer

The Little Schemer is a classic Lisp book, which often comes recommended for newbies to Lisp who need an easier on-ramp to Structure and Interpretation of Computer Programs. Personally, I tried working my way through SICP on my own, made it some way in, and decided learning how to program in Scheme and understanding SICP was too much all at once.

In the preface, the authors write that "The Little Schemer is based on lecture notes from a two-week 'quickie' introduction to Scheme for students with no previous programming experience and an admitted dislike for anything mathematical." Great. I definitely have at least as much programming and math aptitude as that.

Mostly, I have been working on it with note cards with pencil. The advantage is that I can cover the answer while I read the question, and I get the benefit of working out the question before I see the answer. But sometimes, I want to run the code to check my work before revealing the answer.

Setting up Scheme 💾

Unfortunately, there are not setup instructions. Since the book was published in 1996, it seems unlikely setup instructions are going to do much good--it being the era of Windows 95 and Mac System 7. Back then, the internet came in the form of AOL CD-ROMs! 💽

At the time of writing, the first Google result for "install scheme" points to this tutorial, which is for installing MIT Scheme on Windows. Unfortunately, MIT Scheme no longer even supports Windows. But don't worry, if you want, you can get a dialect called Chez Scheme with a Windows installer, as an apt package, or even using Homebrew.

Really, just get Racket 🏸

But don't do any of that. Instead, get Racket. Racket is a descendant of Scheme, and as far as I can tell it stopped calling itself a Scheme because it stopped being so purely minimalist (a sense I get mostly from listening to podcasts).

Racket is nice enough to come with an installer. It also comes with an IDE, which, OK, I was skeptical at first because it looks a little outdated, but it is actually quite nice. Further, it comes with a book called How to Design Programs (which comes with its own manifesto about how SICP is too hard for beginners).

For something simple, you can use Racket from the REPL, and type in the form in the preface for checking if something is an atom. What happens when we try typing this in?



(define atom?
      (lambda (x)
        (and (not (pair? x)) (not (null? x)))))


Enter fullscreen mode Exit fullscreen mode

Error message

Uh oh.

To correct that, try (atom? "atom"). Ok, so string literals have to be wrapped in double quotes. But also take a look at what has to happen with lists:

Quote strings. Quote lists too

So you need to quote lists so the interpreter knows the difference between a list, (quote (a b c)), or '(a b c) for short, and a function a with arguments b and c, (a b c). When people say that in Lisp, code is data, it refers to being able to quote and unquote lists.

But really, use Dr. Racket

Dr. Racket, despite looking like it is from the early 2000s, is great. It supports auto-indentation, auto-closing parens, and Unicode (so you can write λ instead of lambda, or use emoji). There are other nice features, like drawing lines to indicate the scope of variables.

Use Dr. Racket by writing your definitions in the definition area. After you run them, you can interact with your functions in the interaction area. Since Dr. Racket supports many languages, you have to declare #lang racket at the top, or select another language from the language dropdown at the bottom right. For Little Schemer, just use Racket.

Also, Racket includes a test-runner, so you can write tests to make sure that the code does what you expect. Some of the questions and answers give you test cases you can manually run to check the function you are writing. But that gets tiresome, and it is easy to write out your test cases as automated tests.

You can begin your exploration like this:

What Dr. Racket looks like

Here is the definition area as text:



#lang racket
(require test-engine/racket-tests)

(define atom?
  (λ (x)
    (and (not (pair? x)) (not (null? x)))))


(define lat?
  (λ (x)
    (cond
    [(null? x) #t]
    [(atom? (car x)) (lat? (cdr x)) #t]
    [else #f])))


(check-expect (lat? '()) #t)
(check-expect (lat? '("hi")) #t)
(check-expect (lat? '('())) #f)
(test)


Enter fullscreen mode Exit fullscreen mode

And the interactions area:



Welcome to DrRacket, version 7.7 [3m].
Language: racket, with debugging; memory limit: 128 MB.
All 3 tests passed!
> (lat? '("Dr." "Racket"))
#t
> (atom? "️")
#t
> 


Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
jamesliudotcc
James Liu

Posted on July 26, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related