diff --git a/Problems 1-10/problem_1.hs b/Problems 1-10/problem_1.hs index 470d3c8..d5585d8 100644 --- a/Problems 1-10/problem_1.hs +++ b/Problems 1-10/problem_1.hs @@ -2,7 +2,7 @@ myLast :: [a] -> a myLast [] = error "Empty list" myLast [s] = s -myLast (hd : tl) = myLast tl +myLast (_ : tl) = myLast tl -- Alternative Solution using fold myLast' :: [a] -> a diff --git a/Problems 1-10/problem_10.hs b/Problems 1-10/problem_10.hs new file mode 100644 index 0000000..b7e3a88 --- /dev/null +++ b/Problems 1-10/problem_10.hs @@ -0,0 +1,11 @@ +encode :: (Eq a, Num n) => [a] -> [(n, a)] +encode = + foldr + ( \x acc -> case acc of + [] -> [(1, x)] + (hd@(hd_n, hd_a) : tl) -> + if x == hd_a + then (hd_n + 1, hd_a) : tl + else (1, x) : hd : tl + ) + [] diff --git a/Problems 1-10/problem_2.hs b/Problems 1-10/problem_2.hs new file mode 100644 index 0000000..f61f11c --- /dev/null +++ b/Problems 1-10/problem_2.hs @@ -0,0 +1,4 @@ +myButLast :: [a] -> a +myButLast [] = error "List length less than 2" +myButLast [a, _] = a +myButLast (hd : tl) = myButLast tl diff --git a/Problems 1-10/problem_3.hs b/Problems 1-10/problem_3.hs new file mode 100644 index 0000000..43c1e75 --- /dev/null +++ b/Problems 1-10/problem_3.hs @@ -0,0 +1,4 @@ +elementAt :: (Num n, Eq n) => [a] -> n -> a +elementAt [] _ = error "Invalid index" +elementAt (hd : _) 1 = hd +elementAt (_ : tl) k = elementAt tl (k - 1) diff --git a/Problems 1-10/problem_4.hs b/Problems 1-10/problem_4.hs new file mode 100644 index 0000000..d247059 --- /dev/null +++ b/Problems 1-10/problem_4.hs @@ -0,0 +1,8 @@ +-- Simple recursive solution +myLength :: (Num n) => [a] -> n +myLength [] = 0 +myLength (_ : tl) = 1 + myLength tl + +-- Solution using fold +myLength' :: (Num n) => [a] -> n +myLength' = foldl (\acc _ -> acc + 1) 0 diff --git a/Problems 1-10/problem_5.hs b/Problems 1-10/problem_5.hs new file mode 100644 index 0000000..dd82de1 --- /dev/null +++ b/Problems 1-10/problem_5.hs @@ -0,0 +1,2 @@ +myReverse :: [a] -> [a] +myReverse = foldl (flip (:)) [] diff --git a/Problems 1-10/problem_6.hs b/Problems 1-10/problem_6.hs new file mode 100644 index 0000000..5cea901 --- /dev/null +++ b/Problems 1-10/problem_6.hs @@ -0,0 +1,2 @@ +isPalindrome :: (Eq a) => [a] -> Bool +isPalindrome = (==) <$> foldl (flip (:)) [] <*> id diff --git a/Problems 1-10/problem_7.hs b/Problems 1-10/problem_7.hs new file mode 100644 index 0000000..a83b06a --- /dev/null +++ b/Problems 1-10/problem_7.hs @@ -0,0 +1,6 @@ +data NestedList a = Elem a | List [NestedList a] + +flatten :: NestedList a -> [a] +flatten x = case x of + Elem e -> [e] + List l -> concatMap flatten l diff --git a/Problems 1-10/problem_8.hs b/Problems 1-10/problem_8.hs new file mode 100644 index 0000000..0d91460 --- /dev/null +++ b/Problems 1-10/problem_8.hs @@ -0,0 +1,10 @@ +compress :: (Eq a) => [a] -> [a] +compress = + foldr + ( \x acc -> + if acc /= [] + && x == head acc + then acc + else x : acc + ) + [] diff --git a/Problems 1-10/problem_9.hs b/Problems 1-10/problem_9.hs new file mode 100644 index 0000000..6a374a6 --- /dev/null +++ b/Problems 1-10/problem_9.hs @@ -0,0 +1,11 @@ +pack :: (Eq a) => [a] -> [[a]] +pack = + foldr + ( \x acc -> case acc of + [] -> [[x]] + (hd : tl) -> + if x == head hd + then (x : hd) : tl + else [x] : hd : tl + ) + []