[plt-scheme] modifying live code
; This example is based on the development of a
; telemetry filter while it was being used by a
; thread that was pointing a telescope. New
; filters replaced the old while the thread ran.
; The function (filter-latitude) is developed in
; its own file, then loaded in a repl that
; started a thread that receives telemetry, filters
; it, then sends the good data to a telescope.
; The example simulates a system that:
; 1) occasionally receives large errors (5 degrees)
; from a telemetry system on a balloon
; 2) filters latitudes expected to be near 35 degrees
; 3) sends the good data to a telescope.
; The actual filter is more complicated, and only
; latitude is treated here.
; This thread simulates the periodic transfer of
; latitude from telemetry to telescope:
(define loop-in-thread
(lambda()
(transfer-latitude) ; telemetry to telescope
(sleep 0.1) ; simulate more often than real-deal
(loop-in-thread)))
(define transfer-latitude ; runs in thread
(lambda()
(let ((filtered-latitude
(filter-latitude (receive-latitude))))
(if filtered-latitude ;; send if not #f
(send-to-telescope filtered-latitude)))))
;; Simulate a glitch of 5 degrees
;; in 10% of the messages:
(define receive-latitude
(lambda() (+ 35 (if (> 90 (random 100))
0 ; no error 90% of time
5))))
;; This function will be changed without
;; stopping the system:
(define filter-latitude
(lambda( latitude )
latitude)) ; don't filter anything
; simulate errors by incrementing counter
; an error is a latitude more than 3 degrees from 35:
(define error-count 0 )
(define send-to-telescope
(lambda(latitude) ;
(if (< 3 (abs (- latitude 35)))
(set! error-count (+ 1 error-count)))))
;; start the thread
(define tt (thread loop-in-thread))
;; use (e) from repl to see errors climb
(define (e) error-count)
;; In a file named "filter-function."
;; define and test (filter-latitude)
;; (or type the function in the thread's repl,
;; but that is unrealistic.)
;; Filter latitudes more than 2 degrees from 35:
(define filter-latitude ;; define in another file
( lambda ( latitude )
(if (> 2 (abs (- latitude 35)))
latitude
#f)))
;; Load "filter-function" from the thread's repl, and
;; watch the errors stop ( using (e) ).
;; An error-handler could enclose the thread's
;; function to prevent a bad filter from killing
;; the thread.
rac
On May 9, 2006, at 9:48 AM, spdegabrielle at gmail.com wrote:
> Hi, I was wondering if anyone could give an example of modifying a
> running scheme program?
>
> I have read that this was something that users of 'lisp machines'
> could do, and was wondering if it applies to all lisps/schemes?
>
> Regards,
>
> Stephen De Gabrielle
>
>
> --
>
> spdegabrielle at gmail.com
>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme