# Rash thoughts about .NET, C#, F# and Dynamics NAV.

## Wednesday, 17. June 2009

### F# BootCamp – Questions and Answers – part II – Currying

Filed under: C#,English posts,F#,FAKE - F# Make,Informatik,Mathematik,Veranstaltungen — Steffen Forkmann at 12:36 Uhr

Yesterday I was talking about F# at the .NET Developer Group Braunschweig. It was my first talk completely without PowerPoint (just Live-Coding and FlipChart) and I have to admit this is not that easy. But the event was really a big fun and we covered a lot of topics like FP fundamentals, concurrency and domain specific languages (of course I showed Ă˘â‚¬Ĺ“FAKE Ă˘â‚¬â€ś F# MakeĂ˘â‚¬Âť).

Now I have a bit time before I go to the next BootCamp in Leipzig. Today Christian Weyer will show us exciting new stuff about WCF and Azure.

In the meanwhile I will write here about another important question (see first article) from the F# BootCamp in Leipzig:

##### Question 4 Ă˘â‚¬â€ś Try to explain Ă˘â‚¬Ĺ“CurryingĂ˘â‚¬Âť and Ă˘â‚¬Ĺ“Partial ApplicationĂ˘â‚¬Âť. Hint: Please show a sample and use the pipe operator |>.

Obviously this was a tricky question for FP beginners. There are a lot of websites, which give a formal mathematical definition but donĂ˘â‚¬â„˘t show the practical application.

Ă˘â‚¬Ĺ“Currying Ă˘â‚¬Â¦ is the technique of transforming a function that takes multiple arguments (or more accurately an n-tuple as argument) in such a way that it can be called as a chain of functions each with a single argumentĂ˘â‚¬Âť

I want to show how my pragmatic view of the terms here, so letĂ˘â‚¬â„˘s consider this small C# function:

```public int Add(int x, int y)
{
return x + y;
}```

Of course the corresponding F# version looks nearly the same:

`let add(x,y) = x + y`

But letĂ˘â‚¬â„˘s look at the signature: val add : int * int Ă˘â‚¬â€ś> int. The F# compiler is telling us add wants a tuple of ints and returns an int. We could rewrite the function with one blank to understand this better:

`let add (x,y) = x + y`

As you can see the add function actually needs only one argument Ă˘â‚¬â€ś a tuple:

```let t = (3,4)         // val t : int * int

Now we want to curry this function. If youĂ˘â‚¬â„˘d ask a mathematician this a complex operation, but from a pragmatic view it couldnĂ˘â‚¬â„˘t be easier. Just remove the brackets and the comma Ă˘â‚¬â€ś thatĂ˘â‚¬â„˘s all:

`let add x y = x + y`

Now the signature looks different: val add : int -> int Ă˘â‚¬â€ś> int

But whatĂ˘â‚¬â„˘s the meaning of this new arrow? Basically we can say if we give one int parameter to our add function we will get a function back that will take only one int parameter and returns an int.

```let increment = add 1      // val increment : (int -> int)
printfn "%d" (increment 2) // prints 3```

Here Ă˘â‚¬Ĺ“incrementĂ˘â‚¬Âť is a new function that uses partial application of the curryied add function. This means we are fixing one of the parameters of add to get a new function with one parameter less.

But why are doing something like this? WouldnĂ˘â‚¬â„˘t it be enough to use the following increment function?

```let add(x,y) = x + y       // val add : int * int -> int
let increment x = add(x,1) // val increment : int -> int
printfn "%d" (increment 2) // prints 3```

Of course we are getting (nearly) the same signature for increment. But the difference is that we can not use the forward pipe operator |> here. The pipe operator will help us to express things in the way we are thinking about it.

LetĂ˘â‚¬â„˘s say we want to filter all even elements in a list, then calculate the sum and finally square this sum and print it to the console. The C# code would look like this:

```var list = new List<int> {4,2,6,5,9,3,8,1,3,0};
Console.WriteLine(Square(CalculateSum(FilterEven(list))));```

If we donĂ˘â‚¬â„˘t want to store intermediate results we have to write our algorithm in reverse order and with heavily use of brackets. The function we want to apply last has to be written first. This is not the way we think about it.

With the help of curried functions, partial application and the pipe operator we can write the same thing in F#:

```let list = [4; 2; 6; 5; 9; 3; 8; 1; 3; 0]

let square x = x * x```
```list
|> List.filter (fun x -> x % 2 = 0) // partial application
|> List.sum
|> square
|> printfn "%A"                     // partial application```

We describe the data flow in exactly the same order we talked about it. Basically the pipe operator take the result of a function and puts it as the last parameter into the next function.

What should we learn from this sample?

1. Currying has nothing to do with spicy chicken.
2. The |> operator makes life easier and code better to understand.
3. If we want to use |> we need curryied functions.
4. Defining curryied functions is easy Ă˘â‚¬â€ś just remove brackets and comma.
5. We donĂ˘â‚¬â„˘t need the complete mathematical theory to use currying.
6. Be careful with the order of the parameter in a curryied function. DonĂ˘â‚¬â„˘t forget the pipe operator puts the parameter from the right hand side into your function Ă˘â‚¬â€ś all other parameters have to be fixed with partial application.
Tags: , , , , , , , ,

#### 1 Comment »

1. […] F# BootCamp Q&A on this blog […]

Pingback by Partial application if F# and C# » Rash thoughts about .NET, C#, F# and Dynamics NAV. — Friday, 27. January 2012 um 18:55 Uhr