Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# function calling syntax confusion

Tags:

I have a piece of code:

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

Which I wanted to rewrite to:

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

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) 

The error that I get is:

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?

like image 901
Daniel Avatar asked Apr 27 '10 21:04

Daniel


People also ask

Facebook itu apa sih?

Facebook adalah media sosial dan layanan jejaring sosial online Amerika yang dimiliki oleh Meta Platforms.


Video Answer


1 Answers

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 

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)  

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 

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).

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.

like image 86
Tomas Petricek Avatar answered Oct 10 '22 04:10

Tomas Petricek