简体   繁体   English

Haskell函数调用中的括号

[英]Brackets in Haskell function call

I'm struggling to remember how to use brackets when calling Haskell functions. 我在努力记住在调用Haskell函数时如何使用括号。 I come from a C-style background and I'm used to f(comma, separated, args) syntax for calling functions. 我来自C风格的背景,我习惯于f(comma, separated, args)语法来调用函数。 Apparently this is the same as (in Haskell) ((((f) comma) separated) args) but this is just confusing. 显然这与(在Haskell中) ((((f) comma) separated) args)但这只是令人困惑。

Why do I need all these brackets? 为什么我需要所有这些括号?

It's easy to remember the rules regarding parentheses and function calls in Haskell: there aren't any, because parentheses have nothing to do with function calls! 在Haskell中很容易记住关于括号和函数调用的规则:没有,因为括号与函数调用无关

The basic syntax for function calls in Haskell is to just write terms next to each other: function argument is calling function , passing it argument . Haskell中函数调用的基本语法是只写彼此相邻的术语: function argument是调用function ,传递argument In C-like syntax you'd write that as function(argument) . 在类C语法中,您将其写为function(argument) Your C example f(comma, separated, args) would be written in Haskell as f comma separated args . 您的C示例f(comma, separated, args)将以f comma separated args写入Haskell。

Haskell uses parentheses only in the way they are used in high school mathematics: for grouping sub-expressions so that you get a different call structure. Haskell仅在高中数学中使用括号时使用括号:对子表达式进行分组,以便获得不同的调用结构。 For example, in maths 1 + 2 * 3 would call * on 2 and 3, and call + on 1 and the result of the * call. 例如,在数学中, 1 + 2 * 3将在2和3上调用* ,并在1上调用+*调用的结果。 (1 + 2) * 3 changes the grouping so that + is called on 1 and 2, and * called on that result and 3. The same is valid Haskell (and C), with the same meaning, but parentheses with grouping like this can also be useful to group expressions with ordinary functions rather than infix operators. (1 + 2) * 3更改分组,以便在1和2上调用+ ,并且*调用该结果和3.同样有效的Haskell(和C),具有相同的含义,但是括号分组如下使用普通函数而不是中缀运算符对表达式进行分组也很有用。 For example f comma separated args calls f on the arguments comma , separated , and args , while f comma (separated args) means to call separated passing it args , and call f passing it comma and the result of the call to separated . 例如, f comma separated args在参数commaseparated argsargs上调用f ,而f comma (separated args)表示调用separated传递args ,并调用f将其传递给comma ,并将调用的结果separated

There is no need for ((((f) comma) separated) args) ; 不需要((((f) comma) separated) args) ; in fact you would have seen that as an explanation of how f comma separated args is recognised by the language when you use no parentheses. 其实你会看到,作为如何解释f comma separated args通过语言识别的,当你使用括号。 It's wrapping every sub-expression in explicit parentheses so there's no ambiguity to show you what the default is, not saying you actually need all those parentheses. 它将每个子表达式包装在显式括号中,因此没有歧义来向您显示默认值,而不是说您实际上需要所有这些括号。

If you're used to c-style function call syntax, then for a c-style function, eg 如果你习惯了c风格的函数调用语法,那么对于c风格的函数,例如

// defining the function
int plus(int a, int b)
{
    return a + b;
}

// elsewhere, calling the function
plus(a, b); // brackets surrounding only the arguments, comma separated

Then the equivalent Haskell code will be 然后是等效的Haskell代码

-- defining the function
plus :: Int -> Int -> Int
plus a b = a + b

-- elsewhere, calling the function:
(plus a b) -- brackets surrounding the function and the arguments, no commas

To use the example in your question, 要在您的问题中使用该示例,

f(comma, separated, args);

This would be equivalent to 这相当于

(f comma separated args)

But these brackets aren't necessary except sometimes, when the function call is embedded in another expression. 但是,除非有时将函数调用嵌入另一个表达式中,否则这些括号不是必需的。 In haskell, this is also equivalent to 在haskell中,这也相当于

((((f) comma) separated) args)

but these extra brackets are not necessary either, they just highlight the way Haskell treats function application (where each function technically only takes one argument); 但是这些额外的括号也不是必需的,它们只是突出了Haskell处理函数应用程序的方式(其中每个函数在技术上只接受一个参数); so this final case would be similar to the following c function call: 所以最后一种情况类似于以下c函数调用:

f(comma)(separated)(args);

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

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