拆分列表的函数应该叫什么?

发布于 2024-10-26 08:46:53 字数 785 浏览 2 评论 0原文

我想编写一个函数,根据哪些项目满足给定属性 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 技术交流群。

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

发布评论

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

评论(2

哥,最终变帅啦 2024-11-02 08:46:53

我相信您描述的函数是 list-grouping 包中的 breakBefore

Data.List.Groupinghttp://hackage.haskell.org/packages/archive/list-grouping/0.1.1/doc/html/Data-List-Grouping.html

ghci> breakBefore even [3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6]
[[3,1],[4,1,5,9],[2],[6,5,3,5],[8,9,7,9,3],[2,3],[8],[4],[6],[2],[6]]

I 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

ghci> breakBefore even [3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6]
[[3,1],[4,1,5,9],[2],[6,5,3,5],[8,9,7,9,3],[2,3],[8],[4],[6],[2],[6]]
左秋 2024-11-02 08:46:53

我非常喜欢亚当斯建议的基于“break”一词的名字。该函数有很多可能的变体。这是我所期望的(基于 F# 库中使用的命名)。

名为 breakBefore 的函数将采用一个应在其之前中断的元素:

breakBefore :: Eq a => a -> [a] -> [[a]] 

带有 With 后缀的函数将采用某种直接指定何时中断的函数。如果发生故障,这是函数 a ->您想要的 Bool

breakBeforeWith :: (a -> Bool) -> [a] -> [[a]]

您还可以想象一个带有 By 后缀的函数将采用一个键选择器,并在键更改时中断(这有点像 < code>group by,但你可以有多个具有相同键的组):

breakBeforeBy :: Eq k => (a -> k) -> [a] -> [[a]]

我承认名称有点长 - 也许唯一真正有用的函数就是你想要的函数。然而,F# 库似乎相当一致地使用这种模式(例如,sortsortBy 采用键选择器,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:

breakBefore :: Eq a => a -> [a] -> [[a]] 

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 function a -> Bool that you wanted:

breakBeforeWith :: (a -> Bool) -> [a] -> [[a]]

You could also imagine a function with By suffix would take a key selector and break when the key changes (which is a bit like group by, but you can have multiple groups with the same key):

breakBeforeBy :: Eq k => (a -> k) -> [a] -> [[a]]

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 and sortWith 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).

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