Question

The type of map is:

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

So if I would like to map a function with multiple parameters onto an array, something like:

myObviouslyFakeFunction :: Int -> Char -> String -> Char -> Integer -> String
myObviouslyFakeFunction -pattern- = -Very complex transform-

and do something like:

map (myObviouslyFakeFunction 1 'a' "abc" 'b' 2) ["abc", "def", "ghi"]

How could I do this? Would a in the type signature represent the first parameter? A tuple with all of them? A list?

Was it helpful?

Solution

If you want to use myObviouslyFakeFunction like

map (myObviouslyFakeFunction 1 'a' "abc" 'b' 2) ["abc", "def", "ghi"]

then the type of it should be

myObviouslyFakeFunction :: Int -> Char -> String -> Char -> Integer -> String -> ResultType

Because map needs a one-parameter function, in this case with type String -> b (b is a type variable, could be any valid type), as its first argument, and if myObviouslyFakeFunction has the above type,

(myObviouslyFakeFunction 1 'a' "abc" 'b' 2)

will be a String -> ResultType function.

OTHER TIPS

I think, if i got you right, what you want to do is just

map (\x->myObviouslyFakeFunction 1 'a' x 'b' 2) ["abc", "def", "ghi"]

This might clarify a little bit:

myObviouslyFakeFunction :: Int -> Char -> String -> Char -> Integer -> String 
myObviouslyFakeFunction 1 :: Char -> String -> Char -> Integer -> String 
myObviouslyFakeFunction 1 'a' :: String -> Char -> Integer -> String 
myObviouslyFakeFunction 1 'a' "abc" :: Char -> Integer -> String 
myObviouslyFakeFunction 1 'a' "abc" 'b' :: Integer -> String
myObviouslyFakeFunction 1 'a' "abc" 'b' 2 :: String

What you want to give to map is something of type a -> b, but this is of type String. If you had redefined your function so that it would have the following type (for some type b)

myObviouslyFakeFunction' :: Int -> Char -> String -> Char -> Integer -> String -> b

then we would have that

myObviouslyFakeFunction' 1 'a' "abc" 'b' 2 :: String -> b

Then you can apply it to the list of Strings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top