Command History in DrScheme’s REPL

As I like to use a Scheme REPL as a sort of (very) advanced pocket calculator. I frequently would like to refer to results of previous calculations. Although you can paste previous inputs (<Esc> P), I prefer having a special symbol I can use in a subsequent calculation, like in Mathematica or Axiom. Fortunately, this is very easy to achive: DrScheme lets you change its REPL’s constituents so executing the following code will make a global variable “%” available that always contains the result of the last calculation. Moreover, the list “%%” is created that will hold all of your previous results. Best of all its just nine lines of code:


(define % '())
(define %% '())
(current-eval
 (let ([orig-eval (current-eval)])
   (lambda (sexp)
     (let ([result (orig-eval sexp)])
       (set! %% (cons result %%))
       (set! % result)
       result))))

Comments are closed.