I had resisted advertisements on this blog for a long, long while. It's not that
I have anything in particular against ads. I think it's reasonable for a blogger
to add advertising to offset the cost (mostly time) of maintaining a solid
blog. I just hate it when nice, clean-looking blogs start to look like this:

I suppose that I share a lot of the same sentiments on the subject as Jeff Atwood
over at Coding Horror.

About a month ago, I added Google AdSense (at the suggestion of my good friend
Keith Elder). I've tried to keep the ads
relatively low key in order to keep the clutter down. That doesn't result in as
many clicks as it might if I threw them in your faces, but I'm OK with that. I like
to keep my layout clean for those who do come in via the web (and not just the
feed).

The truth is, I don't trust Google AdSense all that much. Context-sensitive ads
are great, but they aren't always accurate. For example, I've mentioned the word "Haskell"
several times in reference to the pure functional programming language,
Haskell. However, the very use of this relatively
uncommon word has triggered Google Ads for the
Haskell Indian Nations University.
Sigh. It's really hard to get behind advertised products when you're not 100%
certain that they'll be relevant to your content.

Recently though, I found a product that I can whole-heartedly recommend. It's a
product that falls directly into my demographic of humor-loving,
technology-lusting geeks: RiffTrax.

What's RiffTrax you ask? Well, do you remember the TV show
Mystery
Science Theater 30001? That's right. The one with the guy and the robots and
the making fun of old B-movies. Well, RiffTrax is that without robots and with
blockbusters instead of B-movies. It still has the guy though. In fact, it's the
same guy from MST3K.

In essence, RiffTrax are feature-length commentaries in MP3 format that you
can purchase, download and sync to your DVDs. Some RiffTrax feature stars and
writers from MST3K, and some even have celebrities joining in
on the fun. For example, my current favorite is a riff on
Jurassic Park that
features none other than Weird Al Yankovic.

Pants-wettingly hilarious. Seriously.

Below is a small sampling of the films that have received the RiffTrax
treatment. There are lots of others. Some have
Jar Jar Binks. Some
have Keanu Reeves. All
will make you howl with laughter.

Welcome to the eighth article in my series about why I look upon the
F# language with
the hormone-driven lust of a 16-year old boy. ([ed.] Dustin's trophy wife has indicated
that the previous metaphor might be a little too vivid.)

If you're just joining us, below is the path that has brought us to this
point.

Today, we're taking a high-level look at F# option types. Option
types are a simple example of a discriminated (or tagged)
union1, although understanding that isn't necessary in order to use them. Simply put, an option type wraps
a value with information indicating whether or not the value exists. For C# or VB programmers, it
may be convenient to think of option types
as a mutant cross between
.NET 2.0 nullable types and the
null object design
pattern.

There are two constructors that instantiate option types. First,
there's the Some
constructor, which takes a value to be wrapped.

> let someValue = Some(42);;

val someValue : int option

And then, there's the None constructor,
which doesn't take anything.

> let noValue = None;;

val noValue : 'a option

NeRd Note

Notice that, in the above
code, F# infers the type of noValue as the generic, 'a option, rather than int option.
That's because, unlike the declaration of someValue,
no
information indicates an int. If you really want to declare a
None value
as type int option, you'd declare it like so:

> let noValue : int option = None;;

val noValue : int option

One of the properties of option types that makes them so compelling is the
ability to pattern match over them.

Now, we can call our isFortyTwo
function to show that the pattern matching works as expected.

> isFortyTwo someValue;;

val it : bool = true

> isFortyTwo noValue;;

val it : bool = false

> isFortyTwo (Some(41));;

val it : bool = false

This is all well and good, but we need a practical example to sink our teeth
into. Let's use the .NET Framework
Stream.ReadByte function as a guinea pig. ([ed.] Dustin is not
implying that you should sink your teeth into guinea pigs. That's disgusting.
Shame on you.)

Stream.ReadByte has a pretty bad code smell. First of all, it returns
an int instead of a byte. Initially, that should seem strange since the method
specifically states that it's a byte generator.
ReadByte
returns -1 when
the current position is at the end of the stream. Because -1 is not expressible
as an unsigned byte, ReadByte returns an int. Of course, that's the second problem:
extra non-obvious
information is encoded into the result value of this function. However, unless you read the documentation, there's no way of knowing
that.

By employing an option type, we can clarify the function and be a bit more honest about
its result.

I don't know about you, but around my house, computer books have a habit of multiplying like rabbits.
Sometimes it seems as if you can't put up your feet without resting them on a
pile of old programming books. There are several reasons why these books proliferate
so:

I like my shelves to reflect an intelligence that I don't actually
possess.

I feel the need to own reference books that I never need to
reference.

I purchase books on the latest and greatest technology before I realize
that I'm not actually interested in said technology.

When I become interested in a topic, I tend to purchase every
book ever written about it—even if a new book duplicates information I
already have.

I buy classics that I have the best intentions of reading... but never do.

I acquire books for a specific project at work, and the project ends.

Because my shelves are bursting at the seams (and the Wife Acceptance Factor
for them has become quite low), it's time for an early Spring
cleaning. If you're interested in some reasonably-priced programming tomes,
previously owned by a lesser-known blogger, feel
free to browse my
Amazon storefront.

(Quiz: How many of the books in above picture do you own?)

(Clarification: The books pictured above are not for sale. Those are keepers!)

Pattern matching syntax is simple and clear. It should be readable by any programmer worth
their salt. In fact, the above
match .. with
block is completely equivalent to the following C#
switch statement:

That's pretty unimpressive. I mean, if pattern matching were identical to
standard switch statements, there really would be nothing exciting about them. Fortunately,
there are some enormous differences that demote switch
statements to a very distant cousin.

The first difference is subtle but profound: pattern matches return values. A
pattern match is very much like a function that takes an argument and returns a
value. Consider the following rewrite of our F# fib
function:

Switch statements don't return values, so we can't assign a switch
statement to a variable. Instead, we must use mutable state and pepper the cases
with break statements.
In essence, a pattern match is like a function while a switch statement is like
a big
GOTO.

In addition, pattern matching supports a wealth of features that truly set
it apart from standard imperative switch statements.

Now that's a bit more interesting! In C# or Visual Basic, we would have to
introduce an if-statement at the beginning of the function to test for an
invalid argument. In F#, the guard is inserted directly as a pattern rule.

Another indispensible feature of F# pattern matching is the ability to bind
values to names.

So far, we've used the match .. with syntax
to define pattern matches. This time, we'll use an alternative syntax that,
although it is not required, easily demonstrates how values can be bound to names within pattern rules.

The alternative syntax can be used in the case where a function is defined
with one argument and simply returns the result of a pattern match on that
argument. In this syntax, the argument is not specified, and the keyword function is
inserted.
The match .. with
statement needs to reference the argument name, but because the argument is
unspecified, it has no name. Consequently, the match .. with
statement must be removed, leaving us with a function that is defined entirely in terms of
pattern matching rules. Because the argument is unnamed, values must be bound to
names within the pattern rules.

In the above code, we bind the name x
in each pattern to make up for the fact
that the argument is unspecified. In addition, the rules for 0 and 1 and have been
combined using an "or" (or "union") pattern. Note that there
are two different ways to bind a value to a name within a pattern rule. First, a
name can simply be explicitly specified, substituted within the pattern. The other way is to use the as keyword.
Both ways are demonstrated above.

The last feature of pattern matching that we'll look at is its capability to
decompose type structures.

Recently,
we saw that F# would automatically convert the result of
Dictionary<TKey,
TValue>.TryGetValue to a
tuple if a
variable isn't specified for the out parameter. In a
comment to that
article, Derek Slager presented a
helper function that returns a default value if TryGetValue
returns false. This helper function is an excellent practical example of a
pattern match that decomposes a tuple value.

In addition to the tuple decomposition, the first rule elegantly binds the second
part of the tuple to the name value.
Sweet!

Because pattern matching is intrinsic to F# programming, we'll see more of it in upcoming articles. As features
supporting pattern matching are introduced in this series, we'll build on
the basics presented here.

Because this book provides source code in Standard ML, it's a fantastic
resource for learning F#. One bit of warning: this book does not teach classic
data structures. While structures such as binomial heaps and red-black trees
are presented, it is assumed that the reader already knows and understands
them.

That's right. This isn't a programming book. It's a diversion. Reading programming books all of the
time is hazardous to your happiness and well-being. Take the time to enjoy something else. You'll thank me later.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent
my employer's view in any way.