Algebraic data types and some new control structures

Second lecture of CIS 194 introduced algebraic data types and some new control structures to deal with them.While this was familiar to me from the last time, I made sure to do each and every exercise, just in case as I haven’t been programming in Haskell for much at all.

Algebraic data types are really cool feature for defining new types. At the simples, they can be used for enumerations:

data Color = Red
           | Green
           | Blue

But they can also be used to define types that store values:

data Coordinates = Polar Double Double
                 | Rectangular Double Double

This defines Coordinates type that can be either Polar or Rectangular and store two double values.

Algebraic data types can be taken apart with pattern matching:

distance :: Coordinates -> Double
distance (Polar angle r)   = r
distance (Rectangular x y) = sqrt(x * x + y * y)

So now we have a function to calculate distance from origo to given point with two different coordinate system. Bonus points for keeping coordinate systems separate, there’s no chance one would accidentally mix polar and rectangular coordinates.

Another (very simple) example of using pattern matching for counting (this one gives funny answer with values less than 1):

count :: Integer -> String
count 1 = "One"
count 2 = "Two"
count _ = "Many"

Case is another way of doing pattern matching:

count n = case n of
             1 -> "One"
             2 -> "Two"
             otherwise -> "Many"

These functions work in same way, giving same results. The main difference is that case can be used in middle of expression, while pattern matching can be only used when defining functions:

report :: Integer -> String
report n = "I got " ++ case n of
                          1 -> "One"
                          2 -> "Two"
                          otherwise -> "Many"

Hardest part for me is to remeber all these new structures, their syntax and what kind of options and differences they have. I think this should get better when I’m more familiar with the language and have spend more time writing code with it.

The main excerise in the lecture consisted on several parts that were tied together at the final step. The goal was to use everything taught so far and analyze a big jumbled log to discover why things went haywire. I really liked how the excercise was structured. All parts were small enough so I didn’t get stuck with any of them for too long time and I could test steps separately, but they were also challenging enough.

I’m really starting to remember why I was so enamoured with Haskell in the first place. While I still struggle with basic things, I can see how elegant and powerful it can be (I have to admit, sometimes I do think why in earth things are so hard and complicated with it).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s