diff --git a/Exercises/exercise-1/Solution.hs b/Exercises/exercise-1/Solution.hs new file mode 100644 index 0000000..eee32b8 --- /dev/null +++ b/Exercises/exercise-1/Solution.hs @@ -0,0 +1,67 @@ +import Data.List (sort) + +-------------------- +-- Exercise 1 +-------------------- +flatSort :: [[Integer]] -> [Integer] +flatSort x = concat (map (\y -> sort y) x) + +-- >>> flatSort [[1,2,3], [1,3,2]] +-- [1,2,3,1,2,3] +-- >>> flatSort [[1], [2]] +-- [1,2] +-- >>> flatSort [[1], [1,2], [1,2,3]] +-- [1,1,2,1,2,3] + +-------------------- +-- Exercise 2 +-------------------- +xs :: [[Integer]] +xs = [[1], [2, 1]] +-- >>> (concat xs) /= (flatSort xs) +-- True + +-------------------- +-- Exercise 3 +-------------------- +flatSort' xs = + case xs of + [] -> [] + x:xs -> sort x ++ flatSort' xs + +-------------------- +-- Exercise 4 +-------------------- +initial :: String -> String -> Bool +initial xs ys = + case xs of + [] -> True + x:xs -> ( + case ys of + [] -> False + y:ys -> + if x == y then + initial xs ys + else + False) + +-- >>> initial "abc" "abcd" +-- True +-- >>> initial "abc" "xabcd" +-- False + +-------------------- +-- Exercise 5 +-------------------- +substring :: String -> String -> Bool +substring xs ys = + case ys of + [] -> False + _ -> initial xs ys || substring xs (tail ys) + +-- >>> substring "abc" "xadbcd" +-- False +-- >>> substring "abc" "xadbabccd" +-- True +-- >>> substring "abc" "ab" +-- False