33.5k views
4 votes
Cooking Curry Implement the function curry-cook , which takes in a Scheme list formals and a quoted expression body. curry-cook should generate a program as a list which is a curried version of a lambda function. The outputted program should be a curried version of a lambda function with formal arguments equal to formals, and a function body equal to body. You may assume that all functions passed in will have more than o formals; otherwise, it would not be curry-able!

For example, if you wanted to curry the function (lambda (x y) (+ x y)), you would set formals equal to '(x y), the body equal to '(+ x y), and make a call to curry-cook : (curry-cook '(x y) '(+ x y)).

scm> (curry-cook '(a) 'a)
(lambda (a) a)
scm> (curry-cook '(x y) '(+ x y))
(lambda (x) (lambda (y) (+ x y)))

(define (curry-cook formals body)
'YOUR-CODE - HERE
)

User BVB
by
8.0k points

1 Answer

0 votes

Final answer:

The 'curry-cook' function in Scheme creates a curried version of a lambda function by breaking it down into a series of functions, each taking one argument from the original formal parameters list.

Step-by-step explanation:

To solve the problem of creating a curried version of a lambda function in Scheme, we need to write the curry-cook function. Currying is a process by which a function with multiple arguments is transformed into a series of functions, each with a single argument. We want to transform a given function with formal parameters and a body into its curried equivalent.

Here is an implementation of curry-cook:

(define (curry-cook formals body)
(if (= (length formals) 1)
lambda, (car formals), body)
` (lambda (, (car formals))
, (curry-cook (cdr formals) body))))

This Scheme code checks if there is only one formal left in the list, in which case it creates a lambda function with that single argument and the given body. Otherwise, it creates a lambda function with the first formal as its argument and a body that is another call to curry-cook with the rest of the formals and the same function body.

User Antoine Laffargue
by
7.9k points