By bananas, I mean banana brackets as in the famous paper “Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire” by Meijer et al. In this post I only focus on bananas, also called catamorphisms. Recursion is core to functional programming. Meijer et al. show that we can treat recursions as separate higher order functions…

## Structural Versus Physical Comparsions in OCaml

The comparison operators in OCaml are polymorphic. That is, you can use them for various data types. Because OCaml is a typed language, like any other operator, you have to apply them to the same type. E.g., comparing a float to an int will give you a type error. OCaml can perform two types of…

## Lexical Scoping in OCaml

Like many other modern languages, OCaml uses lexical (or static) scoping. That is, in OCaml, when your function includes a name that calls a variable, in the function, that variable has the value when the function is defined. The opposite is dynamic scoping, in which the variable has the value when the function is called. …

## Lambda Calculus in OCaml: “fun” and “function”

Lambda is fun! Lambda is certainly fun, but what I mean here is that the λ in lambda calculus is similar to the expression fun in OCaml. Recall that in lambda calculus, we have function expressions and function applications:

1 2 |
λx.λy.x+y (*A function expression*) λx.λy.x+y 3 4 (*A function application*) |

In OCaml, you can express the same with –fun:

1 2 |
fun x y -> x + y (fun x y -> x + y) 3 4 |

Each term separated by a space…

## Interfacing OCaml with PostgreSQL

In the last post I talked about building an OCaml project using Dune. In this post I continue with a more complex project. The project interfaces OCaml and PostgreSQL (a database system) with Caqti (a third-party library that provides type-safe abstraction for interfacing with databases). I loosely follow this tutorial, but instead of using Jbuilder…

## Using Dune to Build an OCaml Project

Dune is a popular OCaml build tool. If you haven’t already, I encourage you to give it a try! Especially if you are building a project that involves more components. See other compiling options here. You can install Dune using OPAM by running (in your system or a particular switch)

1 |
opam install dune |

Writing a Dune File…

## Lazy or Eager? Order of Evaluation in Lambda Calculus and OCaml

Recall in lambda calculus, two items side by side is an application. One applies the left item (the function) to the right item (the input). E.g.:

1 |
f x |

is read as “apply f to x“, in which f and x can be any lambda expressions. Therefore, f and/or x may be expressions that can be evaluated first. …

## Setting Up Merlin in Neovim

My OCaml development environment in Debian has been working well since I set it up. In this post I talk about setting up Merlin in vim and neovim. See this discussion for other well-liked OCaml environment set up. I’ve been using neovim and I like it so I’ll stick with this for now. Setting Up…

## Currying in Lambda Calculus and OCaml

Currying Recall that in lambda calculus, a function can have more than one input, each preceded by a λ symbol. Another way of thinking about more than one input is currying. Currying a function of two inputs turns that function into a function with one input by passing one of the inputs into it. In other…

## Encoding Recursion with the Y Combinator

In this post I’ll go through some exercises and encode some recursive functions with the Y combinator. Encoding with rec Continuing on from my last post, Professor Hutton gave us two exercises in the Y combinator video: Encode loop (the function that just calls itself) with rec. I.e.,

1 |
loop = rec (?) |

Encode the factorial function with rec. I.e.,…