给定以下定义的结构和类型,需要编写乘以两个数字的函数。很难做到这一点。任何建议将不胜感激。
(define-struct Zero ())
(define-struct Succ
([n : Nat]))
(define-type Nat (U Zero Succ))
(: one Nat)
(define one (Succ (Zero)))
(: two Nat)
(define two (Succ one))
( : sub-nat : Nat Nat -> Nat)
(define (sub-nat a y)
(cond
[(Zero? a) a]
[(eq? one y)
(- a y)]))
( : add-nat ( -> Nat Nat Nat))
(define (add-nat a b)
(cond
[(Zero? a) b]
((Zero? b) a)
[else (add-nat (Succ-n a) (Succ b))]))
( : multiply-nat : Number Nat -> Nat)
(define (multiply-nat a b)
(cond
[(Zero? a) a]
[(Zero? b) b]
[else
(add-nat b (multiply-nat (sub-nat a one) b))]))