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.
(+ 1 2)With a quote, Lispex returns the same shape as data.
'(+ 1 2)Result
(+ 1 2)You can also construct a fresh list from evaluated values.
(list 'decision 'allow)Result
(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.
(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.
(and (<= 9 14)
(not #f))Result
#tShort-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.
(or #f 'standard)Result
standardcond names several cases
(define (shipping-decision total member?)
(cond
((>= total 50) '(decision free-shipping))
(member? '(decision discounted-shipping))
(else '(decision standard-shipping))))
(shipping-decision 42 #t)Result
(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.
(define answer '(decision allow))
(list (car answer) (car (cdr answer)))Result
(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
(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.