import Maybe (catMaybes)
safeHead :: [a] -> Maybe a
safeTail :: [a] -> Maybe [a]
safeLast :: [a] -> Maybe a
safeInit :: [a] -> Maybe [a]
safeFunc f [] = Nothing
safeFunc f xs = Just $ f xs
safeHead = safeFunc head
safeTail = safeFunc tail
safeLast = safeFunc last
safeInit = safeFunc init
splitWith :: (a -> Bool) -> [a] -> [[a]]
splitWith _ [] = []
splitWith p xs = helper $ break p xs
where
helper ([],rest) = (splitWith p (dropWhile p rest))
helper (x,rest) = x : (splitWith p (dropWhile p rest))
transpose str = unlines $ loop $ lines str
where
loop xs | all null xs = []
| otherwise = map head xs : (loop (map tail xs))
transpose2 str = unlines $ map catMaybes $ loop $ lines str
where
loop xs | all null xs = []
| otherwise = es : (loop rest)
where
es = map safeHead xs
rest = catMaybes $ map safeTail xs