Created
August 11, 2012 03:40
-
-
Save nyuichi/3320473 to your computer and use it in GitHub Desktop.
amb operator
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
;;; amb stuff | |
;; stack of cc. | |
(define fail '()) | |
;;; nondeterminsm macro operator | |
(define-syntax amb | |
(syntax-rules () | |
((_) ((pop! fail))) | |
((_ a) a) | |
((_ a b ...) | |
(call/cc | |
(lambda (cc) | |
(push! fail (lambda () | |
(cc (amb b ...)))) | |
a))))) | |
(call/cc | |
(lambda (cc) | |
;; initial value for fail | |
(push! fail | |
(lambda () | |
(cc 'no-choise))))) | |
;;; test code | |
(define (divisible? n m) | |
(zero? (mod n m))) | |
(define (square n) | |
(* n n)) | |
(define (*prime? n i) | |
(if (< n (square i)) | |
#t | |
(if (divisible? n i) | |
#f | |
(*prime? n (+ i 1))))) | |
(define (prime? n) | |
(*prime? n 2)) | |
(define (solve) | |
(let ((i (amb 4 6 7)) | |
(j (amb 5 8 11))) | |
(if (prime? (+ i j)) | |
(list i j) | |
(amb)))) | |
(print (solve)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment