Trivia at term-level

In this section, we look at lazy patterns, using the magic hash, controlling inlining and using rewrite rules. These are small things used rarely in applications, but nevertheless are very convenient where applicable.

We'll start with lazy patterns. Where strict pattern annotations use bangs and mean "Evaluate this argument to WHNF immediately," lazy pattern annotations use tildes and imply "Don't even bother pattern-matching unless a binding is really requested." So this errors:

> let f (a,b) = 5
> f undefined
*** Exception: Prelude.undefined

But with a lazy pattern match, we are all okay:

> let f ~(a,b) = 5
> f undefined
5

A more realistic use case for lazy patterns is the classic server-client setting. The client makes requests ...

Get Haskell High Performance Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.