【发布时间】:2016-04-15 10:27:05
【问题描述】:
我是 haskell 的初学者,正在尝试实现自然数的 Church 编码,如 this guide 中所述。
{-# LANGUAGE RankNTypes #-}
newtype Chur = Chr (forall a. (a -> a) -> (a -> a))
zero :: Chur
zero = Chr (\x y -> y)
-- church to int
c2i :: Chur -> Integer
c2i (Chr cn) = cn (+ 1) 0
-- this works
i1 = c2i zero
-- this doesn't
i2 = zero (+ 1) 0
对于i2,我得到一个类型不匹配:
Couldn't match expected type ‘(Integer -> Integer) -> Integer -> t’
with actual type ‘Chur’
Relevant bindings include i2 :: t (bound at test.hs:14:1)
The function ‘zero’ is applied to two arguments,
but its type ‘Chur’ has none
In the expression: zero (+ 1) 0
In an equation for ‘i2’: i2 = zero (+ 1) 0
为什么Chur 可以在封装在函数中时接受参数,但不能没有它?
【问题讨论】:
标签: haskell