|
SketchyLISP Reference |
Copyright (C) 2007 Nils M Holm |
| <<[pred-iter] | [Index] | [qsort]>> |
Conformance: SketchyLISP Extension
Purpose: Compute the product of a sequence of positive numbers.
Arguments:
N - least value of sequence
M - maximum value of seqence
Model:
(define (product n m)
(cond ((= n m) m)
(else (* n (product (+ n 1) m)))))
Implementation:
(define (product n m)
(let ((nn (natural n))
(lim (natural m)))
(letrec
((prod
(lambda (n r)
(cond ((n> n lim) r)
(else (prod (+ n 1) (* n r)))))))
(cond ((n> nn lim)
(bottom '(product: bad range)))
(else (prod nn 1))))))
Example:
(product 5 7) => 210
| <<[pred-iter] | [Index] | [qsort]>> |