Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- import Data.Char
- main :: IO()
- main = do
- -- 1.
- hasElemets :: [Int] -> Bool
- hasElementsPM [] = False
- hasElements xs = not null xs
- hasElements :: [Int] -> Bool
- hasElementsPM [] = False
- hasElementsPM xs = length xs /= 0
- hasElements :: [Int] -> Bool
- hasElementsPM [] = False
- hasElementsPM xs = xs /= []
- -- 2.
- myLengthRecPM :: [Int] -> Int
- myLengthRecPM [] = 0
- myLengthRecPM (_:xs) = 1 + myLengthRecNonPM xs
- myLengthRecNonPM :: [Int] -> Int
- myLengthRecNonPM [] = 0
- myLengthRecNonPM xs
- | null xs = 0
- | otherwise = 1 + (myLengthRecNonPM $ tail xs)
- -- 3.
- getClosedInterval :: Int -> Int -> [Int]
- getClosedInterval x y = [min x y .. max x y]
- -- 4.
- isInside :: Int -> Int -> Int -> Bool
- isInside x y n = elem n [min x y .. max x y]
- -- 5.
- removeFirst :: Int -> [Int] -> [Int]
- removeFirst 0 [] = []
- removeFirst (x:xs)
- | n == x = removeFirst n xs
- | otherwise = removeFirst n xs
- -- 6.
- removeAllRec :: Int -> [Int] -> [Int]
- removeAllRec 0 [] = []
- removeAllRec (x:xs)
- | n == x = removeAllRec n xs
- | otherwise = x : removeAllRec n xs
- removeAllHOF :: Int -> [Int] -> [Int]
- removeAllHOF 0 [] = []
- removeAllHOF n xs = filter (\x -> x /= n) xs
- -- 7.
- incrementByLC :: Int -> [Int] -> [Int]
- incrementByLC 0 [] = []
- incrementByLC n xs = [n + x | x <- xs]
- incrementByHOF :: Int -> [Int] -> [Int]
- incrementByHOF 0 [] = []
- incrementByHOF n xs = map (+n) xs
- -- 8.
- rev n = read $ reverse $ show n
- isPrime n = (n >= 1) && [1,n] == [d | d <- [1.. n], mod n d == 0]
- isPrime' n = (n > 2) && all (\i -> mod n i /= 0) [2.. n-1]
- sumDivs n = sum [d | d <- [1..n], mod n d == 0]
- sumDig n = sum $ map digitToInt $ show n
- -- 9.
- getPrimesLC :: Int -> Int -> [Int]
- getPrimesLC x y = [d | d <- [min x y .. max x y], isPrime d && (elem '7' $ show d)]
- getPrimesHOF :: Int -> Int -> [Int]
- getPrimesHOF x y = filter (\d -> isPrime d && (elem '7' $ show d)) [min x y .. max x y]
- -- 10.
- mySumPM :: [Int] -> Int
- mySumPM [] = 0
- mySumPM (x:xs) = x + mySumPM xs
- mySumNonPM :: [Int] -> Int
- mySumNonPM [] = 0
- mySumNonPM (x:xs)
- | null xs = 0
- | otherwise = x + mySumNonPM xs
- -- 11.
- isPresent :: Int -> [Int] -> Bool
- isPresent 0 [] = False
- isPresent n xs
- | elem n xs == True
- | otherwise = False
- -- 12.
- primesInRangeLC :: Int -> Int -> [Int]
- primesInRangeLC x y = [d | d <- [min x y .. max x y], isPrime d && d >= 3]
- primesInRangeHOF :: Int -> Int -> [Int]
- primesInRangeHOF x y = filter (\d -> isPrime d && d >= 3) [min x y .. max x y]
- -- 13.
- sumUnevenLC :: Int -> Int -> Int
- sumUnevenLC x y = sum [d | d <- [x .. y], mod d 2 /= 0]
- sumUnevenHOF :: Int -> Int -> Int
- sumUnevenHOF x y = sum $ filter (\d -> mod d 2 /= 0) [x .. y]
- -- 14.
- isAscending :: Int -> Int -> Int
- isAscending n = ((mod n 10) < 10) && (mod n 10) >= (mod (div n 10) 10) && isAscending (div n 10)
- -- 15.
- sumSpecialPrimes :: Int -> Int -> Int
- sumSpecialPrimes n d = sum $ take n $ filter (\x -> isPrime x && ((elem n) (intToDigit d)) [1..]
- -- 16.
- myLambda :: (a -> a) -> (a -> a)
- myLambda f = (\x -> f x)
- negatePred :: (a -> Bool) -> (a -> Bool)
- negatePred p = (\x -> not (p x))
- compose :: (c -> a) -> (b -> c) -> (b -> a)
- compose f g = (\x -> f (g x))
- partiallyApply :: (a -> a) -> a -> (a -> a)
- partiallyApply f x = (\y -> f x y)
- -- 17.
- difference :: (a -> a) -> (a -> a)
- difference f = (\(x,y) -> f y - f x)
- -- 18.
- upperBound :: (a -> a) -> a -> (a -> a)
- upperBound f y = (\x -> max (f x) y)
- -- 19.
- sumTuplePM :: (Int, Int) -> Int
- sumTuplePM (x,y) = x + y
- sumTupleNonPM :: (Int, Int) -> Int
- sumTupleNonPM vec = fst vec + snd vec
- -- 20.
- type Point = (Int, Int)
- dividePM :: Point -> Point
- dividePM (x,y) = (div x y, mod x y)
- divideNonPM :: Point -> Point
- divideNonPM vec = (div (fst vec) (snd vec), mod (fst vec) (snd vec))
- -- 21.
- type Rat = (Int, Int)
- normalizeUsingLet :: Rat -> Rat
- normalizeUsingLet (x,y) = let g = gcd x y = (div x d, div y d)
- normalize :: Rat -> Rat
- normalize (x,y) = (div x d, div y d)
- where d = gcd x y
- -- 22.
- getSquares :: Int -> Int -> Int -> [(Int, Int)]
- getSquares s f k = [(x, x*x) | x <- [s, s+k .. f]]
- -- 23.
- type Vector a = (a,a,a)
- sumVectors :: Vector a -> Vector a -> Vector a
- sumVectors (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
- scaleVector :: Vector a -> a -> Vector a
- scaleVector (x,y,z) l = (l*x, l*y, l*z)
- -- 24.
- rev n = read $ reverse $ show n
- isPalindrome :: Int -> Bool
- isPalindrome n = n == rev n
- getDivs :: Int -> Int
- getDivs n = [d | d <- [1..n], mod n d == 0]
- getPalindromes :: Int -> Int
- getPalindromes n = minimum s + maximum s
- where s = getDivs n
- -- 25.
- fS :: Int -> Int
- fS n = mod n 10
- sS :: Int -> Int
- sS n = mod (div n 10) 10
- tS :: Int -> Int
- tS n = mod (div n 100) 10
- fS :: Int -> Int
- fS n = mod (div n 1000) 10
- isArithmetic :: [Int] -> Bool
- isArithmetic [] = False
- isArithmetic xs
- | fS xs == sS xs == tS xs == fS xs = True
- | otherwise = False
- -- 26.
- specialSum :: Int -> Int -> Int
- specialSum x y = sum [d | d <- [x ..y], (elem '6' $ show d) && (mod (d-1) 4 == 0]
- -- 27.
- applyN :: (a -> a) -> a
- applyN n = (\x -> x * n)
- -- 28.
- type Vector a = (a,a,a)
- dotProduct :: Vector a -> Vector a -> a
- dotProduct (x1,x2,x3) (y1,y2,y3) = [(x1*y1) + (x2*y2) + (x3*y3) | ((x1,x2,x3) (y1,y2,y3) <- zip (x1,x2,x3) (y1,y2,y3))]
- crossProduct :: Vector a -> Vector a -> Vector a
- crossProduct (x1, x2, x3) (y1, y2, y3) = sum [ (i*x2y3) + (j*x3*y1) + (k*x1y2) - (y1x2*k) - (x3*y2*i) - (x1*y3*j) | ((i,j,k),(x1,x2,x3),(y1,y2,y3)) <- zip (i,j,k) (x1,x2,x3) (y1,y2,y3)]
- magnitude :: Vector a -> Double
- magnitude (x,y,z) = [sqrt ((x*x) + (y*y) + (z*z)) | (x,y,z) <- zip (x,y,z)]
- -- 28.
- myPolynomial :: (a -> a) -> [a] -> (a -> a)
- myPolynomial f ys = (\x -> sum [n - f(y*n) | (y,n) <- zip ys [1..]])
- -- 29.
- dominates :: (a -> a) -> (a -> a) -> [a] -> Bool
- dominates f g = all (\x -> f x >= g x)
- dominates :: (a -> a) -> (a -> a) -> [a] -> Bool
- dominates f g xs = foldl (\acc x -> acc && f x >= g x) True xs
- isInteresing :: Int -> Bool
- isInteresting n = mod n (sum $ map digitToInt $ show n) == 0
- mySin :: Int -> Int -> Double
- mySin 0 x = 0
- mySin n x = (((-1)^n * x^(2*n + 1)) / (product $ fromIntegral [1 .. 2*n + 1])) + mySin (n-1) x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement