Category: Functional Programming
-
Recursion: An Indispensable Tool For Every Functional Programmer [Example With Insertion Sort]
This is the first of a series of articles that illustrates functional programming (FP) concepts to imperative programmers. As you go through these articles with code examples in Haskell (one of the most popular FP languages), you gain the grounding for picking up any FP languages quickly. Why should you care about FP? See this…
-
Dependent types: formal definition and examples
Dependent types have been gaining a lot of attention among programmers. Some would say it’s “the next thing” in programming. Why is it gaining popularity? In this post, I hope to show some of the advantages of dependent types and get you started on programming in dependent types. To understand dependent types, let’s take a…
-
Programming with Envelopes in OCaml and Haskell
In this post, I continue going through the famous paper “Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire” by Meijer et al. They show that we can treat recursions as separate higher order functions known as recursion schemes. Each of the four things in the paper title corresponds to a type of recursion scheme. …
-
Zip and More in Haskell
In the last post we talked about using unfolds to zip and more in OCaml. In this post we write the equivalent functions in Haskell. Zip is in the standard library in Haskell. It’s actually so elegant that there isn’t much point using unfolds instead. To zip two lists, simply use the zip function: Prelude>…
-
Using Unfolds to Zip and More in OCaml
This post picks up from where we left off here, where I explained anamorphisms. In the last post we looked at iterating with anamorphisms. Here we look at another example of applying unfolds: zip. Zip Zip may not be the most common example of anamorphisms but I want to cover it because it’s included in…
-
Using Unfolds to Iterate in Haskell and OCaml
This post picks up from where we left off here, in which I explained anamorphisms. Here we look at another example of applying unfolds: iterate. Iterate Iterate is one of the most common uses of unfold. The definition of the iterate function is: iterate f x = Cons (x, iterate f (f x)) E.g., let…
-
Programming with Lenses in Haskell and OCaml
In this post, I continue going through the famous paper “Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire” by Meijer et al. They show that we can treat recursions as separate higher order functions known as recursion schemes. Each of the four things in the paper title corresponds to a type of recursion scheme. …
-
Strings and Tries; Haskell Versus OCaml
While doing my OCaml MOOC exercise I came across a trie implementation for looking up words. I find that the way OCaml treats strings is very different from the way Haskell does it. It leads to different ways to implement the trie in the two languages. Strings and Tries in Haskell Haskell is a pure…
-
Eliminating Run Time Errors In OCaml and Haskell
Run Time Versus Compile Time Error Run time errors are those that happen when the program runs, as opposed to compile time errors which happen when the program compiles. In a sense, having compile time errors is no big deal, because the errors guarantee that the ill-defined program won’t be running. The programmer can fix…
-
Folding Nonempty Lists in OCaml and Haskell
In the last post, I talked about catamorphisms in Haskell. Specifically, the advantages of the foldr1 or foldl1 option in Haskell. Namely, (1) not needing to specify the base case and (2) preserving polymorphisms in all applicable types. In this post, I will show another way to fold nonempty list without using foldr1 or foldl1…