Last active
December 18, 2015 08:59
-
-
Save Metaxal/5758394 to your computer and use it in GitHub Desktop.
Allowing for internal definitions in `and'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require (for-syntax syntax/parse)) | |
(define-syntax (and/def stx) | |
(syntax-parse stx | |
#:literals (define define-values) | |
[(_) #'#t] | |
[(_ [define-values (var:id ...) val:expr] rest ...) | |
#'(let-values ([(var ...) val]) | |
(and/def rest ...))] | |
[(_ [define var:id val:expr] rest ...) | |
#'(let ([var val]) (and/def rest ...))] | |
[(_ val:expr) | |
#'val] | |
[(_ val:expr rest ...) | |
#'(and val (and/def rest ...))] | |
)) | |
(module+ test | |
(require rackunit) | |
(check-equal? (and/def) #t) | |
(check-equal? (and/def 5) 5) | |
(check-equal? (and/def (rest '(a b c))) '(b c)) | |
(check-equal? (and/def (define rest '(a b c)) rest) '(a b c)) | |
(check-equal? (and/def #f 5) #f) | |
(check-equal? (and/def 3 | |
(define-values (x y) (values 1 2)) | |
y) | |
2) | |
) | |
(module+ main | |
(and/def 3 | |
(define l '(a b)) | |
l | |
(define-values (x y) (values 3 4)) | |
x | |
(cons x l))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(require (for-syntax syntax/parse)) | |
(define-syntax (and/let stx) | |
(syntax-parse stx | |
[(_) #'#t] | |
[(_ #:let-values (var:id ...) val:expr rest ...) | |
#'(let-values ([(var ...) val]) | |
(and/let rest ...))] | |
[(_ #:let var:id val:expr rest ...) | |
#'(let ([var val]) (and/let rest ...))] | |
[(_ val:expr) | |
#'val] | |
[(_ val:expr rest ...) | |
#'(and val (and/let rest ...))] | |
)) | |
(module+ test | |
(require rackunit) | |
(check-equal? (and/let) #t) | |
(check-equal? (and/let 5) 5) | |
(check-equal? (and/let (rest '(a b c))) '(b c)) | |
(check-equal? (and/let #:let rest '(a b c) rest) '(a b c)) | |
(check-equal? (and/let #f 5) #f) | |
(check-equal? (and/let 3 | |
#:let-values (x y) (values 1 2) | |
y) | |
2) | |
) | |
(module+ main | |
(and/let 3 | |
#:let l '(a b) | |
l | |
#:let-values (x y) (values 3 4) | |
x | |
(cons x l))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(define-syntax-rule (let/ec/check check body ...) | |
(let/ec return | |
(define (check v) | |
(unless v (return #f))) | |
body ... | |
)) | |
(module+ main | |
(define (try-me x) | |
(let/ec/check check | |
(check x) | |
(define y (string->number x)) | |
(check y) | |
(define z (member y '(1 2 3))) | |
(check z) | |
(cons 'a z) | |
)) | |
(try-me "2") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See discussion:
https://groups.google.com/forum/?fromgroups#!topic/racket-users/D1oWQ1ysuek