Why recursive `let` make space effcient?

It’s easiest to understand with pictures:

  • The first version

    repeat x = x : repeat x
    

    creates a chain of (:) constructors ending in a thunk which will replace itself with more constructors as you demand them. Thus, O(n) space.

    a chain

  • The second version

    repeat x = let xs = x : xs in xs
    

    uses let to “tie the knot”, creating a single (:) constructor which refers to itself.

    a loop

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)