Functional Programming in Haskell

Advanced Programming, Lecture 10

Advanced Programming, Lecture 10


M. W.
Diese Lernkartei behandelt fortgeschrittene Konzepte der funktionalen Programmierung in Haskell, insbesondere auf Universitätsniveau. Sie konzentriert sich auf Typen, Funktionen und Datenstrukturen wie Listen, wobei Themen wie Typdefinitionen, rekursive Funktionen und Musterabgleich behandelt werden. Die Karteikarten eignen sich für Studierende und Entwickler, die Haskell vertiefen möchten, insbesondere in Bereichen wie Typinferenz, rekursive Funktionen und die Verwendung von Listen und Datenkonstruktoren.
Karten
20
Lernende
2
Sprache
Deutsch
Kategorie
Informatik
Stufe
Universität
Erstellt / Aktualisiert
24.03.2021 / 05.04.2021

Lernkarten

Rules of pure functional programming

  • no state
  • no side effects
  • immutable variables
  • favor recursion over looping

Key concepts of Haskell

  • Immutable variables
  • pure
  • lazy evaluation
  • everything is expression
  • first-class functions: can be defined anywhere, passed as arguments, returned as values
  • strong type system
  • full type inference
  • pattern matching on data structures
  • parametric polymorphism

Running Haskell Code - Interactively

  • REPL (read-eval-print-loop) for Glasgow Haskell Compiler (GHC): GHCi
  • Commands
    • expression: returns result
    • :t True: show type of expression
    • :l filename.hs: load variables and functions from .hs file
    • :r : reload 

Basic Types

  • Int
  • Integer
  • Float
  • Double
  • Char
  • Bool

Creating Variables

  • Declaration: number :: Int
  • Initialization: number = 43
  • Declaration of Type optional

Function Basics

  • one return value
  • one value as parameter
  • return value is fully determined by parameter
  • no side effects
  • partial definition allowed

Defining a Function

  • Declaration of Type: functionname :: ParameterType -> ReturnType
  • Behavior: functionname inputPattern = returnvalueExpression
  • patterns are tried in definition order

Example

  • somefunc :: Int -> Int
  • somefunc 1 = 0
  • somefunc 0 = 1
  • somefunc x = x

Calling a Function

functionname parameter

Functions with seemingly Multiple Parameters

  • logicXor :: Bool -> Bool -> Bool
  • right associative: logicXor Bool -> (Bool -> Bool)
  • logicXor is higher-order function: takes Bool and returns function of Bool -> Bool
  • curried by default

Calling a Function - Associativity

  • function application is left associative
  • logicXor True True = (logicXor True) True

Partially Applying Operators

  • use prefix notation
    • (+) 3: returns function that applies three
  • infix notation:
    • (< 3)
    • (> 3)
    • returns function that checks if greater or smaller than three

Lambdas

  • anonymous functions
  • \param1 param2 -> returnvalExpression

If-Then-Else

  • if is an expression
  • else is mandatory
  • if conditionExpression then successExpression else failureExpression
  • lazy evaluation: only condition and one of other two is evaluated

Include-Guards

  • instead of using if statement, check condition during pattern matching
  • conditions are checked in order
  • Syntax:
    • function pattern
    • | conditionExpr1 = returnEval1
    • | conditionExpr2 = returnExpr2 ...
  • otherwise: always fulfilled condition
    • abs :: Int -> Int
    • abs x
    • | x<0 = -x
    • | otherwise = x

Where

  • can define local variables with where
  • useful when same expression appears multiple times

functionname param = val1 + val2

where val1 = someExpression

val2 = otherExpression

Advanced Types - Type Definition

  • define type with data constructors
  • data Typename = Dataname1 | Dataname 2 | Dataname N
  • data Shape = Circle | Rect

Advanced types - Type Definition with Data Parameters

  • let Data Constructors take parameters, return values of type Shape
  • data Shape = Circle Float | Rect Float Float
  • Type of Circle: Circle :: Float -> Shape
  • Type of Rect: Rect :: Float -> Float -> Shape

Advanced Types - Generic Types and Type Constructor

  • Adding parameters to the type:
    • data Maybe a = Nothing | Just a
  • Maybe is aquivalent to Optional in Java
  • Maybe is Type Constructor
  • Maybe a is Type
  • Maybe has values nothing or Just Int

List

  • data List a = Nil | Cons a (List a)
  • either empty (Nil)
  • or construction of first element a and rest of the list 

Example

intList :: List Int

intList = Cons 1 (Cons 2 (Cons 3))

Folding of a List

  • use of generic functions
  • foldRight :: (a -> b -> b) -> b -> List a -> b
  • foldRight op def Nil = def
  • foldRight op def (Cons x dx) = x 'op' (foldRight op def dx)

Lernen