; summation1.scm - higher order summation functions
; (from Cousineau and Mauny, The Functional Approach to Programming" in CAML, p42-43)
; sums f(n) from n=a to n=b
(define (sigma f l)
;(display (format "~%l: ~A" l))
(if (> (car l) (cadr l))
0
(+
(f (car l))
(sigma
f
(list
(+ 1 (car l))
(cadr l) )) )))
(define (sqr x) (* x x))
(sigma sqr '(1 5)) ; 55
;-----------------------------------------------------------------------------
(define summation
(lambda (incr test op e f a)
(if (test a)
e
(op
(f a)
(summation incr test op e f (incr a)) ))))
(define summation_int
(lambda (op e f a b)
(summation
(lambda (x)
(+ x 1))
(lambda (x)
(> x b))
op e f a) ))
(define pi
(lambda (f a b)
(summation_int * 1 f a b)))
(define sigma2
(lambda (f a b)
(summation_int + 0 f a b)))
(sigma2 (lambda (n) n) 0 5) ; 15
(sigma2 (lambda (n) (/ 1 n)) 1 2) ; 3/2
(define fact
(lambda (b)
(pi
(lambda (x) x)
1
b) ))
(fact 10) ; 3628800
Text file Source (historic): geocities.com/soho/square/3472
geocities.com/soho/squaregeocities.com/soho
(to report bad content: archivehelp @ gmail)
|
|
|
|
|