Search code examples
stringlisthaskellhigher-order-functionsmap-function

'Map' higher order Haskell function


I have a list, for example:

["Hello", "Goodbye"]

and I want to use map on the list;

I've successfully used map before:

f = ("example" ++)

so then:

map f ["Hello", "Goodbye"]

Would make the list:

["exampleHello", "exampleGoodbye"]

but how can I use the list items in the function f?

For example, if I wanted to repeat the list element, so

["Hello", "Goodbye"]

would become

["HelloHello", "GoodbyeGoodbye"]

How can I do that with map and a function f (and ++)?


Solution

  • Doing

    map (\x -> x++x) ["Hello", "Goodbye"]
    

    results in

    ["HelloHello","GoodbyeGoodbye"]
    

    So f could be defined as f x = (x++x).