guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Syntactic significance of dot


From: Panicz Maciej Godek
Subject: Re: Syntactic significance of dot
Date: Mon, 22 Sep 2014 20:32:06 +0200

2014-09-22 20:12 GMT+02:00 Richard Shann <address@hidden>:
I've come across some (working) scheme code whose meaning I can't
unravel. The problem is there is a "." character whose significance
eludes me. The guile reference doesn't index this character, and I can
only find references to it in writing literal pairs.
But I'm sure someone experienced would recognize what this could be (the
dot occurs on the eleventh line):

  (define (ignatzek-format-exception
           root
           exception-markup
           bass-pitch
           lowercase-root?)

    (make-line-markup
     `(
       ,(name-root root lowercase-root?)
       ,exception-markup
       .
       ,(if (ly:pitch? bass-pitch)
            (list (ly:context-property context 'slashChordSeparator)
                  (name-note bass-pitch #f))
            '()))))

The dot is used in the default representation of improper lists, i.e. lists whose last element is not the '() object. The element appearing after the dot is the element pointed to by the last cdr in the sequence of cons pairs.
Therefore, if you do (cons 'a 'b), you get
(a . b)
and if you nest it in other conses, e.g. (cons 'c (cons 'a 'b)),
you get
(a b . c)

this is a syntactic equivalent of

(a . (b . c))

(i.e. it makes no difference to the reader whether you write '(a b . c) or '(a . (b . c)). Likewise, instead of writing (+ 2 3), you can write (+ . (2 . (3 . ()))) and you'll still get 5. Try it out!)

The limitation is that only one element can (and has to) appear after the period, and at least one element needs to appear before it. Otherwise you get a syntax voilation:

'(1 2 . 3)
===> (1 2 3)
'(1 2 . 3 4)
===> error
'(. 3)
===> 3 (it seems to work in guile incidentally, but fails in racket)

This behaviour is coherent in the context of quasiquote:
(let ((a 1) (b 2) (c 3))
  `(,a ,b . ,c))
yields
(1 2 3)

Note however, that if c was a list, it would be appended to the end:

(let ((a 1)(b 2)(c '(3 4 5)))
  `(,a ,b . ,c))
===> (1 2 3 4 5)

Note also, that using unquote in the tail position is equivalent to using unquote-splicing with the last element, so the last example is equivalent with:

(let ((a 1)(b 2)(c '(3 4 5))
  `(,a ,b ,@c))

HTH
M.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]