tag:blogger.com,1999:blog-27176507594607073142024-03-13T11:15:04.304-07:00CodebightAnonymoushttp://www.blogger.com/profile/05373298317901226698noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-2717650759460707314.post-21271011836539280322014-02-25T14:47:00.002-08:002014-02-25T14:49:04.039-08:00Mathematical Pattern Matching <span style="font-family: Arial,Helvetica,sans-serif;">When first starting function programming from a procedural/object
oriented background, people sometimes have trouble with a certain
feature that has no representation in those language backgrounds. The
concept of types in Haskell, though similar to other functional
languages, is remarkably different from most procedural/OO
languages.</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><br /></span>
<span style="font-family: Arial,Helvetica,sans-serif;">Haskell is inherently based upon manipulation and
usage of types. Really, a type is just a name that represents some
abstract concept. Although used by the compiler to ensure a program
runs correctly, a type is really an identifier for you. Let's begin
with the <a href="http://hackage.haskell.org/package/base-4.6.0.1/docs/Data-Maybe.html#t:Maybe">Maybe</a> type.</span><br />
<br />
<span style="font-family: courier new;">data Maybe a =
Just a<br />
Nothing</span><span style="font-family: arial;"> </span><br />
<br />
<span style="font-family: arial;">If we break this down, </span><span style="font-family: courier new;">Maybe
</span><span style="font-family: arial;">is a name (a type) that is associated with
a value (</span><span style="font-family: courier new;">a</span><span style="font-family: arial;">).
However, this value has the possibility of failure. Think of it like
setting an errno if it fails. In Haskell, the lowest level construct
is a function, and arguments are passed similarly to Lisp: to call a
function you put the function name and arguments are then listed
afterword separated by spaces. For instance, for a function called
add that takes two arguments,</span><br />
<br />
<span style="font-family: arial;"> </span><span style="font-family: courier new;">let
x = add 2 3</span><span style="font-family: arial;"><br /><br />where the </span><span style="font-family: courier new;">let
</span><span style="font-family: arial;">means declaring x with a value of whatever
add returns.</span><br />
<span style="font-family: arial;"><br />Let's go back to our </span><span style="font-family: courier new;">Maybe</span><span style="font-family: arial;">
type. Like I said before, </span><span style="font-family: courier new;">Maybe</span><span style="font-family: arial;">
is really just a label to represent some abstract concept; in this
case, the abstract concept is the possibility of a value. When I said
before that the lowest level Haskell construct is a function, I meant
it. In the case of </span><span style="font-family: courier new;">Maybe</span><span style="font-family: arial;">,
the two things after the equals sign are called constructors. Each
one is a function that will return a value that is </span><span style="font-family: arial;"><i><b>wrapped</b></i></span><span style="font-family: arial;">
in the </span><span style="font-family: courier new;">Maybe </span><span style="font-family: arial;">type.
The </span><span style="font-family: courier new;">Just </span><span style="font-family: arial;">function
takes one parameter and returns a value of the type </span><span style="font-family: courier new;">Maybe</span><span style="font-family: arial;">.
Let's see this in action:</span><br />
<br />
<span style="font-family: arial;"> </span><span style="font-family: courier new;">>
let x = Just 4<br />> x<br />Just 4<br />> :t x<br />Maybe Int</span><br />
<br />
<span style="font-family: arial;">As
we can see here, the </span><span style="font-family: courier new;">Just </span><span style="font-family: arial;">function
takes one parameter and returns a value that is wrapped in the Maybe
type. If this doesn't make any sense to you, that's OK. We'll iterate
on this soon.</span><br />
<span style="font-family: arial;"><br />Haskell has another construct to facilitate
retrieving a value from a type. This is called pattern matching, and
it looks like this:</span><span style="font-family: courier new;"> </span><br />
<br />
<span style="font-family: courier new;">> let x
= Just 4<br />> (Just 4) == x<br />True<br />> (Just 4) == (Just
4)<br />True<br /><br />> let (Just y) = Just 4<br />> y<br />4<br />>
let (Just z) = x<br />> z<br />4<br />> x<br />Just 4</span><span style="font-family: arial;"> </span><br />
<br />
<span style="font-family: arial;">This
is a very important feature of Haskell, and it is also slightly hard
to get your head around for the first time. Let's ditch Haskell for
now and stay in the world of mathematics (where Haskell is (almost)
completely derived from). Since we know that </span><span style="font-family: courier new;">Just
</span><span style="font-family: arial;">is a function, we can rewrite this as a
mathematical equation.</span><br />
<br />
<span style="font-family: arial;"> </span><span style="font-family: courier new;">Given x
= j(4)</span><br />
<span style="font-family: courier new;"><br />j(4) = x<br />j(4) =
j(4)<br />j(y) = j(4)<br />y = 4<br />j(z) = x<br />z = 4</span><span style="font-family: arial;"> </span><br />
<br />
<span style="font-family: arial;">As
you can see, one can simplify and determine that</span><span style="font-family: courier new;">
y </span><span style="font-family: arial;">and </span><span style="font-family: courier new;">z</span><span style="font-family: arial;">
are both equal to </span><span style="font-family: courier new;">4</span><span style="font-family: arial;">,
the originally </span><span style="font-family: arial;"><i><b>wrapped </b></i></span><span style="font-family: arial;">value.
This can also be referred to as type deconstruction. </span><span style="font-family: courier new;">Just
</span><span style="font-family: arial;">is really just a function that represents a
wrapper around some value and the conglomerate of </span><span style="font-family: courier new;">Just
</span><span style="font-family: arial;">and its value is part of the Maybe type. If
we look at pattern matching in Haskell from this mathematical
perspective, then pattern matching (type deconstruction) is really
just solving for the value of an unknown variable.</span><span style="font-family: courier new;"><br /></span><br />
<br />Anonymoushttp://www.blogger.com/profile/05373298317901226698noreply@blogger.com0