2011-04-07 70 views
2

我被问在这给我留下了很困惑一类这个问题,我们提出下列要求:什么类型在Haskell意味着

对于波纹管式声明:

ranPositions :: Image -> Dims -> [Point] 
getBlockSums :: Image -> Dims -> [Point] -> [BlockSum] 
i :: Image 
d :: Dims 

什么以下类型? 是不是上面的?!

ranPositions i d 
getBlockSums i d 

所以,我回答是这样的:

type ranPositions = Array Point Int, (Int, Int) 
type getBlockSums = Array Point Int, (Int, Int) 

// Because (this was given) 

type Image = Array Point Int 
type Dims = (Int, Int) 

除了是错误的,这个问题困惑了我,因为我认为一个函数的类型是什么的::后声明,所以它有已经给了,不是吗?

我可以做一些解释,我会很感激任何帮助。

+1

请查看HaskellWiki中的[Currying](http://www.haskell.org/haskellwiki/Currying) – 2011-04-07 13:16:05

回答

8

类型的ranPosition i d[Point] - (钻营给你返回[Point]功能)

类型的getBlockSums i d[Point] -> [BlockSum] - (钻营为您提供了从[Point]返回功能[BlockSum]功能)

3

当然,但他们要求的类型表达式,而不是函数

这难道不是明显,下面的表达式类型:

foo 
foo a 
foo a b 

都必须是不同的?如果你不清楚,那么回去看看功能应用。

+0

从技术上讲,它们不会有所不同。比较'id','id id','id id id'的类型。但总的来说,是的,它们应该是不同的。 一个很好的例子可能是 (+):: Int - > Int - > Int - 取两个数字并将它们相加 (1+):: Int - > Int - 取一个数字并将其加1 1 + 2):: Int - 取零数字并返回3 – 2011-04-10 13:03:40

+0

@Tyr - 这很棘手,但有人可能会认为id :: forall a。 a - > a是多态的,而表达式(id id)不是,它是一个unknmown类型的单形函数。差别可以在相当人造的例子中看到,例如** case id id f - >(f id,f const)**,它不应该检查,而**(id id,id const)**会。 OTOH,** f - >(f id,f const)**的案例ID不应该键入检查,所以再次证明id和id id是相同的。 – Ingo 2011-04-10 20:40:37

+0

我在GHCi中的输出是:t,以及它在评估中会减少什么。 – 2011-04-10 21:04:09