[r6rs-discuss] [Formal] FIND delivers an ambiguous value

AndrevanTonder andre at het.brown.edu
Wed Nov 1 15:51:06 EST 2006


On Wed, 1 Nov 2006, Eli Barzilay wrote:

> 1. It is much more convenient to write:
>
>     ... (find p list) ...
>
>   than
>
>     ... (cond [(memp p list) => car] [else #f]) ...
>
>   It's a simple concept that should not worth more than a few
>   characters.

I do not think this is making the correct comparison.  If you know a priori 
that the element is present, the comparison should be between

   (find p list)

   (car (memp p list))

If you do not know a priori that the element is present, the comparison should 
be between

   (let ((x (find p list)))
     (if x
         (do-something x)
         ----)))

   (let ((x (memp p list)))
     (if x
         (do-something (car x))
         ----)))

IMO the difference in either case is insignificant.

> 2. I think that *very* often you run into situations where the
>    fragility of `find' is irrelevant.  A typical example:
>
>     (find (lambda (str) (< 10 (string-length str))) strings)

People tend to run into different kinds of bugs, ad I can only speak for myself. 
I have at various times written procedures like find, and several times I have 
run into exactly the kind of bug we are discussing.  The thing is that it is 
very easy in Scheme to write and use predicates that are more polymorphic than 
intended, and the above monomorphic predicate may be more the exception than 
the rule.  For example, the following would seem very reasonable even to the 
eye of an experienced programmer:

(define (element-of x eq-set)
    (find (lambda (elem) (eq? elem x)) eq-set))

and is buggy.

Cheers
Andre



More information about the r6rs-discuss mailing list