I want to determine whether a list contains only null. null? seems like the obvious choice:
> (null? null)
#t
But andmaping over it is not doing what I'd expect:
> (andmap null? '(null))
#f
Am I misunderstanding null? or andmap? I'm new to Racket, but this just doesn't seem right at all based on the docs.
Your problem is that '(null) is not a list containing null (i.e., the empty list). Here are a few options that will work:
(andmap null? '(()))(andmap null? `(,null)) or (andmap null? (quasiquote (unquote null)))(andmap null? (list null)).Explanation of what's going on: the identifier null is bound to a value representing the empty list. When you do 'null, this is equivalent to (quote null) which means "give me a symbol representing the identifier, not what it represents" (like quoting in natural language).
You can avoid this by using the literal that represents null '() or by using quasiquote and unquote to "escape" back to Racket.
For more information about quoting, see the Guide entry on it.
The problem is not with andmap, or with null?. It's the way you're constructing the list that's wrong. In fact, this list: '(null) is equivalent to this: (list 'null):
(equal? '(null) (list 'null))
=> #t
And 'null is not the same as null:
(equal? 'null null)
=> #f
So, what we need is a correct way to express a null value inside a list. Any of the following will work:
(andmap null? '(()))
=> #t
(andmap null? (quote (())))
=> #t
(andmap null? (list '()))
=> #t
(andmap null? (list null))
=> #t
(andmap null? `(,null))
=> #t
(andmap null? (quasiquote (unquote null)))
=> #t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With