Skip to content
Snippets Groups Projects
Commit 94a623c9 authored by ik1g19's avatar ik1g19
Browse files

add sheets

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 139 additions and 0 deletions
double (double 2)
double (2 + 2)
double 4
4 + 4
8
\ No newline at end of file
sum (x:[])
x + sum []
x + 0
x
\ No newline at end of file
quicktros [] = []
quicktros (x:xs) = quicktros rs ++ [x] ++ quicktros ls
where
ls = [ a | a <- xs , a < x ]
rs = [ a | a <- xs , a > x ]
\ No newline at end of file
quicktros [] = []
quicktros (x:xs) = quicktros rs ++ [x] ++ quicktros ls
where
ls = [ a | a <- xs , a <= x ]
rs = [ a | a <- xs , a > x ]
\ No newline at end of file
n = a `div` (length xs)
where
a = 10
xs = [1,2,3,4,5]
\ No newline at end of file
product [] = 1
product (x:xs) = x * (Main.product xs)
\ No newline at end of file
last (x:[]) = x
last (x:xs) = last xs
\ No newline at end of file
fourth1 :: [a] -> a
fourth1 (w:x:y:z:xs) = z
fourth2 :: [a] -> a
fourth2 xs = xs !! 3
fourth3 :: [a] -> a
fourth3 ys = head (tail (tail (tail ys)))
\ No newline at end of file
safetail1 :: [a] -> [a]
safetail1 xs =
if (length xs > 0) then tail xs
else []
safetail2 :: [a] -> [a]
safetail2 xs | length xs > 0 = tail xs
| otherwise = []
safetail3 :: [a] -> [a]
safetail3 [] = []
safetail3 xs = tail xs
\ No newline at end of file
halve :: [a] -> ([a],[a])
halve xs | even listLength = splitAt (listLength `div` 2) xs
| otherwise = ([],[])
where listLength = length xs
\ No newline at end of file
import Data.Char (ord)
--encrypts a given string
--returns the encrypted string and a function to decrypt the string
encrypt :: Int -> String -> (String, String -> String)
encrypt key str = (enc, \str -> [negate key `applyKey` x | x <- str])
where
--applies a key to a character
applyKey :: Int -> Char -> Char
applyKey k c | k + ord c > 122 = toEnum $ k + ord c - 26
| k + ord c < 97 = toEnum $ k + ord c + 26
| otherwise = toEnum $ k + ord c
--encrypts a string with an int
enc :: String
enc = [applyKey key x | x <-str]
\ No newline at end of file
--checks four bank card numbers for errors
--returns true if valid, otherwise false
luhn :: Int -> Int -> Int -> Int -> Bool
luhn w x y z | total `mod` 10 == 0 = True --if the answer is divisible by 10 it is valid
| otherwise = False --otherwise invalid
where
--doubles a number and subtracts 9 if result > 9
luhnDouble :: Int -> Int
luhnDouble n | m > 9 = m - 9
| otherwise = m
where m = 2 * n
total = luhnDouble w + x + luhnDouble y + z
\ No newline at end of file
-- --counts the number of values within three ranges
-- histogram :: Int -> [Int] -> [Int]
-- histogram n xs = [x,y,z]
-- where
-- x = length [a | a <- xs, a >= 0, a <= n - 1] --0 <= x <= n-1
-- y = length [a | a <- xs, a >= n, a <= 2 * n - 1] --n <= x <= 2n-1
-- z = length [a | a <- xs, a >= 2 * n, a <= 3 * n - 1] --2n <= x <= 3n-1
histogram :: Int -> [Int] -> [Int]
histogram n xs | n <= 0 = error "range sizes cannot be negative or 0" --given range is 0 or less
| otherwise = [histogramCounter a | a <- [0..(maximum xs) `div` n]] --given range is valid
where
histogramCounter :: Int -> Int
histogramCounter range = length [a | a <- xs, a >= range * n, a <= (range + 1) * n - 1]
\ No newline at end of file
approxPi :: Int -> Double
approxPi n | n <= 0 = error "negative numbers or 0 cannot be used for approximations of pi"
| otherwise = 2 * sum [product [1..a] / product [1,3..2 * a + 1] | a <- [0..fromIntegral n-1]]
\ No newline at end of file
oddEvenSum :: Int
oddEvenSum = sum [x^2 | x <- [1,3..100]] + sum [y^3 | y <- [0,2..100]]
\ No newline at end of file
grid :: Int -> Int -> [(Int,Int)]
grid m n = [(x,y) | x <- [0..m], y <- [0..n]]
square :: Int -> [(Int,Int)]
square m = [(x,y) | x <- [0..m], y <- [0..m], x /= y]
\ No newline at end of file
replicate :: Int -> a -> [a]
replicate n a = [a | x <- [1..n]]
\ No newline at end of file
pyths :: Int -> [(Int,Int,Int)]
pyths n = [(x,y,z) | x <- [1..n], y <- [1..n], z <- [1..n], x^2 + y^2 == z^2]
\ No newline at end of file
perfect :: Int -> [Int]
perfect n = [x | x <- [2..n], x == (sum . factors) x]
where
factors :: Int -> [Int]
factors v = [x | x <- [1..v], v `mod` x == 0, x /= v]
\ No newline at end of file
positions :: Eq a => a -> [a] -> [Int]
positions a as = find a $ zip as [0..]
where
find :: Eq a => a -> [ (a,b)] -> [b]
find k t = [ v | (k',v) <- t, k==k']
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment