module C9 where import System.Random import Debug.Trace -- fibb 25 fibb n = case n of 0 -> 1 1 -> 1 _ -> fibb (n - 1) + fibb (n - 2) -- take fibbF 30 fibbF = 1 : 1 : zipWith (+) fibbF (tail fibbF) -- x - ultimul element din sir -- y - penultimul element din sir fibbT n = fibbTR 1 1 n where fibbTR x y n = trace (concat $ map ((++" ").show) [n,x,y]) $ if n == 0 then x else fibbTR (x + y) x (n - 1) fibbM n = fibbMHelp n [] !! n where fibbMHelp n fibbs = case n of 0 -> [1] 1 -> [1] _ -> fibbs ++ [fibbs !! (n - 1) + fibbs !! (n - 2)] sieve (p:ns) = p : sieve (filter ((/=0) . (flip mod) p) ns) primes = sieve [2..] check = checkPrimes 30 500 500 $ mkStdGen 145 checkPrimes n limit increase gen = let {(randA, gen2) = next gen; rand = randA `mod` limit} in do putStrLn $ "[" ++ show n ++ "] computing " ++ show rand ++ "th prime." let prime = primes !! rand in putStrLn $ "result: " ++ show prime putStrLn $ take n $ repeat '#' if n > 0 then checkPrimes (n - 1) (limit + increase) increase gen2 else return "Done" --testTrace = trace "this" 5 testTrace = let n = 10 in trace ("this: " ++ show n) (fibbT n) -- https://en.wikibooks.org/wiki/Haskell/Debugging -- https://wiki.haskell.org/Debugging -- Maybe, fmap l = [Nothing, Just 2, Nothing, Just 3] l1 = fmap (fmap (+1)) $ l l2 = filter (/= Nothing) l transform l x = case x of Nothing -> l Just h -> h : l lp = foldl transform [] l -- Monad IO f1 = do l <- getLine; putStrLn l; return (l ++ "a") f2 = do putStr "Enter line 1: " l1 <- getLine putStrLn $ "Line 1 is: " ++ l1 putStr "Enter line 2: " l2 <- getLine putStrLn $ "Line 2 is: " ++ l2 return (l1 ++ l2) -- :t f2