## Seven Languages in Seven Weeks Part 13: Haskell Day 3

*by Justin Michalicek on July 25, 2011, 5:04 a.m. UTC*

Well, this is the end of the book. Day 3 of Haskell. So far most stuff hasn't been too difficult with Haskell, but I do feel like I'm fighting with the syntax to express the logic more often than I am happy with.

The first exercise of day 3 is to create a hash table with has other hash tables as elements and a function that uses the Maybe monad to find elements in the hash table. Haskell already has Data.HashTable which does this, so I am assuming I'm not supposed to just use that, because that's a pretty pointless exercise. The closest I could think of is lists of tuples where each tuple is a key and a value. This isn't a true hash table, but on the surface it appears to behave similarly. The only thing I don't like about this implementation is not having a way to have values of different types. Lists have to be all the same data type and a tuple ("string", "string") is different than a tuple of ("string", Integer) and so they cannot go in the same list together. I used the Maybe monad, which seemed to be the key point of this exercise though, so I'm going to call it a win.

module Main where getValue :: String -> [(String,a)] -> Maybe a getValue key [] = Nothing -- h should be a tuple. t the rest of the list of tuples -- compare the first value of h to the key getValue key (h:t) = if (fst h) == key then Just (snd h) else getValue key t *Main Data.Maybe> let x = [("cow","moo"),("cat","meow")] *Main Data.Maybe> let z = [("stuff",x)] -- Hash of hashes *Main Data.Maybe> (getValue "stuff" z) Just [("cow","moo"),("cat","meow")] *Main Data.Maybe> (getValue "blargh" z) Nothing *Main Data.Maybe> let a = fromJust (getValue "stuff" z) *Main Data.Maybe> getValue "cat" a *Main Data.Maybe> getValue "cow" a Just "moo" *Main Data.Maybe> getValue "asdf" a Nothing

I also tried to at least have my own HashTable data type, but I couldn't get the constructor figured out. I tried creating a new data type HashTable but that needed to derive List (or something like that), but it doesn't appear that you CAN derive list. I also tried the below solution, but I can't figure out how to create data constructor with a variable type while using a nullary type constructor. If I ditch the nullary type constructor, then I can't have a nullary data constructor even though I've seen several examples of doing so (which means there's some minor yet important detail that I haven't caught onto in the examples).

module Main where data Entry = NoEntry | Entry String String deriving(Show) type HashTable = [Entry] new :: HashTable new = [] add :: HashTable -> String -> a -> HashTable add ht key value = [Entry key value]:ht --getValue would go here if I could make the above work Prelude Data.Maybe> :load hash.hs [1 of 1] Compiling Main ( hash.hs, interpreted ) hash.hs:9:23: Couldn't match expected type `Entry' against inferred type `[a]' In the first argument of `(:)', namely `[Entry key value]' In the expression: [Entry key value] : ht In the definition of `add': add ht key value = [Entry key value] : ht

The next two exercises I just have no desire to do given the trouble I've had with this. They were to create a Maze and Node data type to store a maze and then to solve the maze using a List Monad. I couldn't even find anything that clearly defined to me what a List Monad is and I only barely comprehend exactly what a Monad is in general at this point.

There was also another exercise where you create a monad in a different language which does not already contain them. No thanks.

I wish my last day of exercises had been more successful. I really enjoyed this book overall and would have liked to end it with success rather than admitting defeat after several hours of frustration. Unfortunately this last bit hit the point where I'm not having fun and the whole point of this is to have fun, so why keep at it?

Time to play some Fallout: New Vegas and think about my next project. I'll post a final entry about this book, what I liked, and my general thoughts about some of the languages in the next couple of days.