haskell - 什么叫拆分列表的函数?

标签 haskell f# functional-programming sml split

我想编写一个函数,根据满足给定属性的项将列表拆分为子列表 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 . 我应该叫它什么??

最佳答案

我相信您所描述的功能是 breakBefore来自 list-grouping包裹。
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]]

关于haskell - 什么叫拆分列表的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5438558/

相关文章:

c# - F# `try` -`with` block 内的转换和异常过滤器

algorithm - 红黑树包含过多的黑色节点和过少的红色节点

functional-programming - 在函数式编程中使用心理图像

algorithm - Haskell 中的 Maze Solver - 确定迷宫是否无法解决

haskell - 自然数的初代数

haskell - 在处理许多不相关的类型时避免样板

haskell - 要么是 b。两年后的不同 Hoogle 结果?

f# - 使用F#生成类型提供程序类型作为泛型类型参数

scala - Scala的动机是否强调形式语言理论和良好的风格?

haskell - 为什么每个 haskell 类型都被提升(即被 _|_ 占用)?