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

Eli Barzilay eli at barzilay.org
Wed Nov 1 16:05:33 EST 2006


On Nov  1, AndrevanTonder wrote:
> 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.

Well, I think that it is not very common to know that `find' is
actually going to return something (in fact, I don't remember ever
making such an assumption), and OTOH it is very common to know that
if you find something, it cannot be #f.  In other words, I think that:
* It is common to have a list that you know to be #f-free
* It is uncommon to have a list that you know to contain a good
  element, and still use `find'


> > 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.

I can only provide personal evidence of the other side.  For example:

> 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.

I read this and immediately
* wonder why you use `find' in the first place
* assuming that you use `equal?' there, I immediately understand that
  your `eq-set' should be a list of non-#f-values.

(But like I said, that only personal anecdote, from someone who has 40
exams to grade in the very near future...)

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!



More information about the r6rs-discuss mailing list