Solve first lab
This commit is contained in:
parent
f3d045073d
commit
4d3cce8130
1 changed files with 67 additions and 0 deletions
67
Exercises/exercise-1/Solution.hs
Normal file
67
Exercises/exercise-1/Solution.hs
Normal file
|
@ -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
|
Loading…
Reference in a new issue