Search code examples
listhaskellrecursioninfinitelet

Let recursiveness in Haskell


Could someone explain the behaviour of this little piece of code with the following input: [[1,2],[3,4]]?

infiniteList ls = let v = ls ++ v
                  in concat v

The result is an infinite list but I don't realise why. I've understood that the trick is in:

ls ++ v

But how exactly work the let since we do not define the initial value for the variable v?


Solution

  • To evaluate v, just replace it with its definition:

    v = ls ++ v
      = ls ++ ls ++ v
      = ls ++ ls ++ ls ++ v
      = ls ++ ls ++ ls ++ ls ++ v
      = ls ++ ls ++ ls ++ ls ++ ls ++ v
      = ls ++ ls ++ ls ++ ls ++ ls ++ ls ++ v
    

    Clearly this will never end.