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/list.rkt

28 lines
730 B
Racket

8 years ago
#lang racket/base
8 years ago
(require br/define (for-syntax racket/base syntax/parse))
8 years ago
(provide (all-defined-out))
(define-macro (values->list EXPR)
#'(call-with-values (λ () EXPR) list))
8 years ago
(define-syntax (push! stx)
(syntax-parse stx
[(_ ID:id VAL)
#'(set! ID (cons VAL ID))]))
8 years ago
(define-syntax (pop! stx)
(syntax-parse stx
[(_ ID:id)
#'(let ([x (car ID)])
(set! ID (cdr ID))
x)]))
8 years ago
(module+ test
(require rackunit)
(check-equal? '(1 2 3) (values->list (values 1 2 3)))
(check-equal? (let ([xs '(2 3)])
(push! xs 1)
xs) '(1 2 3))
(check-equal? (let ([xs '(1 2 3)])
(define x (pop! xs))
(cons x xs)) '(1 2 3)))