From 2dc820fc5b4057d5d17e3d7937912f6c6d66f60b Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Sun, 6 Dec 2020 20:03:46 +1300 Subject: [PATCH] better split in day 6 --- day6.hs | 11 ++++------- split.hs | 6 ++++++ 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 split.hs diff --git a/day6.hs b/day6.hs index 51af372..28ed045 100644 --- a/day6.hs +++ b/day6.hs @@ -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 diff --git a/split.hs b/split.hs new file mode 100644 index 0000000..09b0359 --- /dev/null +++ b/split.hs @@ -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