;; ---------------------------------------------------------------- ;; Two ways to implement the non-negative integers in Racket. ;; Uncomment the definitions to try an implementation. ;; ---------------------------------------------------------------- #lang racket (provide zero ; a primitive value is-zero? ; a predicate that recognizes zero next ; a function that gives the next number previous) ; a function that gives the previous number ;; ---------------------------------------------------------------- ;; ... as Racket numbers. ;; ---------------------------------------------------------------- (define zero 0) (define is-zero? zero?) (define next add1) (define previous sub1) ; problem at zero ;; ---------------------------------------------------------------- ;; ... as Racket lists. The list's length represents the number. ;; ---------------------------------------------------------------- ;; (define zero '()) ;; (define is-zero? null?) ;; (define next (lambda (n) (cons 1 n))) ;; (define previous rest) ; problem at zero ;; ---------------------------------------------------------------- ;; Using the non-negative integers, regardless of implementation. ;; ---------------------------------------------------------------- ;; zero ;; (is-zero? zero) ;; (is-zero? (next zero)) ;; (next (next (next zero))) (require rackunit) (check-equal? (next (next (previous (next (next zero))))) (next (next (next zero)))) ; ----------------------------------------------------------------