--
-- Mon Mar 6 09:11:06 CST 2017
--
import Parsing
import Data.Char
--2.1
data RPS = Rock | Paper | Scissors
deriving (Eq, Show)
-- 2.2
rps :: RPS -> RPS -> Integer
-- 2.3
rps Rock Rock = 0
rps Paper Paper = 0
rps Scissors Scissors = 0
rps Rock Paper = 2
rps Rock Scissors = 1
rps Paper Rock = 1
rps Paper Scissors = 2
rps Scissors Rock = 2
rps Scissors Paper = 1
-- 2.4
rps' :: RPS -> RPS -> Int
rps' p1 p2 | p1 == p2 = 0
| p1 == Rock && p2 == Scissors = 1
-- f :: Int -> Int -> String
-- f a b = if (a > b) then show a else show b
-- Q4.
-- First interpretation: "Bbbc" -> [("Bb","bc")]
myParser = sat (isUpper) >>= \c ->
char (toLower c) >>= \d ->
return [c,d]
-- Second interpretation: "Bbbc" -> [("Bbbc","")]
myParser2 = sat (isUpper) >>= \c ->
char (toLower c) >>= \d ->
many item >>= \s ->
return ([c,d]++s)
{-
--- 6.3
f :: a -> a
x :: a
f ( f (f x)) --> tripler :: (a -> a) -> a -> a
-}
data Tree a b = Branch b (Tree a b) (Tree a b)
| Leaf a
mytree :: Tree Int String
mytree = Branch "A" (Branch "B" (Leaf (1::Int)) (Leaf (2::Int))) (Leaf (3::Int))
mysubtree :: Tree Int String
mysubtree = Branch "B" (Leaf (1::Int)) (Leaf (2::Int))
mytree2 :: Tree Char Integer
mytree2 = Branch 45 (Leaf 'x') (Leaf 'y')
preorder ::(a -> c) -> (b -> c) -> Tree a b -> [c]
preorder lf bf (Branch v t1 t2) = [bf v] ++ (preorder lf bf t1) ++ (preorder lf bf t2)
preorder lf bf (Leaf v) = [lf v]
postorder ::(a -> c) -> (b -> c) -> Tree a b -> [c]
postorder lf bf (Branch v t1 t2) = (preorder lf bf t1) ++ (preorder lf bf t2) ++ [bf v]
postorder lf bf (Leaf v) = [lf v]
instance (Show a, Show b) => Show (Tree a b) where
show t1 = showhelper "" t1
spacer = " "
showhelper pfx (Branch a st1 st2) = pfx ++ show a ++ "\n" ++
(showhelper (pfx++spacer) st1) ++
(showhelper (pfx++spacer) st2)
showhelper pfx (Leaf a) = pfx ++ show a ++ "\n"