Step 1 showed a complete program. This lesson explains the small set of shapes inside it. The key is to separate a value, a name that refers to a value, and a procedure call that computes a value.
Values can stand alone
Numbers, booleans, strings, characters, and quoted data are values:
42
#t
"Lispex"
#\L
'ready
A bare symbol such as ready is different: Lispex looks it up as a name.
Writing 'ready preserves the symbol as data.
A list in call position asks for work
Read this from the innermost call outward:
(+ 10 (* 2 3))
First Lispex finds +, then evaluates 10, then evaluates (* 2 3) from
left to right. The inner call produces 6, so the outer call becomes the
equivalent of (+ 10 6).
Result
16Some parenthesized forms are not ordinary calls. define, lambda, let, and
if control which parts are evaluated and where names are visible. The syntax
map calls these out explicitly.
Give a value a name
define creates or updates a top-level binding:
(define fee 3)
(+ 20 fee)
Result
23The definition prints nothing; the final addition prints one value.
Use let when a name is needed only for one body:
(let ((base 7))
(* base 2))
Result
14The binding base is visible inside the body and disappears afterward. That
small scope makes a temporary decision easier to inspect.
Create a reusable procedure
Function-definition shorthand names both the procedure and its inputs:
(define (price-with-fee price fee)
(+ price fee))
(price-with-fee 20 3)
Result
23The equivalent long form makes the procedure value visible:
(define price-with-fee
(lambda (price fee)
(+ price fee)))
A procedure checks its arity. Calling price-with-fee with one or three
arguments is a runtime error rather than an implicit default.
Put the pieces together
(define (price-with-fee price fee)
(+ price fee))
(list (price-with-fee 20 3)
(let ((base 7))
(* base 2)))
Result
(23 14)Observation order matters: the first list element is computed, then the
second. list receives the two resulting values and constructs (23 14).
Exercise
Define a procedure named triple that multiplies its input by 3, then return
(12 21) by calling it twice inside list.
Show one answer
(define (triple n)
(* n 3))
(list (triple 4) (triple 7))
Ready for Step 3?
Continue when you can explain why a quoted symbol is data, why (* 2 3) runs
before the outer addition uses it, and where a let name is visible. Next:
Data, Conditions, and Decisions.