Haskell:泛型函数中的绑定值 [英] Haskell: bound-values in generic functions
本文介绍了Haskell:泛型函数中的绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
succ'::(有界a,方程a,枚举a)=> ; a - >
succ'n
| n ==(maxBound :: a)= minBound :: a
|否则= succ n
但这不起作用。如何解决这个问题?
解决方案
你不需要类型注释,它们是错误的根源,重新获得:
succ'::(有界a,Eq a,Enum a)=> a - >
succ'n
| n == maxBound = minBound
|否则= succ n
(这适用于Haskell 98和Haskell 2010, )
另外,我稍微缩进了 |
,因为它们不能与函数的开头对齐;它们是 succ'
定义的一部分,而不是独立代码。
以下是一些测试数据:
data Test = A | B | C
派生(Bounded,Eq,Enum,Show)
test = map succ'[A .. C]
我得到了 [B,C,A]
。
i want to do something like:
succ' :: (Bounded a, Eq a, Enum a) => a -> a
succ' n
| n == (maxBound :: a) = minBound :: a
| otherwise = succ n
but this does not work. how to solve this?
解决方案
You don't need the type annotations, and they're the source of the errors you're getting:
succ' :: (Bounded a, Eq a, Enum a) => a -> a
succ' n
| n == maxBound = minBound
| otherwise = succ n
(This works in Haskell 98 and Haskell 2010, so pretty much any compiler you've got lying around .)
Also, I indented the |
a little, because they can't line up with the start of the function; they're part of the definition of succ'
, not standalone code.
Here's some test data:
data Test = A | B | C
deriving (Bounded, Eq, Enum, Show)
test = map succ' [A .. C]
I got [B,C,A]
.
这篇关于Haskell:泛型函数中的绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文