greghendershott / rackjure

Provide a few Clojure-inspired ideas in Racket. Where Racket and Clojure conflict, prefer Racket.
BSD 2-Clause "Simplified" License
236 stars 17 forks source link

(alist? '((1 2))) => true #11

Closed JohnCGriffin closed 11 years ago

JohnCGriffin commented 11 years ago

If (alist? '((1 . 2))) should be true and (alist? ((1 2))) should be false, I'm thinking that

(and (pair? x)
     (not (empty? (cdr x)))
     (not (pair? (cdr x))))
greghendershott commented 11 years ago

Thank you for using this and for the issue report!

I don't think that's correct. After all, it is valid for a key/value member of a dictionary to have a list value. For instance a dictionary can map symbol? to list?. Therefore an alist should allow the cdr of each member to be a pair; in fact the cdr can be any/c.

To back up and look at plain #lang racket:

#lang racket
(define d '([a . (1 2 3)]
            [b . (4 5 6)]))
(dict? d)        ;=> #t
(dict-ref d 'b)  ;=> '(4 5 6)

d is a valid dict?.

Next, #lang rackjure:

#lang rackjure
(define d (alist 'a '(1 2 3) 
                 'b '(4 5 6)))
(dict? d)       ;=> #t
(alist? d)      ;=> #t
(dict-ref d 'b) ;=> '(4 5 6)
(d 'b)          ;=> '(4 5 6)
('b d)          ;=> '(4 5 6)

I think it's correct for alist? to return #t. In other words, it is sufficient for alist? to test whether each member of the list is pair?. It does not need to, and in fact should not, also check that the cdr of each member is not pair? as you suggest.

Does that make sense? If you agree, I'll close this. If not, please help me understand more, including maybe the use case where you would expected alist? to behave the way you described?

Thanks again.

JohnCGriffin commented 11 years ago

Your reasoning and explanations are sensible. I naturally associate alists with pairs whose cdrs are not pair? but there's certainly no reason why they can't be.

Related to rackjure, it handled the applicative hash that was Clojure-like. My code is so heavily weighted to hash extraction that Clojure had started to look like a plausible replacement to my working Racket code. So, you, Matthias and Asumu led me in a good direction.

For the record, I landed on your code related to: http://www.mail-archive.com/users@racket-lang.org/msg18811.html

Thanks again,


John Griffin, CTO IT Talent Team, LLC www.ittalentteam.com 855-488-8326

On Aug 9, 2013, at 10:35 AM, Greg Hendershott notifications@github.com wrote:

Thank you for using this and for the issue report!

I don't think that's correct. After all, it is valid for a key/value member of a dictionary to have a list value. For instance a dictionary can map symbol? to list?. Therefore an alist should allow the cdr of each member to be a pair; in fact the cdr can be any/c.

To back up and look at plain #lang racket:

lang racket

(define d '([a . (1 2 3)] [b . (4 5 6)])) (dict? d) ;=> #t (dict-ref d 'b) ;=> '(4 5 6) d is a valid dict?.

Next, #lang rackjure:

lang rackjure

(define d (alist 'a '(1 2 3) 'b '(4 5 6))) (dict? d) ;=> #t (alist? d) ;=> #t (dict-ref d 'b) ;=> '(4 5 6) (d 'b) ;=> '(4 5 6) ('b d) ;=> '(4 5 6) I think it's correct for alist? to return #t. In other words, it is sufficient for alist? to test whether each member of the list is pair?. It does not need to, and in fact should not, also check that the cdr of each member is not pair? as you suggest.

Does that make sense? If you agree, I'll close this. If not, please help me understand more, including maybe the use case where you would expected alist? to behave the way you described?

Thanks again.

— Reply to this email directly or view it on GitHub.

greghendershott commented 11 years ago

For the record, I landed on your code related to: http://www.mail-archive.com/users@racket-lang.org/msg18811.html

I remember that.

Yeah, anything using nested dictionaries, like you often find in JSON, can be tedious:

(dict-ref (dict-ref (dict-ref dict 'a) 'b 'c))

Fortunately a combination of the ~> threading macro and applicative dictionaries makes it:

(~> dict 'a 'b 'c)

Which is nearly as simple as in JavaScript:

dict.a.b.c