[英]What are those functional functions called?
I'm looking for a functional way to implement this: 我正在寻找一种实现此功能的方法:
list = [a b c d e f]
foo(list, 3) = [[a d] [b e] [c f]]
A potential solution is: 潜在的解决方案是:
foo(list,spacing) = zip(goo(list,spacing))
Where, for example, 例如,
goo([a b c d e f],3) = [[a b c] [d e f]]
What is foo
and goo
usually called, so I can look for existing solutions rather than reinventing the wheel? foo
和goo
通常称为什么,因此我可以寻找现有的解决方案,而不必重新发明轮子了?
Notes: Rather than trying to explain with words, I've just shown examples that'll be hopefully much easier to get. 注意:我只是想展示一些示例,希望可以更轻松地进行操作,而不是尝试用语言解释。 Arbitrary syntax for broader understanding. 任意语法可提供更广泛的理解。
I do not think there is a built-in function for that. 我认为没有内置函数。 It's easy and nice to implement. 这很容易实现。
I know you do not want the implementation, but one of the tags was Haskell so maybe you want to see this 我知道您不希望实现,但是其中一个标记是Haskell,所以也许您想看看
p :: Int -> [a] -> [[a]]
p n xs = [ [x | (x ,y) <- ys , y `mod` n == i] | i <- [0 .. n - 1] , let ys = zip xs [0 .. ]]
That is pretty functional. 那很实用。
Your goo
function is drop
with flipped arguments. 您的goo
函数是带有翻转参数的drop
。 Given that, you can implement foo
almost like you say in your question: 鉴于此,您可以像在问题中所说的那样实现foo
:
let foo list spacing = zip list (drop spacing list)
This still doesn't exactly give the result you need though, but close: 尽管这仍然不能完全提供您需要的结果,但是请关闭:
Prelude> foo "abcdef" 3
[('a','d'),('b','e'),('c','f')]
EDIT: 编辑:
Reading more carefully, your goo
function is splitAt
with flipped arguments. 仔细阅读后,您的goo
函数将带翻转参数的splitAt
。 Given that, foo
can be defined like this: 鉴于此,可以这样定义foo
:
let foo list spacing = (uncurry zip) $ splitAt spacing list
Which is the same as: 与以下内容相同:
let foo list spacing = let (left, right) = splitAt spacing list
in zip left right
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.