Created
December 23, 2011 11:02
-
-
Save delihiros/1513883 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
-- 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 | |
myeval :: [Combinator] -> [Combinator] | |
myeval [] = [] | |
myeval (com:coms) = | |
if isempty coms then [com] | |
else | |
case com of | |
S -> do x <- head coms | |
y <- head $ tail coms | |
z <- head $ tail $ tail coms | |
myeval (x : z : y : z : []) | |
K -> do x <- head coms | |
y <- tail coms | |
myeval [x] | |
I -> do x <- coms | |
myeval [x] | |
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