使用递归进行分区

时间:2015-11-08 22:20:10

标签: haskell

试着学习一些haskell,我知道这个功能

partition :: (a -> Bool) -> [a] -> ([a], [a])

我可以使用递归而不是使用函数。例如,id有我自己定义的函数

partition':: (a -> Bool) -> [a] -> ([a], [a])

将使用递归

1 个答案:

答案 0 :(得分:3)

这是一个递归定义:

partition':: (a -> Bool) -> [a] -> ([a], [a])
partition' _ [] = ([],[])
partition' f (x:xs) | f x = (x:matched,notMatched)
                    | otherwise = (matched,x:notMatched)
                    where (matched,notMatched) = partition' f xs