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.
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.