0

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
Student
  • 708
  • 4
  • 11
  • 1
    A pair whose `cdr` is a proper list is a proper list. The "dotted" version would be '(c . ((d . 3) . (e . 4))). – molbdnilo Jan 23 '20 at 09:56

2 Answers2

3

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)))
Renzo
  • 26,848
  • 5
  • 49
  • 61
2

If you wanted a standalone definition (one that doesn't depend on an existing definition which you don't agree with), then I think this is reasonable:

(define (dotted-list? c)
  (and (cons? c)
       (cond [(cons? (cdr c))
              (dotted-list? (cdr c))]
             [(null? (cdr c))
              #f]
             [else #t])))

Note that like any simple-minded definition this fails to halt on circular lists.