I’m learning functional programming and writing things down as I go. Some of my understanding may be incomplete or wrong, and I expect to refine it in the fullness of time.
Today I worked on CIS194 Homework 2.
A few interesting constructs from this bit of code below.
I found myself using the $
operator for function composition quite a bit:
scrabbleValueTemplate :: STemplate -> String -> Int
scrabbleValueTemplate stemplate word =
(product $ map multiplier stemplate) * (sum $ map tplValue (zip stemplate word))
It’s cleaner than parentheses.
Binary operators can be partially applied by providing just the first or just the second argument, for example:
filter (/= '?')
The where
clause is also very good for readability:
bestWords :: [String] -> [String]
bestWords words = filter (\word -> scrabbleValueWord word == maxValue) words
where maxValue = maximum $ map scrabbleValueWord words
In most cases it’s probably cleaner to create a named function in the where
clause instead of a lambda. I also find the where
clause more convenient than let... in
.
I also started Erik Meijer’s EdX FP101 course. So far it looks too basic for me but I’ll stick with it for the exercises for now.
That’s it for today!