Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/greencard/ListUtils.lhs

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


\begin{code}

module ListUtils
	( prefix
	, dropSuffix
	, split
	) where


prefix :: Eq a => [a] -> [a] -> Maybe [a] -- what's left
prefix [] ls = Just ls
prefix ls [] = Nothing
prefix (x:xs) (y:ys)
  | x == y    = prefix xs ys
  | otherwise = Nothing

dropSuffix :: Eq a => [a] -> [a] -> [a]
dropSuffix s suf
  | suf `isSuffixOf` s = take (length s - length suf) s
  | otherwise          = s

split :: Eq a => a -> [a] -> [[a]]
split a [] = []
split a as = 
 case break (==a) as of
   (xs,[])   -> [xs]
   (xs,_:ys) -> xs:split a ys

isSuffixOf :: Eq a => [a] -> [a] -> Bool
isSuffixOf suf target =
  reverse suf == take (length suf) (reverse target)

\end{code}


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.