lookup' :: (Eq a)=> a->[(a,b)]-> Maybe b lookup' _ [] = Nothing lookup' a ((k,v):xs) |a==k = Just v |otherwise = lookup' a xs lookup2 :: Int -> [(Int,Int)] -> Maybe Int lookup2 k l = case lookup' k l of Nothing -> Nothing Just k2 -> lookup' (k2*2) l lookup3 :: Int -> [(Int,Int)] -> Maybe Int lookup3 k l = case lookup' k l of Nothing -> Nothing Just k2 -> case lookup' (k2*2) l of Nothing -> Nothing Just k3 -> lookup' (k3+k2) l andThen :: Maybe a -> (a -> Maybe b) -> Maybe b andThen Nothing _ = Nothing andThen (Just a) f = f a lookup3' k l = do k2 <- lookup' k l k3 <- lookup' (k2*2) l lookup' (k3+k2) l lookupE :: (Eq a, Show a)=> a->[(a,b)]-> Either String b lookupE x [] = Left ("Chyba " ++ show x ++ " nenalezelno") lookupE a ((k,v):xs) |a==k = Right v |otherwise = lookupE a xs lookup4 :: Int -> [(Int,Int)] -> Either String Int lookup4 k l = do k2 <- lookupE k l k3 <- lookupE (k2*2) l lookupE (k3+k2) l pozdrav = do s <- getLine putStrLn ("ahoj " ++ s) hra n = do putStrLn "Hadaj cislo" s <- getLine let num = read s if num == n then putStrLn "Gratulace ... -_-" else (if num < n then putStrLn "Cislo je vetsi" else putStrLn "Cislo je mensi") >> hra n data State s a = StateChange (s -> (s, a)) instance Functor (State s) where fmap f (StateChange f') = StateChange (\s -> let (a, b) = f' s in (a, f b)) instance Applicative (State s) where pure a = StateChange (\s -> (s, a)) StateChange f <*> StateChange g = StateChange (\s -> let (s', h) = f s (s'', v) = g s' in (s'', h v)) instance Monad (State s) where return = pure StateChange a >>= f = StateChange (\s -> let (s',r) = a s StateChange b = f r in b s') getState = StateChange (\s -> (s, s)) setState ns = StateChange (\_->(ns,())) runState s (StateChange f) = snd (f s) vynasobStav n = do n' <- getState setState (n' * n) vynasobOdDo a b | a > b = return () | otherwise = do vynasobStav a vynasobOdDo (a+1) b faktorial n = do setState 1 vynasobOdDo 1 n getState pyth = do c <- [0..] a <- [1..c] b <- [1..c] if a*a + b*b == c*c then [undefined,undefined] else [] return (a,b,c)