You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/beautiful-racket-lib/br/cond.rkt

36 lines
817 B
Racket

9 years ago
#lang racket/base
(require (for-syntax racket/base br/syntax)
br/define)
9 years ago
(provide (all-defined-out))
(define-macro (until COND EXPR ...)
#'(let loop ()
(unless COND
EXPR ...
(loop))))
9 years ago
(define-macro (while COND EXPR ...)
#'(let loop ()
(when COND
EXPR ...
(loop))))
9 years ago
(define-macro (forever . EXPRS)
;; todo: would be better with a syntax parameter
(with-pattern
([STOP (datum->syntax #'EXPRS 'stop)])
#'(let/ec STOP
(while #t . EXPRS))))
9 years ago
(module+ test
(require rackunit)
(check-equal? (let ([x 5])
(until (zero? x)
(set! x (- x 1)))
x) 0)
(check-equal? (let ([x 5])
(while (positive? x)
(set! x (- x 1)))
x) 0))