如何取前n项并继续递归?

时间:2021-03-02 15:21:53

I want to achieve something like this:

我想实现这样的目标:

f :: [Int] -> [Int]
f (x@[_,_,_,_]:xs) = g x : f xs

g :: [Int] -> Int
g = ...

So I want to take the first four items in a list calculate something on them and then do the same thing with the remaining items creating a new list of calculated values. It is like a combination of map and take I guess. Is there something like this in haskell?

所以我想取列表中的前四项计算它们上的东西然后对其余的项做同样的事情,创建一个新的计算值列表。我猜这就像地图和地图的结合。haskell有这样的东西吗?

I know I could do something like:

我知道我可以这样做:

map g . chunksOf 4

I just thought it would look much more "haskelly" with recursion

我只是觉得递归看起来更像haskelly

1 个解决方案

#1


6  

p1 : p2 is a pattern that matches a non-empty list if the list's head matches the pattern p1 and its tail matches p2. [p1, p2, p3, p4] is a pattern that matches a four-element list if its elements match p1, p2, p3 and p4 respectively. Thus [_, _, _, _]:xs matches a non-empty list if its first element is a four-element list. Since f takes a list of ints, not a list of lists, this pattern leads to a type error in your function.

p1: p2是一个与非空列表匹配的模式,如果列表的头与模式p1匹配,它的尾部与p2匹配。[p1, p2, p3, p4]是一种模式,如果它的元素分别与p1, p2, p3和p4匹配,那么它就会匹配一个四元素列表。因此[_,_,_,_]:xs匹配一个非空列表,如果它的第一个元素是一个四元素列表。由于f取ints的列表,而不是列表的列表,因此此模式导致函数中的类型错误。

What you want can be expressed as x1 : x2 : x3 : x4 : xs and then where x = [x1, x2, x3, x4]. Of course that's not particularly pretty and it might be more convenient to just use take and drop or splitAt instead (if you really want to do your own recursion, that is).

你想要的可以表示为x1: x2: x3: x4: xs,然后是x = [x1, x2, x3, x4]。当然,这并不是特别漂亮,使用take和drop或者splitAt来代替(如果你真的想自己做递归的话)可能会更方便。

I just thought it would look much more "haskelly" with recursion

我只是觉得递归看起来更像haskelly

I would disagree with that. I'd say it's generally preferable in Haskell to use and combine existing standard library functions instead of writing your own recursive functions.

我不同意这种说法。我想说,在Haskell中,使用和组合现有的标准库函数比编写自己的递归函数更可取。

#1


6  

p1 : p2 is a pattern that matches a non-empty list if the list's head matches the pattern p1 and its tail matches p2. [p1, p2, p3, p4] is a pattern that matches a four-element list if its elements match p1, p2, p3 and p4 respectively. Thus [_, _, _, _]:xs matches a non-empty list if its first element is a four-element list. Since f takes a list of ints, not a list of lists, this pattern leads to a type error in your function.

p1: p2是一个与非空列表匹配的模式,如果列表的头与模式p1匹配,它的尾部与p2匹配。[p1, p2, p3, p4]是一种模式,如果它的元素分别与p1, p2, p3和p4匹配,那么它就会匹配一个四元素列表。因此[_,_,_,_]:xs匹配一个非空列表,如果它的第一个元素是一个四元素列表。由于f取ints的列表,而不是列表的列表,因此此模式导致函数中的类型错误。

What you want can be expressed as x1 : x2 : x3 : x4 : xs and then where x = [x1, x2, x3, x4]. Of course that's not particularly pretty and it might be more convenient to just use take and drop or splitAt instead (if you really want to do your own recursion, that is).

你想要的可以表示为x1: x2: x3: x4: xs,然后是x = [x1, x2, x3, x4]。当然,这并不是特别漂亮,使用take和drop或者splitAt来代替(如果你真的想自己做递归的话)可能会更方便。

I just thought it would look much more "haskelly" with recursion

我只是觉得递归看起来更像haskelly

I would disagree with that. I'd say it's generally preferable in Haskell to use and combine existing standard library functions instead of writing your own recursive functions.

我不同意这种说法。我想说,在Haskell中,使用和组合现有的标准库函数比编写自己的递归函数更可取。