Functions on F# for Fun and Profit
https://fsharpforfunandprofit.com/categories/functions/
Recent content in Functions on F# for Fun and ProfitHugo -- gohugo.ioen-usSat, 07 Jul 2012 00:00:00 +0000Worked example: A stack based calculator
https://fsharpforfunandprofit.com/posts/stack-based-calculator/
Sat, 07 Jul 2012 00:00:00 +0000https://fsharpforfunandprofit.com/posts/stack-based-calculator/In this post, we’ll implement a simple stack based calculator (also known as “reverse Polish” style). The implementation is almost entirely done with functions, with only one special type and no pattern matching at all, so it is a great testing ground for the concepts introduced in this series.
If you are not familiar with a stack based calculator, it works as follows: numbers are pushed on a stack, and operations such as addition and multiplication pop numbers off the stack and push the result back on.Organizing functions
https://fsharpforfunandprofit.com/posts/organizing-functions/
Thu, 10 May 2012 00:00:00 +0000https://fsharpforfunandprofit.com/posts/organizing-functions/Now that you know how to define functions, how can you organize them?
In F#, there are three options:
functions can be nested inside other functions. at an application level, the top level functions are grouped into “modules”. alternatively, you can also use the object-oriented approach and attach functions to types as methods. We’ll look at the first two options in this post, and the third in the next post.Function signatures
https://fsharpforfunandprofit.com/posts/function-signatures/
Wed, 09 May 2012 00:00:00 +0000https://fsharpforfunandprofit.com/posts/function-signatures/It may not be obvious, but F# actually has two syntaxes - one for normal (value) expressions, and one for type definitions. For example:
[1;2;3] // a normal expression int list // a type expression Some 1 // a normal expression int option // a type expression (1,"a") // a normal expression int * string // a type expression Type expressions have a special syntax that is different from the syntax used in normal expressions.Defining functions
https://fsharpforfunandprofit.com/posts/defining-functions/
Tue, 08 May 2012 00:00:00 +0000https://fsharpforfunandprofit.com/posts/defining-functions/We have seen how to create typical functions using the “let” syntax, below:
let add x y = x + y In this section, we’ll look at some other ways of creating functions, and tips for defining functions.
Anonymous functions (a.k.a. lambdas) If you are familiar with lambdas in other languages, this will not be new to you. An anonymous function (or “lambda expression”) is defined using the form:Function associativity and composition
https://fsharpforfunandprofit.com/posts/function-composition/
Mon, 07 May 2012 00:00:00 +0000https://fsharpforfunandprofit.com/posts/function-composition/Function associativity If we have a chain of functions in a row, how are they combined?
For example, what does this mean?
let F x y z = x y z Does it mean apply the function y to the argument z, and then take the result and use it as an argument for x? In which case it is the same as:
let F x y z = x (y z) Or does it mean apply the function x to the argument y, and then take the resulting function and evaluate it with the argument z?How types work with functions
https://fsharpforfunandprofit.com/posts/how-types-work-with-functions/
Fri, 04 May 2012 00:00:00 +0000https://fsharpforfunandprofit.com/posts/how-types-work-with-functions/Now that we have some understanding of functions, we’ll look at how types work with functions, both as domains and ranges. This is just an overview; the series “understanding F# types” will cover types in detail.
First, we need to understand the type notation a bit more. We’ve seen that the arrow notation “->” is used to show the domain and range. So that a function signature always looks like:Partial Application
https://fsharpforfunandprofit.com/posts/convenience-partial-application/
Mon, 16 Apr 2012 00:00:00 +0000https://fsharpforfunandprofit.com/posts/convenience-partial-application/A particularly convenient feature of F# is that complicated functions with many parameters can have some of the parameters fixed or “baked in” and yet leave other parameters open. In this post, we’ll take a quick look at how this might be used in practice.
Let’s start with a very simple example of how this works. We’ll start with a trivial function:
// define a adding function let add x y = x + y // normal use let z = add 1 2 But we can do something strange as well – we can call the function with only one parameter!Functions as interfaces
https://fsharpforfunandprofit.com/posts/convenience-functions-as-interfaces/
Sun, 15 Apr 2012 00:00:00 +0000https://fsharpforfunandprofit.com/posts/convenience-functions-as-interfaces/An important aspect of functional programming is that, in a sense, all functions are “interfaces”, meaning that many of the roles that interfaces play in object-oriented design are implicit in the way that functions work.
In fact, one of the critical design maxims, “program to an interface, not an implementation”, is something you get for free in F#.
To see how this works, let’s compare the same design pattern in C# and F#.Using functions as building blocks
https://fsharpforfunandprofit.com/posts/conciseness-functions-as-building-blocks/
Wed, 11 Apr 2012 00:00:00 +0000https://fsharpforfunandprofit.com/posts/conciseness-functions-as-building-blocks/A well-known principle of good design is to create a set of basic operations and then combine these building blocks in various ways to build up more complex behaviors. In object-oriented languages, this goal gives rise to a number of implementation approaches such as “fluent interfaces”, “strategy pattern”, “decorator pattern”, and so on. In F#, they are all done the same way, via function composition.
Let’s start with a simple example using integers.Using functions to extract boilerplate code
https://fsharpforfunandprofit.com/posts/conciseness-extracting-boilerplate/
Tue, 10 Apr 2012 00:00:00 +0000https://fsharpforfunandprofit.com/posts/conciseness-extracting-boilerplate/In the very first example in this series, we saw a simple function that calculated the sum of squares, implemented in both F# and C#. Now let’s say we want some new functions which are similar, such as:
Calculating the product of all the numbers up to N Counting the sum of odd numbers up to N The alternating sum of the numbers up to N Obviously, all these requirements are similar, but how would you extract any common functionality?