Curried Thoughts 🍯 Point-Free Ramblings 🍯 Unapplied Arguments

Ziyang Liu's blog, mostly about Haskell


  • Home

  • Archives

Solving the "Beautiful Bridges" Problem, Algebraically

07/31/2019
      29 mins

Beautiful Bridges is one of the eleven problems in ACM ICPC World Finals 2019. The problem set can be found here. In this post, we are going to solve this problem in two different ways, both of which involve specifying the problem as a relational hylomorphism, and extracting, via algebraic reasoning, an efficient functional program that conforms to the specification.

Read more »

A Simple Counter Example of Joint Functoriality

07/18/2019
      2 mins

A bifunctor is a functor whose source is a product category. In Haskell, bifunctors have the bimap operation:

Read more »

Efficient Concatenation and Inspection

06/28/2019
      12 mins

Functional programmers know all too well that left-associated concatenation of cons-lists can be a source of inefficiency. For example, if we perform a left-associated concatenation of n singleton cons-lists, it would take O(n2) to force the entire result list, and O(n) just to force the first element. To illustrate the problem, recall that head and ++ are defined as

Read more »

A Quick-and-Dirty Explanation of MonadFix

01/15/2019
      9 mins

This post aims to give a quick-and-dirty explanation of MonadFix. It is quick and dirty because MonadFix is complex and weird stuff (or at least it seems so), and I do not think I am able to present a comprehensive discussion. Instead, this post mainly consists of a simple example that hopefully can give one a rough idea of what MonadFix does and where it might be useful.

Read more »

Defunctionalization for Haskell Type Families

01/08/2019
      11 mins

The first language I used to do purely functional programming is Scala, which I learned when I was at Facebook, and I absolutely loved it. Not long afterwards I started to learn Haskell, and sure enough, Scala quickly became the second favorite. Haskell is simply too beautiful. This motivated me to join Formation in April last year. Being paid to write Haskell on a daily basis feels like a dream, in the sense that my work is now my hobby. As a result, I’ll start to write more Haskell posts here than Scala ones. I still like Scala but I haven’t touched it in a while and I’m fairly rusty at this point.

Read more »

Free Monoids and Free Monads, Free of Category Theory

12/27/2017
      13 mins

Free monoids and free monads are important concepts to understand in functional programming. A free monoid on type A is just List[A]. List is the single most widely used data structure in functional programming and everyone knows what a list is. But not everyone knows why lists are called free monoids, and what is so “free” about it. And it pays to do so, because understanding what free monoids are about will make the concept of free monads much more comprehensible.

Read more »

Recursion Schemes in Scala - An Absolutely Elementary Introduction

11/13/2017
      23 mins

When writing any non-trivial amount of code, we usually factor out the common logic or patterns to improve modularity, facilitate code reuse and avoid repeating ourselves. Recursion schemes is just about that. With recursion schemes you can factor out the recursion patterns in your code, so that you don’t need to write the same types of recursion over and over again. This may sound a little magical to those who aren’t familiar with recursion schemes, but it really isn’t.

Read more »

Stream, Laziness and Stack Safety

09/30/2017
      7 mins

This post contains some notes on chapter 5 (Strictness and laziness) of the Functional Programming in Scala book (a.k.a The Red Book). The chapter covers the basics of non-strict and lazy evaluation in Scala, as well as an implementation of Stream, a lazy version of List.

Read more »

The fix Combinator in Scalaz

08/28/2017
      4 mins

Fixed-point combinator is a really cool thing. It allows us to encode recursion in lambda calculus, which doesn’t have built-in support for recursion. Computerphile recently made a video featuring professor Graham Hutton, author of the book “Programming in Haskell”, giving a brief introduction to the Y combinator.

Read more »

How Trampoline Works in Scala

08/24/2017
      14 mins

Trampoline is a way to make non-tail recursive functions stack-safe. Its Scala implementation is explained by Rúnar Bjarnason in his paper, Stackless Scala With Free Monads, and his book, Functional Programming in Scala. Rúnar’s (old) blog also has a post illustrating the idea. In this post I’d like to apply this technique on a few simple, concrete examples, and show step-by-step how it works on these examples and why it is able to make them stack-safe.

Read more »
1 2 3
Ziyang Liu

Ziyang Liu

22 posts
GitHub
© 2017 - 2025 Ziyang Liu
Powered by Jekyll
Theme - NexT.Mist