I have read a decent amount of material on functional programming now, and the biggest problem I have is getting into the right mindset. I think in OO style and can't quite think of what to do when I need to keep state for longer than a single function call. In OO programming, this would be accomplished with aptly named objects that maintain a set of data over time. I have great difficulty translating this to a functional style. What do you do if you need to work on one set of data for a long time and intermittently collect the results thus far, for instance? (trivial example: generating permutations of characters for a brute-force password cracker)
I can definitely see the benefit of a functional approach. For you FP masters, are there any good books explaining how you create a decent system (+5000 lines, for instance) while maintaining good source code structure and readability? I really want to learn this.
I'll provide a solution for your 'trivial example' in something like Haskell. I hope you see a pattern.
Suppose you have a somehow defined the types State, Permutation and Hash - and the following functions in terms of them:
-- A function to extract the current permutation
extract :: State -> Permutation
-- A function to step to the next state
next :: State -> State
-- Of course you need a way to see if a particular permutation
-- does actually match the password
try :: Hash -> Permutation -> Bool
-- And an initial State - perhaps corresponding to the identity
-- permutation - to start things
init :: State
Now you can crack passwords:
crack :: Hash -> [Permutation]
crack hash = filter (try hash) perms
where states = iterate next init
perms = map extract states
iterate produces the new states - the results so far are collected with map and filter. Because functional data structures are usually ephemeral you can just predict that intermediate values are actually final.
I hope this was helpful. Please do not hesitate to ask any questions.
Universities should offer something like this when trying to teach Haskell or Scheme alongside Java - especially the explanation of why you'd do it is better than I was taught.
+1 - My school (Ga Tech) used Scheme as its intro language when I was there and I could've benefited from being sold on it a little more (too bad there was no Hacker News then). I wasn't alone in being annoyed that I had to mess around with this weird little language before getting into Java and C. I did end up enjoying it though.
Yep! I totally agree. The key takeaway is to shed some light on the motivation behind these concepts which hones your sense of judgement as to what to use when..
I've already convinced myself that FP is worth my time, but this article is great to help along your friendly neighborhood "Java programmer." I just wish I had read this 5 years ago!
I can definitely see the benefit of a functional approach. For you FP masters, are there any good books explaining how you create a decent system (+5000 lines, for instance) while maintaining good source code structure and readability? I really want to learn this.