Skip to content

Instantly share code, notes, and snippets.

@kazh98
Created October 30, 2013 15:46
Show Gist options
  • Save kazh98/7234940 to your computer and use it in GitHub Desktop.
Save kazh98/7234940 to your computer and use it in GitHub Desktop.
Non-deterministic Lambda Syntax Implemented in Racket
#lang racket
(define *ambitions* '())
(define (nd-choice n)
(let/cc return
(for ((i (in-range n)))
(let/cc cont
(set! *ambitions* (cons cont *ambitions*))
(return i))
(set! *ambitions* (cdr *ambitions*)))
n))
(define-syntax nd-lambda
(syntax-rules ()
((_ (args ...) body ...)
(begin
(set! *ambitions* '())
(lambda (args ...)
(let ((result (begin body ...)))
(cond
(result result)
((null? *ambitions*) #f)
(else ((car *ambitions*))))))))))
;;;
;;; EXAMPLE
;;;
(define >=7
(nd-lambda (n) (= (nd-choice n) 7)))
(>=7 5) ;=> #f
(>=7 10) ;=> #t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment