Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Opera, Safari or Firefox browser.

Haskell Workshop

Introduction for everyone

What is Haskell?

  • general purpose
  • functional language
  • typed, it's all about types
  • work faster with fewer mistakes
  • it's like learning programing again

Functional programming

  • programming paradigm
  • modelled after mathematical functions

Functions

  • expressions
  • applied to input
  • reduced/evaluated once applied

Functions are first-class

  • as value
  • as argument
  • as output

Pure functions

  • referential transparency
  • same input --> same output
  • like in math

Valid function

  • 𝑓(10) = 𝐴
  • 𝑓(200) = B
  • 𝑓(9000) = C

NOT a valid function

  • 𝑓(123) = X
  • 𝑓(123) = Y
  • 𝑓(1000) = Z

Still a very valid function

  • 𝑓(1) = A
  • 𝑓(2) = A
  • 𝑓(3) = A

Lambdas

  • λx.x + 1
  • head
  • body

Beta reduction


                ( λx.x + 1 ) 2
                    [ x := 2 ] # applying 2
         ( λ[ x := 2 ].x + 1 )
                     ( 2 + 1 )
                             3 # reduced to identity function
      

Now with functions!


        # Applications are left associative
       '1'      ( λx.x )( λy.y ) z
       '2'  ( ( λx.x )( λy.y ) ) z # rewrite as
       '3'       [ x := ( λy.y ) ] # apply from left to right
       '4'              ( λy.y ) z
       '5'              [ y := z ]
       '6'                       z # reduced to identity
       '7'                         # "z" is a free variable
      

Multiple arguments


         # It is just a shorthand for nested Lambda
         '1'          λxy.x + y
         '2'        λx.λy.x + y
         '3'    λx.( λy.x + y )

         # apply with x = 1 and y = 2
         '1'           ( λxy.x + y ) 1 2
         '2'         λx.( λy.x + y ) 1 2
         '3'  λ[ x := 1 ].( λy.x + y ) 2 # apply x = 1
         '4'              ( λy.1 + y ) 2
         '5'       ( λ[ y := 2 ].1 + y ) # apply y = 2
         '6'                       1 + 2
         '7'                           3

         # Reached beta normal form (cannot reduce further)
      

GHC

  • GHC: Glasgow Haskell Compiler
  • GHCi: (i)nterative intepreter and debugger

Install

  • There are many ways to install
  • Stack - The Haskell Tool Stack

Start your new project

Project structure

Main.hs

Lib.hs

Haskell Extension

VSCode Syntax/type support

Lambdas in Haskell

Basic Datatypes Structure

Data declaration

Types

  • Numbers (Int, Integer, Float, Double, Rational, Scientific)
  • Strings
  • Bool
  • tuples `let tup = (1, 2)`
  • lists `let list = [1, 2, 3, 4]`

Inspect types - :type

Inspect anything!

Inspect with :info

Typeclasses

  • type defines how that type in particular is created
  • typeclass defines how a set of types are consumed or used in computations
  • typeclass is somewhat like an interface

Typeclass example

Eq instance

Show instance (exercise)

Thank you