Skip to content

Instantly share code, notes, and snippets.

@glguy
Created July 29, 2014 02:22
Show Gist options
  • Save glguy/dca0073ee2c7097e68c6 to your computer and use it in GitHub Desktop.
Save glguy/dca0073ee2c7097e68c6 to your computer and use it in GitHub Desktop.
import Data.Array
import Control.Comonad
data Pointer a = Pointer !(Array Int a) !Int
deriving (Show)
instance Functor Pointer where
fmap f (Pointer a i) = Pointer (fmap f a) i
instance Comonad Pointer where
extract (Pointer a i) = a ! i
extend f (Pointer a i) = Pointer a' i
where
a' = listArray (bounds a)
[f (Pointer a j) | j <- range (bounds a)]
next :: Pointer a -> Maybe (Pointer a)
next (Pointer a i)
| i < hi = Just (Pointer a (i+1))
| otherwise = Nothing
where
(_,hi) = bounds a
prev :: Pointer a -> Maybe (Pointer a)
prev (Pointer a i)
| lo < i = Just (Pointer a (i-1))
| otherwise = Nothing
where
(lo,_) = bounds a
rule16 :: Pointer Bool -> Bool
rule16 p = case prev p of
Nothing -> False
Just p1 -> extract p1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment