| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The List class and actions for lists | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Synopsis | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Documentation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The List typeclass | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class (MonadPlus l, Monad (ItemM l)) => List l where | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
data ListItem l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
List operations for MonadPlus | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cons :: MonadPlus m => a -> m a -> m a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Prepend an item to a MonadPlus | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fromList :: MonadPlus m => [a] -> m a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Convert a list to a MonadPlus > fromList [] :: Maybe Int Nothing > fromList [5] :: Maybe Int Just 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter :: MonadPlus m => (a -> Bool) -> m a -> m a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filter for any MonadPlus > filter (> 5) (Just 3) Nothing | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
repeat :: MonadPlus m => a -> m a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Standard list operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
takeWhile :: List l => (a -> Bool) -> l a -> l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
genericTake :: (Integral i, List l) => i -> l a -> l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
scanl :: List l => (a -> b -> a) -> a -> l b -> l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
transpose :: List l => l (l a) -> l (l a) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
zip :: List l => l a -> l b -> l (a, b) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
zipWith :: List l => (a -> b -> c) -> l a -> l b -> l c | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Non standard List operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foldrL :: List l => (a -> ItemM l b -> ItemM l b) -> ItemM l b -> l a -> ItemM l b | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foldr for Lists. the result and 'right side' values are monadic actions. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foldlL :: List l => (a -> b -> a) -> a -> l b -> ItemM l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
An action to do foldl for Lists | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foldl1L :: List l => (a -> a -> a) -> l a -> ItemM l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toList :: List l => l a -> ItemM l [a] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
An action to transform a List to a list > runIdentity $ toList "hello!" "hello!" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
lengthL :: (Integral i, List l) => l a -> ItemM l i | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Consume a list (execute its actions) and return its length > runIdentity $ lengthL [1,2,3] 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
lastL :: List l => l a -> ItemM l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Consume all items and return the last one > runIdentity $ lastL "hello" 'o' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
merge2On :: (Ord b, List l) => (a -> b) -> l a -> l a -> l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Merge two lists sorted by a criteria given the criteria > merge2On id "01568" "239" "01235689" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mergeOn :: (Ord b, List l) => (a -> b) -> l (l a) -> l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Merge many lists sorted by a criteria given the criteria > mergeOn length [["hi", "hey", "hello"], ["cat", "falcon"], ["banana", "cucumber"]] ["hi","cat","hey","hello","banana","falcon","cucumber"] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations useful for monadic lists | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
execute :: List l => l a -> ItemM l () | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Execute the monadic actions in a List | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
joinM :: List l => l (ItemM l a) -> l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Transform a list of actions to a list of their results > joinM [Identity 4, Identity 7] [4,7] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mapL :: List l => (a -> ItemM l b) -> l a -> l b | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
iterateM :: List l => (a -> ItemM l a) -> ItemM l a -> l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Monadic version of iterate. Can be used to produce trees given a children of node function. import Data.List.Tree (bfsLayers) take 3 $ bfsLayers (iterateM (\i -> [i*2, i*2+1]) [1] :: ListT [] Int) [[1],[2,3],[4,5,6,7]] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
takeWhileM :: List l => (a -> ItemM l Bool) -> l a -> l a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations for non-monadic lists | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sortOn :: Ord b => (a -> b) -> [a] -> [a] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Convert between List types | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
transformListMonad :: (List l, List k) => (forall x. ItemM l x -> ItemM k x) -> l a -> k a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Transform the underlying monad of a list given a way to transform the monad > import Data.List.Tree (bfs) > bfs (transformListMonad (\(Identity x) -> [x, x]) "hey" :: ListT [] Char) "hheeeeyyyyyyyy" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
listStateJoin :: (List l, List k, ItemM l ~ StateT s (ItemM k)) => l a -> ItemM l (k a) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
listStateJoin can transform a ListT (StateT s m) a to a StateT s m (ListT m a). When iterating a list, a state is already maintained and passed along in the form of the location along the list. This joins the inner StateT s into the list. The list will fork the state given to it and won't share its changes. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Produced by Haddock version 2.7.2 |