Study Note of Scheme

==> On Studying Process <==

The Ten Commandments

The First commandment

When recurring on a list of atoms, lat, ask two questions about it: (null? lat) and else. When recurring on a number, n, ask two questions about it: (zero? n) and else. When recurring on a list of S-expressions, l, ask three questions bout it: (null? l), (atom? (car l)), and else.

The Second Commandment

Use cons to build lists.

The Third Commandment

When building a list, describe the first typical element, and then cons it onto the natural recursion.

The Fourth Commandment

Always change at least one argument while recurring. When recurring on a list of atoms, lat, use (cdr lat). When recurring on a number, n, use (sub1 n). And when recurring on a list of S-expressions, l, use (car l) and (cdr l) if neither (null? l) nor (atom? (car l)) are true.

It must be changed to be closer to termination. The changing argument must be tested in the termination condition:
when using cdr, test termination with null? and
when using sub1, test termination with zero?.

The Fifth Commandment

When building a value with +, always use 0 for the value of the terminating line, for adding 0 does not change the value of an addition.

When building a value with x, always use 1 for the value of the terminating line, for multiplying by 1 does not change the value of a multiplication.

When building a value with cons, always consider () for the value of the terminating line.

The Sixth Commandment

Simplify only after the function is correct.

The Seventh Commandment

Recur on the subparts that are of the same nature:

  • On the sublists of a list.
  • On the subxpressions of an arithmetic expression.

The Eighth Commandment

Use help functions to abstract from representations.

The Ninth Commandment

Abstract common patterns with a new function.

The Tenth Commandment

Build functions to collect more than one value at a time.

The Five Rules

The Law of Car

The primitive car is defined only for non-empty lists.

The Law of Cdr

The primitive cdr is defined only for non-empty lists. The cdr of any non-empty list is always another list.

The Law of Cons

The primitive cons takes two arguments.
The second argument to cons must be a list.
The result is a list.

The Lwa of Null?

The primitive null? is defined only for lists.

The Law of Eq?

The primitive eq? takes two arguments.
Each must be a non-numeric atom.

Chapter 1

  1. atom is a string of characters, except with a left “(“ or right “)” parenthesis.
  2. list is a collection of atom(s) enclosed by parentheses.
  3. All atoms are S-expressions.
  4. All lists are S-expressions.

e.g.: How many S-expressions are in the list:
(((how) are) ((you) (doing so)) far)
and what are they?

Ans: Three, ((how) are), ((you) (doing so)), and far.

  1. null list (or empty list) contains zero S-expressions enclosed by parentheses. ()

  2. car is the first atom of the list.
  3. There is no car for an atom.
  4. There is no car for an null list.

refer to The Law of Car

  1. “(car l)” is another way to ask for “the car of the list l.”
  2. “cdr” is pronounced “could-er.”

Installation

# on Arch Linux
$ sudo pacman -Sy mit-scheme

# on Ubuntu Linux
$ sudo apt-get install mit-scheme -y

Reference

The Little Schemer

0%