这段代码如何翻译成 Haskell?
我正在与 Haskell 以及使用递归来迭代事物的想法作斗争。
例如,如何
// this might seem silly but I need to do it
list1 = empty list
list2 = list of numbers
for i from 0 to N // N being a positive integer
for each number in list2
if number == i, add to list1
转化为“功能范式”?任何指导将不胜感激。
I'm struggling with Haskell, and the idea of using recursion to iterate over things.
For instance, how would
// this might seem silly but I need to do it
list1 = empty list
list2 = list of numbers
for i from 0 to N // N being a positive integer
for each number in list2
if number == i, add to list1
translate into the 'functional paradigm'? Any guidance would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
抱歉,但我忍不住使用更好的算法...
编辑:事后看来,这有点矫枉过正,因为OP首先试图实现排序算法。
Sorry, but I can't help but use a better algorithm...
Edit: In hindsight this is a little bit of overkill, since the OP was trying to implement a sorting algo in the first place.
一步一步进行:
我们将其视为给定的,因此
list2
仍然只是一个数字列表。在 Haskell 中执行此操作的正确方法通常是使用列表。惰性意味着仅在使用时才计算值,因此从 0 到 N 遍历列表最终与此处的循环相同。因此,只要
[0..n]
就可以了;我们只需要弄清楚如何处理它。给定“for every”,我们可以推断出我们需要遍历整个
list2
;我们还不知道我们用它做什么。我们正在构建
list1
,因此理想情况下我们希望它成为表达式的最终结果。这也意味着在每个递归步骤中,我们希望结果是“到目前为止”的list1
。为此,我们需要确保在进行过程中传递每个步骤的结果。因此,开始讨论它的实质:
filter
函数查找列表中与某个谓词匹配的所有元素;我们将在这里使用filter (== i) list2
来查找我们想要的内容,然后将其附加到上一步的结果中。因此,每个步骤将如下所示:处理内部循环。退一步来说,我们需要对列表
[0..n]
中的每个值i
运行此函数,并传递list1
值一起迈出每一步。这正是折叠函数的用途,在本例中step
正是我们左折叠所需的:如果您想知道递归在哪里,请使用
filter
和foldl
正在为我们做这件事。根据经验,当有更高级别的函数可以为您执行递归时,通常最好避免直接递归。也就是说,这里的算法在很多方面都很愚蠢,但我不想深入探讨这一点,因为它似乎会分散你对实际问题的注意力,而不是有帮助。
Going step by step:
We'll take this as a given, so
list2
is still just a list of numbers.The correct way to do this in Haskell is generally with a list. Laziness means that the values will be computed only when used, so traversing a list from 0 to N ends up being the same thing as the loop you have here. So, just
[0..n]
will do the trick; we just need to figure out what to do with it.Given "for each" we can deduce that we'll need to traverse the entirety of
list2
here; what we do with it, we don't know yet.We're building
list1
as we go, so ideally we want that to be the final result of the expression. That also means that at each recursive step, we want the result to be thelist1
we have "so far". To do that, we'll need to make sure we pass each step's result along as we go.So, getting down to the meat of it:
The
filter
function finds all the elements in a list matching some predicate; we'll usefilter (== i) list2
here to find what we're after, then append that to the previous step's result. So each step will look like this:That handles the inner loop. Stepping back outwards, we need to run this for each value
i
from the list[0..n]
, with thelist1
value being passed along at each step. This is exactly what fold functions are for, and in this casestep
is exactly what we need for a left fold:If you're wondering where the recursion is, both
filter
andfoldl
are doing that for us. As a rule of thumb, it's usually better to avoid direct recursion when there are higher-level functions to do it for you.That said, the algorithm here is silly in multiple ways, but I didn't want to get into that because it seemed like it would distract from your actual question more than it would help.
a<-list2
选取 list2 的每个数字a>=0, a<=N
检查所选号码是否满足所有这些条件如果满足条件,则将 a 放入新列表中
一旦 list2 的所有元素都被检查并放入一个新列表中,我们就对此进行排序
结果列表被分配给list1
a<-list2
picks up each number of list2a>=0, a<=N
check if the picked number meets ALL these conditionsif conditions are met, a is put into a new list
Once all the elements of list2 have been thus checked and put into a new list, we do a sort on this
Resulting list is assigned to list1