NULLs

Sami Mäkelä mrsmkl@saunalahti.fi
Fri, 16 Jul 1999 23:27:45 +0300


Hans-Dieter.Dreier@materna.de wrote:

> I'm not so familiar with functional languages, although I think I get your point. I'll try to translate your example to "imperative"; please tell me when I'm wrong:
>
> "Maybe a" is a parameterised type (template in C++ speak),
> "a" would be the template parameter.
> "Just" is needed to allow pattern matching to distinguish between "a" and "Nothing". Otherwise "Nothing" could legally be substituted for "a".
>
> "Just" is a constant, so to speak, where "a" is a variable. How does the compiler know that? Capitalization? If so, why is "maybe_square" lowercase?
>

Just and Nothing are really constructors for type Maybe. Compiler deals with them much like functions. It is true that in some languages they must be capitalized.
maybe_square is a function; functions are handled in functional languages same way as all other values

>"Maybe Int" in the definition of maybe_square makes sure that "a" actually must be an Int.
>Can it also be derived from an Int? (if "derived" makes any sense here)

Haskell doesn't have derivation in same sense as OO languages, but you could change the type to

>maybe_square :: Num a => Maybe a -> Maybe a

this means that type a must be derived from type class Num.

>But why do you write "Just a" in the last line instead of "Just Int"?
>And why the parentheses around "(Just a)"?

in "Just a", 'a' is variable that is bound if the function argument matches this case. parentheses are needed so that the compiler knows that "Just a" is a single
pattern

> The comment at the beginning says "...algebraic datatype...".
>Does "algebraic" mean "numerical"?
>How do we know it is algebraic - it looks rather generic to me, could by any type (say, a string)?

no it doesn't mean numerical; it can be any type. another name for algebraic datatypes is (disjoint) sum types