Values, Names, and Procedures

Learn how Lispex reads literals and calls, then bind names with `define` and `let` and create reusable procedures with `lambda`.

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:

LISPEX
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:

LISPEX
(+ 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

OUTPUT
16

Some 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:

LISPEX
(define fee 3)
(+ 20 fee)

Result

OUTPUT
23

The definition prints nothing; the final addition prints one value.

Use let when a name is needed only for one body:

LISPEX
(let ((base 7))
  (* base 2))

Result

OUTPUT
14

The 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:

LISPEX
(define (price-with-fee price fee)
  (+ price fee))

(price-with-fee 20 3)

Result

OUTPUT
23

The equivalent long form makes the procedure value visible:

LISPEX
(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

LISPEX
(define (price-with-fee price fee)
  (+ price fee))

(list (price-with-fee 20 3)
      (let ((base 7))
        (* base 2)))

Result

OUTPUT
(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
LISPEX
(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.