简体   繁体   English

了解 Haskell 中的类型和术语

[英]Understanding types and terms in Haskell

Which of the types below are the same type:以下哪些类型属于同一类型:

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

ii. ii. (a -> b) -> c -> d (a -> b) -> c -> d

iii. iii. a -> (b -> c) -> d a -> (b -> c) -> d

iv. iv. a -> b -> (c -> d) a -> b -> (c -> d)

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

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

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

(b) In the following terms what are the types of the functions f given that x,y,z:: Integer: (b) 在下列术语中,给定 x,y,z:: Integer 的函数 f 的类型是什么:

i.一世。 (fx) (y, z) (fx) (y, z)

ii. ii. fxyz fxyz

iii. iii. f (x,y,z) f (x,y,z)

iv. iv. f (x,(y,z)) f (x,(y,z))

(c) Give the types of the following terms (if indeed they type) and indicate which are equal: (c) 给出下列术语的类型(如果它们确实是类型的话)并指出哪些是相等的:

i.一世。 "abcd" “A B C D”

ii. ii. [('a','b'),('c','d')] [('A B C D')]

iii. iii. ('a':['b']):('c':['d']) ('A B C D'])

iv. iv. 'a':('b':'c':'d':[]) 'A B C D':[])

v. ["ab","cd"] v. ["ab","cd"]

I'm not looking for a solution but I need help understanding the usage of () and its meaning.我不是在寻找解决方案,但我需要帮助理解 () 的用法及其含义。 thank you.谢谢你。

In types, -> associates to the right, ie a -> b -> c actually means a -> (b -> c) .在类型中, ->关联到右侧,即a -> b -> c实际上表示a -> (b -> c) This is a function which takes an argument of type a and returns a function of type b -> c .这是一个 function ,它接受 a 类型a参数并返回b -> c

By comparison, (a -> b) -> c is a function which takes as argument a function of type a -> b , and returns a value of type c . By comparison, (a -> b) -> c is a function which takes as argument a function of type a -> b , and returns a value of type c .

Here are a few examples这里有一些例子

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"

When calling a function, as in fxyz , application associates to the left, as in (((fx) y) z) .当调用 function 时,如fxyz ,应用程序关联到左侧,如(((fx) y) z) For example, these are equivalent:例如,这些是等价的:

foo 5 True
(foo 5) true

By contrast, (,,,,) with commas inside is the way to form tuples, and is unrelated to application.相比之下, (,,,,)里面有逗号是组成元组的方式,与应用无关。 Hence, [('a','b'),('c','d')] is a list of pairs.因此, [('a','b'),('c','d')]是对的列表。 Instead, in your example ('a':['b']):('c':['d']) there are no commas, so parentheses are only for grouping, and the expression has the same meaning of相反,在您的示例中('a':['b']):('c':['d'])没有逗号,因此括号仅用于分组,表达式具有相同的含义

x : y
where x = 'a':['b']
      y = 'c':['d']

Try thinking about what types should x and y have, and then think about the type of x: y , if any.试着想想xy应该有什么类型,然后想想x: y的类型,如果有的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM