How to process a list that contains "undefined" values?

Hi Folks, How would you find the maximum value in this list: [undefined, 10, undefined, 20] Is it bad practice to create lists that contain undefined values? If yes, what is a better way to express in a list a "no value present/available" value? For example would it be better to express the above list using Maybe: [Nothing, Just 10, Nothing, Just 20] Or perhaps something else? What do you recommend? /Roger

On Jul 2, 2011, at 10:09 AM, Costello, Roger L. wrote:
For example would it be better to express the above list using Maybe:
[Nothing, Just 10, Nothing, Just 20]
That is exactly the right way to do it.
Or perhaps something else?
What do you recommend?

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. If you absolutely have to do something like that, you would have to use Control.Exception.catch to catch the undefined values and ignore them/remove them from the list. But if your undefined values are not so nice as Prelude.undefined but actually nonterminating computations, calculating the maximum won't terminate either.
Is it bad practice to create lists that contain undefined values?
That depends on how they will be further processed. Sometimes it's perfectly fine to have undefined values in a list, sometimes not.
If yes, what is a better way to express in a list a "no value present/available" value?
For example would it be better to express the above list using Maybe:
[Nothing, Just 10, Nothing, Just 20]
Yes. If possible, indicating the absence of a value via Maybe is the right thing to do. Or something including a message why there is no value in a particular place, like Either err val.
Or perhaps something else?
What do you recommend?
[Maybe Integer], [Either Message Integer], whatever is more appropriate; then you can use functions like catMaybes or partitionEithers, rights, lefts, ... to cleanly process the list.

On Sat, 2 Jul 2011 16:32:37 +0200
Daniel Fischer
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"?
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.

On Sunday 03 July 2011, 08:40:03, Mike Meyer wrote:
On Sat, 2 Jul 2011 16:32:37 +0200
Daniel Fischer
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?)

On Sat, Jul 2, 2011 at 10:09, Costello, Roger L.
How would you find the maximum value in this list:
[undefined, 10, undefined, 20]
Is it bad practice to create lists that contain undefined values?
"undefined" is not "null"; "Nothing" is. "undefined" represents a computation that never produces a result: an infinite loop or a crash. Some but by far not all of these can be characterized at runtime and turned into exceptions. (The literal Prelude.undefined explicitly throws an exception, but could just as well be "let x = x in x" and let the ghc runtime catch it.) The type tells you this right out, by the way:
undefined :: a
What can you do with a "value" that can be any conceivable type, and about which nothing else can be said? It has no handles, no behavior; the only possible interpretation is that it's not a value at all. -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms

On Jul 2, 2011, at 11:04 AM, Brandon Allbery wrote:
The type tells you this right out, by the way:
undefined :: a
What can you do with a "value" that can be any conceivable type, and about which nothing else can be said? It has no handles, no behavior; the only possible interpretation is that it's not a value at all.
Its type makes undefined valuable in creating polymorphic functions. You can see many examples of it in my article for the Monad.Reader: "How to refold a map."
"undefined" is rendered by the symbol for "bottom" in the formatted code. ____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d@vidplace.com

On Jul 2, 2011, at 11:04 AM, Brandon Allbery wrote:
What can you do with a "value" that can be any conceivable type, and about which nothing else can be said?
Also, here's an implementation of incremental maps using finger trees and undefined.
____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d@vidplace.com
participants (5)
-
Brandon Allbery
-
Costello, Roger L.
-
Daniel Fischer
-
David Place
-
Mike Meyer