Name:
Anonymous
2012-11-14 4:47
(define (reduce op id lis)
(if (null? lis)
id
(reduce op (op id (car lis)) (cdr lis))))
(define (flip f)
(lambda (x y) (f y x)))
(define (curry f . args)
(lambda args2
(apply f (append args args2))))
(define (evaluate f x) (f x))
(define (compose . fns)
(let ((rev-fns (reverse fns)))
(lambda args
(reduce (flip evaluate) (apply (car rev-fns) args) (cdr rev-fns)))))
(define (collect . fns)
(lambda args
(map (curry (flip apply) args) fns)))
(define (wrap f . fns)
(compose (curry apply f) (apply collect fns)))
(define (if-fn condition true false)
(lambda args
(if (apply condition args)
(apply true args)
(apply false args))))
(define (constant-fn value)
(lambda args value))
(define (id x) x)
(define factorial-helper
(if-fn (compose (curry = 0) caddr list)
(compose cadr list)
(compose (wrap apply car id) (collect car (wrap * cadr caddr) (compose (curry (flip -) 1) caddr)) list)))
(define factorial (curry factorial-helper factorial-helper 1))
Name:
Anonymous
2012-11-18 1:35
dup :: (a -> a -> b) -> (a -> b)
dup f x = f x x
if' :: Bool -> a -> a -> a
if' x y z = if x then y else z
fact :: Integer -> Integer
fact = dup(dup.(.(.fact.(+(-1))).(*)).(.).(`if'`1).(==0))
Of course, I could have just used product and enumFromTo, but where's the fun in that?