数字在列表倍数 [英] Multiples of Numbers in a List

查看:148
本文介绍了数字在列表倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将打印给定数字的合并,排序的列表清单的倍数?

 取10(倍数[4,5])
 

  4,5,8,10,12,15,16,20,24,25
 

我有它的工作尺寸为2或1的列表,但我需要一个更通用的解决方案:)

解决方案

 多XS = X * Y | Y'LT  -  [1 ..]中,X, - 的xs]
 

这是应该做的。主要的问题是,它是有点难以控制多少个号码,你应该

要避免的结果多人数相等的结果列表上应用Data.List.nub。这是不是非常复杂,可以做到更快,但做这项工作。

How would I print the multiples of a list of given numbers in a merged, sorted list?

I.e.

take 10 (multiples [4,5])

gives

4,5,8,10,12,15,16,20,24,25

I've got it working for lists of size 2 or 1 but I need a more general solution :)

解决方案

multi xs = [x*y | y <- [1..], x <- xs ]

That should do. The main problem is that it is a little hard to control how many numbers you should take.

To avoid multiple equal numbers in the result apply Data.List.nub on the resulting list. This isn't terribly sophisticated and can be done faster but does the job.

这篇关于数字在列表倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆