From f9a68d0ae1be6142006f1580f8aebc8d5b7237d2 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 25 Jul 2014 12:39:18 -0700 Subject: [PATCH] update sugar/list --- list.rkt | 13 ++++++++++++- macro.rkt | 32 -------------------------------- misc.rkt | 8 -------- scribblings/list.scrbl | 8 ++++++++ 4 files changed, 20 insertions(+), 41 deletions(-) delete mode 100644 macro.rkt diff --git a/list.rkt b/list.rkt index 56ce6a6..3eefadb 100644 --- a/list.rkt +++ b/list.rkt @@ -1,4 +1,5 @@ #lang racket/base +(require (for-syntax racket/base)) (require racket/list racket/set) (require "define.rkt" "len.rkt" "coerce.rkt") @@ -58,4 +59,14 @@ (error (string-append "members-unique? failed because " (if (= (len duplicate-keys) 1) "item isn’t" "items aren’t") " unique:") duplicate-keys)) - result)) \ No newline at end of file + result)) + + +;; for use inside quasiquote +;; instead of ,(when ...) use ,@(when/splice ...) +;; to avoid voids +(provide when/splice) +(define-syntax (when/splice stx) + (syntax-case stx () + [(_ test body) + #'(if test (list body) '())])) \ No newline at end of file diff --git a/macro.rkt b/macro.rkt deleted file mode 100644 index f157e3b..0000000 --- a/macro.rkt +++ /dev/null @@ -1,32 +0,0 @@ -#lang racket/base -(require (for-syntax racket/base)) -(require racket/syntax) - -(provide define->macro) - - -(define-syntax define->macro - (syntax-rules () - [(_ (proc-name arg ... . rest-arg) proc-expr ...) (define->macro proc-name (λ(arg ... . rest-arg) proc-expr ...))] - [(_ proc-name proc) (define-syntax proc-name - (syntax-id-rules () - [(proc-name expr (... ...)) (proc expr (... ...))] - [proc-name proc]))])) - - -;(define foo (λ(x) (add1 x))) -;(define->macro foo (λ(x) (add1 x))) - -;(foo 4) -;(map foo '(2 4 6)) - -;(define (bar x) (add1 x)) -;(define->macro (bar x y) (+ x y)) - -;(bar 4 1) -;(map bar '(2 4 6) '(2 4 6)) - -;(define-syntax zam (add1 x)) - -;(zam 4) -;(map zam '(2 4 6)) \ No newline at end of file diff --git a/misc.rkt b/misc.rkt index 8f8ed5d..19a0606 100644 --- a/misc.rkt +++ b/misc.rkt @@ -21,11 +21,3 @@ [else (format "~a bytes" bytecount)])) -;; for use inside quasiquote -;; instead of ,(when ...) use ,@(when/splice ...) -;; to avoid voids -(provide when/splice) -(define-syntax (when/splice stx) - (syntax-case stx () - [(_ test body) - #'(if test (list body) '())])) \ No newline at end of file diff --git a/scribblings/list.scrbl b/scribblings/list.scrbl index d7517ef..25d5dac 100644 --- a/scribblings/list.scrbl +++ b/scribblings/list.scrbl @@ -73,4 +73,12 @@ Same as @racket[members-unique?], but if the members are not unique, raises a de (members-unique?/error '(a b c d e f a b)) ] +@defform[(when/splice test expr)] +A special version of @racket[when] that you can use inside @racket[quasiquote] to suppress @racket[void] values when @racket[_test] is @racket[#f]. As the name suggests, it works in conjunction with the @litchar["@"] splicing operator. +@examples[#:eval my-eval +`(,(when (even? 2) "hooray")) +`(,(when (even? 3) "hooray")) +`(,@(when/splice (even? 2) "hooray")) +`(,@(when/splice (even? 3) "hooray")) +]