Edit Files In A REPL
April 7, 2017
Here is my REPL editor, which is stored in file
edfile.ss and loaded when I start up my Scheme system:
(define-top-level-value '*editor* "ed") (define-top-level-value '*edfile* #f)
(define (ed . args) (if (pair? args) (set-top-level-value! '*edfile* (car args))) (if (not (top-level-value '*edfile*)) (error 'ed "file not found")) (system (string-append (top-level-value '*editor*) " " (top-level-value '*edfile*))) (load (top-level-value '*edfile*)))
This is specific to Chez Scheme, and is ugly because of the change in Scheme top-level semantics that was brought in with R6RS. The asterisks around the variable names are a Lisp convention; they are known colloquially as “earmuffs” and mark global variables. Here is a sample editing session, where I write a Sieve of Eratosthenes:
phil@haydn ~ $ scheme edfile.ss Chez Scheme Version 9.4.1 Copyright 1984-2016 Cisco Systems, Inc. > (ed "primes.ss") primes.ss: No such file or directory a (define (primes n) (let ((sieve (make-vector (+ n 1) #t))) (let loop ((p 2) (ps (list))) (cond ((< n p) (reverse ps)) ((vector-ref sieve p) (do ((i (* p p) (+ i p))) ((< n i)) (vector-set! siev i #f)) (loop (+ p 1) (cons p ps))) (else (loop (+ p 1) ps)))))) . wq 338 > (primes 50) Exception: variable siev is not bound Type (debug) to enter the debugger. > (ed) 338 /siev (let ((sieve (make-vector (+ n 1) #t))) // ((vector-ref sieve p) // (vector-set! siev i #f)) s//&e/p (vector-set! sieve i #f)) wq 339 > (primes 50) (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47) > (exit)
Yes, I really do use
ed. As you see above, it flows very nicely in an interactive session.
You can see the program at http://ideone.com/dWZveh.
Pages: 1 2