拆分列表的函数应该叫什么?
我想编写一个函数,根据哪些项目满足给定属性 p
将列表拆分为子列表。我的问题是如何调用该函数。我将在 Haskell 中给出示例,但在 F# 或 ML 中也会出现同样的问题。
split :: (a -> Bool) -> [a] -> [[a]] --- split lists into list of sublists
连接起来的子列表是原始列表:
concat (split p xss) == xs
每个子列表都满足 initial_p_only p
属性,也就是说 (A) 子列表以满足 p
的元素开头,并且是因此不为空,并且 (B) 没有其他元素满足 p
:
initial_p_only :: (a -> Bool) -> [a] -> Bool
initial_p_only p [] = False
initial_p_only p (x:xs) = p x && all (not . p) xs
所以准确地说,
all (initial_p_only p) (split p xss)
如果原始列表中的第一个元素不满足 p
,则拆分失败。
该函数需要调用除 split
之外的其他名称。 我应该叫它什么?
I want to write a function that splits lists into sublists according to what items satisfy a given property p
. My question is what to call the function. I'll give examples in Haskell, but the same problem would come up in F# or ML.
split :: (a -> Bool) -> [a] -> [[a]] --- split lists into list of sublists
The sublists, concatenated, are the original list:
concat (split p xss) == xs
Every sublist satisfies the initial_p_only p
property, which is to say (A) the sublist begins with an element satisfying p
—and is therefore not empty, and (B) no other elements satisfy p
:
initial_p_only :: (a -> Bool) -> [a] -> Bool
initial_p_only p [] = False
initial_p_only p (x:xs) = p x && all (not . p) xs
So to be precise about it,
all (initial_p_only p) (split p xss)
If the very first element in the original list does not satisfy p
, split fails.
This function needs to be called something other than split
. What should I call it??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您描述的函数是 list-grouping 包中的
breakBefore
。Data.List.Grouping
:http://hackage.haskell.org/packages/archive/list-grouping/0.1.1/doc/html/Data-List-Grouping.htmlI believe the function you're describing is
breakBefore
from the list-grouping package.Data.List.Grouping
: http://hackage.haskell.org/packages/archive/list-grouping/0.1.1/doc/html/Data-List-Grouping.html我非常喜欢亚当斯建议的基于“break”一词的名字。该函数有很多可能的变体。这是我所期望的(基于 F# 库中使用的命名)。
名为
breakBefore
的函数将采用一个应在其之前中断的元素:带有
With
后缀的函数将采用某种直接指定何时中断的函数。如果发生故障,这是函数a ->您想要的 Bool
:您还可以想象一个带有
By
后缀的函数将采用一个键选择器,并在键更改时中断(这有点像 < code>group by,但你可以有多个具有相同键的组):我承认名称有点长 - 也许唯一真正有用的函数就是你想要的函数。然而,F# 库似乎相当一致地使用这种模式(例如,
sort
、sortBy
采用键选择器,sortWith
采用比较器函数)。也许可以为更多列表处理函数提供这三种变体(并且为这三种类型提供一些一致的命名模式是个好主意)。
I quite like some name based on the term "break" as adamse suggests. There are quite a few possible variants of the function. Here is what I'd expect (based on the naming used in F# libraries).
A function named just
breakBefore
would take an element before which it should break:A function with the
With
suffix would take some kind of function that directly specifies when to break. In case of brekaing this is the functiona -> Bool
that you wanted:You could also imagine a function with
By
suffix would take a key selector and break when the key changes (which is a bit likegroup by
, but you can have multiple groups with the same key):I admit that the names are getting a bit long - and maybe the only function that is really useful is the one you wanted. However, F# libraries seem to be using this pattern quite consistently (e.g. there is
sort
,sortBy
taking key selector andsortWith
taking comparer function).Perhaps it is possible to have these three variants for more of the list processing functions (and it's quite good idea to have some consistent naming pattern for these three types).