这段代码如何翻译成 Haskell?

发布于 2024-12-05 12:57:40 字数 309 浏览 2 评论 0原文

我正在与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪异←思 2024-12-12 12:57:40

抱歉,但我忍不住使用更好的算法...

let goodNumber n = (0 <= n && n < N)
let list1 = sort (filter goodNumber list2)

编辑:事后看来,这有点矫枉过正,因为OP首先试图实现排序算法。

Sorry, but I can't help but use a better algorithm...

let goodNumber n = (0 <= n && n < N)
let list1 = sort (filter goodNumber list2)

Edit: In hindsight this is a little bit of overkill, since the OP was trying to implement a sorting algo in the first place.

旧伤还要旧人安 2024-12-12 12:57:40

一步一步进行:

list2 = 数字列表

我们将其视为给定的,因此 list2 仍然只是一个数字列表。

for i from 0 to N // N 是正整数

在 Haskell 中执行此操作的正确方法通常是使用列表。惰性意味着仅在使用时才计算值,因此从 0 到 N 遍历列表最终与此处的循环相同。因此,只要 [0..n] 就可以了;我们只需要弄清楚如何处理它。

对于 list2 中的每个数字

给定“for every”,我们可以推断出我们需要遍历整个 list2 ;我们还不知道我们用它做什么。

如果 number == i,则添加到 list1

我们正在构建 list1,因此理想情况下我们希望它成为表达式的最终结果。这也意味着在每个递归步骤中,我们希望结果是“到目前为止”的 list1。为此,我们需要确保在进行过程中传递每个步骤的结果。

因此,开始讨论它的实质:

filter 函数查找列表中与某个谓词匹配的所有元素;我们将在这里使用 filter (== i) list2 来查找我们想要的内容,然后将其附加到上一步的结果中。因此,每个步骤将如下所示:

step :: (Num a) => [a] -> a -> [a]
step list1 i = list1 ++ filter (== i) list2

处理内部循环。退一步来说,我们需要对列表 [0..n] 中的每个值 i 运行此函数,并传递 list1 值一起迈出每一步。这正是折叠函数的用途,在本例中 step 正是我们左折叠所需的:

list2 :: (Num a) => [a]
list2 = -- whatever goes here...

step :: (Num a) => [a] -> a -> [a]
step list1 i = list1 ++ filter (== i) list2

list1 :: (Num a) => a -> [a]
list1 n = foldl step [] [0..n]

如果您想知道递归在哪里,请使用 filterfoldl 正在为我们做这件事。根据经验,当有更高级别的函数可以为您执行递归时,通常最好避免直接递归。


也就是说,这里的算法在很多方面都很愚蠢,但我不想深入探讨这一点,因为它似乎会分散你对实际问题的注意力,而不是有帮助。

Going step by step:

list2 = list of numbers

We'll take this as a given, so list2 is still just a list of numbers.

for i from 0 to N // N being a positive integer

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.

for each number in list2

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.

if number == i, add to list1

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 the list1 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 use filter (== 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:

step :: (Num a) => [a] -> a -> [a]
step list1 i = list1 ++ filter (== i) list2

That handles the inner loop. Stepping back outwards, we need to run this for each value i from the list [0..n], with the list1 value being passed along at each step. This is exactly what fold functions are for, and in this case step is exactly what we need for a left fold:

list2 :: (Num a) => [a]
list2 = -- whatever goes here...

step :: (Num a) => [a] -> a -> [a]
step list1 i = list1 ++ filter (== i) list2

list1 :: (Num a) => a -> [a]
list1 n = foldl step [] [0..n]

If you're wondering where the recursion is, both filter and foldl 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.

何以笙箫默 2024-12-12 12:57:40
let list1 = sort [a | a<-list2, a>=0, a<=N]

a<-list2 选取 list2 的每个数字
a>=0, a<=N 检查所选号码是否满足所有这些条件
如果满足条件,则将 a 放入新列表中
一旦 list2 的所有元素都被检查并放入一个新列表中,我们就对此进行排序
结果列表被分配给list1

let list1 = sort [a | a<-list2, a>=0, a<=N]

a<-list2 picks up each number of list2
a>=0, a<=N check if the picked number meets ALL these conditions
if 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文