2013年5月29日 星期三

NCNU LISP HW8

exercise 8: Combining three numbers with exponentiation and multiplication
  • Refer to exercise 2.5 but the pair is replaced with a triple. That is, the interface functions are :
    (combine a b c), (first x), (second x), (third x)
(define (combine a b c)
  (do-combine a b c 1)
(define (do-combine a b c num)
(if (= a 0)
(if (= b 0)
(if (= c 0) num
           (do-combine a b (- c 1) (* 5 num))
)
   (do-combine a (- b 1) c (* 4 num))
)
   (do-combine (- a 1) b c (* 3 num))
  )
)
(define (first x)
(common x 3)
)
(define (second x)
(common x 4)
)
(define (third x)
(common x 5)
)
(define (common n f)
(define (do-common x num)
(if (> (remainder x f) 0)
   num
    (do-common (/ x f) (+ num 1))
)
)
 (do-common n 0)
)



沒有留言:

張貼留言