February 2010 (1)
September 2009 (1)
May 2009 (1)
April 2009 (1)
March 2009 (4)
January 2009 (3)
November 2008 (2)
October 2008 (2)
September 2008 (1)
August 2008 (5)
July 2008 (3)
June 2008 (1)
May 2008 (5)
April 2008 (8)
March 2008 (3)
February 2008 (1)
January 2008 (2)
December 2007 (2)
November 2007 (4)
October 2007 (17)
September 2007 (9)
Becoming lazy, strict and functional again
Friday, September 11 2009
Functional programming is the new hotness: functions as first class citizens, closures, and map/filter/reduce & co seem to be infiltrating the imperative languages rapidly.
Of course FP has been the new hotness for a long time. For example, I remember being taught Gofer, a forerunner of today’s Haskell, in the mid-90s by the long-suffering Steve Reeves. My brain had already been corrupted by BASIC and Pascal, so I didn’t really appreciate what was so great about it. I do clearly remember memorising the mantras that side effects are evil, and that real programming can be done effectively with a pencil and paper.
Something must have stuck because I have over time adopted a style where I avoid side-effects, declare as few variables as possible, avoid re-assigning to variables, decompose big procedures into tiny little ones, and use vectors and dictionaries a lot – all habits supported by my favourite scripting languages. Perl, Python and Tcl all support an unsophisticated functional style and once you learn them well you yearn for features like map and filter in more imperative languages.
In the last couple of years I’ve become vaguely interested in Haskell, and bought some books, but never really did anything with it. But in the last month or so I have decided to really try and come to grips with it. Partly I think because I’m writing a lot of Perl, and its untyped slipperiness is troubling me; partly because I’m writing a lot of Python, and its Haskelly list comprehensions are lovely; and partly because we have a lump of useful Haskell code at work which I just don’t understand AT ALL, and I want to.
So yesterday I finished my first successful functional language program in about 15 years. It does something useful for me: it works out the optimal progression of weights in a weight training cycle. I have enjoyed the mental exercise considerably as I try to do things the Haskell way rather than just translating an imperative solution into the nearest equivalent. I doubt that I’ve succeeded in being Haskelly, but these things don’t happen overnight.
The only thing that’s really annoyed me so far is discovering that the primitive operation to join a list of strings, which every other language I know calls “join”, is known in Haskell as “intercalate”. (OK, intercalate is a more general function on lists of lists, but still).
import System.Environment (getArgs, getProgName)
import Data.List
main = do
args <- getArgs
progName <- getProgName
case args of
[goal, workouts, weights] -> putStrLn (message goal workouts weights)
_ -> putStrLn (“Usage: ” ++ progName ++ ” goal workout weights”)
message goal workouts weights = show (workoutCycle g wo wg)
where g = read goal :: Float
wo = read workouts :: Float
wg = map read (words weights)::[Float]
– subsequences is not in Ubuntu Jaunty’s version of GHC, unfortunately.
subsequences xs = [] : subsequences’ xs
where subsequences’ [] = []
subsequences’ (x:xs) = [x] : concatMap (\ys -> [ys, x:ys])
(subsequences’ xs)
steps start end workouts = let step = ((end-start)/(workouts-1))
in [start, start + step .. end]
threshold workouts goal increment = start >= 0.60 && start <= 0.8
where start = (goal – increment * workouts) / goal
bestIncrement workouts goal increments = case find (threshold workouts goal) increments of
Just increment -> increment
Nothing -> head increments
bestStart workouts goal increments = case find (threshold workouts goal) increments of
Just increment -> goal – (workouts * increment)
Nothing -> goal * 0.7
roundToIncrement d n = let raw = n / d
low = fromInteger(floor (raw) ) * d
high = low + d
mid = d / 2 + low
in if n <= mid
then low
else high
workoutCycle goal workouts weights = map (roundToIncrement increment) (steps start goal workouts)
where increment = bestIncrement workouts goal increments
start = bestStart workouts goal increments
increments = tail (map (foldl (+) 0) (subsequences weights ))
Rendered at 2010-03-14 07:12:07