Programs do useful work when they can distinguish code from data and select one result. 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, the same shape is returned 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 has 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; 0, "", and () are all
true values. Use an explicit predicate when you mean “non-empty,” “positive,”
or another domain condition.
and and or stop early
and evaluates from left to right and stops on #f. or stops on its first
true value. That 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.
cond 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 tells a reader what
the answer means without requiring knowledge of the procedure that produced
it.
Inspect a list when you need to
For a proper 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 4?
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: First Decision Rule.