Reputation: 11
I need to understand how can I access to an input of a function which is given to another function as an input.
For example; I have a function called f which simply does (define f (lambda (x) (if (null? x) #t (car x))))
this. I need to write a function which takes this f as an input and returns another function such that,
-Define a function (twoback f)
which takes a function f as its input.
-As its output, it should return a new function g which has the following behaviour:
g(x) = #t if x is the empty list or a list of length 1.
= f(y) where y=(cdr x) otherwise.
And the function will called like this : ((twoback f3) (list #t #f #t #f))
So actually my question is : How can I access the list given with the function call in the function that I'm going to write(the twoback function) ? Because i need to check whether it is empty or not.
Upvotes: 0
Views: 77
Reputation: 235984
Short answer, by currying the x
parameter. Something like this:
(define twoback
(lambda (f)
(lambda (x)
...))) ; fill-in with the logic requested
The above will define a procedure called twoback
which receives f
as parameter, and in turn it will return a new procedure which receives x
as parameter. This second procedure being returned is the one called g
in the question, from it you can access both f
and x
as you would normally do.
Now just complete the ...
part with the expected output.
Upvotes: 2
Reputation: 1213
I think this is what you mean:
(define (twoback f)
(lambda (x)
(if
(or (null? x) (null? (cdr x)))
#t
(f (cdr x)))))
(define f (lambda (x) (if (null? x) #t (car x))))
Upvotes: 0