了解 Haskell 中的类型和术语

Understanding types and terms in Haskell

以下哪些类型是同一类型:

我。 a -> b -> c -> d

二。 (a -> b) -> c -> d

三。 a -> (b -> c) -> d

四。 a -> b -> (c -> d)

v。 (a -> b) -> (c -> d)

六。 ((a -> b) -> c) -> d

七。 a -> (b -> (c -> d))

(b) 在下列术语中,给定的函数 f 的类型是什么 x,y,z:: 整数:

我。 (f x) (y, z)

二。 fxyz

三。 f (x,y,z)

四。 f (x,(y,z))

(c) 给出下列项的类型(如果它们确实键入)并指出哪些是相等的:

我。 “abcd”

二。 [('a','b'),('c','d')]

三。 ('a':['b']):('c':['d'])

四。 'a':('b':'c':'d':[])

v。 [“ab”,“cd”]

我不是在寻找解决方案,但我需要帮助来理解 () 的用法及其含义。 谢谢。

在类型中,->关联到右边,即a -> b -> c实际表示a -> (b -> c)。这是一个接受 a 类型参数和 returns 类型 b -> c.

函数的函数

相比之下,(a -> b) -> c 是一个函数,它以 函数 类型 a -> b 和 returns 类型的值作为参数c.

这里有几个例子

foo :: Int -> Bool -> String
-- the same as foo :: Int -> (Bool -> String)
-- takes Int, returns function
foo n = \b -> if b && n> 5 then "some result" else "some other result"

bar :: (Int -> Bool) -> String
-- takes function, returns string
bar f = if f 43 then "hello" else "good morning"

-- bar can be called in this way
test :: String
test = bar (\n -> n > 34)    -- evaluates to "hello"

调用函数时,如f x y z,应用程序关联到左侧,如(((f x) y) z)。例如,这些是等价的:

foo 5 True
(foo 5) true

相比之下,(,,,,)里面带逗号的是构成元组的方式,与应用无关。因此,[(’a’,’b’),(’c’,’d’)] 是一个对列表。相反,在您的示例 (’a’:[’b’]):(’c’:[’d’]) 中没有逗号,因此括号仅用于分组,并且表达式与

具有相同的含义
x : y
where x = 'a':['b']
      y = 'c':['d']

试着想想xy应该有什么类型,然后再想想x : y的类型,如果有的话。