[racket] member et al.

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

On Thu, Nov 11, 2010 at 1:21 PM, Eli Barzilay <eli at barzilay.org> wrote:
> About a minute ago, Carl Eastlund wrote:
>> 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)
>
> The above is "an example", substitute appropriately.

As is mine.  We have a dict library, a sequence library, and loop
comprehensions for a reason.  Code doing any kind of key/value lookup
should not have to use cond, car, or cdr to do so.

--Carl


Posted on the users mailing list.