NULLs

Hans-Dieter.Dreier@materna.de Hans-Dieter.Dreier@materna.de
Fri, 16 Jul 1999 18:48:41 +0200


--JeU20OJgAVFd1hlNPkgAP31rchCe0z08
Content-type: text/plain; charset="ISO-8859-1"
Content-transfer-encoding: quoted-printable

sami@pefletti.saunalahti.fi wrote:

>Hans-Dieter.Dreier@materna.de wrote:
>
>> NULLs
>> =3D=3D=3D=3D=3D
>>
>> Often there is a situation where a special value is needed that expresse=
s
>the fact that there is "no" value.
>> <skip>
>> NULLs are very convenient but they also introduce some problems:
>> <skip>
>> 1. They do not fit nicely into a statical type system.
>> 2. They require (runtime) checks whereever they can appear in order to
>ensure that no invalid operations are performed on them.
>>
>> Any ideas or comments?
>>
>
>in modern functional languages there is a powerful mechanism called algebr=
aic
>datatypes. they can be used to express many kinds of datastructures. one
>common datatype
>is an optional datatype that can include a value or then not. for example =
in
>Haskell:
>
>-- defines algebraic datatype with two constructors, Nothing that includes=
 no
>value and Just that
>-- includes a value of type a
>data Maybe a =3D Nothing | Just a
>
>-- then you can define functions for both cases using pattern matching
>maybe_square :: Maybe Int -> Maybe Int
>maybe_square Nothing =3D Nothing
>maybe_square (Just a) =3D Just (a*a)



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 w=
hen 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 co=
mpiler know that? Capitalization? If so, why is "maybe_square" lowercase?

"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)

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

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 an=
y type (say, a string)?

Sorry for the delayed answer; I accidentally tried to post this answer firs=
t to your mail account, but it bounced.
If you post follow-ups to the Ultra mailing list, this would allow others t=
o participate.
--

Regards,

Hans-Dieter Dreier
(Hans-Dieter.Dreier@materna.de)=

--JeU20OJgAVFd1hlNPkgAP31rchCe0z08
Content-type: text/plain; charset="ISO-8859-1"
Content-transfer-encoding: quoted-printable

IDENTIFIKATIONSANGABEN:
a24520a.txt IA5 DX-MAIL X.400 User Agent=

--JeU20OJgAVFd1hlNPkgAP31rchCe0z08--