
There are some functions that I regularly use which are so general that I wonder if they should be in the Prelude. Maybe I have overlooked them or some generalizations of them. Here are the ones that I most frequently use: {- | Compositional power of a function, i.e. apply the function n times to a value. -} nest :: Int -> (a -> a) -> a -> a nest 0 f x = x nest n f x = f (nest (n-1) f x) {- | Split the list at the occurrences of a separator into sub-list. This is a generalization of 'words'. -} chop :: (a -> Bool) -> [a] -> [[a]] chop p s = let (l, s') = break p s in l : case s' of [] -> [] (_:rest) -> chop p rest {- | Returns 'Just' if the precondition is fulfilled. -} toMaybe :: Bool -> a -> Maybe a toMaybe False _ = Nothing toMaybe True x = Just x

{- | Compositional power of a function, i.e. apply the function n times to a value. -} nest :: Int -> (a -> a) -> a -> a nest 0 f x = x nest n f x = f (nest (n-1) f x)
nest n f x = iterate f x !! n That might render 'nest' somewhat superfluous.
{- | Split the list at the occurrences of a separator into sub-list. This is a generalization of 'words'. -} chop :: (a -> Bool) -> [a] -> [[a]] chop p s = let (l, s') = break p s in l : case s' of [] -> [] (_:rest) -> chop p rest
I like 'chop'. It belongs in Data.List, I'd say.
{- | Returns 'Just' if the precondition is fulfilled. -} toMaybe :: Bool -> a -> Maybe a toMaybe False _ = Nothing toMaybe True x = Just x
Could you give an example to show what makes 'toMaybe' a particularly useful function?

At 03:48 28/07/04 +0200, Peter Simons wrote:
ariep writes:
I like 'chop'. It belongs in Data.List, I'd say.
Yes, I also have been needing a function like this frequently. It would be a nice addition.
Me too! #g ------------ Graham Klyne For email: http://www.ninebynine.org/#Contact
participants (4)
-
ariep@xs4all.nl
-
Graham Klyne
-
Henning Thielemann
-
Peter Simons