---
title: "Thunks in Haskell"
description: "Understanding lazy evaluation and thunks in Haskell. Practical notes and code examples on Thunks in Haskell by Hemanth HM."
url: "https://h3manth.com/scribe/thunks-in-haskell/"
canonical_url: "https://h3manth.com/scribe/thunks-in-haskell/"
markdown_url: "https://h3manth.com/scribe/blog/thunks-in-haskell.md"
publishDate: "2014-06-14T00:00:00.000Z"
author: "Hemanth HM"
category: "haskell"
tags: ["haskell","functional"]
word_count: 98
reading_time_minutes: 1
x_markdown_tokens: 261
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Thunks in Haskell

> Understanding lazy evaluation and thunks in Haskell. Practical notes and code examples on Thunks in Haskell by Hemanth HM.

Understanding [thunks](https://en.wikipedia.org/wiki/Thunk_%28functional_programming%29) with Fibonacci:

```haskell
let fib = 0 : 1 : zipWith (+) fib (tail fib)
take 10 fib  -- [0,1,1,2,3,5,8,13,21,34]
```

## What are Thunks?

In Haskell, `(4+2,7)` is stored as `(_,7)` where `_` is the thunk - a value not yet computed.

```haskell
let x = (4+2,7)
:print x  -- ((_t1::Integer),7)

snd x  -- 7
:print x  -- ((_t2::Integer),7)  -- still not evaluated!

fst x  -- 6
x  -- (6,7)  -- now it's evaluated
```

Thunks are only evaluated when needed. Partial evaluation creates more thunks - values are computed lazily on demand.

From [haskell-rascal](https://github.com/hemanth/haskell-rascal).
