SlideShare uma empresa Scribd logo
1 de 39
Refresher

                 Sebastian Rettig


“Work on Haskell began in 1987 when aa committee of
 “Work on Haskell began in 1987 when committee of
researchers got together to design aa kick-asslanguage.” ([1])
 researchers got together to design kick-ass language.” ([1])
Imperative Programming
●   Variables (value placeholder)
       –   Value can change during program flow
●   Modules, Classes, Functions, Procedures
●   Control Flow Structures (IF THEN ELSE,
     Loops (WHILE), Goto)
Functional Programming
●   No Variables
●   Functions only, eventually stored in
     Modules
       –   Behavior do not change, once defined
       –   → Function called with same parameter
            calculates always the same result
●   Function definitions (Match Cases)
●   Recursion
Haskell Refresher Quiz
●   What's the red one ?
●




maxList :: (Ord a) => [a] -> a

maxList [] = error “empty”
maxList [x] = x
maxList (x:xs)
    | x > maxTail = x
    | otherwise = maxTail
    where maxTail = maxList xs
Haskell Refresher Quiz
●   What's the red one ?
●




maxList :: (Ord a) => [a] -> a
                                 Function
maxList [] = error “empty”
maxList [x] = x
maxList (x:xs)
    | x > maxTail = x
    | otherwise = maxTail
    where maxTail = maxList xs
Haskell Refresher Quiz
●   What's the red one ?
●




maxList :: (Ord a) => [a] -> a

maxList [] = error “empty”
maxList [x] = x
maxList (x:xs)
    | x > maxTail = x
    | otherwise = maxTail
    where maxTail = maxList xs
Haskell Refresher Quiz
●   What's the red one ?
●




maxList :: (Ord a) => [a] -> a
                                 Function Header
maxList [] = error “empty”
maxList [x] = x
maxList (x:xs)
    | x > maxTail = x
    | otherwise = maxTail
    where maxTail = maxList xs
Haskell Refresher Quiz
●   What's the red one ?
    maxList :: [Int] -> Int

    maxList [] = error “empty”
    maxList [x] = x
    maxList (x:xs)
     | x > maxTail = x
     | otherwise = maxTail
     where maxTail = maxList xs
Haskell Refresher Quiz
●   What's the red one ?
●
    maxList :: [Int] -> Int

●   maxList [] = error “empty”
    maxList [x] = x
    maxList (x:xs)
     | x > maxTail = x            Function Body
     | otherwise = maxTail
     where maxTail = maxList xs
Haskell Functions
●   function contains header and body
●   header consists of type definition:
    <funcname> :: <param> [ -> <param_n>] -> <result>
●   body consists of pattern rules and calculation
     <funcname> <paramalias_1> [<paramalias_n>] =
    {calc}
●   Example (2 params if type [Int] & Int, result Bool):
          isHead :: [Int] -> Int -> Bool
          isHead xs i = i==head xs
Haskell Refresher Quiz
●    What's the red one ?
●




    maxList :: [Int] ->   Int

    maxList [] = error “empty”
    maxList [x] = x
    maxList (x:xs)
     | x > maxTail = x
     | otherwise = maxTail
     where maxTail = maxList xs
Haskell Refresher Quiz
●    What's the red one ?
●




    maxList :: [Int] ->   Int
                                  Type
    maxList [] = error “empty”
    maxList [x] = x
    maxList (x:xs)
     | x > maxTail = x
     | otherwise = maxTail
     where maxTail = maxList xs
Types in Haskell (1)
Starts with uppercase first character

●   Int bounded from -2147483648 to 2147483647
●   Integer unbounded (for big numbers) but slower than Int
●   Float floating point (single precision)
●   Double floating point (double precision)
●   Bool boolean
●   Char character
●   String list of Char (String == [Char])
Types in Haskell (2)
●   Lists: must be homogenous (entries from the same type)
        –   [] empty List
        –   [Int] List of Int
        –   But e.g. [Int, Bool] not allowed (not homogenous)!
●   Tuples: can contain different types BUT have fixed length
        –   () empty tuple (has only 1 value)
        –   (Int, Bool) tuple of a pair Int, Bool
        –   (Int, Bool, Bool) tuple of Int, Bool, Bool
Lists & List Comprehension
●   Example: double entries in list
           doubleList [] = []
           doubleList (x:xs) = x:x:doubleList xs
       –   e.g. [1,2,3] → x=1; xs=[2,3]
●   Multiplication of a list of values:
           Hugs> product [1..5]
             120
●   or in a function:
           fac n = product [1..n]
Haskell Refresher Quiz
●    What's the red one ?
●
maxList :: [Int] -> Int

maxList [] =          error “empty”
maxList [x] = x
maxList (x:xs)

    | x > maxTail = x
    | otherwise = maxTail
    where maxTail = maxList xs
Haskell Refresher Quiz
●    What's the red one ?
●
maxList :: [Int] -> Int

maxList [] =
maxList [x] = x
                      error “empty”
                                       Pattern
maxList (x:xs)
                                      Matching
    | x > maxTail = x
    | otherwise = maxTail
    where maxTail = maxList xs
Haskell Refresher Quiz
●   What's the red one ?
●   maxList :: [Int] -> Int

    maxList [] = error “empty”
    maxList [x] = x
    maxList (x:xs)

     | x > maxTail = x
     | otherwise = maxTail

     where maxTail = maxList xs
Haskell Refresher Quiz
●   What's the red one ?
●


    maxList :: [Int] -> Int

    maxList [] = error “empty”
    maxList [x] = x
    maxList (x:xs)

     | x > maxTail = x
     | otherwise = maxTail        Guards
     where maxTail = maxList xs
Haskell Refresher Quiz
●   What's the red one ?
●
    maxList :: [Int] -> Int

    maxList [] = error “empty”
    maxList [x] = x
    maxList (x:xs)
     | x > maxTail = x
     | otherwise = maxTail


     where maxTail = maxList xs
Haskell Refresher Quiz
●   What's the red one ?
●
    maxList :: [Int] -> Int

    maxList [] = error “empty”
    maxList [x] = x
    maxList (x:xs)
     | x > maxTail = x
     | otherwise = maxTail
                                      Additional
     where maxTail = maxList xs
                                  'where' clause
Pattern Matching (1)
●   create matching rules:
        fac 0 = 1
        fac n = n * fac (n-1)
●   use wildcards to ignore parameters in
     pattern:
        take 0 _ = []
        take _ [] = []
        take n (x:xs) = [x] ++ take (n-1) xs
Pattern Matching (2)
●   use Guards to separate a matching case deeper:
            myFilter _ [] = []
            myFilter f (x:xs)
               | f==x = x:myFilter g xs
               | otherwise = myFilter f xs
               where g = 2*f
        –   like an IF THEN ELSE
        –   Guard Condition evaluate to Bool (True/False)
●   eventually define values with where-clause
            myFilter 2 [1,2,3,4,5,6] results to [2,4]
Recursion
●   Recursion vs. Final Recursion:
countX :: Int -> [Int] -> Int           ●   Hugs> countX 3 [1,4,3,5,3]
countX x [] = 0                              2
countX x (y:ys)
  | x==y = 1 + countX x ys
  | otherwise = countX x ys

                     countXFinal :: Int -> [Int] -> Int -> Int
                     countXFinal x [] accu = accu
                     countXFinal x (y:ys) accu
                       | x==y = countXFinal x ys accu+1
                       | otherwise = countXFinal x ys accu
●   use accumulator to reduce stack usage
●   Hugs> countXFinal 3 [1,4,3,5,3] 0
        2
Haskell Refresher Quiz
  ●   What's the red one ?
  ●




maxList ::   (Ord a) =>   [a] -> a

maxList [] = error “empty”
maxList [x] = x
maxList (x:xs)
 | x > maxTail = x
 | otherwise = maxTail
 where maxTail = maxList xs
Haskell Refresher Quiz
  ●   What's the red one ?
  ●




maxList ::   (Ord a) =>   [a] -> a
                                     Typeclass
maxList [] = error “empty”
maxList [x] = x
maxList (x:xs)
 | x > maxTail = x
 | otherwise = maxTail
 where maxTail = maxList xs
Haskell Refresher Quiz
 ●     What's the red one ?
 ●




maxList :: (Ord       a   ) =>    [a] -> a

maxList [] = error “empty”
maxList [x] = x
maxList (x:xs)
     | x > maxTail = x
     | otherwise = maxTail
     where maxTail = maxList xs
Haskell Refresher Quiz
 ●     What's the red one ?
 ●




maxList :: (Ord       a   ) =>    [a] -> a
                                             Type variable
maxList [] = error “empty”
maxList [x] = x
maxList (x:xs)
     | x > maxTail = x
     | otherwise = maxTail
     where maxTail = maxList xs
Type Polymorphism (1)
●   Statically typed, but Compiler can read type from
      context (type inference)
●   → no need to set type explicitly
●   → makes function more generic for different
     kinds of types (type polymorphism)
       –   Why should I use quicksort :: [Int] -> [Int]
       –   even if I also want to sort character?
           Hugs> quicksort ['f','a','d','b']
              "abdf"
Type Polymorphism (2)
●   the header of our previous implementations could be
    fact :: Int -> Int
    maxList :: [Int] -> Int
●   but is only limited to Int, but maxList could also
      handle Char
●   → why not make it generic?
    maxList :: [a] -> a
●   but what happens, if the corresponding Type is not
      comparable or cannot be ordered?
Type Polymorphism (3)
●   Solution: use Typeclasses
    maxList :: (Ord a) => [a] -> a
●   then we can be sure to use (<,<=, ==, /=, >=, >)
●   function header can contain multiple typeclasses
    maxList :: (Ord a, Eq b) => [a] -> [b] -> a
●   In Haskell-Interpreter: to list the function header
    :t <function_name>
Typeclasses (1)
●   define properties of the types
●   like an interface
●   Typeclasses:
         –   Eq can be compared
         –   Ord can be ordered (>, <, >=, <=) (extending Eq)
         –   Show can be shown as string
         –   Read opposite of Show
         –   Enum sequentially ordered types (can be enumerated
             and usable in List-Ranges ['a'..'e'])
Typeclasses (2)
●   Typeclasses:
         –   Bounded upper/lower bound (minBound, maxBound)
         –   Num types behave like numbers (must already be Show, Eq)
         –   Integral contains only integrals (subclass of Num)
         –   Floating corresponding real numbers (subclass of Num)
●   if all Types of tuple are in same Typeclass → Tuple also in
        Typeclass
Haskell Refresher Quiz
●   Which will terminate?
    (1)   head [1..]
    (2)   last [1..]
    (3)   tail [1,2,3,4,5,6]
    (4)   init (take 5 (cycle [1..]))
Haskell Refresher Quiz
●   Which will terminate?
    (1) head [1..]
    (2) last [1..]
    (3) tail [1,2,3,4,5,6]
    (4) init (take 5 (cycle [1..]))
●   (1), (3) and (4) because of
    Lazy Evaluation
Lazy Evaluation
●   Function execution only if result is needed
●   → Program = series of data-transformations
●   Example: A(B(C(x)))
       –   If A needs result from B → call B
       –   If B needs result from C → call C
Haskell Refresher Quiz
●   Which one is faster?
    (1)   head (take 5 [1,2,3,4,5,6])
    (2)   head (take 5 [1..10])
    (3)   head (take 5 [1..])
    (4)   head (take 5 (cycle [1..10]))
Haskell Refresher Quiz
●   Which one is faster?
    (1) head (take 5 [1,2,3,4,5,6])
    (2) head (take 5 [1..10])
    (3) head (take 5 [1..])
    (4) head (take 5 (cycle [1..10]))
●   Everyone is equal because of
    Lazy Loading
Sources
[1] Haskell-Tutorial: Learn you a Haskell (http://learnyouahaskell.com/,
    2012/03/15)
[2] The Hugs User-Manual (
    http://cvs.haskell.org/Hugs/pages/hugsman/index.html, 2012/03/15)
[3] The Haskellwiki (http://www.haskell.org/haskellwiki, 2012/03/15)

Mais conteúdo relacionado

Mais procurados

Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Philip Schwarz
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data TypesRokonuzzaman Rony
 
密度比推定による時系列データの異常検知
密度比推定による時系列データの異常検知密度比推定による時系列データの異常検知
密度比推定による時系列データの異常検知- Core Concept Technologies
 
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural NetworksPaper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural NetworksChenYiHuang5
 
ラムダ計算入門
ラムダ計算入門ラムダ計算入門
ラムダ計算入門Eita Sugimoto
 
Intro to SVE 富岳のA64FXを触ってみた
Intro to SVE 富岳のA64FXを触ってみたIntro to SVE 富岳のA64FXを触ってみた
Intro to SVE 富岳のA64FXを触ってみたMITSUNARI Shigeo
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Rick Copeland
 
クラシックな機械学習の入門  10. マルコフ連鎖モンテカルロ 法
クラシックな機械学習の入門  10. マルコフ連鎖モンテカルロ 法クラシックな機械学習の入門  10. マルコフ連鎖モンテカルロ 法
クラシックな機械学習の入門  10. マルコフ連鎖モンテカルロ 法Hiroshi Nakagawa
 
代数的データ型をラムダ計算の中で表現する方法
代数的データ型をラムダ計算の中で表現する方法代数的データ型をラムダ計算の中で表現する方法
代数的データ型をラムダ計算の中で表現する方法syamino
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cythonfuzzysphere
 
パーセプトロン型学習規則
パーセプトロン型学習規則パーセプトロン型学習規則
パーセプトロン型学習規則Shuhei Sowa
 
正則化項について
正則化項について正則化項について
正則化項についてArata Honda
 
LLVM Instruction Selection
LLVM Instruction SelectionLLVM Instruction Selection
LLVM Instruction SelectionShiva Chen
 
連続最適化勉強会
連続最適化勉強会連続最適化勉強会
連続最適化勉強会shima o
 

Mais procurados (20)

Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
 
Knapsack
KnapsackKnapsack
Knapsack
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 
密度比推定による時系列データの異常検知
密度比推定による時系列データの異常検知密度比推定による時系列データの異常検知
密度比推定による時系列データの異常検知
 
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural NetworksPaper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
 
ラムダ計算入門
ラムダ計算入門ラムダ計算入門
ラムダ計算入門
 
Intro to SVE 富岳のA64FXを触ってみた
Intro to SVE 富岳のA64FXを触ってみたIntro to SVE 富岳のA64FXを触ってみた
Intro to SVE 富岳のA64FXを触ってみた
 
Planar graph
Planar graphPlanar graph
Planar graph
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
 
Huffman Encoding Pr
Huffman Encoding PrHuffman Encoding Pr
Huffman Encoding Pr
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 
クラシックな機械学習の入門  10. マルコフ連鎖モンテカルロ 法
クラシックな機械学習の入門  10. マルコフ連鎖モンテカルロ 法クラシックな機械学習の入門  10. マルコフ連鎖モンテカルロ 法
クラシックな機械学習の入門  10. マルコフ連鎖モンテカルロ 法
 
代数的データ型をラムダ計算の中で表現する方法
代数的データ型をラムダ計算の中で表現する方法代数的データ型をラムダ計算の中で表現する方法
代数的データ型をラムダ計算の中で表現する方法
 
Wrapping a C++ library with Cython
Wrapping a C++ library with CythonWrapping a C++ library with Cython
Wrapping a C++ library with Cython
 
パーセプトロン型学習規則
パーセプトロン型学習規則パーセプトロン型学習規則
パーセプトロン型学習規則
 
正則化項について
正則化項について正則化項について
正則化項について
 
LLVM Instruction Selection
LLVM Instruction SelectionLLVM Instruction Selection
LLVM Instruction Selection
 
連続最適化勉強会
連続最適化勉強会連続最適化勉強会
連続最適化勉強会
 

Semelhante a 03. haskell refresher quiz

Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Mattersromanandreg
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskellgoncharenko
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Philip Schwarz
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scaladjspiewak
 
Comparing Haskell & Scala
Comparing Haskell & ScalaComparing Haskell & Scala
Comparing Haskell & ScalaMartin Ockajak
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant Kalkar
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckFranklin Chen
 
FunScript 2013 (with speakers notes)
FunScript 2013 (with speakers notes)FunScript 2013 (with speakers notes)
FunScript 2013 (with speakers notes)Zach Bray
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevJavaDayUA
 
Functional programming with_scala
Functional programming with_scalaFunctional programming with_scala
Functional programming with_scalaRaymond Tay
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in ScalaAyush Mishra
 

Semelhante a 03. haskell refresher quiz (20)

04. haskell handling
04. haskell handling04. haskell handling
04. haskell handling
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
 
01. haskell introduction
01. haskell introduction01. haskell introduction
01. haskell introduction
 
02. haskell motivation
02. haskell motivation02. haskell motivation
02. haskell motivation
 
bobok
bobokbobok
bobok
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
Comparing Haskell & Scala
Comparing Haskell & ScalaComparing Haskell & Scala
Comparing Haskell & Scala
 
Practical cats
Practical catsPractical cats
Practical cats
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
08. haskell Functions
08. haskell Functions08. haskell Functions
08. haskell Functions
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheck
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
FunScript 2013 (with speakers notes)
FunScript 2013 (with speakers notes)FunScript 2013 (with speakers notes)
FunScript 2013 (with speakers notes)
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
 
Functional programming with_scala
Functional programming with_scalaFunctional programming with_scala
Functional programming with_scala
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in Scala
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 

Último

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 

Último (20)

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 

03. haskell refresher quiz

  • 1. Refresher Sebastian Rettig “Work on Haskell began in 1987 when aa committee of “Work on Haskell began in 1987 when committee of researchers got together to design aa kick-asslanguage.” ([1]) researchers got together to design kick-ass language.” ([1])
  • 2. Imperative Programming ● Variables (value placeholder) – Value can change during program flow ● Modules, Classes, Functions, Procedures ● Control Flow Structures (IF THEN ELSE, Loops (WHILE), Goto)
  • 3. Functional Programming ● No Variables ● Functions only, eventually stored in Modules – Behavior do not change, once defined – → Function called with same parameter calculates always the same result ● Function definitions (Match Cases) ● Recursion
  • 4. Haskell Refresher Quiz ● What's the red one ? ● maxList :: (Ord a) => [a] -> a maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 5. Haskell Refresher Quiz ● What's the red one ? ● maxList :: (Ord a) => [a] -> a Function maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 6. Haskell Refresher Quiz ● What's the red one ? ● maxList :: (Ord a) => [a] -> a maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 7. Haskell Refresher Quiz ● What's the red one ? ● maxList :: (Ord a) => [a] -> a Function Header maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 8. Haskell Refresher Quiz ● What's the red one ? maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 9. Haskell Refresher Quiz ● What's the red one ? ● maxList :: [Int] -> Int ● maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x Function Body | otherwise = maxTail where maxTail = maxList xs
  • 10. Haskell Functions ● function contains header and body ● header consists of type definition: <funcname> :: <param> [ -> <param_n>] -> <result> ● body consists of pattern rules and calculation <funcname> <paramalias_1> [<paramalias_n>] = {calc} ● Example (2 params if type [Int] & Int, result Bool): isHead :: [Int] -> Int -> Bool isHead xs i = i==head xs
  • 11. Haskell Refresher Quiz ● What's the red one ? ● maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 12. Haskell Refresher Quiz ● What's the red one ? ● maxList :: [Int] -> Int Type maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 13. Types in Haskell (1) Starts with uppercase first character ● Int bounded from -2147483648 to 2147483647 ● Integer unbounded (for big numbers) but slower than Int ● Float floating point (single precision) ● Double floating point (double precision) ● Bool boolean ● Char character ● String list of Char (String == [Char])
  • 14. Types in Haskell (2) ● Lists: must be homogenous (entries from the same type) – [] empty List – [Int] List of Int – But e.g. [Int, Bool] not allowed (not homogenous)! ● Tuples: can contain different types BUT have fixed length – () empty tuple (has only 1 value) – (Int, Bool) tuple of a pair Int, Bool – (Int, Bool, Bool) tuple of Int, Bool, Bool
  • 15. Lists & List Comprehension ● Example: double entries in list doubleList [] = [] doubleList (x:xs) = x:x:doubleList xs – e.g. [1,2,3] → x=1; xs=[2,3] ● Multiplication of a list of values: Hugs> product [1..5] 120 ● or in a function: fac n = product [1..n]
  • 16. Haskell Refresher Quiz ● What's the red one ? ● maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 17. Haskell Refresher Quiz ● What's the red one ? ● maxList :: [Int] -> Int maxList [] = maxList [x] = x error “empty” Pattern maxList (x:xs) Matching | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 18. Haskell Refresher Quiz ● What's the red one ? ● maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 19. Haskell Refresher Quiz ● What's the red one ? ● maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail Guards where maxTail = maxList xs
  • 20. Haskell Refresher Quiz ● What's the red one ? ● maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 21. Haskell Refresher Quiz ● What's the red one ? ● maxList :: [Int] -> Int maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail Additional where maxTail = maxList xs 'where' clause
  • 22. Pattern Matching (1) ● create matching rules: fac 0 = 1 fac n = n * fac (n-1) ● use wildcards to ignore parameters in pattern: take 0 _ = [] take _ [] = [] take n (x:xs) = [x] ++ take (n-1) xs
  • 23. Pattern Matching (2) ● use Guards to separate a matching case deeper: myFilter _ [] = [] myFilter f (x:xs) | f==x = x:myFilter g xs | otherwise = myFilter f xs where g = 2*f – like an IF THEN ELSE – Guard Condition evaluate to Bool (True/False) ● eventually define values with where-clause myFilter 2 [1,2,3,4,5,6] results to [2,4]
  • 24. Recursion ● Recursion vs. Final Recursion: countX :: Int -> [Int] -> Int ● Hugs> countX 3 [1,4,3,5,3] countX x [] = 0 2 countX x (y:ys) | x==y = 1 + countX x ys | otherwise = countX x ys countXFinal :: Int -> [Int] -> Int -> Int countXFinal x [] accu = accu countXFinal x (y:ys) accu | x==y = countXFinal x ys accu+1 | otherwise = countXFinal x ys accu ● use accumulator to reduce stack usage ● Hugs> countXFinal 3 [1,4,3,5,3] 0 2
  • 25. Haskell Refresher Quiz ● What's the red one ? ● maxList :: (Ord a) => [a] -> a maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 26. Haskell Refresher Quiz ● What's the red one ? ● maxList :: (Ord a) => [a] -> a Typeclass maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 27. Haskell Refresher Quiz ● What's the red one ? ● maxList :: (Ord a ) => [a] -> a maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 28. Haskell Refresher Quiz ● What's the red one ? ● maxList :: (Ord a ) => [a] -> a Type variable maxList [] = error “empty” maxList [x] = x maxList (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maxList xs
  • 29. Type Polymorphism (1) ● Statically typed, but Compiler can read type from context (type inference) ● → no need to set type explicitly ● → makes function more generic for different kinds of types (type polymorphism) – Why should I use quicksort :: [Int] -> [Int] – even if I also want to sort character? Hugs> quicksort ['f','a','d','b'] "abdf"
  • 30. Type Polymorphism (2) ● the header of our previous implementations could be fact :: Int -> Int maxList :: [Int] -> Int ● but is only limited to Int, but maxList could also handle Char ● → why not make it generic? maxList :: [a] -> a ● but what happens, if the corresponding Type is not comparable or cannot be ordered?
  • 31. Type Polymorphism (3) ● Solution: use Typeclasses maxList :: (Ord a) => [a] -> a ● then we can be sure to use (<,<=, ==, /=, >=, >) ● function header can contain multiple typeclasses maxList :: (Ord a, Eq b) => [a] -> [b] -> a ● In Haskell-Interpreter: to list the function header :t <function_name>
  • 32. Typeclasses (1) ● define properties of the types ● like an interface ● Typeclasses: – Eq can be compared – Ord can be ordered (>, <, >=, <=) (extending Eq) – Show can be shown as string – Read opposite of Show – Enum sequentially ordered types (can be enumerated and usable in List-Ranges ['a'..'e'])
  • 33. Typeclasses (2) ● Typeclasses: – Bounded upper/lower bound (minBound, maxBound) – Num types behave like numbers (must already be Show, Eq) – Integral contains only integrals (subclass of Num) – Floating corresponding real numbers (subclass of Num) ● if all Types of tuple are in same Typeclass → Tuple also in Typeclass
  • 34. Haskell Refresher Quiz ● Which will terminate? (1) head [1..] (2) last [1..] (3) tail [1,2,3,4,5,6] (4) init (take 5 (cycle [1..]))
  • 35. Haskell Refresher Quiz ● Which will terminate? (1) head [1..] (2) last [1..] (3) tail [1,2,3,4,5,6] (4) init (take 5 (cycle [1..])) ● (1), (3) and (4) because of Lazy Evaluation
  • 36. Lazy Evaluation ● Function execution only if result is needed ● → Program = series of data-transformations ● Example: A(B(C(x))) – If A needs result from B → call B – If B needs result from C → call C
  • 37. Haskell Refresher Quiz ● Which one is faster? (1) head (take 5 [1,2,3,4,5,6]) (2) head (take 5 [1..10]) (3) head (take 5 [1..]) (4) head (take 5 (cycle [1..10]))
  • 38. Haskell Refresher Quiz ● Which one is faster? (1) head (take 5 [1,2,3,4,5,6]) (2) head (take 5 [1..10]) (3) head (take 5 [1..]) (4) head (take 5 (cycle [1..10])) ● Everyone is equal because of Lazy Loading
  • 39. Sources [1] Haskell-Tutorial: Learn you a Haskell (http://learnyouahaskell.com/, 2012/03/15) [2] The Hugs User-Manual ( http://cvs.haskell.org/Hugs/pages/hugsman/index.html, 2012/03/15) [3] The Haskellwiki (http://www.haskell.org/haskellwiki, 2012/03/15)