Created
March 31, 2016 10:17
-
-
Save kmtr/1c995734b8fa38f60db787aff4fb2865 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Chapter4 ( | |
halve, | |
halveX, | |
safetailA, | |
safetailB, | |
safetailC, | |
or1, | |
or2, | |
or3, | |
or4, | |
and1, | |
and2, | |
mult, | |
) where | |
halve :: [a] -> ([a], [a]) | |
halve xs = halve' xs (length xs) | |
halve' :: [a] -> Int -> ([a], [a]) | |
halve' xs n | n == 0 = (front, back) | |
| length front == length back = (front, back) | |
| otherwise = halve' xs (n-1) | |
where | |
front = take n xs | |
back = drop n xs | |
halve2' :: [a] -> Int -> ([a], [a]) | |
halve2' _ 0 = ([], []) | |
halve2' xs n = if length front == length back | |
then (front, back) | |
else halve' xs (n-1) | |
where | |
front = take n xs | |
back = drop n xs | |
halveX :: [a] -> ([a], [a]) | |
halveX xs = halveX' [] xs | |
halveX' :: [a] -> [a] -> ([a], [a]) | |
halveX' xs ys | length xs >= length ys = (xs, ys) | |
| otherwise = halveX' (reverse((head ys) : xs)) (drop 1 ys) | |
safetailA :: [a] -> [a] | |
safetailA xs = if null xs then [] else tail xs | |
safetailB :: [a] -> [a] | |
safetailB xs | null xs = [] | |
| otherwise = tail xs | |
safetailC :: [a] -> [a] | |
safetailC [] = [] | |
safetailC (_ : xs) = xs | |
or1 :: Bool -> Bool -> Bool | |
False `or1` False = False | |
_ `or1` _ = True | |
or2 :: Bool -> Bool -> Bool | |
False `or2` b = b | |
True `or2` _ = True | |
or3 :: Bool -> Bool -> Bool | |
or3 a b | a == b = a | |
| otherwise = True | |
or4 :: Bool -> Bool -> Bool | |
True `or4` True = True | |
True `or4` False = True | |
False `or4` True = True | |
False `or4` False = False | |
and1 :: Bool -> Bool -> Bool | |
and1 a b = | |
if a then | |
if b then True | |
else False | |
else | |
False | |
and2 :: Bool -> Bool -> Bool | |
and2 a b = if a then b else False | |
mult :: Num a => a -> a -> a -> a | |
mult = \x -> (\y -> (\z -> x * y * z)) | |
{- | |
abs' n | n >= 1 = n | |
| n == 0 = abs(n + 1) | |
| otherwise = -1 | |
swap (x, y) = (y, x) | |
pair x y = (x, y) | |
test ['a', _ , _] = True | |
test _ = False | |
palindrome xs = reverse xs == xs | |
twice f x = f (f x) | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment