Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main = do
- print $ "hello world"
- print $ "Enter some text right in console!"
- plus :: Int -> Int -> Int
- plus x y = x + y
- minus :: Int -> Int -> Int
- minus x y = x - y
- mult :: Int -> Int -> Int
- mult x y = x * y
- divs :: Int -> Int -> Int
- divs x y = x / y
- divs' :: Int -> Int -> Int
- divs' x y = div x y
- mods :: Int -> Int -> Int
- mods x y = mod x y
- addThree :: Int -> Int -> Int -> Int
- addThree x y z = x + y + z
- factorial :: Int -> Int
- factorial n = product [1..n]
- bigger :: Int -> Int -> Bool
- bigger x y = if x > y then x else y
- lucky :: Int -> String
- lucky 7 = "You got the right number!"
- lucky x = "Sorry, you're out of luck!"
- sayMe :: Int -> String
- sayMe 1 = "One"
- sayMe 2 = "Two"
- sayMe 3 = "Three"
- sayMe 4 = "Four"
- sayMe 5 = "Five"
- sayMe x = "Not between 1 and 5"
- guessTheNumber :: Int -> String
- guessTheNumber 10 = "How did you do that? You got it, mate!"
- guessTheNumber x = "You are out of luck, sorry!"
- factorial' :: Int -> Int
- factorial' 0 = 1
- factorial' 1 = 1
- factorial' n = n * factorial (n-1)
- charName :: Char -> String
- charName 'a' = "Albert"
- charName 'b' = "Broseph"
- charName 'c' = "Cercil"
- addVectors1 :: (Ord a, Num a) => (a,a) -> (a,a) -> (a,a)
- addVectors1 x y = (fst x + fst y, snd x + snd y)
- addVectors2 :: (Ord a, Num a) => (a,a) -> (a,a) -> (a,a)
- addVectors2 (x1,y1) (x2,y2) = (x1+x2,y1+y2)
- first :: (a,b,c) -> a
- first (x, _, _) = x
- second :: (a,b,c) -> b
- second (_, y, _) = y
- third :: (a,b,c) -> c
- third (_, _, z) = z
- head :: [Int] -> Int
- head [] = error "Can't call head on empty list, dummy"
- head (x:_) = x
- -- here we don't care about our first argument
- length :: [Int] -> Int
- length [] = 0
- length (_:xs) = 1 + length xs
- -- but here we are interested in our first argument
- sum :: [Int] -> Int
- sum [] = 0
- sum (x:xs) = x + sum xs
- bmiTell :: (RealFloat a) => a -> String
- bmiTell bmi
- | bmi <= 18.5 = "You are underweight"
- | bmi <= 25.0 = "You are normal weight"
- | bmi <= 30.0 = "You are overweight"
- | otherwise = "If you contunue to eat, you are gonna die"
- bmiTell1 :: (RealFloat a) => a -> a -> String
- bmiTell1 weight height
- | weight / height ^ 2 <= 18.5 = "You are underweight"
- | weight / height ^ 2 <= 25.0 = "You are normal weight"
- | weight / height ^ 2 <= 30.0 = "You are overweight"
- | otherwise = "You are a whale, congratulations"
- bmiTell2 :: (RealFloat a) => a -> a -> String
- bmiTell2 weight height
- | bmi <= 18.5 = "You are underweight"
- | bmi <= 25.0 = "You are normal weight"
- | bmi <= 30.0 = "You are overweight"
- | otherwise = "You are a whale, congratulations"
- where
- bmi = weight / height ^ 2
- bmiTell3 :: (RealFloat a) => a -> a -> String
- bmiTell3 weight height
- | bmi <= skinny = "You are underweight"
- | bmi <= normal = "You are normal weight"
- | bmi <= overweight = "You are overweight"
- | otherwise = "You are a whale, congratulations"
- max1 :: Int -> Int -> Int
- max1 a b
- | a > b = a
- | otherwise = b
- min1 :: Int -> Int -> Int
- min1 a b
- | a > b = b
- | otherwise = a
- maxThree :: Int -> Int -> Int -> Int
- maxThree a b c = if (a > b) && (a > c) then a else if (b > a) && (b > c) then b else c
- maxThree1 :: Int -> Int -> Int -> Int
- maxThree1 a b c
- | (a > b) && (a > c) = a
- | (b > a) && (b > c) = b
- | otherwise = c
- myCompare :: Int -> Int -> String
- myCompare a b
- | a > b = "GT"
- | a < b = "LT"
- | otherwise = "EQ"
- lastDigit :: Int -> Int
- lastDigit n = mod n 10
- removeLastDigit :: Int -> Int
- removeLastDigit n = div n 10
- areNotEqualOneLine :: Int -> Int -> Bool
- areNotEqualOneLine a b = a == b
- areNotEqualGuards :: Int -> Int -> Bool
- areNotEqualGuards a b
- | a == b = True
- | otherwise = False
- inside :: Int -> Int -> Int -> Bool
- inside a b x
- | (x >= a) && (x <= b) = True
- | otherwise = False
- factoriel :: Int -> Int
- factoriel 0 = 1
- factoriel 1 = 1
- factoriel n = n * factoriel (n-1)
- factIter :: Int -> Int
- factIter n = helper n 1
- where
- helper :: Int -> Int -> Int
- helper 0 result = result
- helper leftOver result = helper (leftOver - 1) (result*leftOver)
- fibonacci :: Int -> Int
- fibonacci 0 = 0
- fibonacci 1 = 1
- fibonacci n = fibonacci (n-1) + fibonacci (n-2)
- fibIter :: Int -> Int -> Int
- fibIter n = helper n 1 0
- where
- helper :: Int -> Int -> Int -> Int
- helper 0 n1 _ = n1
- helper 1 _ n2 = n2
- helper n n1 n2 = helper (n-1) n2 (n2+n1)
- -- Recursion 1
- reverseInteger :: Int -> Int
- reverseInteger 0 = 0
- reverseInteger n = (mod n 10)*10 + reverseInteger (div n 10)
- isPalindrome :: Int -> Bool
- isPalindrome 0 = False
- isPalindrome n = n == reverseInteger n
- sumDigitsRec :: Int -> Int
- sumDigitsRec n = mod n 10 + sumDigitsRec (div n 10)
- powRec :: Int -> Int -> Int
- powRec _ n = 1
- powRec x n = x * powRec (n-1)
- isPrime :: Int -> Bool
- isPrime 1 = False
- isPrime 2 = True
- isPrime n = n >= 1 && helper 2
- where
- helper :: Int -> Bool
- helper i
- | (fromIntegral i) >= (sqrt $ fromIntegral n) = True
- | mod n i == 0 = False
- | otherwise = helper (i+1)
- sumDivs :: Int -> Int
- sumDivs n = helper 1
- where
- helper :: Int -> Int
- helper currentDiv
- | currentDiv >= n = n
- | mod n currentDiv == 0 = currentDiv + helper (currentDiv+1)
- | otherwise = helper (currentDiv+1)
- isPerfect :: Int -> Bool
- isPerfect n = n == sumDivs n
- hasIncDigits :: Int -> Bool
- hasIncDigits 0 = False
- hasIncDigits n = (n < 10) || mod n 10 >= mod (div n 10) 10 && hasIncDigits (div n 10)
- -- Recursion 2
- numDig :: Int -> Int
- numDig n
- | n < 0 = error "n was negative"
- | n < 10 = 1
- | otherwise = 1 + numDig (div n 10)
- isNarcistic :: Int -> Bool
- isNarcistic n = n == helper n (numDig n)
- where
- helper :: Int -> Int -> Int
- helper 0 nd = 0
- helper leftover nd = (mod leftover 10)^nd + helper (div leftover 10) nd
- calculateSum :: Int -> Int -> Int
- calculateSum x n = helper 0 0
- where
- helper :: Int -> Int -> Int
- helper sumOfDegree degree
- | n <= degree = sumOfDegree + x^degree
- | otherwise = helper (sumOfDegree + x^degree) (degree+1)
- findMax :: Int -> Int
- findMax n = helper n 0
- where
- helper :: Int -> Int
- helper num currMax
- | (mod num 10) <= currMax = helper (div num 10) (div num 10)
- | otherwise = helper (div num 10) currMax
- sumNumbers :: Int -> Int -> Int
- sumNumbers start finish = helper $ min start finish
- where
- helper :: Int -> Int
- helper i
- | i > max start finish = 0
- | hasDecDigits i = i + helper (i+1)
- | otherwise = helper (i+1)
- subNum :: Int -> Int -> Bool
- subNum x y = helper y (10^numDig x)
- where
- helper :: Int -> Int -> Bool
- helper y dNum
- | x > y = False
- | mod y dNum == x = True
- | otherwise = helper (div y 10) dNum
- hasDecDigits :: Int -> Int
- hasDecDigits n = (n < 10) || mod n 10 <= mod (div n 10) 10 && hasDecDigits (div n 10)
- digitalRoot :: Int -> Int
- digitalRoot n
- | n <= 9 = 1
- | otherwise = digitalRoot (sumDigitsRec n)
- mySin :: Int -> Int -> Int
- mySin 0 x = x
- mySin n x = ((-1)**n * x**(2*n + 1)) / (fromIntegral $ factoriel (2*n + 1)) + mySin (n-1) x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement