RosettaCodeData/Task/Elementary-cellular-automaton/Haskell/elementary-cellular-automaton-5.hs
2023-07-01 13:44:08 -04:00

8 lines
268 B
Haskell

instance Comonad Cycle where
extract (Cycle _ _ x _) = x
duplicate x@(Cycle n _ _ _) = fromList $ take n $ iterate shift x
where shift (Cycle n _ x (r:::rs)) = Cycle n x r rs
step rule (Cycle _ l x (r:::_)) = rule l x r
runCA rule = iterate (=>> step rule)