[Haskell-cafe] Trapped by the Monads
lennart at augustsson.net
Tue Sep 20 20:59:34 EDT 2005
There's a big difference.
You can see you are doing something fishy, and the compiler
can too, and it can warn you.
Michael Walter wrote:
> int *p=...;
> int x=*p;
> p = ...
> Just x = p
> So actually, there is few difference between dereferencing a pointer
> without checking for 0, and extracting the Maybe value without
> handling Nothing, apart from that it leads to undefined behavior in C
> which in fact isn't really a point against "hybrid variables".
> On 9/20/05, Lennart Augustsson <lennart at augustsson.net> wrote:
>>Mark Carter wrote:
>>>The typical example in C is:
>>>mem = malloc(1024)
>>>Malloc returns 0 to indicate that memory cannot be allocated, or a
>>>memory address if it can. The variable mem is a so-called hybrid
>>>variable; it crunches together 2 different concepts: a boolean value
>>>(could I allocate memory?) and an address value (what is the address
>>>where I can find my allocated memory).
>>>It's considered a bad idea because it makes it easy for programmers to
>>>use the value inappropriately - witness the number of programmers who
>>>pass in 0 as a memory location.
>>This is a bad idea in C, because you cannot force programmers to test
>>the return value properly.
>>The Maybe type in Haskell is a good idea, because you must test the
>>a Maybe value to extract the real value. (Using the Maybe monad this
>>can be hidden.)
>> -- Lennart
>>Haskell-Cafe mailing list
>>Haskell-Cafe at haskell.org
More information about the Haskell-Cafe