This is a course page of David Casperson 

From time to time I shall post solutions to homework problems here.
fun power (x,n) = let fun pow3 (_,0,a) = a  pow3 (x,1,a) = x*a  pow3 (x,n,a) = let val d = (case n mod 2 of 0 => a  1 => x*a) in pow3(x, n div 2,d) end in pow3(x,n,1.0) endThis uses
case
which we haven't formally looked
at, but the
meaning should be apparent. We could equivalently write
fun power (x,n) = let fun pow3 (_,0,a) = a  pow3 (x,1,a) = x*a  pow3 (x,n,a) = let val d = (if n mod 2 = 0 then a else x*a) in pow3(x, n div 2,d) end in pow3(x,n,1.0) end
letrec
to
define a local function with an accumulator.
(define (reverse alist) (letrec ((reverse2 (lambda (blist answer) (cond ((null? blist) answer) ((cons? blist) (reverse2 (cdr blist) (cons (car blist) answer))))) )) (reverse2 alist '())))Slightly more straitforward is
(define (reverse alist) (reverse2 alist '())) (define (reverse2 blist answer) (cond ((null? blist) answer) ((cons? blist) (reverse2 (cdr blist) (cons (car blist) answer)))) ))
(define (foldr f b alist) (cond ((null? alist) b) (else (f (car alist) (foldr f b (cdr alist))))))
published