User Tools

Site Tools


codesnippets:firsthaskellprogram

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
    • 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 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 unit type
    • hence IO () is the data type IO with type parameter ()

Explanation, step by step

  • Haskell code repeated, from above

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
  • 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 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 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

  • Prelude is part of library base (Prelude)
  • package 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

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
You could leave a comment if you were logged in.
codesnippets/firsthaskellprogram.txt · Last modified: by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki