import Data.Char factorial :: Int -> Int factorial 0 = 1 factorial n = n * factorial (n - 1) na _ _ = 1 na2 :: Double -> Int -> Double _ `na2` 0 = 1 z `na2` n = if n < 0 then error "zaporny arg" else z * (z `na` (n-1)) x `na3` n = if n < 0 then error "zaporny arg" else x `na` n -- dfct :: Int -> Int dfct1 0 = 1 dfct1 1 = 1 dfct1 n = n * dfct1 (n - 2) dfct2 n = if (n == 0 || n == 1) then 1 else n * dfct2 (n - 2) -- [1..10], [20..], [3,6..40], [3,6..] dfct3 n = if even n then product [2,4..n] else product [1,3..n] dfct4 n = product [n,n-2..1] posledni_naive s = s !! (length s - 1) posledni :: [a] -> a posledni [] = error "prazdny zoznam" posledni (x:[]) = x posledni (_:s ) = posledni s zacatek :: [a] -> [a] zacatek [] = error "prazdny zoznam" zacatek [x] = [] zacatek (x:s) = x : zacatek s -- zacatek [1,2,3] ~> zacatek (1:[2,3]) ~> 1 : zacatek [2,3] ~> -- ~> 1 : zacatek (2:[3]) ~> 1 : 2 : zacatek [3] ~> 1 : 2 : [] ~> [1,2] -- f :: a -> (a -> (Int -> [a])) -- ((f "x") "") 4 -- map f [x1, x2, x3] ~> [f x1, f x2, f x3] toUpperStr :: String -> String toUpperStr s = map toUpper s toUpperStr2 :: [Char] -> [Char] toUpperStr2 = map toUpper s2m2 :: Int -> [Int] s2m2 n = map (^2) [0..n] -- prefix: (+) 2 3, infix: 2 + 3 -- operatorove sekcie: lava (2+), prava (+3) odmocniny s = map sqrt ( filter (>0) s) odmocniny2 = map sqrt . filter (>0) -- t x = f (g x) -- t x = (f . g) x -- t = f . g -- pomenujeme zip_x, aby sme nekolidovali s preddefinovanym zip zip_x [] _ = [] zip_x _ [] = [] zip_x (x:s) (y:t) = (x, y) : zip s t my_zip3 (x:s) (y:t) (z:u) = (x,y,z) : my_zip3 s t u my_zip3 _ _ _ = [] my_unzip3 [] = ([], [], []) my_unzip3 ((x,y,z):s) = (x:u, y:v, z:w) where (u,v,w) = my_unzip3 s add_seq s = zipWith (+) s [1..] {- dlhy komentar na viac riadkov - asd - asd - asd sa d -}