如何在 Haskell 中定义任意 arity 函数,其中包含 0 的 arity ?
我当前定义任意数量函数的方法如下,其中 A 是累加器,E 是输入参数类型,R 是结果类型。
combine :: A -> E -> A
class X r where
foo :: A -> E -> r
instance X R where
foo :: A -> E -> R
instance X r => X ( E -> r ) where
foo :: A -> E -> E -> r
foo ( a :: A ) ( x :: E ) =
foo ( a `combine` e :: A )
doFoo = foo emptyA
但是 foo 的最小数量是 1。 foo 的最小数量仍然是 A -> 。 E-> R,而doFoo是E-> R. 我也想要 doFoo :: R. 如何?
My current approach to define a function of arbitrary arity is below, with A being an accumulator, E being the input argument type, and R being the result type.
combine :: A -> E -> A
class X r where
foo :: A -> E -> r
instance X R where
foo :: A -> E -> R
instance X r => X ( E -> r ) where
foo :: A -> E -> E -> r
foo ( a :: A ) ( x :: E ) =
foo ( a `combine` e :: A )
doFoo = foo emptyA
But the minimum arity of foo is 1. The minimum for foo is still A -> E -> R, and doFoo is E -> R. I'd also like to have doFoo :: R. How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
又怎样呢
?
您可能想查看 PrintfType 实例。正是因为他们,我才能够给出答案。
What about
?
You may want to have a look at the PrintfType instances. It's only because of them that I was able to provide an answer.