Skip to content

Instantly share code, notes, and snippets.

@raine
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raine/75ae368148a556e0fc0d to your computer and use it in GitHub Desktop.
Save raine/75ae368148a556e0fc0d to your computer and use it in GitHub Desktop.
module Cycle where
import Data.List (elemIndex)
import Data.Functor ((<$>))
data Direction = L | R deriving (Show)
cycleList :: (Eq a) => Direction -> [a] -> a -> Maybe a
cycleList _ [] x = Nothing
cycleList L xs x = ((rotate R xs) !!) <$> elemIndex x xs
cycleList R xs x = ((rotate L xs) !!) <$> elemIndex x xs
rotate :: Direction -> [a] -> [a]
rotate L (x:xs) = xs ++ [x]
rotate R xs = last xs : init xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment