Safe HaskellSafe-Inferred

Lessons.Lesson08

Description

Notes taken by Julija Mikeliūnaitė

Synopsis

Documentation

fl :: [Int] Source #

Monad is the most powerful class. Bind method (>>=) gives the illusion of sequential computation.

Functors ignore the order of execution.

| Minimal implementation of a functor: fmap :: Functor f => (a -> b) -> f a -> f b It takes a pure function (which takes a and returns b), a functor of a, and returns a functor of b.

The function was applied to every element of the list.

>>> fl
[5,4]

fm :: Maybe Integer Source #

If the list is empty, the result is also an empty list.

>>> fm
Nothing

fe :: Either String Integer Source #

fe' :: Either Integer Integer Source #

The left value is considered a "bad" value.

>>> fe'
Left 41

fio :: IO String Source #

Takes an input and returns the value with "!"" in the end. Returned type is IO String. IO is a type which presents computations which originate in the external world.

p :: [Integer] Source #

Applicative functor is between functor and monad.

pure is pretty much the same as return: pure :: Applicative f => a -> f a return :: Monad m => a -> m a

>>> p
[5]

am :: Maybe Integer Source #

Signature *, sometimes called "spaceship". (*) :: Applicative f => (a -> b) -> f a -> f b It is similar to fmap, but instead of a Functor, it uses an Applicative.

Signature $ is synonymous with fmap.

>>> am
Just 46

am' :: Maybe Integer Source #

am'' :: Maybe Integer Source #

al :: [Integer] Source #

Sources of values are completely independent, computed in parallel.

>>> al
[2,3,3,4,4,5]

ml :: [Integer] Source #

Sources of values depend on values that are above, line-by-line.

>>> ml
[2,3,3,4,4,5]

newtype Parser a Source #

newtype is used if ADT has a single constructor.

Constructors

Parser 

Fields

  • runParser :: String -> Either String (a, String)
     

Instances

Instances details
Alternative Parser Source #

Signature |, sometimes called alternative

Instance details

Defined in Lessons.Lesson08

Methods

empty :: Parser a

(<|>) :: Parser a -> Parser a -> Parser a

some :: Parser a -> Parser [a]

many :: Parser a -> Parser [a]

Applicative Parser Source # 
Instance details

Defined in Lessons.Lesson08

Methods

pure :: a -> Parser a

(<*>) :: Parser (a -> b) -> Parser a -> Parser b

liftA2 :: (a -> b -> c) -> Parser a -> Parser b -> Parser c

(*>) :: Parser a -> Parser b -> Parser b

(<*) :: Parser a -> Parser b -> Parser a

Functor Parser Source # 
Instance details

Defined in Lessons.Lesson08

Methods

fmap :: (a -> b) -> Parser a -> Parser b

(<$) :: a -> Parser b -> Parser a

Monad Parser

If a list of names is provided, it returns a list of numbers (type IO [Integer]).

>>> :t mapM queryAge ["VI", "A", "U"]
mapM queryAge ["VI", "A", "U"] :: IO [Integer]

Return type is [IO Integer] (not IO [Integer]).

>>> :t map queryAge ["VI", "A", "U"]
map queryAge ["VI", "A", "U"] :: [IO Integer]

Usually functions with _'s drop the final result and only run side-effects. Running a bunch of computations when you don't really care about the result.

>>> :t mapM_ queryAge ["VI", "A", "U"]
mapM_ queryAge ["VI", "A", "U"] :: IO ()

Closest thing Haskell has to loop (similar to foreach in other languages). Usually used for running a computation over some collection. Works the same way as mapM_ but has swapped arguments.

>>> :t forM_ ["VI", "A", "U"] queryAge
forM_ ["VI", "A", "U"] queryAge :: IO ()

The number next to M shows how many arguments the first function takes. Works as ($).

>>> liftM2 (+) (Just 4) (Just 6)
Just 10

Lifts a pure function into a monadic function.

>>> liftM3 (\a b c -> a + b - c) (Just 4) (Just 6) Nothing
Nothing

Same result as before.

>>> liftA3 (\a b c -> a + b - c) (Just 4) (Just 6) Nothing
Nothing

Using our helpers in the context of parsers (making them useful).

>>> :t threeLetters
threeLetters :: Parser String

After the first parser completes its course, the second parser continues with the unparsed string.

>>> runParser (sequenceA [threeLetters, threeLetters, threeLetters]) "asdasdasd!"
Right (["asd","asd","asd"],"!")

Pretty much every program is just a traversable.

Allows to write parser combinators in a monadic way.

Instance details

Defined in Lessons.Lesson09

Methods

(>>=) :: Parser a -> (a -> Parser b) -> Parser b

(>>) :: Parser a -> Parser b -> Parser b

return :: a -> Parser a

parseLetter :: Parser Char Source #

If a number is parsed, then it returns Left value.

>>> runParser parseLetter "skfhsdk"
Right ('s',"kfhsdk")