Is there a way to show and save all “final” definitions entered into a Scheme REPL into a text file?
Say, if I have defined in the REPL:
(define (increase x) (+ 1 x))
(define (multbytwo x) (* 3 x)) ; let us say this was an error
(define (multbytwo x) (* 2 x))
I want to save this into an .scm-file with the content:
(define (increase x) (+ 1 x))
(define (multbytwo x) (* 2 x))
- i.e. the multbytwo function that got defined erraneously shall be “forgotten” due to re-definition. Is this possible?
None that I know of. There is however many Lisps that can save it’s image so that you can start up with all definitions. You won’t be able to actually see the definitions though.
I would have kept my definitions in a file (or in the IDE like in DrRacket) and started the repl with loading that file. When changing definition you copy/past it back to your definition file when you’re satisfied.
Alternatively, you could make a small REPL:
;; These are outside in case we break out of the REPL
(define env '())
(define (print-env)
(newline)
(for-each (lambda (x) (display (cdr x)) (newline)) env)
(newline))
(define (repl)
(define (define? expr)
(and (pair? expr)
(or (eq? 'define (car expr))
(eq? 'set! (car expr)))))
(define (define-name expr)
(if (pair? (cadr expr))
(caadr expr)
(cadr expr)))
(define (extend-env name expr)
(let ((found (assq name env)))
(if found
(set-cdr! (car found) expr)
(set! env (cons (cons name expr) env)))))
(let ((expr (read)))
(if (define? expr)
(let ((name (define-name expr)))
(eval expr (interaction-environment))
(extend-env name expr)
(repl))
(let ()
(display (eval expr (interaction-environment)))
(newline)
(repl)))))
(display "Outer REPL: If an error occurs, rerun with (repl)nIn both REPL and outside yo may print definitions by calling (print-env)n")
(repl)
This one is very crude. At the very first error it will exit the loop. In andoutside repl however, you may print the definitions by running (print-env).