summaryrefslogtreecommitdiff
path: root/1-10
diff options
context:
space:
mode:
Diffstat (limited to '1-10')
-rw-r--r--1-10/1.hs3
-rw-r--r--1-10/10.hs9
-rw-r--r--1-10/2.hs4
-rw-r--r--1-10/3.hs5
-rw-r--r--1-10/4.hs2
-rw-r--r--1-10/5.hs4
-rw-r--r--1-10/6.hs3
-rw-r--r--1-10/7.hs4
-rw-r--r--1-10/8.hs4
-rw-r--r--1-10/9.hs5
-rw-r--r--1-10/t.hs2
11 files changed, 45 insertions, 0 deletions
diff --git a/1-10/1.hs b/1-10/1.hs
new file mode 100644
index 0000000..0d2f07a
--- /dev/null
+++ b/1-10/1.hs
@@ -0,0 +1,3 @@
+myLast [] = error "empty list"
+myLast [x] = x
+myLast (_:xs) = myLast xs
diff --git a/1-10/10.hs b/1-10/10.hs
new file mode 100644
index 0000000..0cc6a9a
--- /dev/null
+++ b/1-10/10.hs
@@ -0,0 +1,9 @@
+pack :: Eq a => [a] -> [[a]]
+
+pack [] = []
+pack [x] = [[x]]
+pack (x:xs) = if x == head y then (x:y):ys else [x]:(y:ys) where (y:ys) = pack xs
+
+encode :: Eq a => [a] -> [(Int, a)]
+
+encode xs = map (\x -> (length x, head x)) $ pack xs
diff --git a/1-10/2.hs b/1-10/2.hs
new file mode 100644
index 0000000..baeb995
--- /dev/null
+++ b/1-10/2.hs
@@ -0,0 +1,4 @@
+myButLast [] = error "empty list"
+myButLast [x] = error "list with single element"
+myButLast (x:[y]) = x
+myButLast (x:xs) = myButLast xs
diff --git a/1-10/3.hs b/1-10/3.hs
new file mode 100644
index 0000000..660ae64
--- /dev/null
+++ b/1-10/3.hs
@@ -0,0 +1,5 @@
+elementAt [] _ = error "out of range"
+elementAt (x:_) 1 = x
+elementAt (_:xs) k
+ | k < 1 = error "out of range" -- deal with the infinite list
+ | otherwise = elementAt xs (k - 1)
diff --git a/1-10/4.hs b/1-10/4.hs
new file mode 100644
index 0000000..28c1423
--- /dev/null
+++ b/1-10/4.hs
@@ -0,0 +1,2 @@
+myLength [] = 0
+myLength (_:xs) = 1 + myLength xs
diff --git a/1-10/5.hs b/1-10/5.hs
new file mode 100644
index 0000000..b18724c
--- /dev/null
+++ b/1-10/5.hs
@@ -0,0 +1,4 @@
+myReverse l = f l []
+ where
+ f [] b = b
+ f (x:xs) b = f xs (x:b)
diff --git a/1-10/6.hs b/1-10/6.hs
new file mode 100644
index 0000000..24f4d8c
--- /dev/null
+++ b/1-10/6.hs
@@ -0,0 +1,3 @@
+import Data.List
+isPalindrome :: Eq a => [a] -> Bool
+isPalindrome l = foldl' (\flag (a, b) -> flag && a == b) True (zip l (reverse l))
diff --git a/1-10/7.hs b/1-10/7.hs
new file mode 100644
index 0000000..0ebd030
--- /dev/null
+++ b/1-10/7.hs
@@ -0,0 +1,4 @@
+data NestedList a = Elem a | List [NestedList a]
+
+flatten (Elem x) = [x]
+flatten (List xs) = concat $ map flatten xs
diff --git a/1-10/8.hs b/1-10/8.hs
new file mode 100644
index 0000000..01d1781
--- /dev/null
+++ b/1-10/8.hs
@@ -0,0 +1,4 @@
+compress :: Eq a => [a] -> [a]
+compress (x:y:xs) = if x == y then compress(y:xs) else x:compress(y:xs)
+compress [] = []
+compress [x] = [x]
diff --git a/1-10/9.hs b/1-10/9.hs
new file mode 100644
index 0000000..e9630f4
--- /dev/null
+++ b/1-10/9.hs
@@ -0,0 +1,5 @@
+pack :: Eq a => [a] -> [[a]]
+
+pack [] = []
+pack [x] = [[x]]
+pack (x:xs) = if x == head y then (x:y):ys else [x]:(y:ys) where (y:ys) = pack xs
diff --git a/1-10/t.hs b/1-10/t.hs
new file mode 100644
index 0000000..6a4448d
--- /dev/null
+++ b/1-10/t.hs
@@ -0,0 +1,2 @@
+grp [] = []
+grp (x:xs) = (x:(filter (==x) xs)):(grp $ filter (/=x) xs)