-- Demonstrates State and Memoization monads on various examples -- by Taylor Campbell, 3/2008 module Main where import EditDistanceSM import Fibonacci import ParserST main :: IO () main = do mainFib mainEdit mainParse mainFib :: IO () mainFib = do putStr "Enter a natural number: " line <- getLine let n = read line putStrLn ("Fibonacci(" ++ show n ++ "): " ++ show (memoFib n)) mainEdit :: IO () mainEdit = do putStrLn "Enter two strings on separate lines:" a <- getLine b <- getLine putStrLn ("Edit distance: " ++ show (memoEditDistance a b)) mainParse :: IO () mainParse = do putStr "Enter a tree to parse: " input <- getLine putStrLn (maybe "Parse error!" show (parse testParser input)) data SimpleTree = Leaf | Branch SimpleTree SimpleTree deriving Show testParser :: Parser SimpleTree testParser = simpleTree simpleTree :: Parser SimpleTree simpleTree = simpleLeaf ! simpleBranch simpleLeaf :: Parser SimpleTree simpleLeaf = (char ? (== '*')) >-> (\ _ -> Leaf) simpleBranch :: Parser SimpleTree simpleBranch = ((char ? (== '(')) -# simpleTree # simpleTree #- (char ? (== ')'))) >-> (\ (left, right) -> Branch left right)