Haskell function returning Integer

How do I write a function that returns everything to Integer?

ie

f True = 1
f False =0
f 1 = 1
f 2.30 = 2


The only functions of type a -> Integer will be constant functions. This is a free theorem guaranteed by the parametricity of the type (modulo some fudging because of some unsound corners of Haskell).

Here's an example of such a function:

f :: a -> Integer
f _ = 1

There's no way to do this in general, and it's not at all advisable, but the cases you mention, and some others, can be obtained using an ugly hack:

f :: Enum n => n -> Integer
f x = toInteger (fromEnum x)

As a general rule, it's best to simply pretend that the Enum class does not exist, because it is extremely badly designed and some of its instances are badly behaved.

链接地址: http://www.djcxy.com/p/80798.html

上一篇: 引导一个编译器:为什么?

下一篇: Haskell函数返回整数