Ask HN: Thinking in Functional programming
I have been finding it very difficult to implement simple problems. I have become familiar with the syntax (got over the = operator typing every other place from my finger memory). While I am comfortable, I feel like I know few keywords without knowing where to use, akin to learning a new language - saying hi, bye, but unable to form a sentence. That is my ponder stop..almost pushes me into procrastination mode
How do I Think in Functional. I catch myself thinking oops(i think of classes,methods..)and i correct myself and end up with something procedural like.
With ref to Uncle Bob's link below , how do I think "inside out"?
II) instead of having another question, I thought of asking it as a sub question from the same link below - TDD in clojure. for one, I don't see a mock object ever created in clojure...please correct me.
I was wondering how to do "TDD" in LISPy languages.
http://blog.objectmentor.com/articles/2009/07/19/uncle-bob-jsps-learning-clojure
This way of programming seems incompatible with TDD and I would recommend practicing it in order to start thinking functional.
Paul Graham talks about it here:
http://www.paulgraham.com/progbot.html
I don't program in Lisp or any other functional language, but I use this style a lot programming in Python and code ends up being pretty functional.
Reading SICP is like taking a freezing shower; it's a bit of a shock at first, but afterward everything is crisp and clear. Colors are brighter and love is lovelier after you read SICP.
Ok, maybe it's not gold-and-bunnies good, but it's pretty good, and should help remove your cognitive stumbling block.
If you want to learn how to deal with the awkward situations that arise from using a purely functional language, I'd recommend hanging out with the Haskell guys.
SICP is legendary, but for a different reason. Lisp is useful for its simplicity and elegance. Modeling an imperative procedure in the timeless way that Clojure calls for is not necessarily pretty.
That said, SICP will still teach you to think in terms of HOFs and functions as abstractions, so it'll definitely help out. And it's free online, so the only investment is time.
There's also an F#/C# book scheduled to come out later this year that looks like it might be good for learning the functional thought process: http://www.amazon.com/Functional-Programming-Real-World-Exam...
I found this blog post from Stuart Halloway to be informative as well: http://blog.thinkrelevance.com/2008/9/25/pcl-clojure-chapter...
Functional programming is very much about decoupling "pure functional" computation and side-effects. Using pattern matching you can quite easily write a little machine that performs side-effects given different values. These things are simple and about as useful to unit-test as accessor methods are in oo code. Static typing checks do fine. Also, if it is broken, it tends to be severly broken and not lead to undetected or hard-to-find bugs but simply not work at all.
The interesting code to test is that which is given a bunch of parameters and returns a value that would instruct the side-effectful machine about what to do. This function can be tested with lots of inputs, and you check that it gives the correct instructions (without executing them).
This style of programming exists in OO as well.
Another reason to use Mocks is that they are queried for values that one need control over to rig the unit test. I.e. just a disguised parameters. FP simply doesnt do that.
1. Read lots of clojure code. There is clojure-contrib which has lots of little libraries, read through them and see how things are done.
2. I will second the SICP suggestion, atleast the first few chapters. Bottom up programming, building layers of abstraction, is a natural way of doing things in the lisp world.
3. The REPL is your friend. Having a really good working environment is crucial to initial learning as well as later development. I would strongly recommend taking the time to set up one of the clojure working environments(Slime/VimClojure/Enclojure) as a first thing, if you have not already done so.
4. It takes a while, but it gets easier. Keep at it.
I spent substantial amount of time setting up every environment mentioned and ended up with Clojure-Box. I have not tried emacs before; ideally, i should be trying a new language in a known setup, but I prefer the stability of clojure box, so i am taking my baby steps in emacs too.
make everything you can immutable. find a problem where you can build most of it without changing 'the state of the world'. function takes data, spits out answer which is feed into the next.
refactor commonalities from part 1 by passing around functions, closures, etc etc. all your good functional fun toys.
add your mutable core around the outside.
SICP is one of the single most valuable and important texts in CS. Little Schemer is structured as a real training regimen to get your mental muscles ready to handle that theory.
http://java.ociweb.com/mark/clojure/article.html#Testing