Skip to content

Instantly share code, notes, and snippets.

@ijp
Created November 7, 2011 20:47
Show Gist options
  • Save ijp/1346120 to your computer and use it in GitHub Desktop.
Save ijp/1346120 to your computer and use it in GitHub Desktop.
;; Syntax parameter examples
(define-syntax-parameter abort
(syntax-rules ()))
(define-syntax forever
(syntax-rules ()
[(forever body ...)
(call/cc (lambda (abort-k)
(syntax-parameterize
([abort (syntax-rules () [(_) (abort-k #f)])])
(let loop () body ... (loop)))))]))
(define-syntax-parameter it
(syntax-rules ()))
(define-syntax aif
(syntax-rules ()
[(aif test then else)
(let ([it* test])
(syntax-parameterize ([it (identifier-syntax it*)])
(if it* then else)))]))
(define-syntax while
(syntax-rules ()
[(while test body ...)
(forever (aif test (begin body ...) (abort)))]))
(define-syntax until
(syntax-rules ()
[(until test body ...)
(while (not test) body ...)]))
(let ((i 10))
(until (zero? i)
(format #t "i:~a~%" i)
(set! i (- i 1))))
(newline)
(let ((j (list 0 1 2 3 4))
(k 0))
(while (member k j)
(format #t "passed with ~a~%" it)
(set! k (+ k 1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment