本文介绍了什么是在Haskell中添加列表的惯用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想在Haskell中添加两个列表。



以下是我所做的:

  addLists ::(Integral a)=> [a]  - > [a]  - > [a] 
addLists xs ys = map add $ zip xs ys
其中add(x,y)= x + y


解决方案

库函数,它使用提供的函数组合两个列表。它完全符合你在这里的要求,并得到:

  addLists = zipWith(+)

它使用(+)来组合作为更多参数给出的列表元素。 / p>

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] -> [a]
addLists xs ys = map add $ zip xs ys
    where add (x, y) = x+y
解决方案

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.

这篇关于什么是在Haskell中添加列表的惯用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 23:01