Quote:Original post by Muhammad HaggagQuote:Original post by SamLowry
Both look ok to me (maybe you should curry your functions, but that's a detail), but you can simplify a bit.
Am I correct in assuming that your simplifications assume the functions are curried?
The "simplifications" I made make use of currying yes. The 'mul' example you quoted consists of two steps: the first step uses a rule (\xy.expr y) == (\x.expr) if expr does not contain y, the second step uses (\x.\y.body) == (\xy.body). In lambda-calculus and other languages supporting currying, this is indeed a simplification. In Scheme, I'm not so sure.
Quote:Quote:
mul = \a b.\s z.a (b s) z
===
mul = \a b.\s.a (b s)
===
mul = \a b s.a (b s)
For some reason, I couldn't translate this to Scheme correctly. Can you give me a hand?
For my first attempt, I cheated a little bit and defined 2 macros which helped me with the currying.
(define-syntax curry (syntax-rules () ((_ (x y . xs) . body) (lambda (x) (curry (y . xs) . body))) ((_ (x) . body) (lambda (x) . body)))); (call f x y); becomes; ((f x) y)(define-syntax call (syntax-rules () ((_ func x y . xs) (call (func x) y . xs)) ((_ func x) (func x))))(define zero (curry (s z) z))(define succ (curry (n s z) (s (call n s z))))(define add (curry (a b s z) (call a s (call b s z))))(define mul (curry (a b s) (a (b s))))
Without the use of macros, it becomes
(define zero (lambda (s) (lambda (z) z)))(define succ (lambda (n) (lambda (s) (lambda (z) (s ((n s) z))))))(define add (lambda (a) (lambda (b) (lambda (s) (lambda (z) ((a s) ((b s) z)))))))(define mul (lambda (a) (lambda (b) (lambda (s) (a (b s))))))