Jan 29, 2008

Currying of a function with unfixed argument.

Some examples of Function Currying.


(define (curried+ a)
(lambda (b) (+ a b)))

((curried+ 1) 2)



(define (curried-mean a)
(lambda (b)
(lambda (c) (/ (+ (+ a b) c) 3))))

(((curried-mean 1) 2) 3)



When the function has arguments with which the number is unfixed.


(define (append lst x)
(if (null? lst)
(list x)
(let ((l (if (list? lst) lst (list lst))))
(cons (car l) (append (cdr l) x)))))

(define (curried-list x)
(lambda (y)
(if (null? y)
x
(curried-list (append x y)))))

(((((curried-list 1) 2) 3) 4) '())


"'()" is unwanted...

No comments: