From 9c608c32a9387a1dd1cb045e5822733ad181ccf8 Mon Sep 17 00:00:00 2001 From: Determinant Date: Sun, 28 May 2017 20:29:11 -0400 Subject: finish vol 2 --- 11-20/14.hs | 4 ++++ 11-20/15.hs | 7 +++++++ 11-20/16.hs | 7 +++++++ 11-20/17.hs | 6 ++++++ 11-20/18.hs | 6 ++++++ 11-20/19.hs | 6 ++++++ 11-20/20.hs | 6 ++++++ 7 files changed, 42 insertions(+) create mode 100644 11-20/14.hs create mode 100644 11-20/15.hs create mode 100644 11-20/16.hs create mode 100644 11-20/17.hs create mode 100644 11-20/18.hs create mode 100644 11-20/19.hs create mode 100644 11-20/20.hs (limited to '11-20') diff --git a/11-20/14.hs b/11-20/14.hs new file mode 100644 index 0000000..6071891 --- /dev/null +++ b/11-20/14.hs @@ -0,0 +1,4 @@ +dupli :: [a] -> [a] + +dupli [] = [] +dupli (x:xs) = x:x:(dupli xs) diff --git a/11-20/15.hs b/11-20/15.hs new file mode 100644 index 0000000..f11d61c --- /dev/null +++ b/11-20/15.hs @@ -0,0 +1,7 @@ +repli :: [a] -> Int -> [a] + +repli l n = rep l n + where rep [] i = [] + rep (x:xs) i + | i > 0 = x:(rep (x:xs) (i - 1)) + | otherwise = rep xs n diff --git a/11-20/16.hs b/11-20/16.hs new file mode 100644 index 0000000..74cc946 --- /dev/null +++ b/11-20/16.hs @@ -0,0 +1,7 @@ +dropEvery :: [a] -> Int -> [a] + +dropEvery l n = d l 1 + where d [] i = [] + d (x:xs) i + | i == n = d xs 1 + | otherwise = x:d xs (i + 1) diff --git a/11-20/17.hs b/11-20/17.hs new file mode 100644 index 0000000..e058b27 --- /dev/null +++ b/11-20/17.hs @@ -0,0 +1,6 @@ +split :: [a] -> Int -> ([a], [a]) + +split [] _ = ([], []) +split l@(x:xs) n + | n > 0 = let (ys, zs) = split xs (n - 1) in (x:ys, zs) + | otherwise = ([], l) diff --git a/11-20/18.hs b/11-20/18.hs new file mode 100644 index 0000000..e2ec1fb --- /dev/null +++ b/11-20/18.hs @@ -0,0 +1,6 @@ +slice :: [a] -> Int -> Int -> [a] + +slice (x:xs) n m + | n > 1 = slice xs (n - 1) (m - 1) + | m > 0 = x:slice xs 0 (m - 1) +slice _ _ _ = [] diff --git a/11-20/19.hs b/11-20/19.hs new file mode 100644 index 0000000..f1e795b --- /dev/null +++ b/11-20/19.hs @@ -0,0 +1,6 @@ +rotate :: [a] -> Int -> [a] + +rotate l n = let (a, b) = rot l (if n < 0 then (n + length l) else n) in b ++ a + where rot l@(x:xs) n + | n > 0 = let (a, b) = rot xs (n - 1) in (x:a, b) + | otherwise = ([], l) diff --git a/11-20/20.hs b/11-20/20.hs new file mode 100644 index 0000000..c6a3ba8 --- /dev/null +++ b/11-20/20.hs @@ -0,0 +1,6 @@ +removeAt :: Int -> [a] -> (Maybe a, [a]) + +removeAt k (x:xs) + | k > 1 = let (a, b) = removeAt (k - 1) xs in (a, x:b) + | k == 1 = (Just x, xs) +removeAt _ l = (Nothing, l) -- cgit v1.2.3-70-g09d2