-- interaktivni program "myslim si cislo" test n = do s <- getLine printRec 5 s printRec 0 s = do return () printRec n s = do putStrLn s printRec (n-1) s mysli n = do s <- getLine if read s < n then do putStrLn "vecsi" mysli n else if read s > n then do putStrLn "mensi" mysli n else do putStrLn "spravne" -- mhoisto V3 a b c vypisovat [a b c]: data V3 x = V3 x x x instance (Show x) => Show (V3 x) where show (V3 a b c) = "[" ++ show a ++ " " ++ show b ++ " " ++ show c ++ "]" -- rozsirte integery o +Inf, -Inf, NaN, vyrobte porovnani a vypsani data InfInt = PlusInfty | MinusInfty | Finite Int instance Show (InfInt) where show PlusInfty = "+inf" show MinusInfty = "-inf" show (Finite x) = show x instance Eq (InfInt) where (==) PlusInfty PlusInfty = True (==) MinusInfty MinusInfty = True (==) (Finite a) (Finite b) = a == b (==) _ _ = False instance Ord (InfInt) where MinusInfty <= _ = True _ <= PlusInfty = True (Finite a) <= (Finite b) = a <= b _ <= _ = False -- datovy typ na Z (mod 7) s funkcnim +, -, * data Z7 = Z7 Int deriving Show z7lift2 op (Z7 a) (Z7 b) = Z7 $ op a b `mod` 7 z7lift1 op (Z7 a) = Z7 $ op a `mod` 7 instance Num (Z7) where (+) = z7lift2 (+) (-) = z7lift2 (-) (*) = z7lift2 (*) negate = z7lift1 negate signum = z7lift1 signum fromInteger a = Z7 $ fromInteger a `mod` 7 abs = id -- fold na Tree z minula data Tree a = Nil | Node (Tree a) a (Tree a) deriving Show instance Foldable Tree where foldr _ a Nil = a foldr f a (Node lt c rt) = let r = foldr f a rt cent = f c r l = foldr f cent lt in l sumTree :: Num a => Tree a -> a sumTree = foldl (+) 0 -- map na Tree z minula (Functor, <$>, fmap) instance Functor Tree where fmap f Nil = Nil fmap f (Node l c r) = (Node (fmap f l) (f c) (fmap f r)) -- koho by to zajimalo.... instance Applicative Tree where pure x = Node Nil x Nil Nil <*> _ = Nil _ <*> Nil = Nil (Node l f r) <*> v@(Node _ c _) = Node (l <*> v) (f c) $ Node (f <$> v) (f c) $ r <*> v data OpCounter container t = OpCnt Int (container t) deriving Show countOps = OpCnt 0 instance Functor a => Functor (OpCounter a) where fmap f (OpCnt n a) = OpCnt (n+1) $ fmap f a instance Applicative a => Applicative (OpCounter a) where pure = countOps . pure OpCnt n1 f <*> OpCnt n2 v = OpCnt (n1+n2+1) $ f <*> v opCounterDemo :: OpCounter Tree Int opCounterDemo = (*2) <$> (countOps (Node (Node Nil (+1) Nil) (*2) (Node Nil (+10) Nil)) <*> pure 5)