Created
August 28, 2019 20:40
-
-
Save ruliana/d5877e7c35b1977877f85e1b46632ba0 to your computer and use it in GitHub Desktop.
Function selection based on pre-conditions in Chez Scheme (very nice exercise using call/cc)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (pre-condition assertion) 'noop) | |
(define (try . procs) | |
(let loop ([procs procs]) | |
(define proc (car procs)) | |
(define (make-pre-condition k) | |
(lambda (assertion) | |
(when (not assertion) | |
(k (lambda () (loop (cdr procs))))))) | |
(define (exec k) | |
(fluid-let ([pre-condition (make-pre-condition k)]) | |
(let ([v (proc)]) | |
(lambda () v)))) | |
(define rslt (call/cc exec)) | |
(rslt))) | |
(define (a) 1) | |
(define (b) (pre-condition #f) 2) | |
(define (c) 3) | |
(printf "~s\n" (try a b c)) | |
(printf "~s\n" (try b c)) | |
(printf "~s\n" (try c)) | |
(printf "~s\n" (try a)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment