Skip to content

Instantly share code, notes, and snippets.

@TimboKZ
Last active May 8, 2016 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimboKZ/db66bd9097972a813091c000cceb1001 to your computer and use it in GitHub Desktop.
Save TimboKZ/db66bd9097972a813091c000cceb1001 to your computer and use it in GitHub Desktop.
Haskell solutions, 2015 to 2013
import Test.QuickCheck
combi:: (Num a, Ord a) => [(a, a)] -> [a]
combi [] = [0, 0]
combi xs = [sum ys] ++ [sum zs]
where ys = [x | (x, y) <- xs, x > y]
zs = [y | (x, y) <- xs, x > y]
zipWith':: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' f a b = [f (a!!x) (b!!x) | x <- [0 .. len - 1]]
where len = min (length a) (length b)
qSort:: (Num a, Ord a) => [a] -> [a]
qSort [] = []
qSort (x:xs) = [y | y <- xs, y <= x] ++ [x] ++ [y | y <- xs, y > x]
filterFirst:: (a -> Bool) -> [a] -> [a]
filterFirst _ [] = []
filterFirst p (x:xs) | p x = x:xs
| otherwise = xs
filterLast:: (a -> Bool) -> [a] -> [a]
filterLast _ [] = []
filterLast p xs = if not $ p (last xs) then init xs else xs
backwards:: IO()
backwards = do
putStrLn "Input a string:"
string <- getLine
putStrLn (reverse string)
mapFuns::[(a -> b)] -> a -> [b]
mapFuns funs a = [f a | f <- funs]
foldr':: (a -> a -> a) -> a -> [a] -> a
foldr' _ a [] = a
foldr' f a (x:xs) = f x (foldr' f a xs)
split:: [a] -> ([a], [a])
split xs = ([a | i <- [1..(length xs)], odd i,let a = xs!!(i-1)], [a | i <- [1..(length xs)], even i,let a = xs !! (i - 1)])
matches:: Eq a => a -> [a] -> [a]
matches x xs = [y | y <- xs, y == x]
squareNum:: [Int] -> Int
squareNum xs = foldr (+) 0 (map (\x -> x * x) xs)
isort':: Ord a => [a] -> [a]
isort' [] = []
isort' (x:xs) = isortHelper x (isort' xs)
isortHelper:: Ord a => a -> [a] -> [a]
isortHelper a [] = [a]
isortHelper a (x:xs) | a < x = a : x : xs
| otherwise = x : (isortHelper a xs)
data Set a = Set [a] deriving Show
mapSet:: Ord a => (a -> b) -> Set a -> Set b
mapSet f (Set xs) = Set [f x | x <- xs]
filterSet:: Ord a => (a -> Bool) -> Set a -> Set a
filterSet f (Set xs) = Set [x | x <- xs, f x]
memSet:: Ord a => a -> Set a -> Bool
memSet e (Set []) = False
memSet e (Set (x:xs)) | e == x = True
| e < x = False
| otherwise = memSet e (Set xs)
sum':: [Int] -> Int
sum' [] = 0
sum' (x:xs) = x + sum' xs
sumAcc:: Int -> [Int] -> Int
sumAcc acc [] = acc
sumAcc acc (x:xs) = sumAcc (acc + x) xs
sumInts:: IO Int
sumInts = do
val <- getLine
let number = read val :: Int
if number == 0
then do
return 0
else do
newNumber <- sumInts
return (number + newNumber)
prop_sum:: [Int] -> Bool
prop_sum xs = sum' xs == sumAcc 0 xs
{--
Base case: sum' [] == sumAcc 0 []
RHS = sum' []
= 0 by def
LHS = sumAcc 0 []
= 0 by def
= RHS
IH: Assume sum' xs = sumAcc 0 xs
Prove sum' (x:xs) = sumAcc 0 (x:xs)
LHS = sum' (x:xs)
= x + sum' xs by def of sum'
RHS = sumAcc 0 (x:xs)
= sumAcc x xs by def of sumAcc
= x + sumAcc 0 xs by assumption from the question
= LHS by IH
Hence true for all natural numbers
--}
sameHead:: Eq a => [a] -> [a] -> Bool
sameHead [] _ = False
sameHead _ [] = False
sameHead (x:xs) (y:ys) = x == y
biggerThan:: Ord a => a -> [a] -> [a]
biggerThan _ [] = []
biggerThan a (x:xs) | x > a = x : (biggerThan a xs)
| otherwise = biggerThan a xs
sameList:: Eq a => [a] -> [a] -> Bool
sameList [] [] = True
sameList _ [] = False
sameList [] _ = False
sameList (x:xs) (y:ys) | x /= y = False
| otherwise = sameList xs ys
sumPosSqr:: [Int] -> Int
sumPosSqr xs =
foldr (+) 0 (map (\x -> x * x) positive)
where
positive = filter (\x -> x > 0) xs
wonkyLen:: IO()
wonkyLen = do
putStrLn "Input a string:"
string <- getLine
let len = length string
putStr "The length of the string is "
putStrLn (show (len * 3 + 1))
zipWith'':: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith'' f xs ys = [f x y | (x, y) <- zip xs ys]
absDiff:: Float -> Float -> Float
absDiff x y | x >= y = x - y
| otherwise = y - x
nearEq:: Float -> Float -> Float -> Bool
nearEq a x y = a > absDiff x y
probDist:: [Float] -> Bool
probDist xs = nearEq 0.0001 1.0 (sum xs)
rv:: [a] -> [Float] -> [(a, Float)]
rv xs ys | length xs /= length ys = []
| not (probDist ys) = []
| otherwise = zip xs ys
alphanums:: [Char] -> [Char]
alphanums xs = [x | x <- xs, not(x `elem` ['.', ',', ';', '`', ' '])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment