Search code examples
schemeguile

Dotted lists in Guile Scheme


How can I specifically check for dotted-lists in the form (a . b) Guile? The dotted-list of srfi-1 strangely returns #t also for e.g. Numbers (since when are numbers Lists too? https://www.gnu.org/software/guile/manual/html_node/SRFI_002d1-Predicates.html)! And pair? will evaluate to #t also for normal lists. Is there a way to differentiate the (a . b) construct from other things, whereas the b part (the cdr) could itself be any object including other association lists etc.?

This is what i didn't expect and can't understand:

(dotted-list? '(c . ((d . 3)
                     (e . 4)))) ; ===> #f

(dotted-list? 3) ; ===> #t

Solution

  • Note that (atom . (x1 ... xn)) is another way of writing (atom x1 ... xn), so (c . ((d . 3) (e . 4))) is just equivalent to (c (d . 3) (e . 4)) which is nothing more than a three element list (and for this reason dotted-list? returns false in this case).

    If you don't like the definition of dotted-list? given in srf-1 then define your own version:

    (define (my-dotted-list? l)
      (and (dotted-list? l)
           (pair? l)))