3 ms·
Here's an AOP-style comefrom syntax you can play with by setting DrRacket's language to R5RS: (define-syntax comefrom (syntax-rules () ((comefrom f
by brlewis 15y ago
Here's an AOP-style comefrom syntax you can play with by setting DrRacket's language to R5RS:
(define-syntax comefrom
(syntax-rules ()
((comefrom function expr ...)
(let ((old-function function))
(set! function
(lambda args
(begin expr ...)
(apply old-function args)))))))
(define (square x)
(* x x))
(define (hypotenuse a b)
(sqrt (+ (square a) (square b))))
(comefrom square
(display "I'm a square!\n"))
(hypotenuse 3 4)
The example in the Wikipedia article is more like this:
(define-syntax comefrom
(syntax-rules ()
((comefrom function)
(call-with-current-continuation
(lambda (here)
(set! function here))))))
(define (noop)
; Do nothing
#t)
(define (main)
(let ((i 0))
(comefrom noop)
(if (< i 5)
(begin
(set! i (+ i 1))
(display "Hello world!\n")
(noop)))))
(main)