[Haskell-cafe] How do I avoid stack overflows?
DavidA
polyomino at f2s.com
Thu Mar 15 16:18:38 EDT 2007
Hi.
I'm trying to write some code which involves lots of matrix multiplications,
but whenever I do too many, I get stack overflows (in both GHCi 6.4.2, and
Hugs May 2006). The following code exhibits the problem.
import List (transpose)
u <.> v = sum $ zipWith (*) u v
a <<*>> b = multMx a (transpose b)
where
multMx [] _ = []
multMx (u:us) bT = map (u <.>) bT : multMx us bT
id3 = [[1,0,0],[0,1,0],[0,0,1]]
test = iterate (<<*>> id3) id3 !! 1000000
I tried to fix the problem using seq, as follows:
iterate' f x = x : seq x' (iterate' f x') where x' = f x
test' = iterate' (<<*>> id3) id3 !! 1000000
However, in both cases, the code causes stack overflows in both interpreters.
(It sometimes kills GHCi, which I guess is a bug.)
Any ideas?
Thanks.
More information about the Haskell-Cafe
mailing list