How can I learn to think like a functional programmer?

Answer on @Quora by @m00nlight223 to How can I learn to think like a functional programmer?  To me,  one key experience to change from imperative style to functional style is to abstract the operation to handle the data-structure, and use high-order function like map or fold/reduce to handle the data-structure like the following example   1(defn add-to-trie [trie x] 2  (assoc-in trie x (merge (get-in trie x) {:val x :terminal true}))) 3  4(defn build-trie [coll trie] 5  "Build a trie over the values in the specified seq coll." 6  (reduce add-to-trie trie coll))    The above Clojure code use an collection to store some words and use reduce  to build a trie. This is perfectly functional style without state change of variable.   You may also read some good books on functional programming(I highl recommend SICP which also has free edition online and teaching videos). Or you can do some challenge programming in one functional language, for this 4clojure – Welcome! is a perfect place for clojure programmer, it require you to implement some programming tasks(such as group-by  in the core-language of Clojure), after you finish more and more of the exercise, I think you will be better and better in programming in thinking in a functional style.

Answer by Yushi Wang:

When I first learn functional programming, it seems weird to program without variables and the operation to update variables. But in fact, 90% of the time, you can use functional style to programming without the change of state, and at most time, it is much better in the functional style.

But when you program in functional language more and more, it will show that it is more nature to program in this style(with high-order functions like map, filter, foldl, foldr etc.).

To me,  one key experience to change from imperative style to functional style is to abstract the operation to handle the data-structure, and use high-order function like map or fold/reduce to handle the data-structure like the following example

(defn add-to-trie [trie x]
  (assoc-in trie x (merge (get-in trie x) {:val x :terminal true})))

(defn build-trie [coll trie]
  "Build a trie over the values in the specified seq coll."
  (reduce add-to-trie trie coll))

The above Clojure code use an collection to store some words and use

reduce

to build a trie. This is perfectly functional style without state change of variable.

You may also read some good books on functional programming(I highl recommend SICP which also has free edition online and teaching videos). Or you can do some challenge programming in one functional language, for this 4clojure – Welcome! is a perfect place for clojure programmer, it require you to implement some programming tasks(such as

group-by

in the core-language of Clojure), after you finish more and more of the exercise, I think you will be better and better in programming in thinking in a functional style.

How can I learn to think like a functional programmer?

Advertisements

Leave a comment

Filed under Life

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s