F#函数调用语法混乱 [英] F# function calling syntax confusion

查看:68
本文介绍了F#函数调用语法混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码:

links
    |> Seq.map (fun x -> x.GetAttributeValue ("href", "no url"))

我想重写为:

links
    |> Seq.map (fun x -> (x.GetAttributeValue "href" "no url"))

但是F#编译器似乎不喜欢这样.我的印象是这两个函数调用是可以互换的:

But the F# compiler doesn't seem to like that. I was under the impression that these two function calls were interchangeable:

f (a, b)
(f a b)

我得到的错误是:

不能从此代码位置访问带有2个参数的成员或对象构造函数'GetAttributeValue'.方法'GetAttributeValue'的所有可访问版本都带有2个参数.

The member or object constructor 'GetAttributeValue' taking 2 arguments are not accessible from this code location. All accessible versions of method 'GetAttributeValue' take 2 arguments.

这似乎很有趣,因为它似乎表明它需要我提供的东西.我在这里想念什么?

Which seems amusing, as it seems to indicate that it needs what I'm giving it. What am I missing here?

推荐答案

使用F#编写的常规函数​​调用不带括号,并且参数之间用空格分隔.定义多个参数的函数的简单方法是编写以下代码:

A usual function call in F# is written without parentheses and parameters are separated by spaces. The simple way to define a function of several parameters is to write this:

let add a b = a + b

正如Pascal所指出的那样,这种指定参数的方式称为currying-这个想法是一个函数只接受一个参数,而结果是一个接受第二个参数并返回实际结果的函数(或另一个函数).当调用这样的简单函数时,您将编写add 10 5,编译器(原则上)将其解释为((add 10) 5).这具有一些不错的优点-例如,它允许您使用部分函数应用程序,在其中您仅指定函数的前几个参数:

As Pascal noted, this way of specifying parameters is called currying - the idea is that a function takes just a single parameter and the result is a function that takes the second parameter and returns the actual result (or another function). When calling a simple function like this one, you would write add 10 5 and the compiler (in principle) interprets this as ((add 10) 5). This has some nice advantages - for example it allows you to use partial function application where you specify only a first few arguments of a function:

let addTen = add 10 // declares function that adds 10 to any argument
addTen 5  // returns 15
addTen 9  // returns 19

此功能在处理列表时非常有用,例如:

This feature is practically useful for example when processing lists:

// The code using explicit lambda functions..
[ 1 .. 10 ] |> List.map (fun x -> add 10 x) 

// Can be rewritten using partial function application:
[ 1 .. 10 ] |> List.map (add 10) 

现在,让我们进入令人困惑的部分-在F#中,您还可以使用元组,元组是简单的数据类型,可以将多个值分组为一个值(请注意,元组与任何函数都不相关道路).例如,您可以编写:

Now, let's get to the confusing part - in F#, you can also work with tuples, which are simple data types that allow you to group multiple values into a single values (note that tuples aren't related to functions in any way). You can for example write:

let tup = (10, "ten")    // creating a tuple
let (n, s) = tup         // extracting elements of a tuple using pattern 
printfn "n=%d s=%s" n s  // prints "n=10 s=ten"

当编写一个函数时,该函数的参数用圆括号括起来,中间用逗号隔开,实际上,您正在编写的函数中,该参数的参数为​​一个元组:

When you write a function that takes parameters in parentheses separated by a comma, you're actually writing a function that takes a single parameter which is a tuple:

// The following function:
let add (a, b) = a * b

// ...means exactly the same thing as:
let add tup = 
  let (a, b) = tup  // extract elements of a tuple
  a * b

// You can call the function by creating tuple inline:
add (10, 5)
// .. or by creating tuple in advance
let t = (10, 5)
add t

这是另一种类型的函数-它采用一个元组的单个参数,而第一个版本是采用两个参数(使用currying)的函数.

This is a function of a different type - it takes a single parameter which is a tuple, while the first version was a function that took two parameters (using currying).

在F#中,情况要复杂得多-.NET方法显示为以元组为参数的方法(因此您可以使用括号表示法来调用它们),但是它们在某种程度上受到限制(例如,您不能首先创建一个元组,然后调用只给它元组的方法.另外,已编译的F#代码实际上不会以咖喱形式生成方法(因此,您不能直接从C#中使用部分函数应用程序).这是由于性能原因-在大多数情况下,您可以指定所有参数,这样可以更高效地实现.

In F#, the situation is a bit more complicated than that - .NET methods appear as methods that take a tuple as a parameter (so you can call them with the parenthesized notation), but they are somewhat limited (e.g. you cannot create a tuple first and then call the method giving it just the tuple). Also, the compiled F# code doesn't actually produce methods in the curried form (so you cannot use partial function application directly from C#). This is due to performance reasons - most of the times, you specify all arguments and this can be implemented more efficiently.

但是,原理是一个函数要么采用多个参数,要么采用元组作为参数.

However, the principle is that a function either takes multiple parameters or takes a tuple as a parameter.

这篇关于F#函数调用语法混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆