Closed ghost closed 4 years ago
An Association List was exactly what i guessed, the problem with pair?
is that (pair? '(1 2 3)) ; ===> #t
.
In your last example, something of the form (atom . list)
is the same as (cons 'atom list)
which result in a proper list:
'(b . (1 2 3)) ; ===> (b 1 2 3)
(cons 'b '(1 2 3)) ; ===> (b 1 2 3)
Hence there will be a problem with the last pair in case 2:
2:
'((a . 1)
(b . 2)
(c . '((d . 3)
(e . 4))))
since we get:
'(c . ((d . 3) (e . 4))) ; ===> (c (d . 3) (e . 4))
;; '(x . (y z)) is just the notation for (x y z)
and there is a disharmony in the entries and why (alist? '((a . 1) (b . (1 2 3))))
evals to #f.
For normal (strictly speaking) dotted-lists a predicate of this form would work:
(define (ly/dotted-list? L)
"Returns true only for L in form of '(x . y)."
(and (dotted-list? L)
(pair? L)))
(define (alist? obj)
"Returns #t if all elements of OBJ are of type ly/dotted-list?."
(every ly/dotted-list? obj))
(alist? '((a . 1) (b . 2) (c . 3))) ; ===> #t
However your code works also on the last example for me:
(define (alist? obj)
(and (list? obj)
(every pair? obj)))
(alist?
'((a . 1)
(b . (1 2 3)))) ; ===> #t
Thank you for that. Unfortunately it doesn't work, probably there's a misunderstanding what
alist
stands for. Please try to determine what in the source was misleading you so we can fix the wording.alist
does not reference "a list" but "association list", which is a list whose every element is a pair:but also in nested form:
whereas a regular list is not an
alist?
The original predicate results in
#t
for 1: and 2:, and#f
for 3. Your proposal correctly evalutes for 1: but switches the other two, the existing implementation handles these three cases correctly.However, while testing this I realized there still is an error in my definition:
should evaluate to
#t
because this is an alist, but the(1 2 3)
list seems to spoil it. So there is something to be done, although unfortunately not the proposed solution.