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…

Programming with Bananas in Haskell (Versus OCaml)

In the last post I talked about catamorphisms in OCaml.  In this post I compare that with Haskell.  We’ll see that Haskell gives you more options when implementing catamorphisms.  You don’t need to know Haskell to understand this post, I’ll introduce the required Haskell syntax along the way. Catamorphisms in…

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: λx.λy.x+y (*A function expression*) λx.λy.x+y 3 4 (*A function application*) In OCaml,…

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.: 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…