module W05_1
where
import Prelude hiding (splitAt)
-- Split a list into two at the element whose position is specified by the
-- first argument to the function
--
-- Examples: splitAt 0 [1,2,3,4,5] = ([],[1,2,3,4,5])
-- splitAt 3 [1,2,3,4,5] = ([1,2,3],[4,5])
--
splitAt :: Int -> [a] -> ([a],[a])
splitAt 0 xs = ([], xs)
splitAt n (x:xs) = let
(ys, zs) = splitAt (n - 1) xs
in
(x:ys, zs)
-- Given a nested list (ie, list of lists) of integers, yield a list that
-- contains the largest element of each of the sublists (and zero for empty
-- sublists)
--
-- Example: largest [[1,2,3], [2,3,1], [4,0], []] = [3,3,4,0]
--
largest :: [[Int]] -> [Int]
largest [] = []
largest (xs:xss) = largerThan 0 xs : largest xss
where
largerThan k [] = k
largerThan k (x:xs)
| x > k = largerThan x xs
| otherwise = largerThan k xs