Difference between revisions of "Instances of List and Maybe"
Jump to navigation
Jump to search
(Class instances of list) |
(No difference)
|
Revision as of 14:03, 20 June 2018
This pages lists all class instances of List in base, with examples of use. You can run the examples in GHCi.
Semigroup
> import Data.Semigroup
> [1,2] <> [3,4]
[1,2,3,4]
Monoid
> [] == mempty
True
Functor
> fmap (+1) [2, 3]
[3, 4]
Applicative
> [(+1),(+2)] <*> [3,4]
[4,5,5,6]
Alternative
> import Control.Applicative
> [1] <|> empty <|> [2]
[1,2]
Monad
> (\lst -> do x<-lst; return (x*x)) [1,2,3]
[1,4,9]
MonadPlus
> import Control.Monad
> (\lst -> do x<-lst; guard (x*x<5); return (x*x)) [1,2,3]
[1,4]
Foldable
> foldr (+) 0 [1,2]
3
Traversable
> traverse (\x -> [0..x]) [0..2]
[[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[0,1,2]]
> sequenceA [[1,2],[3,4]]
[[1,3],[1,4],[2,3],[2,4]]