Next: , Previous: , Up: srfi ilists   [Index]


2.40.5 Quotation

The various Scheme standards permit, but do not require, Scheme implementations to treat quoted pairs and lists as immutable. Thus whereas the expression (set-car! (list 1 2 3) 10) evaluates to the list (10 2 3), the expression (set-car! '(1 2 3) 10) is not portable and in fact an error.

This SRFI recommends that implementations that provide both this SRFI and immutable quotations should cause quotations to return the same immutable pairs that this SRFI describes. This means that the standard Scheme pair and list operations, as well as libraries like SRFI-1 which are built on them, should accept both mutable and immutable pairs: thus (car (ilist 1 2)) should evaluate to ‘1’.

This SRFI further recommends that read should return mutable pairs and lists when reading list structure. No recommendation is made about the behavior of write, display, and similar output procedures on immutable lists.

Syntax: iq ?datum

To make life easier for Scheme programmers, given that many implementations do not provide immutable quotation, the syntax keyword iq is provided as part of this SRFI.

It is analogous to quote, but takes an arbitrary number of literals and constructing an ilist from them, with any pairs in the literals converted to ipairs. It is useful for providing constant ipair–based objects.

(iq)            ≡ '()
(iq 1)          ≡ (ilist '1)
(iq (1))        ≡ (ilist (ilist '1))
(iq 1 . 2)      ≡ (ipair '1 '2)
(iq 1 2)        ≡ (ilist '1 '2)
(iq 1 2 3)      ≡ (ilist '1 '2 '3)

(iq (1) 2 3)    ≡ (ilist (ilist '1) '2 '3)
(iq 1 (2) 3)    ≡ (ilist '1 (ilist '2) '3)
(iq 1 2 (3))    ≡ (ilist '1 '2 (ilist '3))

(iq (1 2) 3)    ≡ (ilist (ilist '1 '2) '3)
(iq 1 (2 3))    ≡ (ilist '1 (ilist '2 '3))

(iq (x y) z)    ≡ (ilist (ilist 'x 'y) 'z)
(iq x (y z))    ≡ (ilist 'x (ilist 'y 'z))

Note that pairs within literal vectors or other implementation–dependent literals will not be converted:

(iq #(1 2))     ≡ (ilist '#(1 2))
(iq #(1 (2)))   ≡ (ilist '#(1 (2)))

see how the second example contains an implementation–dependent list ‘(2)’ rather than an ilist object.

Unfortunately, there is no ilist analogue of ‘'’ to abbreviate the notation, so we save keystrokes by using iq (rather than iquote) and omitting the top–level parentheses.

(define L
  (iq 1 2 3))

(icar L)        ⇒ 1
(icadr L)       ⇒ 2
(icaddr L)      ⇒ 3

(define P
  (iq 1 . 2))

(icar P)        ⇒ 1
(icdr P)        ⇒ 2

Quotation and quasiquotation

As Vicare extension, the library (srfi :116 quotations) exports the following keyword syntactic bindings.

Syntax: iquote ?datum

Similar to the R6RS syntax quote: take a single datum and expand to an expression in which all the pairs are transformed into immutable pairs.

(iquote)                error→ no match
(iquote 1 2)            error→ no match

(iquote 1)              ≡ 1
(iquote (1))            ≡ (ilist 1)
(iquote (1 . 2))        ≡ (ipair 1 2)
(iquote (1 2))          ≡ (ilist 1 2)
(iquote (1 2 3))        ≡ (ilist 1 2 3)

(iquote ((1) 2 3))      ≡ (ilist (ilist 1) 2 3)
(iquote (1 (2) 3))      ≡ (ilist 1 (ilist 2) 3)
(iquote (1 2 (3)))      ≡ (ilist 1 2 (ilist 3))

(iquote ((1 2) 3))      ≡ (ilist (ilist 1 2) 3)
(iquote (1 (2 3)))      ≡ (ilist 1 (ilist 2 3))

(iquote ((x y) z))      ≡ (ilist (ilist 'x 'y) 'z)
(iquote (x (y z)))      ≡ (ilist 'x (ilist 'y 'z))

;; vector templates
(iquote #(1 2))         ≡ '#(1 2))
(iquote #(1 (2)))       ≡ (vector 1 (ilist 2)))
(iquote (#(1 2)))       ≡ (ilist '#(1 2)))

(iquote #(1 (2 #(3 4) 5)))
≡ (vector 1 (ilist 2 (vector 3 4) 5)))

(iquote #(x (y #(s u) z)))
≡ (vector 'x (ilist 'y (vector 's 'u) 'z)))

(iquote #(1 (2 #(3 (4 . 5)) 6)))
≡ (vector 1 (ilist 2 (vector 3 (ipair 4 5)) 6)))
Syntax: iquasiquote ?datum
Auxiliary Syntax: iunquote
Auxiliary Syntax: iunquote-splicing

Similar to the R6RS syntax iquasiquote: take a single datum and expand to an expression in which outer pairs are transformed into immutable pairs. The datum may contain unquoted expressions.

The arguments of a iunquote-splicing form:

(iunquote-splicing ?expr ...)

must be expressions returning ilists.

Some basic examples:

(iquasiquote 1)                    ≡ 1
(iquasiquote (1))                  ≡ (iquote (1))
(iquasiquote (1 . 2))              ≡ (iquote (1 . 2))
(iquasiquote (1 2))                ≡ (iquote (1 2))
(iquasiquote (1 2 . 3))            ≡ (iquote (1 2 . 3))
(iquasiquote ((1) (2)))            ≡ (iquote ((1) (2)))
(iquasiquote ((1 2) (3 4)))        ≡ (iquote ((1 2) (3 4)))
(iquasiquote ((1 2) (3 4)))        ≡ (iquote ((1 2) (3 4)))
(iquasiquote ((1 . 2) (3 . 4)))    ≡ (iquote ((1 . 2) (3 . 4)))
(iquasiquote ((1 . 2) . (3 . 4)))  ≡ (iquote ((1 . 2) . (3 . 4)))
(iquasiquote ((1 2) . 3))          ≡ (iquote ((1 2) . 3))
(iquasiquote ((1 . 2) . 3))        ≡ (iquote ((1 . 2) . 3))

some iunquote examples:

(iquasiquote (iunquote 1))                 ≡ 1
(iquasiquote (iunquote (iquote (1 2 3))))  ≡ (iquote (1 2 3))
(iquasiquote ((iunquote) (+ 1 2)))         ≡ (iquote ((+ 1 2)))
(iquasiquote (1 (iunquote (+ 2 3))))       ≡ (iquote (1 5))
(iquasiquote (1 . (iunquote (+ 2 3))))     ≡ (ipair 1 (+ 2 3))

(iquasiquote ((iunquote (+ 10 1) (+ 20 2) (+ 30 3)) (+ 8 9)))
≡ (iquote (11 22 33 (+ 8 9)))

(iquasiquote #((iunquote (+ 1 2)) (+ 8 9)))
≡ `#(3 ,(iquote (+ 8 9)))

some iunquote-splicing examples:

(iquasiquote ((iunquote-splicing (iquote (1 2 3)))))
≡ (iquote (1 2 3))

(iquasiquote ((iunquote-splicing) 1))
≡ (iquote (1))

(iquasiquote ((iunquote-splicing '())
              ()))
≡ (ilist '())

(iquasiquote ((iunquote-splicing '()) . ()))
≡ '()

(iquasiquote ((iunquote-splicing (ilist (+ 1 2))) . 4))
≡ (ipair 3 4)

two examples on the difference between iunquote and iunquote-splicing.

(iquasiquote ((iunquote (ilist (+ 10 1))
                        (ilist (+ 20 2))
                        (ilist (+ 30 3)))
              (+ 8 9)))
≡ (iquote ((11) (22) (33) (+ 8 9)))

(iquasiquote ((iunquote-splicing (ilist (+ 10 1))
                                 (ilist (+ 20 2))
                                 (ilist (+ 30 3)))
              (+ 8 9)))
≡ (iquote (11 22 33 (+ 8 9)))

Next: , Previous: , Up: srfi ilists   [Index]