[racket] member et al.

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Thu Nov 11 13:19:15 EST 2010

On Thu, Nov 11, 2010 at 1:14 PM, Eli Barzilay <eli at barzilay.org> wrote:
>> I got inspired by the #:when form in `for' loops that flattens
>> nesting, and wrote a `cond*' macro that allows this instead:
>>
>> (cond* [... some stuff ...]
>>         #:with (define m ...)
>>         [(... something about m ...)  (... something using m ...)]
>>         ... more things referring to m ...)
>>
>> It's more general, since it doesn't require encoding the condition
>> you want to test as a truthiness value. Also, the `define' doesn't
>> have to be a `define' - it can be any legal expression.
>
> 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)

--Carl


Posted on the users mailing list.