What is an idiomatic way to add lists in Haskell?

前端 未结 3 1209
梦谈多话
梦谈多话 2021-01-04 15:44

Suppose I want to add two lists in Haskell. What is the most usual way to do this?

Here\'s what I did:

addLists :: (Integral a) => [a] -> [a]          


        
3条回答
  •  情话喂你
    2021-01-04 16:07

    There is a zipWith library function that combines two lists by using a supplied function. It does exactly what you want here and you get:

    addLists = zipWith (+)
    

    This uses (+) to combine the elements of lists given as further arguments.

提交回复
热议问题