107-quasiquote.scm (2476B)
1 ; One-level quasiquote: backtick reads as (quasiquote ...); inside that 2 ; template, (unquote x) splices a single evaluated value, and 3 ; (unquote-splicing xs) splices the elements of xs in place. Atoms in 4 ; the template are returned literally; nested quasiquote forms are not 5 ; supported -- we don't recurse into them, so a `(quasiquote ...) form 6 ; sitting inside the template is preserved as-is. 7 8 ;; --- Atom template: returns the atom ------------------------------------- 9 (if (= 7 `7) 0 (sys-exit 1)) 10 (if (eq? 'foo `foo) 0 (sys-exit 2)) 11 (if (equal? '() `()) 0 (sys-exit 3)) 12 13 ;; --- All-literal list template ------------------------------------------ 14 (if (equal? (list 1 2 3) `(1 2 3)) 0 (sys-exit 4)) 15 (if (equal? '(a b c) `(a b c)) 0 (sys-exit 5)) 16 17 ;; --- ,x evaluates one element in place ---------------------------------- 18 (define x 10) 19 (if (equal? (list 1 10 3) `(1 ,x 3)) 0 (sys-exit 6)) 20 (if (equal? (list 'a 11 'b) `(a ,(+ x 1) b)) 0 (sys-exit 7)) 21 22 ;; --- ,x at head and tail positions -------------------------------------- 23 (if (equal? (list 10 'b) `(,x b)) 0 (sys-exit 8)) 24 (if (equal? (list 'a 10) `(a ,x)) 0 (sys-exit 9)) 25 26 ;; --- ,@xs splices a list in place --------------------------------------- 27 (define xs (list 2 3 4)) 28 (if (equal? (list 1 2 3 4 5) `(1 ,@xs 5)) 0 (sys-exit 10)) 29 30 ;; --- ,@ at the head of the list ----------------------------------------- 31 (if (equal? (list 2 3 4 'tail) `(,@xs tail)) 0 (sys-exit 11)) 32 33 ;; --- ,@ at the tail of the list ----------------------------------------- 34 (if (equal? (list 'head 2 3 4) `(head ,@xs)) 0 (sys-exit 12)) 35 36 ;; --- ,@'() splices nothing ---------------------------------------------- 37 (if (equal? (list 'a 'b) `(a ,@'() b)) 0 (sys-exit 13)) 38 39 ;; --- Mixed: literals, ,, ,@ in one template ----------------------------- 40 (if (equal? (list 'h 10 2 3 4 'mid 11 't) 41 `(h ,x ,@xs mid ,(+ x 1) t)) 42 0 (sys-exit 14)) 43 44 ;; --- Nested-list template (no ,) is preserved literally ----------------- 45 (if (equal? '((a b) (c d)) `((a b) (c d))) 0 (sys-exit 15)) 46 47 ;; --- , inside a nested list still evaluates at this depth --------------- 48 (if (equal? (list (list 1 10) 'k) `((1 ,x) k)) 0 (sys-exit 16)) 49 50 ;; --- ,@ inside a nested list splices into that nested list -------------- 51 (if (equal? (list (list 1 2 3 4) 'k) `((1 ,@xs) k)) 0 (sys-exit 17)) 52 53 ;; --- (quasiquote x) prefix form parses identically to `x ---------------- 54 (if (equal? (list 1 10 3) (quasiquote (1 (unquote x) 3))) 0 (sys-exit 18)) 55 56 (sys-exit 0)