zhaw-fup/Exercises/exercise-1/Solution.hs

68 lines
1.3 KiB
Haskell
Raw Normal View History

2024-03-14 18:03:10 +00:00
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