Skip to content

Commit

Permalink
Better solution for evenOddListMerge
Browse files Browse the repository at this point in the history
  • Loading branch information
amogh09 committed Feb 3, 2022
1 parent 82d726f commit 8e57cf3
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions epi_judge_haskell_solutions/evenOddListMerge.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import TestFramework.TestRunner
import Data.List (partition)

evenOddMerge :: [a] -> [a]
evenOddMerge xs = fmap snd ls ++ fmap snd rs where
(ls,rs) = partition (even . fst) ([0..] `zip` xs)

partition :: (a -> Bool) -> [a] -> ([a],[a])
partition f [] = ([],[])
partition f (x:xs) = let (ls,rs) = partition f xs
in if f x then (x:ls,rs) else (ls,x:rs)
evenOddMerge xs =
let (evens, odds) = partition (even . fst) . zip (iterate (+1) 0) $ xs
in fmap snd evens ++ fmap snd odds

main :: IO ()
main = goTest
evenOddMerge
(intList . head)
Expand Down

0 comments on commit 8e57cf3

Please sign in to comment.