Difference between revisions of "Instances of List and Maybe"

From HaskellWiki
Jump to navigation Jump to search
(align List and Maybe examples)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
This pages lists all class instances of List in base, with examples of use. You can run the examples in GHCi.
+
This pages compares the [[Typeclassopedia]] classes of List and Maybe, with examples of use. You can run the examples in GHCi.
 
==Semigroup==
 
   
  +
{| class="wikitable"
  +
|+ Semigroup
  +
|- style="text-align: center"
  +
! List
  +
! Maybe
  +
|- style="vertical-align: top"
  +
|
 
<haskell>
 
<haskell>
 
> import Data.Semigroup
 
> import Data.Semigroup
Line 8: Line 13:
 
[1,2,3,4]
 
[1,2,3,4]
 
</haskell>
 
</haskell>
  +
|
  +
<haskell>
  +
> import Data.Semigroup
  +
> (Just [1,2]) <> (Just [3,4])
  +
Just [1,2,3,4]
  +
</haskell>
  +
|}
   
==Monoid==
 
   
  +
{| class="wikitable"
  +
|+ Monoid
  +
|- style="text-align: center"
  +
! List
  +
! Maybe
  +
|- style="vertical-align: top"
  +
|
 
<haskell>
 
<haskell>
> [] == mempty
+
> mempty :: [Int]
  +
[]
True
 
  +
> [1,2] <> mempty
  +
[1,2]
 
</haskell>
 
</haskell>
  +
|
  +
<haskell>
  +
> mempty :: Maybe [Int]
  +
Nothing
  +
> (Just [1,2]) <> mempty
  +
Just [1,2]
  +
</haskell>
  +
  +
|}
   
==Functor==
 
   
  +
{| class="wikitable"
  +
|+ Functor
  +
|- style="text-align: center"
  +
! List
  +
! Maybe
  +
|- style="vertical-align: top"
  +
|
 
<haskell>
 
<haskell>
 
> fmap (+1) [2, 3]
 
> fmap (+1) [2, 3]
 
[3, 4]
 
[3, 4]
  +
> fmap (+1) []
  +
[]
 
</haskell>
 
</haskell>
  +
|
  +
<haskell>
  +
> fmap (+1) (Just 2)
  +
Just 3
  +
> fmap (+1) Nothing
  +
Nothing
  +
</haskell>
  +
  +
|}
   
==Applicative==
 
   
  +
{| class="wikitable"
  +
|+ Applicative
  +
|- style="text-align: center"
  +
! List
  +
! Maybe
  +
|- style="vertical-align: top"
  +
|
 
<haskell>
 
<haskell>
 
> [(+1),(+2)] <*> [3,4]
 
> [(+1),(+2)] <*> [3,4]
 
[4,5,5,6]
 
[4,5,5,6]
  +
> [] <*> [3,4]
  +
[]
 
</haskell>
 
</haskell>
  +
|
  +
<haskell>
  +
> (Just (+1)) <*> (Just 2)
  +
Just 3
  +
> Nothing <*> (Just 2)
  +
Nothing
  +
</haskell>
  +
|}
  +
   
==Alternative==
 
   
  +
{| class="wikitable"
  +
|+ Alternative
  +
|- style="text-align: center"
  +
! List
  +
! Maybe
  +
|- style="vertical-align: top"
  +
|
 
<haskell>
 
<haskell>
 
> import Control.Applicative
 
> import Control.Applicative
 
> [1] <|> empty <|> [2]
 
> [1] <|> empty <|> [2]
 
[1,2]
 
[1,2]
  +
> empty <|> [2]
  +
[2]
 
</haskell>
 
</haskell>
  +
|
  +
<haskell>
  +
> import Control.Applicative
  +
> (Just 1) <|> empty <|> (Just 2)
  +
Just 1
  +
> Nothing <|> (Just 2)
  +
Just 2
  +
</haskell>
  +
|}
   
==Monad==
 
   
  +
  +
{| class="wikitable"
  +
|+ Monad
  +
|- style="text-align: center"
  +
! List
  +
! Maybe
  +
|- style="vertical-align: top"
  +
|
  +
<haskell>
  +
> mul xs ys = do x<-xs; y<-ys; return (x*y)
  +
> mul [1,2] [3,4,5]
  +
[3,4,5,6,8,10]
  +
</haskell>
  +
|
 
<haskell>
 
<haskell>
> (\ lst1 lst2 -> do x<-lst1; y<-lst2; return (x*y)) [1,2] [3,4]
+
> mul x' y' = do x<-x'; y<-y' return (x*y)
  +
> mul (Just 2) (Just 5)
[3,4,6,8]
 
  +
Just 10
 
</haskell>
 
</haskell>
  +
|}
   
==MonadPlus==
 
   
  +
  +
{| class="wikitable"
  +
|+ MonadPlus
  +
|- style="text-align: center"
  +
! List
  +
! Maybe
  +
|- style="vertical-align: top"
  +
|
 
<haskell>
 
<haskell>
 
> import Control.Monad
 
> import Control.Monad
> (\lst -> do x<-lst; guard (x*x<5); return (x*x)) [1,2,3]
+
> squares5 x' = do x<-x'; guard (x*x<5); return (x*x)
  +
> squares5 [1,2,3]
 
[1,4]
 
[1,4]
  +
> squares5 []
  +
[]
  +
</haskell>
  +
|
  +
<haskell>
  +
> import Control.Monad
  +
> squares5 x' = do x<-x'; guard (x*x<5); return (x*x)
  +
> squares5 (Just 2)
  +
Just 4
  +
> squares5 (Just 3)
  +
Nothing
  +
> squares5 Nothing
  +
Nothing
 
</haskell>
 
</haskell>
  +
|}
   
==Foldable==
 
   
  +
  +
{| class="wikitable"
  +
|+ Foldable
  +
|- style="text-align: center"
  +
! List
  +
! Maybe
  +
|- style="vertical-align: top"
  +
|
 
<haskell>
 
<haskell>
 
> foldr (+) 0 [1,2]
 
> foldr (+) 0 [1,2]
 
3
 
3
 
</haskell>
 
</haskell>
  +
|
  +
<haskell>
  +
> foldr (+) 1 (Just 2)
  +
3
  +
</haskell>
  +
|}
   
==Traversable==
 
   
  +
  +
{| class="wikitable"
  +
|+ Traversable
  +
|- style="text-align: center"
  +
! List
  +
! Maybe
  +
|- style="vertical-align: top"
  +
|
 
<haskell>
 
<haskell>
 
> traverse (\x -> [0..x]) [0..2]
 
> traverse (\x -> [0..x]) [0..2]
Line 68: Line 205:
 
[[1,3],[1,4],[2,3],[2,4]]
 
[[1,3],[1,4],[2,3],[2,4]]
 
</haskell>
 
</haskell>
  +
|
  +
<haskell>
  +
> traverse (\x -> [x, x*x]) (Just 3)
  +
[Just 3,Just 9]
  +
> sequenceA [Just 3, Just 4]
  +
Just [3,4]
  +
> sequenceA [Just 3, Nothing]
  +
Nothing
  +
> sequenceA (Just [3,4])
  +
[Just 3,Just 4]
  +
</haskell>
  +
|}
  +
  +
  +
To do : MonadFix

Latest revision as of 15:53, 21 June 2018

This pages compares the Typeclassopedia classes of List and Maybe, with examples of use. You can run the examples in GHCi.

Semigroup
List Maybe
> import Data.Semigroup
> [1,2] <> [3,4]
[1,2,3,4]
> import Data.Semigroup
> (Just [1,2]) <> (Just [3,4])
Just [1,2,3,4]


Monoid
List Maybe
> mempty :: [Int]
[]
> [1,2] <> mempty
[1,2]
> mempty :: Maybe [Int]
Nothing
> (Just [1,2]) <> mempty
Just [1,2]


Functor
List Maybe
> fmap (+1) [2, 3]
[3, 4]
> fmap (+1) []
[]
> fmap (+1) (Just 2)
Just 3
> fmap (+1) Nothing
Nothing


Applicative
List Maybe
> [(+1),(+2)] <*> [3,4]
[4,5,5,6]
> [] <*> [3,4]
[]
> (Just (+1)) <*> (Just 2)
Just 3
> Nothing <*> (Just 2)
Nothing


Alternative
List Maybe
> import Control.Applicative
> [1] <|> empty <|> [2]
[1,2]
> empty <|> [2]
[2]
> import Control.Applicative
> (Just 1) <|> empty <|> (Just 2)
Just 1
> Nothing <|> (Just 2)
Just 2


Monad
List Maybe
> mul xs ys = do x<-xs; y<-ys; return (x*y)
> mul [1,2] [3,4,5]
[3,4,5,6,8,10]
> mul x' y' = do x<-x'; y<-y' return (x*y)
> mul (Just 2) (Just 5)
Just 10


MonadPlus
List Maybe
> import Control.Monad
> squares5 x' = do x<-x'; guard (x*x<5); return (x*x)
> squares5 [1,2,3]
[1,4]
> squares5 []
[]
> import Control.Monad
> squares5 x' = do x<-x'; guard (x*x<5); return (x*x)
> squares5 (Just 2)
Just 4
> squares5 (Just 3)
Nothing
> squares5 Nothing
Nothing


Foldable
List Maybe
> foldr (+) 0 [1,2]
3
> foldr (+) 1 (Just 2)
3


Traversable
List Maybe
> 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]]
> traverse (\x -> [x, x*x]) (Just 3)
[Just 3,Just 9]
> sequenceA [Just 3, Just 4]
Just [3,4]
> sequenceA [Just 3, Nothing]
Nothing
> sequenceA (Just [3,4])
[Just 3,Just 4]


To do : MonadFix