[racket] member et al.

From: Neil Toronto (neil.toronto at gmail.com)
Date: Thu Nov 11 13:39:39 EST 2010

Eli Barzilay wrote:
> About a minute ago, Carl Eastlund wrote:
>> On Thu, Nov 11, 2010 at 1:14 PM, Eli Barzilay <eli at barzilay.org> wrote:
>>> So, you advocate eliminating `member' etc for predicates (why I
>>> started with saying that this is much stickier than just adding a
>>> `member?').  So we started with
>>>
>>>  (cdr (assq x alist))
>>>
>>> then Scheme upgraded this to
>>>
>>>  (cond [(assq x alist) => cdr]
>>>        [else #f])
>>>
>>> and you suggest continuing with
>>>
>>>  (cond #:with (define p (assq x alist))
>>>        [(pair? p) (cdr p)]
>>>        [else #f])
>>>
>>> or, with more well-behaved proper sub lists
>>>
>>>  (cond #:with (define p (assq x alist))
>>>        [(not (eq? p #f)) (cadr p)]
>>>        [else #f])
>>>
>>> (Yes, you can still use `pair?' but that would be ugly for the same
>>> reason.)
>>>
>>> I also wonder how many newbies (or people that just want to type less)
>>> will fall into traps like
>>>
>>>  (if (member? x l)
>>>    (+ 1 (find x l))
>>>    0)
>>>
>>> My loud "ugh" should be expected now.
>> (dict-ref x alist)
> 
> The above is "an example", substitute appropriately.

Do you have other examples in mind? Truthiness values are a substitute 
Maybe monad, so we could potentially find it anywhere Haskell types use 
Maybe. Regexp matching? Where else?

Truthiness values require nonlocal changes to deal with the None (#f) 
case, just like the Maybe monad does. Sounds like a good reason to avoid 
them, and stick to higher-level control, or passing to "value-missing" 
thunks like in hash-ref.

Also, I can read this only because I'm already insane:

     (cond [(assq x alist) => cdr]
           [else #f])

Neil T



Posted on the users mailing list.