In Haskell, what does a map function mean when you only pass a list?

The Haskell project I am provided with for debugging has instances in the code where it is map

used with only one parameter - a list.

for example

printReports :: [Report] -> IO ()
printReports = putStrLn . unlines . map show

      

and

printRuns' :: [Run] -> IO ()
printRuns' = putStrLn . unlines . map showRecipes'

      

What does map

/ does mean / does in this context?

+3


source to share


4 answers


Card type:

map :: (a -> b) -> [a] -> [b]

      

So, you need to provide a function of a

to b

and a list of types a

.



In your examples the function has already been set ( show

and showRecipes'

), so you need only provide printReports

and printRuns'

list.

What happened is called a partially applied function , see here http://www.haskell.org/haskellwiki/Partial_application

+10


source


As others have said, map show

is a partially usable feature and reading on that is a good idea. But the code you provided is also an example of point-free programming style, and it may be easier for you to understand it as such. The function printReports

can also be written as

printReports xs = (putStrLn . unlines . map show) xs

      

or, equivalently,

printReports xs = putStrLn . unlines . map show $ xs

      



Generally speaking, all of the following are equivalent:

myFunction x y z = someExpression x y z
myFunction x y = someExpression x y
myFunction x = someExpression x
myFunction = someExpression

      

This is a slight oversimplification, but it will help you.

+10


source


Typically if you have something like this ...

foo = bar . foobar . blafasel

      

... you can mentally substitute dots on $

and add a variable on both sides

foo x = bar $ foobar $ blafasel x

      

Why this works has already been explained in other answers, but this trick helps me to read the silent style without thinking.

+3


source


In this context, the map applies the show function to each report in the list. The result should look like this: [show report0, show report1, ..., show reportn]

Say otherwise, you pass a list of reports and map functions map for each type of report element in the show function list.

The construction of your function follows the point style .

+2


source







All Articles