| Safe Haskell | Safe-Inferred |
|---|
Lessons.Lesson09
Contents
Description
Notes taken by Julija Mikeliūnaitė
Synopsis
- queryAge :: String -> IO Integer
- threeThreeLetterWords :: Parser [String]
- stateful :: State String Int
- combined :: State String (Int, Int)
- combined' :: State String (Int, Int)
- combined'' :: State String (Int, Int)
Documentation
queryAge :: String -> IO Integer Source #
Take a list as a traversable, pass a list of Maybies. Returns a Maybe containing a list.
>>>sequenceA [Just 42, Just 5]Just [42,5]
If the passed list contains a Nothing, Nothing is returned.
>>>sequenceA [Just 42, Just 5, Nothing]Nothing
Works pretty much as list comprehension.
>>>sequenceA [[42], [13, 45]][[42,13],[42,45]]
Returns empty list.
>>>sequenceA [[42], [13, 45], []][]
Because a Left value was passed, it also returns a Left value.
>>>sequenceA [Left 0, Right 43, Right 45]Left 0
Returns the inner value (Left 45).
>>>sequenceA $ Right (Left 45)Left 45
Swaps the traversable with applicative.
>>>sequenceA $ Just (Right 45)Right (Just 45)
Collects all the inputs. Inner values are Just monadic values, type is IO [String]. Result is an IO list of pure values, which makes life easier.
>>>:t sequenceA [getLine, getLine, getLine]sequenceA [getLine, getLine, getLine] :: IO [String]
Takes pure value (name) and returns a monadic value.
threeThreeLetterWords :: Parser [String] Source #
stateful :: State String Int Source #
State monad - it tries to pretend that Haskell is a normal programming language with a mutable state. | State has two params: String - type of state, and Int - type of computation result. get - gets global state, put - changes global state. In this case, global is super local.
>>>runState stateful "initial"(7,"I am a new state")
combined :: State String (Int, Int) Source #
7 is the old state length, 16 is the new one.
>>>runState combined "initial"((7,16),"I am a new state")
combined'' :: State String (Int, Int) Source #
Orphan instances
| Monad Parser Source # | If a list of names is provided, it returns a list of numbers (type IO [Integer]).
Return type is [IO Integer] (not 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.
Closest thing Haskell has to loop (similar to
The number next to M shows how many arguments the first function takes. Works as ($).
Lifts a pure function into a monadic function.
Same result as before.
Using our helpers in the context of parsers (making them useful).
After the first parser completes its course, the second parser continues with the unparsed string.
Pretty much every program is just a traversable. Allows to write parser combinators in a monadic way. |