Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

; A Natural is one of
; - 'Zero
; - (make-next n)
; where n is a Natural
(define-struct next (nat))
; f : Natural -> ...

(define (f n)
(cond (symbol? n) … (next? n) …(f (next-nat n))…))

...

; add-Nat : Natural Natural -> Natural
; Returns the result of adding two Naturals.
(define (add-Nat n m) (cond (symbol? n) m (next? n) (make-next (add-Nat (next-nat n) m))))

Exercises 

  • Use the stepper on (add-Nat Two Two)to see how it works.

...

; A natural is one of
; - 0
; - (add1 n)
; where n is a natural
; f : natural -> ...
(define (f n) (cond (zero? n) … (positive? n) …(f (sub1 n))…))

...

#Factorial, which is defined as

0! = 1, and
(

...

n

...

+1)! = (

...

n

...

+1) × (

HTML

n

HTML

!).

HTML
HTML

...