【发布时间】:2018-09-04 13:05:20
【问题描述】:
它的签名必须是这样的:
groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
我的函数应该根据元素是否相等或其中是否存在严格单调递增的部分列表,将输入列表的元素分组到输出列表。
终止示例:
-- groupBy (==) groups the equal elements
groupBy (==) [0, 0, 1, 1, 2, 2] == [[0, 0], [1, 1], [2, 2]]
groupBy (==) [0, 1, 2] == [[0], [1], [2]]
-- groupBy (<) returns the strictly monotonous incrementing partlists
groupBy (<) [0, 1, 2, 1, 2, 3] == [[0,1,2],[1,2,3]]
groupBy (<) [3, 4, 5] == [[3, 4, 5]]
groupBy (>=) [3, 3, 1, 5] == [[3,3,1],[5]] --- monotonous decrementing
-- partlists, where the consecutive elements' difference is 1:
groupBy (\x y -> abs (x - y) == 1) [0, 1, 3, 4] == [[0, 1], [3, 4]]
groupBy (\x y -> abs (x - y) == 1) [1, 2, 3, 2, 1, 10, 11] == [[1,2,3,2,1],[10,11]]
提前感谢您的帮助:)
【问题讨论】:
-
就写吧。你具体卡在哪里了?
-
甚至不知道从哪里开始,但这是明天的任务:(
-
网上有很多很好的Haskell教程;随时咨询某人以获取有关从哪里开始的一些想法,并在您编写的代码遇到具体问题时回来。至于你的另一半评论……我高中的乐队老师在他的门上贴了一张纸条:“你的计划不周不构成我的紧急情况。”。内化改变了我的生活,我希望你能像我一样发现它有帮助(尽管在你第一次糟糕的计划紧急情况下听到它确实很糟糕)。
标签: haskell functional-programming