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

14 lines
465 B
Haskell

{-# LANGUAGE DeriveFunctor #-}
import Control.Comonad
import Data.InfList (InfList (..))
import qualified Data.InfList as Inf
data Cycle a = Cycle Int a a (InfList a) deriving Functor
view (Cycle n _ x r) = Inf.take n (x ::: r)
fromList [] = let a = a in Cycle 0 a a (Inf.repeat a)
-- zero cycle length ensures that elements of the empty cycle will never be accessed
fromList lst = let x:::r = Inf.cycle lst
in Cycle (length lst) (last lst) x r