Last active
September 15, 2015 21:05
-
-
Save ekozhura/a328274b1ba920969324 to your computer and use it in GitHub Desktop.
Introduction to Functional Programming
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
initTake xs = take (length xs - 1) xs | |
initRev = reverse . tail . reverse | |
initRec = reverse . initAux [] | |
where | |
initAux acc (x:[]) = acc | |
initAux acc xs = initAux (head xs : acc) (tail xs) | |
initRec2 (x:[]) = [] | |
initRec2 xs = (head xs) : initRec2 (tail xs) |
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
lastRev xs = head (reverse xs) | |
lastFold xs = foldl(\acc x -> x) 0 xs | |
lastRec xs = lastAux (head xs) (tail xs) | |
where | |
lastAux x [] = x | |
lastAux x xs = lastAux (head xs) (tail xs) | |
lastRec2 (x:[]) = x | |
lastRec2 (x:xs) = lastRec2 xs | |
lastRec3 xs | length xs == 1 = head xs | |
| length xs > 1 = lastRec3 (tail xs) |
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
zipRec :: [a] -> [b] -> [(a,b)] | |
zipRec (x:xs) (y:ys) = (x,y) : zipM xs ys | |
zipRec _ _ = [] | |
pairs :: [a] -> [(a,a)] | |
pairs xs = zipRec xs (tail xs) | |
pairsRec :: [a] -> [(a,a)] | |
pairsRec (x:(y:xs)) = (x,y) : pairsRec (xs) | |
pairsRec _ = [] | |
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] | |
factorsEx n = [x | x <- [1..n], mod n x == 0, n /= x] | |
perfects n = [x | x <- [1..n], x == sum (factorsEx x)] | |
scalar :: [Int] -> [Int] -> Int | |
scalar xs ys = sum [x * y | (x, y) <- zip xs ys] |
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
-- decide if all logical values in a list are true: | |
and2 :: [Bool] -> Bool | |
and2 = foldl (\x y -> x && y) True | |
and3 :: [Bool] -> Bool | |
and3 [] = True | |
and3 (x:xs) = x && and3 xs | |
-- concatenate a list of lists | |
concat2 :: [[a]] -> [a] | |
concat2 = foldl (\x xs-> x ++ xs) [] | |
concat3 :: [[a]] -> [a] | |
concat3 [] = [] | |
concat3 (x:xs) = x ++ (concat3 xs) | |
concat4 xs = concAux [] xs | |
where | |
concAux acc [] = acc | |
concAux acc (x:xs) = concAux (acc ++ x) xs | |
-- produce a list with n identical elements | |
replicate2 :: Int -> a -> [a] | |
replicate2 n val = repAux n [] | |
where | |
repAux 0 acc = acc | |
repAux n acc = repAux (n - 1) (acc++[val]) | |
replicate3 n val = [x | x <- [val], y <- [1..n]] | |
-- select the nth element of a list | |
selN :: [a] -> Int -> a | |
selN xs n | n == 0 = head xs | |
| otherwise = selN (tail xs) (n - 1) | |
selN2 :: [a] -> Int -> a | |
selN2 (x:xs) 0 = x | |
selN2 (x:xs) n = selN2 xs (n - 1) | |
-- decide if a value is an element of a list | |
-- elem :: Eq a => a -> [a] -> Bool | |
inList :: Eq a => a -> [a] -> Bool | |
inList x xs | null xs = False | |
| x == head xs = True | |
| otherwise = inList x (tail xs) |
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
dropMy :: Int -> [a] -> [a] | |
dropMy _ [] = [] | |
dropMy 0 xs = xs | |
dropMy n (x:xs) = dropMy (n - 1) xs | |
append :: [a] -> [a] -> [a] | |
append acc [] = acc | |
append acc (x:xs) = append (acc ++ (x:[])) xs | |
append2 :: [a] -> [a] -> [a] | |
append2 [] ys = ys | |
append2 (x:xs) ys = x : (append2 xs ys) |
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
safetail :: [a] -> [a] | |
safetail [] = [] | |
safetail (_:xs) = xs | |
safetailEx xs | null xs = [] | |
| otherwise = ys | |
where (y:ys) = xs | |
safetailCond xs = if null xs then [] else ys | |
where (y:ys) = xs |
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
-- redefine map f and filter p using foldr | |
map2 :: (a -> b) -> [a] -> [b] | |
map2 f xs = foldr (\x acc -> [f x] ++ acc) [] xs | |
filter2 :: (a -> Bool) -> [a] -> [a] | |
filter2 p xs = foldr (\x acc -> if p x then x:acc else acc) [] xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment