如何在 haskell 中获取一个列表并生成两个列表作为输出?
例如,我正在研究的一个问题要求创建一个程序,该程序需要一个 谓词和一个列表并返回两个列表的列表,第一个 输入列表中传递谓词的那些元素, 其次是那些不按顺序排列的,例如
tear (>5) [1,10,2,12,3,13]
=> [[10,12,13],[1,2,3]]
到目前为止我有什么:
tear f [] = []
tear f (x:xs)
| f x = x: tear f xs
| otherwise = tear f xs
产生
tear (>5) [1,10,2,12,3,13]
[10,12,13]
最佳答案
按照@WillemVanOnsem 的建议使用二元组:
tear f [] = ([],[])
tear f (x:xs)
| f x = (x:y,z)
| otherwise = (y,x:z)
where (y,z) = tear f xs
关于list - 如何在 Haskell 中生成两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54062945/