简体   繁体   English

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

[英]How to Take Lists of Any Types as Function Parameters Haskell

Hello I want to take lists of any type eg [Int] , [Char] , etc as parameters to a function.您好,我想将任何类型的列表(例如[Int][Char]等)作为函数的参数。

Basically all I want to do is something along the lines of:基本上我想做的就是以下几点:

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

Were [a] could be a list of any type. [a]可以是任何类型的列表。

Well, you pretty much got it already.嗯,你几乎已经明白了。 Just write the signature like that and define your function.只需像这样写签名并定义您的功能。 Except in a real-life application you will (probably; see @amalloy's comment) need to require a to belong to some type class, in order to do something meaningful with it:除了在现实生活中的应用程序中,您(可能;请参阅@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM