Data, Conditions, and Decisions

Preserve symbols and lists as data, choose with if and cond, use short-circuit logic, and return a labelled decision value.

When programs can distinguish code from data and select one result, they perform useful work. Lispex keeps both ideas visible. Quote preserves data, while conditionals decide which expression is evaluated.

Quote keeps a form as data

Without a quote, Lispex treats a non-empty list as something to evaluate.

LISPEX
(+ 1 2)

With a quote, Lispex returns the same shape as data.

LISPEX
'(+ 1 2)

Result

OUTPUT
(+ 1 2)

You can also construct a fresh list from evaluated values.

LISPEX
(list 'decision 'allow)

Result

OUTPUT
(decision allow)

Use quote for fixed literal data. Use list when one or more elements must be computed.

if makes one two-way choice

if takes a test, a result for true, and a result for false.

LISPEX
(if (>= 62 50)
    '(decision free-shipping)
    '(decision standard-shipping))

Only the selected branch runs. Only #f is false. The values 0, "", and () are all true values. When you mean “non-empty,” “positive,” or another domain condition, use an explicit predicate, a test that answers true or false.

and and or stop early

and evaluates from left to right and stops on #f. or stops on its first true value. not flips the true or false it is given. Stopping evaluation early makes a compound eligibility test readable.

LISPEX
(and (<= 9 14)
     (not #f))

Result

OUTPUT
#t

Short-circuiting is part of the evaluation rule. An expression to the right is not evaluated after the result is already known.

The value it stopped on is the value you get back. It is not turned into a boolean.

LISPEX
(or #f 'standard)

Result

OUTPUT
standard

cond names several cases

LISPEX
(define (shipping-decision total member?)
  (cond
    ((>= total 50) '(decision free-shipping))
    (member? '(decision discounted-shipping))
    (else '(decision standard-shipping))))

(shipping-decision 42 #t)

Result

OUTPUT
(decision discounted-shipping)

The clauses are tested from top to bottom. A high total wins before membership is considered. else is the fallback. The labelled list shows a reader what the answer means without requiring them to know the procedure that produced it.

Inspect a list when you need to

For an ordinary list, one that ends with the empty list, car returns the first value and cdr returns the remaining list.

LISPEX
(define answer '(decision allow))
(list (car answer) (car (cdr answer)))

Result

OUTPUT
(decision allow)

Prefer direct construction and predicates in ordinary rules. Manual car/cdr chains are useful for learning and low-level access, but named helpers often communicate intent better.

Exercise

Write age-decision so ages 18 and above return (decision allow) and lower ages return (decision deny under-age).

Show one answer
LISPEX
(define (age-decision age)
  (if (>= age 18)
      '(decision allow)
      '(decision deny under-age)))

(age-decision 17)

The result is (decision deny under-age).

Ready for Step 5?

Continue when you can explain the difference between '(decision allow) and (decision allow), name the three positions in if, and say why 0 is not false. Next comes First Decision Rule.