softshipper
softshipper

Reputation: 34071

Why do I get an exception loop

I tried following expression in prelude:

let x = x in x

and I've got following exception

Exception: <<loop>>

Why is the expression recursive?

Upvotes: 1

Views: 178

Answers (1)

Alec
Alec

Reputation: 32309

let bindings in Haskell are (mutually) recursive, meaning that you can refer to any of the defined variables/functions (things to the left of the = signs) in any of their definitions (the stuff to the right of the = sign). For the case where you have arguments (functions), this is pretty much always the intuitive expected behaviour.

let fact n = if n == 0 then 1 else n * fact (n - 1) in fact 5

In the above, you probably are not surprised that fact (n - 1) can be used in the definition of fact n. In your example, you are using x in its own definition.

When Haskell tries to evaluate let x = x in x, it keeps trying to expand x (into the RHS x) hence the loop.

Upvotes: 7

Related Questions