Skip to content

Instantly share code, notes, and snippets.

@nyuichi
Created August 11, 2012 03:40
Show Gist options
  • Save nyuichi/3320473 to your computer and use it in GitHub Desktop.
Save nyuichi/3320473 to your computer and use it in GitHub Desktop.
amb operator
;;; 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