Skip to content

Instantly share code, notes, and snippets.

@Ino4137
Created May 5, 2018 20:31
Show Gist options
  • Select an option

  • Save Ino4137/938703da4ab03e2b2eb5e288bbe3602d to your computer and use it in GitHub Desktop.

Select an option

Save Ino4137/938703da4ab03e2b2eb5e288bbe3602d to your computer and use it in GitHub Desktop.
frc wars
import Control.Monad.Trans.State
import Data.List
frc sequ = fst $ execState (mapM_ check sequ) (Nothing,[])
check :: Eq a => a -> State (Maybe a, [a]) ()
check x = do
(status,prev) <- get
case status of
Just x ->
put (status,prev)
Nothing ->
if x `elem` prev then
put (Just x, prev)
else
put (status, x:prev)
frc2, frc3 :: Eq a => [a] -> Maybe a
frc2 = safeHead . fst . foldr (\x (s,l) ->
if x `elem` l then
(x:s,l)
else (s,x:l)
) ([],[])
where
safeHead [] = Nothing
safeHead (x:_) = Just x
frc3 = f []
where
f _ [] = Nothing
f l (x:xs) = if x `elem` l then Just x else f (x:l) xs
main = print . frc2 . flip take testData2 . read =<< getLine
where
testData2 = cycle "ab"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment