Skip to content

Instantly share code, notes, and snippets.

@ruliana
Created August 28, 2019 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruliana/d5877e7c35b1977877f85e1b46632ba0 to your computer and use it in GitHub Desktop.
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)
(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