Browse thread
[Caml-list] Some/None
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Samuel Lacas <Samuel.Lacas@t...> |
| Subject: | Re: [Caml-list] Some/None |
Oliver Bandel a écrit 1.8K le Wed, Apr 24, 2002 at 04:23:34PM +0200:
#
# OK, In had typed it into the toplevel and as
# expected, it does not give back a pure type
# (like int's or strings).
# It gives back
# None
# or
# Some 8
# or
# Some "hello"
Naturally. The type ('int option) is not the same as the type "int",
because you have the "None" value.
# Is this really a common way of programming?
I suppose so.
# If I work on a list of integers or a list of
# strings, does it really have advantages to
# use such a type?
Yes: it allows you to manage an error case for a function supposed to
return a value of type a, but which may sometimes return nothing. It's
lighter than exceptions (I think) and much more safe than having a
"null" pointer-style ugly coding (or other twisted way such as saying
"return -1" if not found).
# Pattern match itself is not functional.
Don't know, but without much thinking, no imperative language having a
similar mechanism built-in comes to my mind. Don't trust me, though :)
# How can such things be expressed in a functional
# way? (Maybe I have to try it in Haskell, it's
# constraints to be functional are much stronger;
# when using Ocaml it seems to be that very often
# the imperative style creeps in - even unconsciously).
Haskell preludes contains:
-- Maybe type
data Maybe a = Nothing | Just a deriving (Eq, Ord, Read, Show)
which serves exactly the same. Replace Maybe by option, Nothing by
None, and Just by Some. No "unconscious" imperative creeping here :)
sL
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners