Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Lab2 where
- fact :: Integer -> Integer
- fact x
- | x < 0 = error "negative"
- | x == 0 = 1
- | otherwise = x * fact(x - 1)
- max2 :: Integer -> Integer -> Integer
- x `max2` y
- | x > y = x
- | otherwise = y
- roots :: Double -> Double -> Double -> (Double, Double)
- roots a b c
- | disc < 0 = error "no roots"
- | disc == 0 = (-b/(aa), -b/(aa))
- | otherwise = ((-b - sqrt(disc) )/ (aa),(-b + sqrt(disc)) / (aa))
- where disc = b^2 - 4 * a * c
- aa = 2 * a
- fun :: [(Double, Double)] -> [Double]
- fun xs = [avg s l | (s, l) <- xs]
- where avg s l = s / l
- scylindr :: Double -> Double -> Double
- scylindr r h =
- let
- osns = pi * r^2
- boks = 2 * pi * r * h
- in
- 2 * osns + boks
- fun1 :: Int -> Int -> [Int]
- fun1 0 n = []
- fun1 k n = (n - k + 1):fun1 (k - 1) n
- isOdd:: Int -> Bool
- isOdd x = mod x 2 /= 0
- fun2Helper :: Int -> [Int]
- fun2Helper 0 = []
- fun2Helper n = if isOdd n then n:fun2Helper(n-1) else fun2Helper(n-1)
- fun2 :: Int -> [Int]
- fun2 n = reverse(fun2Helper n)
- fun3Helper :: Int -> [Int]
- fun3Helper 0 = []
- fun3Helper n = if isOdd n then fun3Helper(n-1) else n:fun3Helper(n-1)
- fun3 :: Int -> [Int]
- fun3 n = reverse(fun3Helper n)
- fun4 :: Int -> Int -> [Int]
- fun4 0 n = []
- fun4 k n = (n - k + 1)*(n - k + 1):fun4 (k - 1) n
- fac :: (Integral a) => a -> a
- fac 0 = 1
- fac n = n * fac(n - 1)
- fun5 :: Int -> [Int]
- fun5 n = [fac x | x <- [1 .. n]]
- fun6H :: Int -> [Int]
- fun6H (-1) = []
- fun6H n = 2^n :fun6H (n - 1)
- fun6 :: Int -> [Int]
- fun6 n = reverse(fun6H n)
- treug :: Int -> Int
- treug 1 = 1
- treug n = n + treug (n-1)
- piram :: Int -> Int
- piram 1 = 1
- piram n = treug n + piram (n-1)
- fun8 :: Int -> [Int]
- fun8 n = [piram x | x <- [1 .. n]]
- avg' :: [Double] -> Double -> Double -> Double
- avg' [] n s = s/n
- avg' (x:xs) n s = avg' xs (n+1) (s+x)
- avg :: [Double] -> Double
- avg x = avg' x 0 0
- removeItem :: Int -> [Int] -> [Int]
- removeItem n (x:xs) = if n == 1 then xs else x : removeItem (n-1) xs
- addElem :: [Int] -> [Int] -> [Int]
- addElem [] [] = []
- addElem (x:xs) [] = x:xs
- addElem [] (y:ys) = y:ys
- addElem (x:xs) (y:ys) = (x + y) : addElem xs ys
- swapEvenOdd :: [Int] -> [Int]
- swapEvenOdd [] = []
- swapEvenOdd [x] = [x]
- swapEvenOdd (x:y:xs) = y : x : swapEvenOdd xs
- twopow :: Int -> Int
- twopow n | (n == 1) = 2
- | (even n) = w * w
- | otherwise = (w * w) + (w * w)
- where w = twopow (n `div` 2)
- isOdd:: Int -> Bool
- isOdd x = mod x 2 /= 0
- removeOdd :: [Int] -> [Int]
- removeOdd [] = []
- removeOdd (x:xs) = if isOdd x then removeOdd xs else x : removeOdd xs
- removeEmpty :: [String] -> [String]
- removeEmpty [] = []
- removeEmpty (x:xs) = if x == "" then removeEmpty xs else x : removeEmpty xs
- countTrue :: [Bool] -> Integer
- countTrue [] = 0
- countTrue (x:xs) = if x == True then 1 + countTrue xs else countTrue xs
- makePositive :: [Int] -> [Int]
- makePositive [] = []
- makePositive (x:xs) = if x < 0 then (-x) : makePositive xs else x : makePositive xs
- delete :: Char -> String -> String
- delete _ "" = ""
- delete x (y:ys) = if x == y then delete x ys else y : delete x ys
- substitute :: Char -> Char -> String -> String
- substitute _ _ "" = ""
- substitute x y (z:zs) = if x == z then y : substitute x y zs else z : substitute x y zs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement