理解 Haskell 中的类型和术语

moa*_*ait -3 syntax haskell

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

一世。a -> b -> c -> d

ii. (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) 在下面的术语中,给定 x,y,z:: Integer,函数 f 的类型是什么:

一世。(fx) (y, z)

ii. 知乎

三、f (x,y,z)

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

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

一世。“A B C D”

ii. [('A B C D')]

三、('A B C D'])

四、'A B C D':[])

v. ["ab","cd"]

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

chi*_*chi 5

在类型中,->与右侧相关联,即a -> b -> c实际上意味着a -> (b -> c)。这是一个函数,它接受一个 type 参数a并返回一个 type 函数b -> c

相比之下,(a -> b) -> c是一个函数,它将一个type的函数作为参数a -> b,并返回一个 type 的值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"
Run Code Online (Sandbox Code Playgroud)

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

foo 5 True
(foo 5) true
Run Code Online (Sandbox Code Playgroud)

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

x : y
where x = 'a':['b']
      y = 'c':['d']
Run Code Online (Sandbox Code Playgroud)

尝试考虑应该xy拥有哪些类型,然后考虑 的类型(x : y如果有)。