为了
x :: Integer -> [a] -> [a]
x = take
y :: Integer -> [a] -> [a]
y _ [] = []
y n xs = take n xs
无论是Hugs还是GHC都会报告类型错误:
ERROR "test.hs":5 - Type error in application
*** Expression : take n xs
*** Term : n
*** Type : Integer
*** Does not match : Int
ERROR "test.hs":8 - Type error in explicitly typed binding
*** Term : x
*** Type : Int -> [a] -> [a]
*** Does not match : Integer -> [a] -> [a]
这是因为 "take" 的签名是 "Int -> ..."。有没有办法告诉类型系统直接将 Integer 转换为 Int(而不需要 fromIntegral),或者更好的方法(保留 Integer 的非约束大小),来 "构建" take 的版本(或任何其他明确使用 Int 的函数)用于 Integer?还是我必须编写自己的 Prelude 函数版本?目前,我的代码要么充斥着 fromIntegral(如果 Integer 的大小超过 Int 的维度,则无法运行),要么充满了标准函数的琐碎重新实现,这感觉非常笨重。
Integral i => i -> [a] -> [a]
),那就是重载版本。 - hugomg