How to write a procedure?

How do you write a procedure that outputs a function? (Racket)

  • How do you write a procedure that outputs a function? (Racket) Would the procedure output a lambda?

  • Answer:

    Do you mean: how do I use higher-order functions in order to yield another function? If this is so, take a look at this: (lambda (f) (lambda (x) (f x))) What this does is: 1: create an anonymous closure initialized with "f" as a parameter 2: within the scope of the first body, create a closure with "x" as a parameter 3: apply f to x (f being the closure on top of x) What this means is that (before (f x) is evaluated), f is declared as a function with x as its argument. Applying f to x is what this whole expression returns: in this case, nothing much since we haven't assigned any value except closures. Using this as a start point, consider what happens when a closure returns another closure: the inner closure gets evaluated AFTER the outter closure has passed its value around in its body. It's called delayed evaluation and permits one to pass procedures as values so that the inner procedure can expand later. Here's a concrete example of how benefic this can be. Let's define cons, car and cdr using only higher-order functions (closures): (define (cons kar kdr) (lambda (kons) (kons kar kdr))) (define (car kons) (kons (lambda (kar kdr) kar))) (define (cdr kons) (kons (lambda (kar kdr) kdr))) To understand this, pass the definitions around but remember that (define (foo bar) ...) means (define foo (lambda (bar) ...))...

Aspen van Wood at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

I hope I'm not being too spoily: (define answer (lambda (question) (lambda (answer) `((question: ,question) (answer: ,answer))))) then try: > ((answer 'thequestion) 'withananswer) in REPL.

ypb

Another way to do the same: (define (output-function) (define (f x) x) f) And you can test it like this: (define test (output-function)) (test 10) ; returns 10

Óscar López

Think about it this way. It's easy to output a number from a function: (define (output-num x) 7) We can also predefine the output: (define num 7) (define (output-num x) num) Now it's easy to see how to output a function -- just refer to it in the body. (define (a-function x) "hello") (define (output-function y) a-function) And, just like with literal numbers, we can write our function in the body of the definition, using lambda: (define (output-function y) (lambda (x) "hello"))

Sam Tobin-Hochstadt

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.