6

A lambda expression which takes a function (of one argument) and a number, and applies the function to twice the number.

3 Answers 3

9

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)))
Sign up to request clarification or add additional context in comments.

Comments

5

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)

Comments

2

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.