Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme define/lambda shorthand

Tags:

lambda

scheme

In Scheme, how can I make use of the define/lambda shorthand for nested lambda expressions within my define?

For example given the following procedure...

(define add
  (lambda (num1 num2)
    (+ num1 num2)))

One can shorten it to this:

(define (add num1 num2)
  (+ num1 num2))


However, how can I shorten the following function similarly ?

(define makeOperator
  (lambda (operator)
    (lambda (num1 num2)
      (operator num1 num2))))

;example useage - equivalent to (* 3 4):
((makeOperator *) 3 4)
like image 709
Cam Avatar asked May 31 '10 16:05

Cam


1 Answers

(define (makeOperator operator)
  (lambda (num1 num2)
    (operator num1 num2)))

The second lambda can not be shortened.

Well you could shorten it to (define (makeOperator operator) operator), if you don't want to enforce that the returned function takes exactly two arguments.

like image 186
sepp2k Avatar answered Sep 21 '22 19:09

sepp2k