Zip with default value instead of dropping values?

后端 未结 9 813
夕颜
夕颜 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 18:52

    You don't have to compare list lengths. Try to think about your zip function as a function taking only one argument xs and returning a function which will take ys and perform the required zip. Then, try to write a recursive function which recurses on xs only, as follows.

    type Result = [Int] -> [(Int,Int)]
    myZip :: [Int] -> Result
    myZip []     = map (\y -> (0,y)) -- :: Result
    myZip (x:xs) = f x (myZip xs)    -- :: Result
       where f x k = ???             -- :: Result
    

    Once you have found f, notice that you can turn the recursion above into a fold!

提交回复
热议问题