better split in day 6

This commit is contained in:
Thorn Avery 2020-12-06 20:03:46 +13:00
parent 52f550dc86
commit 2dc820fc5b
2 changed files with 10 additions and 7 deletions

11
day6.hs
View File

@ -16,10 +16,7 @@ match as bs = (sort . nub) $ filter (`elem` bs) as
split :: Eq a => a -> [a] -> [[a]]
split _ [] = []
split v as = split' v as [] []
split' :: Eq a => a -> [a] -> [a] -> [[a]] -> [[a]]
split' _ [] a as = as ++ [a]
split' v (b:bs) a as
| b == v = split' v bs [] (as ++ [a])
| otherwise = split' v bs (a ++ [b]) as
split d as = chunk : (split d rest)
where
chunk = takeWhile (/= d) as
rest = drop 1 $ dropWhile (/= d) as

6
split.hs Normal file
View File

@ -0,0 +1,6 @@
split :: Eq a => a -> [a] -> [[a]]
split _ [] = []
split d as = chunk : (split d rest)
where
chunk = takeWhile (/= d) as
rest = drop 1 $ dropWhile (/= d) as