HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulefmlist-0.9.4Haskell98

Data.FMList

FoldMap lists: lists represented by their foldMap function.

Examples:

-- A right-infinite list
c = 1 `cons` c
-- A left-infinite list
d = d `snoc` 2
-- A middle-infinite list ??
e = c `append` d
*> head e
1
*> last e
2
  • 1 type
  • 30 values
  • Packagefmlist-0.9.4
  • Exports33
  • LanguageHaskell98
  • LicenceBSD-3-Clause
  • SourceFMList.hs
newtypenewtype FMList a
#

FMList is a foldMap function wrapped up in a newtype.

Constructors

Instances11Monad, Functor, MonadFail, Applicative, Foldable, Traversable, …
valuetransform
  1. :: forall m. Monoid m => (a -> m) -> b -> m
  2. -> FMList b
  3. -> FMList a
#

The function transform transforms a list by changing the map function that is passed to foldMap.

It has the following property:

transform a . transform b = transform (b . a)

For example:

  •   m >>= g
  • = flatten (fmap g m)
  • = flatten . fmap g $ m
  • = transform foldMap . transform (. g) $ m
  • = transform ((. g) . foldMap) m
  • = transform (\f -> foldMap f . g) m

Construction

8 declarations
methodempty :: f a
#

The identity of <|>

empty <|> a     == a
a     <|> empty == a

Basic functions

8 declarations

Folding

10 declarations
methodtoList :: t a -> [a]
#

List of elements of a structure, from left to right. If the entire list is intended to be reduced via a fold, just fold the structure directly bypassing the list.

Examples

Basic usage:

Example1 expression
toList Nothing[]
Example1 expression
toList (Just 42)[42]
Example1 expression
toList (Left "foo")[]
Example1 expression
toList (Node (Leaf 5) 17 (Node Empty 12 (Leaf 8)))[5,17,12,8]

For lists, toList is the identity:

Example1 expression
toList [1, 2, 3][1,2,3]
valuefoldMapA
  1. :: (Foldable t, Applicative f, Monoid m)
  2. => a -> f m
  3. -> t a
  4. -> f m
#

Map each element of a structure to an action, evaluate these actions from left to right, and concat the monoid results.

Unfolding

5 declarations
valuerepeat :: a -> FMList a
#

repeat buids an infinite list of a single value. While infinite, the result is still accessible from both the start and end.

valuecycle :: FMList a -> FMList a
#

cycle repeats a list to create an infinite list. It is also accessible from the end, where last (cycle l) equals last l.

valueunfold :: (b -> FMList (Either b a)) -> b -> FMList a
#

unfold builds a list from a seed value. The function takes the seed and returns an FMList of values. If the value is Right a, then a is appended to the result, and if the value is Left b, then b is used as seed value in a recursive call.

A simple use of unfold (simulating unfoldl):

*> unfold (\b -> if b == 0 then empty else Left (b-1) `pair` Right b) 10
fromList [1,2,3,4,5,6,7,8,9,10]
valueunfoldr :: (b -> Maybe (a, b)) -> b -> FMList a
#

unfoldr builds an FMList from a seed value from left to right. The function takes the element and returns Nothing if it is done producing the list or returns Just (a,b), in which case, a is a appended to the result and b is used as the next seed value in a recursive call.

A simple use of unfoldr:

*> unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10
fromList [10,9,8,7,6,5,4,3,2,1]