Joe Fasel wrote:
>
> S.D.Mechveliani <[EMAIL PROTECTED]> writes
> | Marcin 'Qrczak' Kowalczyk <[EMAIL PROTECTED]> writes
> | > partition _ [] = ([], [])
> | > partition p (x:xs) = if p x then (x:ys, zs) else (ys, x:zs)
> | > where (ys, zs) = partition p xs
> | >
> | > runs your example in constant space.
> |
> |
> | Probably, this will do. What the Haskell implementors say?
> | And we have to add that this was suggested 2-3 days ago by someone in
> | this maillist.
>
> This probably works fine for many applications, but of course,
> it is a different function. What applications of partition need
> to preserve list order?
Er, I think it does preserve list order, and is the same function,
except for laziness, as the current prelude version, and in laziness, it
is a win over the current prelude. What am I missing?
On the other hand, just adding a ~ (tilde) to the current partition in
the prelude gives the same result:
partition :: (a -> Bool) -> [a] -> ([a],[a])
partition p xs = foldr select ([],[]) xs
where select x ~(ts,fs) | p x = (x:ts,fs)
| otherwise = (ts,x:fs)
I do think that Qrczak's version is more clear, since it doesn't use
foldr or a local "sub-function".
Thanks,
Matt <[EMAIL PROTECTED]>