Reputation: 408
How do you define a function in another function that can be accessed globally in racket?
Upvotes: 0
Views: 4260
Reputation: 18917
The only easy way I know of is to define a binding at the top level that you can later modify inside a function:
Welcome to Racket v6.2.1.
-> (define gf add1)
-> (define (redefine-gf) (set! gf sub1))
-> (gf 1)
2
-> (redefine-gf)
-> (gf 1)
0
Upvotes: 1
Reputation: 417
Hmm, the question seems rather odd as leeor remarked.
How do you define a function in another function that can be accessed globally in racket?
When the function g
is defined inside the function f
, which one do you mean should be globally accessible? In case of g
that effort is odd. But if you just wanted to ask how to formally define a helper function g
inside f
that can only be called indirectly by calling f
then it would look like this:
(define (f n)
(define (g n)
n)
(g n))
This obviously only outputs the input. But you can't call g
itself by name outside of f
. You can have as many definitions as you want inside an enclosing function body as long as it's not the last statement in this outer function.
In any case you might wish to take a look: Racket Guide
Upvotes: 0