Zip with default value instead of dropping values?

后端 未结 9 814
夕颜
夕颜 2020-12-01 18:30

I\'m looking for a function in haskell to zip two lists that may vary in length.
All zip functions I could find just drop all values of a lists that is longer than the o

9条回答
  •  遥遥无期
    2020-12-01 19:11

    You can append an inifinte list of 0 or 1 to each list and then take the number you need from the result zipped list:

    zipWithDefault :: a -> b -> [a] -> [b] -> [(a,b)]
    zipWithDefault da db la lb = let len = max (length la) (length lb)
                                     la' = la ++ (repeat da)
                                     lb' = lb ++ (repeat db)
                                 in take len $ zip la' lb'  
    

提交回复
热议问题