有多个参数的地图

时间:2014-03-26 09:42:23

标签: haskell functional-programming higher-order-functions

地图的类型是:

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

因此,如果我想将具有多个参数的函数映射到数组上,请执行以下操作:

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

并执行以下操作:

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

我怎么能这样做?类型签名中的a代表第一个参数吗?一个包含所有这些元组的元组?一个清单?

3 个答案:

答案 0 :(得分:4)

如果您想使用myObviouslyFakeFunction喜欢

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

那么它的类型应该是

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

因为map需要单参数函数,在这种情况下类型String -> bb是一个类型变量,可以是任何有效类型),作为它的第一个参数,并且如果myObviouslyFakeFunction具有上述类型,

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

将是String -> ResultType函数。

答案 1 :(得分:2)

我想,如果我找对你,你想做的只是

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

答案 2 :(得分:1)

这可能会澄清一点:

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

您要为map提供的内容类型为a -> b,但其类型为String。如果您重新定义了函数,那么它将具有以下类型(对于某些类型b

myObviouslyFakeFunction' :: Int -> Char -> String -> Char -> Integer -> String -> b
然后我们会有那个

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

然后您可以将其应用于String s。

列表