如何将任何类型的列表作为函数参数Haskell

时间:2016-10-12 00:27:00

标签: list haskell function-parameter

您好我想要获取任何类型的列表,例如[Int][Char]等作为函数的参数。

基本上我想要做的就是:

xyz :: [a] -> [a] -> (Int, Int)

[a]可以是任何类型的列表。

1 个答案:

答案 0 :(得分:1)

嗯,你几乎已经知道了。只需编写这样的签名并定义您的功能。除了在实际应用程序中,您(可能会看到@ amalloy的评论)需要a来属于某个类型类,以便用它做一些有意义的事情:

xyz :: Integral a => [a] -> [a] -> (Int, Int)
xyz ls1 ls2 = (x, y) where
    x = fromInteger $ toInteger $ sum ls1
    y = fromInteger $ toInteger $ sum ls2