On to the final language featured in the book, Haskell. I first came across Haskell in a fantastic course in college that was all about different programming paradigms as well as the history and evolution of programming languages. In addition to learning about a whole bunch of different languages, programming exercises in the course were in Haskell. I remember being blown away by pattern matching (e.g in defining functions) and destructuring in Haskell, but unfortunately can't remember too much else. It was a really informative class for me as it opened my eyes to different ways programming could 'be', at the time i was pretty much jest learning to code and had only been exposed to C++, so my impression of all programming was that it was somewhat like C++. Taking this course and programming in Haskell was informative in broadening my perspective (though the class was a bit over my head at times—I don't even think i had taken data structures by the time i took this class and i remember a lecture on implementing a basic lisp interpreter in lisp that left me swimming for a bit); and leaving me with an appreciation of how different languages approach modelling computation. In many ways this book is like that class!
Anyway onto the text, day one was relatively straightforward (and a lot of my solutions built off of stuff remembered from prolog). Getting an environment setup and kicking the tires with expressions, function definitions and list processing. I don't have much to add to the code itself, which i will post below. It was interesting trying to solve the map coloring problem, who'da thunk list comprehensions would be so useful.
Another thing I noted is that I wrote very few type annotations, now this really was a small collection of small independent functions so that isn't saying much yet, but the type inference is nice, and Haskell is reputed to have quite a powerful type inference system. It will be interesting to see how this works out as I know the type system is a major aspect of Haskell.
module Main where | |
-- Q1. How many different ways can you write the function allEven? | |
-- allEven using built in higher order functions | |
ae1 :: [Integer] -> Bool | |
ae1 x = all even x | |
-- allEven with recursion | |
ae2 [] = True | |
ae2 (h:t) = if even h then ae2 t else False | |
-- Longer form of the above with guards and a let expression | |
ae3 list | |
| list == [] = True | |
| otherwise = | |
let | |
h = head list | |
t = tail list | |
in if even h then ae3 t else False | |
-- Q2. Write a function that takes a list and returns it in reverse | |
-- with guards | |
rev list | |
| list == [] = [] | |
| otherwise = | |
let | |
h = head list | |
t = tail list | |
revtail = rev t | |
in revtail ++ [h] | |
-- without guards | |
rev2 [] = [] | |
rev2 list = let | |
h = head list | |
t = tail list | |
revtail = rev t | |
in revtail ++ [h] | |
-- pattern match in the let expression | |
rev3 [] = [] | |
rev3 list = let | |
(h:t) = list | |
revtail = rev t | |
in revtail ++ [h] | |
-- pattern match sooner (in the pattern matcher :) | |
rev4 [] = [] | |
rev4 (h:t) = let | |
revtail = rev t | |
in revtail ++ [h] | |
--pattern match two liner (just a shorter version of above) | |
rev5 [] = [] | |
rev5 (h:t) = rev5(t) ++ [h] | |
-- Q3. Write a function that builds 2-tuples with all possible combinations of two of the | |
-- colors black, white, blue, yellow and red. Note you should only include one of (black, blue) | |
-- and (blue, black) | |
-- uh huh, you know what it is... [a list comprehension] | |
wiz_khalifa list = [ [fc, sc] | fc <- list, sc <- list, fc < sc ] | |
-- usage wiz_khalifa["black", "white", "blue", "yellow", "red"] | |
-- Q4. Write a list comprehension to procude a childhood multiplication table | |
timestable = [ (fn, sn , fn * sn) | fn <- [1..12], sn <- [1..12] ] | |
-- Q5 Solve the map coloring problem using Haskell (this problem is solved in Prolog in section 4.2) | |
-- http://en.wikipedia.org/wiki/Graph_coloring | |
-- http://en.wikipedia.org/wiki/Four_color_theorem | |
-- Color 5 states (Alabama, Mississippi, Georgia, Tennessee, Florida) with three colors (red, green ,blue) | |
-- so that no neighboring states have the same color | |
states = ["Alabama", "Mississippi", "Georgia", "Tennessee", "Florida"] | |
colors = ["red", "green", "blue"] | |
--, (state2, col2) | |
colorings = [[(state1, col1), (state2, col2), (state3, col3), (state4, col4), (state5, col5)] | | |
state1 <- states, col1 <- colors, | |
state2 <- states, col2 <- colors, | |
state3 <- states, col3 <- colors, | |
state4 <- states, col4 <- colors, | |
state5 <- states, col5 <- colors, | |
state1 == "Alabama", | |
state2 == "Mississippi", | |
state3 == "Georgia", | |
state4 == "Tennessee", | |
state5 == "Florida", | |
-- rules for alabama | |
col1 /= col2, | |
col1 /= col3, | |
col1 /= col4, | |
col1 /= col5, | |
-- rules for missisippi | |
col2 /= col5, | |
-- rules for georgia | |
col3 /= col4, | |
col3 /= col5 | |
-- rules for other two states are already covered | |
] |