Folds on F# for Fun and Profit
https://fsharpforfunandprofit.com/categories/folds/
Recent content in Folds on F# for Fun and ProfitHugo -- gohugo.ioen-usTue, 25 Aug 2015 00:00:00 +0000Trees in the real world
https://fsharpforfunandprofit.com/posts/recursive-types-and-folds-3b/
Tue, 25 Aug 2015 00:00:00 +0000https://fsharpforfunandprofit.com/posts/recursive-types-and-folds-3b/This post is the sixth in a series.
In the previous post, we briefly looked at some generic types.
In this post, we’ll do some deeper dives into some real-world examples of using trees and folds.
Series contents Here’s the contents of this series:
Part 1: Introduction to recursive types and catamorphisms A simple recursive type Parameterize all the things Introducing catamorphisms Benefits of catamorphisms Rules for creating a catamorphism Part 2: Catamorphism examples Catamorphism example: File system domain Catamorphism example: Product domain Part 3: Introducing folds A flaw in our catamorphism implementation Introducing fold Problems with fold Using functions as accumulators Introducing foldback Rules for creating a fold Part 4: Understanding folds Iteration vs.Generic recursive types
https://fsharpforfunandprofit.com/posts/recursive-types-and-folds-3/
Mon, 24 Aug 2015 00:00:00 +0000https://fsharpforfunandprofit.com/posts/recursive-types-and-folds-3/This post is the fifth in a series.
In the previous post, we spent some time understanding folds for specific domain types.
In this post, we’ll broaden our horizons and look at how to use generic recursive types.
Series contents Here’s the contents of this series:
Part 1: Introduction to recursive types and catamorphisms A simple recursive type Parameterize all the things Introducing catamorphisms Benefits of catamorphisms Rules for creating a catamorphism Part 2: Catamorphism examples Catamorphism example: File system domain Catamorphism example: Product domain Part 3: Introducing folds A flaw in our catamorphism implementation Introducing fold Problems with fold Using functions as accumulators Introducing foldback Rules for creating a fold Part 4: Understanding folds Iteration vs.Understanding Folds
https://fsharpforfunandprofit.com/posts/recursive-types-and-folds-2b/
Sun, 23 Aug 2015 00:00:00 +0000https://fsharpforfunandprofit.com/posts/recursive-types-and-folds-2b/This post is the fourth in a series.
In the previous post, I introduced “folds”, a way of creating top-down iterative functions for recursive types.
In this post, we’ll spend some time understanding folds in more detail.
Series contents Here’s the contents of this series:
Part 1: Introduction to recursive types and catamorphisms A simple recursive type Parameterize all the things Introducing catamorphisms Benefits of catamorphisms Rules for creating a catamorphism Part 2: Catamorphism examples Catamorphism example: File system domain Catamorphism example: Product domain Part 3: Introducing folds A flaw in our catamorphism implementation Introducing fold Problems with fold Using functions as accumulators Introducing foldback Rules for creating a fold Part 4: Understanding folds Iteration vs.Introducing Folds
https://fsharpforfunandprofit.com/posts/recursive-types-and-folds-2/
Sat, 22 Aug 2015 00:00:00 +0000https://fsharpforfunandprofit.com/posts/recursive-types-and-folds-2/This post is the third in a series.
In the first post, I introduced “catamorphisms”, a way of creating functions for recursive types, and in the second post, we created a few catamorphism implementations.
But at the end of the previous post, I noted that all the catamorphism implementations so far have had a potentially serious flaw.
In this post, we’ll look at the flaw and how to work around it, and in the process look at folds, tail-recursion and the difference between “left fold” and “right fold”.Catamorphism examples
https://fsharpforfunandprofit.com/posts/recursive-types-and-folds-1b/
Fri, 21 Aug 2015 00:00:00 +0000https://fsharpforfunandprofit.com/posts/recursive-types-and-folds-1b/This post is the second in a series.
In the previous post, I introduced “catamorphisms”, a way of creating functions for recursive types, and listed some rules which can be used to implement them mechanically. In this post, we’ll use these rules to implement catamorphisms for some other domains.
Series contents Here’s the contents of this series:
Part 1: Introduction to recursive types and catamorphisms A simple recursive type Parameterize all the things Introducing catamorphisms Benefits of catamorphisms Rules for creating a catamorphism Part 2: Catamorphism examples Catamorphism example: File system domain Catamorphism example: Product domain Part 3: Introducing folds A flaw in our catamorphism implementation Introducing fold Problems with fold Using functions as accumulators Introducing foldback Rules for creating a fold Part 4: Understanding folds Iteration vs.Introduction to recursive types
https://fsharpforfunandprofit.com/posts/recursive-types-and-folds/
Thu, 20 Aug 2015 00:00:00 +0000https://fsharpforfunandprofit.com/posts/recursive-types-and-folds/In this series, we’ll look at recursive types and how to use them, and on the way, we’ll look at catamorphisms, tail recursion, the difference between left and right folds, and more.
Series contents Here’s the contents of this series:
Part 1: Introduction to recursive types and catamorphisms A simple recursive type Parameterize all the things Introducing catamorphisms Benefits of catamorphisms Rules for creating a catamorphism Part 2: Catamorphism examples Catamorphism example: File system domain Catamorphism example: Product domain Part 3: Introducing folds A flaw in our catamorphism implementation Introducing fold Problems with fold Using functions as accumulators Introducing foldback Rules for creating a fold Part 4: Understanding folds Iteration vs.Working with non-monoids
https://fsharpforfunandprofit.com/posts/monoids-part3/
Fri, 25 Oct 2013 00:00:00 +0000https://fsharpforfunandprofit.com/posts/monoids-part3/In the previous posts in this series, we only dealt with things that were proper monoids.
But what if the thing you want to work with is not a monoid? What then? Well, in this post, I’ll give you some tips on converting almost anything into a monoid.
In the process, we’ll be introduced to a number of important and common functional design idioms, such as preferring lists rather than singletons, and using the option type at every opportunity.Monoids in practice
https://fsharpforfunandprofit.com/posts/monoids-part2/
Thu, 24 Oct 2013 00:00:00 +0000https://fsharpforfunandprofit.com/posts/monoids-part2/In the previous post, we looked at the definition of a monoid. In this post, we’ll see how to implement some monoids.
First, let’s revisit the definition:
You start with a bunch of things, and some way of combining them two at a time. Rule 1 (Closure): The result of combining two things is always another one of the things. Rule 2 (Associativity): When combining more than two things, which pairwise combination you do first doesn’t matter.Monoids without tears
https://fsharpforfunandprofit.com/posts/monoids-without-tears/
Wed, 23 Oct 2013 00:00:00 +0000https://fsharpforfunandprofit.com/posts/monoids-without-tears/If you are coming from an OO background, one of the more challenging aspects of learning functional programming is the lack of obvious design patterns. There are plenty of idioms such as partial application, and error handling techniques, but no apparent patterns in the GoF sense.
In this post, we’ll look at a very common “pattern” known as a monoid. Monoids are not really a design pattern; more an approach to working with many different types of values in a common way.Match expressions
https://fsharpforfunandprofit.com/posts/match-expression/
Thu, 28 Jun 2012 00:00:00 +0000https://fsharpforfunandprofit.com/posts/match-expression/Pattern matching is ubiquitous in F#. It is used for binding values to expressions with let, and in function parameters, and for branching using the match..with syntax.
We have briefly covered binding values to expressions in a post in the “why use F#?” series, and it will be covered many times as we investigate types.
So in this post, we’ll cover the match..with syntax and its use for control flow.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?