====== First Haskell programs ======
* main function with output, as a complete code example
* compiles, error and warning free, with compiler: GHC 8.10.4, using compiler option -Wall
*
main :: IO ()
main = print 'x'
* executes with the following output:
* 'x'
* concise explanation of the code
* function ''main'' is always the entry point of a Haskell program
* this is the function that demands the evaluation of the function right to the equal sign (''='')
* function ''main'' equals to function ''print'' with '''x' '' as the first parameter
* NOTE: The function ''print'' is a function that will only be evaluated when its evaluation is demanded. Hence, the function ''print'' not a command.
* concise explanation of the code elements
* {{:codesnippets:firstprogramwithfunctionmain.png|}}
* first line declares that function ''main'' has type ''IO ()''
* second line declares that function ''main'' equals to ''print 'x' ''
* whereas ''print'', is a function that is imported from a default package
* and 'x' is the character ''x''
* default package is ''[[codesnippets:prelude|Prelude]]''
* in the second line ''main'', ''print'', and '''x' '' are functions
* ''main'' has no parameter but has type ''IO ()''
* ''print'' has one parameter and has type ''Show a => a -> IO ()''
* '''x''' has no parameter, and has type ''Char''
* ''IO'' is a parametric type declared as ''data IO a''
* whereas a is a type parameter, that can be any type
* ''()'' is the [[https://en.wikipedia.org/wiki/Unit_type|unit type]]
* hence ''IO ()'' is the data type ''IO'' with type parameter ''()''
===== Explanation, step by step =====
* Haskell code repeated, from above
*
main :: IO ()
main = print 'x'
==== first line declares that function ''main'' has type ''IO ()'' ====
* ''IO ()'' is just a type signature with the resulting type ''IO ()'', and no parameters
* see the following code as examples for different type signatures:
*
main :: IO ()
main = printString sResult
printString :: String -> IO ()
printString s = print s
sResult :: String
sResult = fun 5 'a'
fun :: Int -> Char -> String
fun n ch = replicate n ch
* {{:codesnippets:typesignatureoffunctionfun.png?nolink|}}
* symbol ''->'' separates parameter from other parameter, and separates from the result type that is on the right hand side
* the example shows the follwing 4 type signatures
* ''IO ()''
* result type: ''IO ()'', no parameter
* ''String -> IO ()''
* result type: ''IO ()'', type of parameter: ''String''
* ''String''
* result type: ''String''
* ''Int -> Char -> String''
* result type: ''String'', type of first parameter: ''Int'', type of second parameter: ''String''
* NOTE:
* Types start always with upper case letters.
* Type signatures can also have type parameters, and type constraints.
* Type type parameters start always with lower case letters.
* the example executes with the following output:
*
"aaaaa"
==== second line declares that function ''main'' equals to ''print 'x''' ====
* expression '' main = print 'x' '' is called a [[codesnippets:bindingandpatternmatching|binding]]
* it binds ''print 'x' '' to function ''main''
* when function ''main'' evaluates it evaluates ''print'' with parameter '''x' ''
* expression ''sResult = fun 5 'a' '' is just another [[codesnippets:bindingandpatternmatching|binding]]
* it binds ''fun 5 'a' '' to function ''sResult''
* when function ''sResult'' evaluates it evaluates ''fun'' with parameter ''5'' and '''a' ''
==== default package is Prelude ====
* ''[[codesnippets:prelude|Prelude]]'' is part of library ''base'' ([[https://hackage.haskell.org/package/base-4.15.0.0/docs/Prelude.html|Prelude]])
* package ''[[codesnippets:prelude|Prelude]]'' includes standard modules (e.g. module System.IO)
* enables basic elements like functions, types, type classes, and instances
* the import of such elements can be controlled in different ways
* see also [[codesnippets:avoidconflictprelude|Avoiding name conflict with Prelude]]
===== ✎ =====
~~DISCUSSION~~