[Haskell-cafe] Re: Optimizing cellular automata evaluation (round 2)
Mirko Rahn
rahn at ira.uka.de
Mon Dec 3 05:44:35 EST 2007
> http://hpaste.org/4151#a1
It is interesting, that the naive implementation
import Data.List (tails)
neighbours :: Int -> [a] -> [[a]]
neighbours w = rotL . take w . map (take 3) . tails . cycle
rotL :: [a] -> [a]
rotL xs = last xs : init xs
type Rule a = [a] -> a
step :: Int -> Rule a -> [a] -> [a]
step w f = map f . neighbours w
rule110 :: Rule Char
rule110 " " = ' '
rule110 "X " = ' '
rule110 "XXX" = ' '
rule110 _ = 'X'
main = let f = step 149 rule110
init = replicate 148 ' ' ++ "X"
in mapM_ putStrLn $ take 10000 $ iterate f init
is only 3 times slower than your quite complex, hard to follow and hard
to debug implementation.
As always, I prefer to write most code in Haskell, quick, easy, nice,
reasonable fast, ... If speed matters, I switch to some lower level
language, as you did staying inside Haskell.
/BR, Mirko Rahn
More information about the Haskell-Cafe
mailing list