0

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?

5
  • 2
    (myObviouslyFakeFunction 1 'a' "abc" 'b' 2) is a String, not a function. Commented Mar 26, 2014 at 9:47
  • any chance you could clarify? I feel like I'm missing something incredibly basic... Why isn't it a function? Doesn't the above define it as a function (that returns a string?) To paraphrase a language I am more familiar with is that not equivalent to public string myObviouslyFakeFunction(int i, char c, string s, char c2, int i2){}? How does it differ? Commented Mar 26, 2014 at 9:50
  • @AbrahamP - Its a string because you've provided all the arguments to myObvisoulyFakeFunction. You could partially apply a function a1 -> a2 .. -> aN -> String -> b to create a function String -> b and pass that to map, but myObviouslyFakeFunction does not follow that pattern. Commented Mar 26, 2014 at 9:52
  • 1
    Let's say you've got a function f :: Int -> Int. Then f 1 is an Int. Commented Mar 26, 2014 at 9:52
  • @AbrahamP In Java (or whatever that is) it would be equivalent to myObviouslyFakeFunction(1, 'a', "abc", 'b', 2) which would be a string, not a function. Commented Mar 26, 2014 at 9:57

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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"]

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.