How do I create a functional REPL?

I know I can use (read-eval-print-loop), but by default it has no #%top-interaction or #%app or anything.

I would also like to be able to augment it with specific bindings.

3 Likes

You need to set the current-namespace: by default, this parameter is unconnected to any code.

The template is

(parameterize ([current-namespace N])
  (read-eval-print-loop))

A reasonable value for N is (make-base-namespace), which is like using racket/base for all initial bindings.

If you want to augment bindings, you can

(define-namespace-anchor this-module)

and use (namespace-anchor->namespace this-module) as N. Then any bindings in the current module (including those provided by requires) are available in the REPL.

7 Likes

module->namespace is another reasonable value. It gives you bindings inside any arbitrary module. The REPL in tools like Dr Racket, xrepl, and Racket Mode is, as a starting point, (parameterize ([current-namespace (module->namespace mod-path)]) (read-eval-print-loop)).

6 Likes