[Haskell-beginners] How to process a list that contains "undefined" values?
Daniel Fischer
daniel.is.fischer at googlemail.com
Sun Jul 3 10:47:40 CEST 2011
On Sunday 03 July 2011, 08:40:03, Mike Meyer wrote:
> On Sat, 2 Jul 2011 16:32:37 +0200
>
> Daniel Fischer <daniel.is.fischer at googlemail.com> wrote:
> > On Saturday 02 July 2011, 16:09:05, Costello, Roger L. wrote:
> > > Hi Folks,
> > >
> > > How would you find the maximum value in this list:
> > > [undefined, 10, undefined, 20]
> >
> > Not. There is no proper maximum in the presence of undefined.
>
> Doesn't that mean the maximum value for this list is also "undefined"?
Yes, and that way, a simple call to maximum does the right thing (note
however, that it doesn't necessarily does the right thing for lists of
Double/Float containing NaNs).
I assumed the OP wanted the maximum of the defined values, in which case
such a situation is best avoided by using Maybe or something serving
similar purposes.
> That matches my intuition, which is that undefined should be a
> "contagious" value, so that any value that depends on evaluating an
> undefined value is itself undefined.
Right.
Except that in IO exceptions can be caught which muddies the waters
(is
foo = bar `Control.Exception.catch`
(\(ErrorCall msg) -> if msg == "Prelude.undefined"
then return 1 else return 2)
legit?)
More information about the Beginners
mailing list