Difference between revisions of "Instances of List and Maybe"
Jump to navigation
Jump to search
(→Monad) |
(compare List and Maybe) |
||
Line 1: | Line 1: | ||
− | This pages |
+ | This pages compares the base classes of List and Maybe, with examples of use. You can run the examples in GHCi. |
− | |||
− | ==Semigroup== |
||
+ | {| class="wikitable" |
||
+ | |+ Semigroup |
||
+ | |- |
||
+ | ! List |
||
+ | ! Maybe |
||
+ | |- |
||
+ | | |
||
<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 |
||
+ | |- |
||
+ | ! List |
||
+ | ! Maybe |
||
+ | |- |
||
+ | | |
||
<haskell> |
<haskell> |
||
− | > |
+ | > 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 |
||
+ | |- |
||
+ | ! List |
||
+ | ! Maybe |
||
+ | |- |
||
+ | | |
||
<haskell> |
<haskell> |
||
> fmap (+1) [2, 3] |
> fmap (+1) [2, 3] |
||
[3, 4] |
[3, 4] |
||
</haskell> |
</haskell> |
||
+ | | |
||
+ | <haskell> |
||
+ | > fmap (+1) (Just 2) |
||
+ | Just 3 |
||
+ | > fmap (+1) Nothing |
||
+ | Nothing |
||
+ | </haskell> |
||
+ | |||
+ | |} |
||
− | ==Applicative== |
||
+ | {| class="wikitable" |
||
+ | |+ Applicative |
||
+ | |- |
||
+ | ! List |
||
+ | ! Maybe |
||
+ | |- |
||
+ | | |
||
<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 |
||
+ | |- |
||
+ | ! List |
||
+ | ! Maybe |
||
+ | |- |
||
+ | | |
||
<haskell> |
<haskell> |
||
> import Control.Applicative |
> import Control.Applicative |
||
Line 37: | Line 104: | ||
[1,2] |
[1,2] |
||
</haskell> |
</haskell> |
||
+ | | |
||
+ | <haskell> |
||
+ | > import Control.Applicative |
||
+ | > (Just 1) <|> (Just 2) |
||
+ | Just 1 |
||
+ | > Nothing <|> (Just 2) |
||
+ | Just 2 |
||
+ | </haskell> |
||
+ | |} |
||
+ | |||
− | ==Monad== |
||
+ | {| class="wikitable" |
||
+ | |+ Monad |
||
+ | |- |
||
+ | ! List |
||
+ | ! Maybe |
||
+ | |- |
||
+ | | |
||
<haskell> |
<haskell> |
||
− | > |
+ | > mul xs ys = do x<-xs; y<-ys; return (x*y) |
− | [3,4, |
+ | > mul [1,2] [3,4,5] |
+ | [3,4,5,6,8,10] |
||
</haskell> |
</haskell> |
||
+ | | |
||
+ | <haskell> |
||
+ | > squares x' = do x<-x'; return (x*x) |
||
+ | > squares (Just 3) |
||
+ | Just 9 |
||
+ | </haskell> |
||
+ | |} |
||
+ | |||
− | ==MonadPlus== |
||
+ | {| class="wikitable" |
||
+ | |+ MonadPlus |
||
+ | |- |
||
+ | ! List |
||
+ | ! Maybe |
||
+ | |- |
||
+ | | |
||
<haskell> |
<haskell> |
||
> import Control.Monad |
> import Control.Monad |
||
− | > |
+ | > squares5 x' = do x<-x'; guard (x*x<5); return (x*x) |
+ | > squares5 [1,2,3] |
||
[1,4] |
[1,4] |
||
</haskell> |
</haskell> |
||
+ | | |
||
+ | <haskell> |
||
+ | > 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> |
||
+ | |} |
||
− | ==Foldable== |
||
+ | |||
+ | {| class="wikitable" |
||
+ | |+ MonadPlus |
||
+ | |- |
||
+ | ! List |
||
+ | ! Maybe |
||
+ | |- |
||
+ | | |
||
<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 |
||
+ | |- |
||
+ | ! List |
||
+ | ! Maybe |
||
+ | |- |
||
+ | | |
||
<haskell> |
<haskell> |
||
> traverse (\x -> [0..x]) [0..2] |
> traverse (\x -> [0..x]) [0..2] |
||
Line 68: | Line 198: | ||
[[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,4]) |
||
+ | [Just 3,Just 4] |
||
+ | </haskell> |
||
+ | |} |
Revision as of 16:50, 20 June 2018
This pages compares the base classes of List and Maybe, with examples of use. You can run the examples in GHCi.
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]
|
List | Maybe |
---|---|
> mempty :: [Int]
[]
> [1,2] <> mempty
[1,2]
|
> mempty :: Maybe [Int]
Nothing
> (Just [1,2]) <> mempty
Just [1,2]
|
List | Maybe |
---|---|
> fmap (+1) [2, 3]
[3, 4]
|
> fmap (+1) (Just 2)
Just 3
> fmap (+1) Nothing
Nothing
|
List | Maybe |
---|---|
> [(+1),(+2)] <*> [3,4]
[4,5,5,6]
> [] <*> [3,4]
[]
|
> (Just (+1)) <*> (Just 2)
Just 3
> Nothing <*> (Just 2)
Nothing
|
List | Maybe |
---|---|
> import Control.Applicative
> [1] <|> empty <|> [2]
[1,2]
|
> import Control.Applicative
> (Just 1) <|> (Just 2)
Just 1
> Nothing <|> (Just 2)
Just 2
|
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]
|
> squares x' = do x<-x'; return (x*x)
> squares (Just 3)
Just 9
|
List | Maybe |
---|---|
> import Control.Monad
> squares5 x' = do x<-x'; guard (x*x<5); return (x*x)
> squares5 [1,2,3]
[1,4]
|
> squares5 x' = do x<-x'; guard (x*x<5); return (x*x)
> squares5 (Just 2)
Just 4
> squares5 (Just 3)
Nothing
> squares5 Nothing
Nothing
|
List | Maybe |
---|---|
> foldr (+) 0 [1,2]
3
|
> foldr (+) 1 (Just 2)
3
|
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,4])
[Just 3,Just 4]
|