Reputation: 135
A lambda expression which takes a function (of one argument) and a number, and applies the function to twice the number.
Upvotes: 6
Views: 879
Reputation: 994579
Applying the function to twice the number:
(lambda (f x) (f (* 2 x)))
Applying the function to the number twice (which is what you may have intended to ask):
(lambda (f x) (f (f x)))
Upvotes: 9
Reputation: 1417
Greg's answer is correct, but you might think about how you might break apart this problem to find the answer yourself. Here is one approach:
; A lambda expression
;(lambda () )
; which takes a function (of one argument) and a number
;(lambda (fun num) )
; and applies the function
;(lambda (fun num) (fun num))
; to twice the number
;(lambda (fun num) (fun (* 2 num)))
((lambda (fun num) (fun (* 2 num))) + 12)
Upvotes: 5
Reputation: 1417
Here is another way to approach it:
Write a Contract, Purpose, and Header:
;; apply-double : function -> number -> any
;; to apply a given function to double a given number
(define (apply-double fun num) ...)
Write some Tests:
(= (apply-double identity 10) 20)
(= (apply-double - 15) -30)
(= (apply-double / 7) 1/14)
Define the function:
(define (apply-double fun num)
(fun (* 2 num)))
This is an abbreviation of the recipe here: http://www.htdp.org/2003-09-26/Book/
Upvotes: 2