MosesA
MosesA

Reputation: 965

MapReduce in Haskell

I'm trying to implement the MapReduce function. I have to do it without any parallelism, I have this so far:

import Data.Map
import Data.Char

map_ list = toList $ fromListWith (+) [(x, 1) | x <- list]

main_ = map_ $ getWords "test.txt" --getting error here

prettyprint [(x, y):xs] =  x ++ " : " ++ y -- trying to print out list

-- Loads words from a text file into a list.
getWords :: FilePath -> IO [String]
getWords path = do contents <- readFile path
               return ([map toLower x | x <- words contents]) --- Ambiguous occurrence `map'

The main task is to use MapReduce to get a list of words from a file and the frequency of every new word.

I don't understand how the reduce works in this case. I just know that I am mapping a list to a function that calculates the frequency of the words. How can it be reduced?

Upvotes: 1

Views: 1485

Answers (1)

ErikR
ErikR

Reputation: 52039

Just addressing your compilation problems...

Both the Prelude and Data.Map define the function map.

A good way to fix the ambiguity is to qualify the import of Data.Map like this:

import qualified Data.Map as M

and then use M.map to refer to the function in Data.Map and plain map to refer to the Prelude version.

The problem in main_ is that getWords is an IO [String], so you need to extract the list with the <- operator before you can use it:

main_ = do ws <- getWords
           -- do something with ws
           print $ map_ ws

Upvotes: 4

Related Questions