[Haskell-beginners] Doubts about functional programming paradigm

Daniel Bergey bergey at alum.mit.edu
Fri Dec 11 16:22:24 UTC 2015


On 2015-12-11 at 10:07, Abhishek Kumar <abhishekkmr18 at gmail.com> wrote:
> I am a beginner in haskell.I have heard a lot about haskell being great for
> parallel programming and concurrency but couldn't understand why?Aren't
> iterative algorithms like MapReduce more suitable to run parallely?Also how
> immutable data structures add to speed?I'm having trouble understanding
> very philosophy of functional programming, how do we gain by writing
> everything as functions and pure code(without side effects)?
> Any links or references will be a great help.

Functional languages make it easy to decompose problems in the way that
MapReduce frameworks require.  A few examples (fold is another name for
reduce):

sum :: [Double] -> Double
sum xs = foldr (+) 0 xs

sumSquares :: [Double] -> Double
sumSquares xs = foldr (+) 0 (map (**2) xs)

-- foldMap combines the map & fold steps
-- The Monoid instance for String specifies how to combine 2 Strings
-- Unlike numbers, there's only one consistent option
unlines :: [Text] -> Text
unlines xs = foldMap (`snoc` '\n') xs

We'd need a few changes[1] to make this parallel and distribute across many
computers, but expressing the part that changes for each new MapReduce
task should stay easy.

Immutable data by default helps with concurrency.  Speed may or may not be
the goal.  We want to be able to distribute tasks (eg, function calls)
across processor cores, and run them in different order, without
introducing race conditions.

Simon Marlow's book is great at explaining parallel & concurrent
concepts, and the particular tools for applying them in Haskell:
http://chimera.labs.oreilly.com/books/1230000000929

bergey

Footnotes: 
[1]  OK, many changes.



More information about the Beginners mailing list