[Haskell-beginners] Finite self-referential list hangs.

tcollin371 tcollin371 at earthlink.net
Sat Mar 17 18:32:24 CET 2012


I am trying to generate a search tree for a logic puzzle using a list that generates itself by using earlier states to derive later states.  The search tree is finite, but when it reaches the end, it hangs and while the list doesn't grow, it doesn't finish.

I've boiled the problem down to a much simpler list as follows:

tryThis :: [Int]
tryThis = 3 : [ x | x <- tryThis' tryThis]
  where
    tryThis' :: [Int] -> [Int]
    tryThis' [] = []
    tryThis' (x : xs) = result' x ++ tryThis' xs
      where
        result' :: Int -> [Int]
        result' 3 = [2, 2, 2]
        result' 2 = [1, 1]
        result' 1 = [0]
        result' 0 = []

When I load this into GHCi and type tryThis, it prints a list of all of the numbers I would expect, then hangs.  Does the self-generating technique not work with finite lists?  Shouldn't tryThis' just generate a final empty list that would terminate this?  How can this be written to successfully terminate?

Thank you for any help.

-TC




More information about the Beginners mailing list