- datatypes
- Monoid class hierarchy
- Monad class hierarchy
- IO
- why Monads are so popular
- datatypes
# http://hackage.haskell.org/package/base-4.12.0.0/docs/src/GHC.Base.html#String
data Bool = False | True
data Ordering = LT | EQ | GT
data Char = C# Char#
type String = [Char]
data Int = I# Int#
data PeanoNumber = Zero | Suc PeanoNumber
data () = ()
data Unit = Unit
data Unit = MkUnit
data [] a = [] | a : [a]
data List a = Nil | Cons a (List a)
example :: [Int]
example = [1, 2, 3]
example = 1 : 2 : 3 : []
example :: List Int
example = [1, 2, 3]
example = Cons 1 (Cons 2 (Cons 3 Nil))
data Void = Void Void
data Maybe a = Nothing | Just a
data Either a b = Left a | Right b
data (a, b) = (a, b)
data (a, b, c) = (a, b, c)
data (a, b, c, d) = (a, b, c, d)
...
data Tuple a b = Tuple a b
type MyTupleWithFixedArgumentTypes = Tuple Bool Int
example :: Tuple Bool Int
example = Tuple True 1
exampleList :: [Tuple True Int]
exampleList = [Tuple True 1, Tuple False 2]
data Tree v = Leaf
| Node (Tree v) v (Tree v)
treeExample :: Tree Int
treeExample = Node (Node Leaf 1 Leaf) 2 (Node Leaf 2 Leaf)
data Position = Boss | Secretary | Employee String
data User = User {
name :: String
, postition :: Position
, password :: String
, username :: String
}
data UserValidationError = EmptyName | PasswordsDoesntMatch
mkUserWithValidation :: Json -> Either UserValidationError User
mkUserWithValidation json = ....
mkUserWithValidation :: Json -> Maybe User
mkUserWithValidation json = ....
- functions
- Monoid class hierarchy
- Monad class hierarchy
- IO
https://www.quora.com/What-is-an-IO-Monad
data IO a = IO (State# RealWorld -> (# State# RealWorld, a #))
https://wiki.haskell.org/IO_inside
Warning: The following story about IO is incorrect in that it cannot actually explain some important aspects of IO (including interaction and concurrency). However, some people find it useful to begin developing an understanding.
The 'main' Haskell function has the type:
main :: RealWorld -> ((), RealWorld)
where 'RealWorld' is a fake type used instead of our Int. It's something like the baton passed in a relay race. When 'main' calls some IO function, it passes the "RealWorld" it received as a parameter. All IO functions have similar types involving RealWorld as a parameter and result. To be exact, "IO" is a type synonym defined in the following way:
type IO a = RealWorld -> (a, RealWorld)
main = do a <- ask "What is your name?"
b <- ask "How old are you?"
return ()
ask s = do putStr s
readLn
Look at the next example:
main = do putStr "What is your name?"
a <- readLn
putStr "How old are you?"
b <- readLn
print (a,b)
This code is desugared into:
main = putStr "What is your name?"
>> readLn
>>= \a -> putStr "How old are you?"
>> readLn
>>= \b -> print (a,b)
main = do putStr "What is your name?"
putStr "How old are you?"
putStr "Nice day!"
main = (putStr "What is your name?")
>> ( (putStr "How old are you?")
>> (putStr "Nice day!")
)
readLn :: Read a => IO a putStrLn :: String -> IO ()
You can embed categories in one another