Why does this code using shadowing `let` bindings hang?
Asked Answered
D

1

8

Running this code:

j = let x = 4
    in let x = x * x
       in x

in the interpreter:

ghci> j
... no response ...

hangs with very little CPU utilization. Why is this? I expected j = 16.

Driscoll answered 10/8, 2012 at 13:13 Comment(0)
D
16

According to the Haskell report, section 3.12:

Let expressions have the general form let { d1 ; … ; dn } in e, and introduce a nested, lexically-scoped, mutually-recursive list of declarations (let is often called letrec in other languages). The scope of the declarations is the expression e and the right hand side of the declarations.

(emphasis mine)

So in the second let, where x = x * x, all xs refer to the same binding, none refer to the outer x = 4 binding.

Driscoll answered 10/8, 2012 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.