模式匹配何时需要括号?

时间:2022-01-29 17:42:57

Using this code :

使用此代码:

-- Store a person's name, age, and favourite Thing.
data Person = Person String Int Thing
  deriving Show

brent :: Person
brent = Person "Brent" 31 SealingWax

stan :: Person
stan  = Person "Stan" 94 Cabbage

getAge :: Person -> Int
getAge (Person _ a _) = a

to access age of stan use :

访问stan使用年龄:

getAge stan

prints : 94

印刷品:94

Defining stan does not require brackets.

定义stan不需要括号。

However getAge Person "a" 1 Cabbage causes error :

但是getAge Person“a”1 Cabbage会导致错误:

<interactive>:60:8:
    Couldn't match expected type `Person'
                with actual type `String -> Int -> Thing -> Person'
    Probable cause: `Person' is applied to too few arguments
    In the first argument of `getAge', namely `Person'
    In the expression: getAge Person "a" 1 Cabbage

I need to use brackets :

我需要使用括号:

*Main> getAge (Person "a" 1 Cabbage)
1

Why are brackets required in this case ? But when defining stan = Person "Stan" 94 Cabbage does not require brackets ?

为什么在这种情况下需要括号?但是当定义stan = Person“Stan”时94 Cabbage不需要括号?

1 个解决方案

#1


getAge Person "a" 1 Cabbage

is parsed as

被解析为

(((getAge Person) "a") 1) Cabbage

i.e. this would have to be a function accepting a Person-constructor and three more arguments, not a function accepting a single Person-value.

即,这必须是一个接受Person构造函数和另外三个参数的函数,而不是接受单个Person值的函数。

Why it's done this way? Well, it makes multi-parameter functions much nicer. For instance, Person itself is a function, taking three arguments (the data type's fields). If Haskell didn't just feed arguments one-by-one, you would also need to write Person ("Brent", 31, Sealingwax).

为什么这样做?嗯,它使多参数功能更好。例如,Person本身就是一个函数,它带有三个参数(数据类型的字段)。如果Haskell不是一个一个地提供参数,你还需要写Person(“Brent”,31,Sealingwax)。

The parsing rules Haskell uses are actually much simpler than in most other languages, and they allow partial application very naturally, which is really useful. For instance,

Haskell使用的解析规则实际上比大多数其他语言简单得多,并且它们非常自然地允许部分应用,这非常有用。例如,

GHCi> map (Person "Brent" 31) [Cabbage, SealingWax]
[Person "Brent" 31 Cabbage, Person "Brent" 31 SealingWax]

GHCi>地图(人“布伦特”31)[卷心菜,SealingWax] [人“布伦特”31卷心菜,人“布伦特”31 SealingWax]

#1


getAge Person "a" 1 Cabbage

is parsed as

被解析为

(((getAge Person) "a") 1) Cabbage

i.e. this would have to be a function accepting a Person-constructor and three more arguments, not a function accepting a single Person-value.

即,这必须是一个接受Person构造函数和另外三个参数的函数,而不是接受单个Person值的函数。

Why it's done this way? Well, it makes multi-parameter functions much nicer. For instance, Person itself is a function, taking three arguments (the data type's fields). If Haskell didn't just feed arguments one-by-one, you would also need to write Person ("Brent", 31, Sealingwax).

为什么这样做?嗯,它使多参数功能更好。例如,Person本身就是一个函数,它带有三个参数(数据类型的字段)。如果Haskell不是一个一个地提供参数,你还需要写Person(“Brent”,31,Sealingwax)。

The parsing rules Haskell uses are actually much simpler than in most other languages, and they allow partial application very naturally, which is really useful. For instance,

Haskell使用的解析规则实际上比大多数其他语言简单得多,并且它们非常自然地允许部分应用,这非常有用。例如,

GHCi> map (Person "Brent" 31) [Cabbage, SealingWax]
[Person "Brent" 31 Cabbage, Person "Brent" 31 SealingWax]

GHCi>地图(人“布伦特”31)[卷心菜,SealingWax] [人“布伦特”31卷心菜,人“布伦特”31 SealingWax]