Last active
August 29, 2015 14:08
-
-
Save raine/75ae368148a556e0fc0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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