Skip to content

Instantly share code, notes, and snippets.

@delihiros
Created December 23, 2011 13:08
Show Gist options
  • Save delihiros/1514163 to your computer and use it in GitHub Desktop.
Save delihiros/1514163 to your computer and use it in GitHub Desktop.
-- Sxyz -> xz yz
-- Kxy -> x
-- Ix -> x
data Combinator = S | K | I | E
deriving (Show, Eq)
char2ski :: Char -> Combinator
char2ski c = case c of
'S' -> S
'K' -> K
'I' -> I
_ -> E
ski2char :: Combinator -> Char
ski2char c = case c of
S -> 'S'
K -> 'K'
I -> 'I'
_ -> 'E'
s x y z = x z (y z)
k x y = x
i x = x
isempty :: [a] -> Bool
isempty [] = True
isempty _ = False
list :: a -> [a]
list a = [a]
myeval :: [Combinator] -> [Combinator]
myeval [] = []
myeval (com:coms) =
if isempty coms then [com]
else
case com of
S -> do x <- list $ head coms
y <- list $ head $ tail coms
z <- tail $ tail coms
x ++ z ++ y ++ z
K -> list $ head coms
I -> coms
str2ski :: String -> [Combinator]
str2ski [] = []
str2ski (c:cs) = char2ski c : str2ski cs
ski2str :: [Combinator] -> String
ski2str [] = []
ski2str (c:cs) = ski2char c : ski2str cs
main :: IO ()
main = do cs <- getContents
putStrLn $ ski2str $ myeval $ str2ski cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment