Created
August 14, 2011 11:48
-
-
Save akiradeveloper/1144821 to your computer and use it in GitHub Desktop.
Siritori solver in Haskell.
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
-- Still under development but I will show you my code. | |
-- Haskell is beautiful. | |
import Control.Monad | |
import Data.List | |
type Elem = (Int, Int) | |
-- Either Monad is not defined yet in ghc of 6.x | |
instance Monad (Either a) where | |
Right a >>= f = f a | |
Left a >>= _ = Left a | |
return = Right | |
getEither :: Either a a -> a | |
getEither (Right a) = a | |
getEither (Left a) = a | |
-- Generating all the candidate array which is evaluated lazily. | |
allCombination = f :: [a] -> [[a]] | |
where | |
g = map permutations . h | |
f = concat . g | |
h = filterM (\x -> [True, False]) :: [a] -> [[a]] | |
-- foldM :: (a->b->m a)->a->[b]->m a | |
detectChain :: [Elem] -> [Elem] | |
detectChain = getEither . foldM f [] | |
where | |
f [] x = Right [x] | |
f ((a,b):xs) (c,d) | |
| b == c = Right $ (c,d):(a,b):xs | |
| otherwise = Left $ (a,b):xs | |
-- returns (the max chain, and its length) | |
solve :: [Elem] -> [Elem] | |
solve = undefined | |
main = do | |
content <- getContents | |
let elements = read content | |
print $ solve elements |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment