Merging two lists in Haskell

前端 未结 6 1567
轮回少年
轮回少年 2020-11-29 09:26

Can\'t figure out how to merge two lists in the following way in Haskell:

INPUT:  [1,2,3,4,5] [11,12,13,14]

OUTPUT: [1,11,2,12,3,13,4,14,5]         


        
6条回答
  •  广开言路
    2020-11-29 10:01

    EDIT: Take a look at Ed'ka's answer and comments!

    Another possibility:

    merge xs ys = concatMap (\(x,y) -> [x,y]) (zip xs ys)
    

    Or, if you like Applicative:

    merge xs ys = concat $ getZipList $ (\x y -> [x,y]) <$> ZipList xs <*> ZipList ys
    

提交回复
热议问题