ruby while循环转换为haskell [英] ruby while loop translated to haskell
问题描述
我刚开始学习一些Haskell和函数式编程,但是我发现它很难得到它的一个结果:)
我正在尝试翻译向Haskell提供一小段 ruby 代码(因为我喜欢函数式编程的概念,并且Haskell提出并且甚至更多,因为我来自一个数学领域,Haskell似乎非常数学):
类整数
def factorial
f = 1;因为我在1 ..自己; f * = i;结束; f
end
end
boundary = 1000
m = 0
#棕色数字 - 整数对(m,n)其中n阶乘等于m
的平方根,而m <=边界
n = 0
,而n <=边界
如果((n.factorial + 1)==(m ** 2))
n + = 1
end
$ b,则放置(#{m},#{n}) $ bm + = 1
end
我只能算出如何进行阶乘:
let factorial n = product [1..n]
我无法弄清楚如何做Haskell中的while循环或等价物,尽管我发现了一些对我来说很难理解的例子。
这个想法是循环从 0
(或者 1
)并且继续(with增量为1),直到达到边界(在我的代码中 1000
)。存在边界的原因是因为我正在考虑开始并行执行相同操作但按不同间隔执行的任务,所以我预期的结果会更快地返回(一次操作将在 1 $上完成c $ c>至
10000
,另一个位于 10000
至 100000
如果有人能够帮助解决这个问题,我将非常感激:)
试试这个:
let results = [(x,y)| x < - [1..1000],y < - [1..1000],1 + fac x == y * y]
其中fac n = product [1..n]
这是一个列表理解。有关此处的更多信息。
将其映射到您的Ruby代码
-
m
和n中的嵌套循环
被替换为x
和y
。基本上,在指定范围内(本例中为1到1000)迭代x
和y
。
- 最后的检查是您获取布朗数字的过滤条件。
-
其中
允许我们创建一个辅助函数来计算阶乘。
请注意,我们可以计算阶乘就像这样:
(1 + product [1..x])== y * y
最终,左侧的(x,y)
意味着它返回一个元组列表(x,y)
,这是你的布朗数字。
好的,这个应该在.hs文件中工作:
results :: [(Integer,Integer)] - 使用Int修复溢出问题
results = [(x,y)| x < - [1..1000],y < - [1..1000],fac x == y * y]
其中fac n = product [1..n]
I've just started learning a bit of Haskell and functional programming, but I find it very difficult getting a hang of it :)
I am trying to translate a small piece of ruby code to Haskell (because I like the concept functional programming and Haskell proposes and even more because I come from a mathematics field and Haskell seems very mathematical):
class Integer
def factorial
f = 1; for i in 1..self; f *= i; end; f
end
end
boundary = 1000
m = 0
# Brown Numbers - pair of integers (m,n) where n factorial is equal with square root of m
while m <= boundary
n = 0
while n <= boundary
puts "(#{m},#{n})" if ((n.factorial + 1) == (m ** 2))
n += 1
end
m += 1
end
I could only figure out how to do factorials:
let factorial n = product [1..n]
I cannot figure out how to do the while loops or equivalent in Haskell, even though I found some examples that were far to confusing for me.
The idea is that the loops start from 0
(or 1
) and continue (with an increment of 1) until it reaches a boundary (in my code is 1000
). The reason there is a boundary is because I was thinking of starting parallel tasks that do the same operation but on different intervals so the results that I expect are returned faster (one operation would be done on 1
to 10000
, another on 10000
to 100000
, etc.).
I would really appreciate it if anyone could help out with this :)
Try this:
let results = [(x,y) | x <- [1..1000], y <- [1..1000] ,1 + fac x == y*y]
where fac n = product [1..n]
This is a list comprehension. More on that here.
To map it to your Ruby code,
- The nested loops in
m
andn
are replaced withx
andy
. Basically there is iteration over the values ofx
andy
in the specified ranges (1 to 1000 inclusive in this case). - The check at the end is your filter condition for getting Brown numbers.
where
allows us to create a helper function to calculate the factorial.
Note that instead of a separate function, we could have computed the factorial in place, like so:
(1 + product[1..x]) == y * y
Ultimately, the (x,y)
on the left side means that it returns a list of tuples (x,y)
which are your Brown numbers.
OK, this should work in your .hs file:
results :: [(Integer, Integer)] --Use instead of `Int` to fix overflow issue
results = [(x,y) | x <- [1..1000], y <- [1..1000] , fac x == y*y]
where fac n = product [1..n]
这篇关于ruby while循环转换为haskell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!