Declarative Representation

It is more blessed to represent data declaratively than to build it into code.

(define *cardinals* '((one 1) (two 2) (three 3)))

(define (number-value word) (safe-cadr (assq word *cardinals*)) )


(define (number-value word)
  (case word
    ((one) 1)
    ((two) 2)
    ((three) 3)
    (else #f) ))

Adding number words in Spanish will require changing the table in the first version, but changing code in the second version.

Rule: Design your programs to be easy to change. Tables are usually easier to change than code.

Contents    Page-10    Prev    Next    Page+10    Index