zhaw-fup/Exercises/exercise-1/Solution.hs
2024-03-14 19:03:10 +01:00

67 lines
1.3 KiB
Haskell

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