SlideShare a Scribd company logo
1 of 19
Download to read offline
Introduction

                 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-ass language.”([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 (Memory)
Haskell Features
●   Pure Functional Programming Language
●   Lazy Evaluation
●   Pattern Matching and Guards
●   List Comprehension
●   Type Polymorphism
Haskell Functions (1)
●   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}

         –   more later...
●   Example (2 params if type [Int] & Int, result Bool):
             isHead :: [Int] -> Int -> Bool
             isHead xs i = i==head xs
Haskell Functions (2)
●   You want an Interface? No Problem
    myMap :: (Float -> Int) -> [Int] -> [Int]

●   first parameter of myMap is a function
●   allows all functions, with Float parameter and result of
    type Int
●   interface also individual extendable
    myMap :: (Float -> Bool -> Int) -> [Int] -> [Int]

●   or include a sub-level interface
    myMap :: ((Int -> Float) -> Bool -> Int) -> [Int] -> [Int]
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
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]
List Comprehension (1)
●   Example Quicksort:
quicksort [] = []
quicksort (x:xs) =
    quicksort [y | y <- xs,y<x] ++ [x] ++ quicksort[y | y <- xs,y>=x]

        –   Who the f??k needs an array?
                 ●   Lists are dynamic, flexible & part of the
                       language definition = deeply integrated
        –   List expression (x:xs)
                 ●   x is head of list (value),
                 ●   xs is tail (list)
                 ●   also individual extendable (x:y:ys)
        –   List Generator: [<value> | <pipe>, <match> ]
List Comprehension (2)
                        void qsort(int a[], int lo, int hi)

Quicksort in C: ([2])
                        {
                          int h, l, p, t;

                            if (lo < hi) {
                              l = lo;
                              h = hi;
                              p = a[hi];

                                do {
                                  while ((l < h) && (a[l] <= p))
                                      l = l+1;
                                  while ((h > l) && (a[h] >= p))
                                      h = h-1;
                                  if (l < h) {
                                      t = a[l];
                                      a[l] = a[h];
                                      a[h] = t;
                                  }
                                } while (l < h);

                                a[hi] = a[l];
                                a[l] = p;

                                qsort( a, lo, l-1 );
                                qsort( a, l+1, hi );
                            }
                        }
List Comprehension (3)
●   Example: double entries in list
          doubleList [] = []
          doubleList (x:xs) = x:x:doubleList xs
●   Multiplication of a list of values:
          Hugs> product [1..5]
            120
●   or in a function:
          fac n = product [1..n]
Type Polymorphism
●   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"
Recursion (1)
●   we have no loops → use Recursion:
    myMap :: Int -> [Int] -> [Int]
    myMap v [] = []    --    Recursion Anchor!
    myMap v (x:xs) = [v*x] ++ myMap v xs
●   Recursion Anchor contains the break rule
        –   endless loop = anchorless recursion
            isTrue :: Bool    Bool
            isTrue b = b && isTrue b
Recursion (2)
●    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
History
●   Haskell 1.0 (1990) – 1.4
●   Haskell 98 (1997)
        –   quasi standard language definition
        –   Foreign Function Interface included ([3])
●   Haskell Prime (2006 = ~)
        –   Haskell 2010 (2009)
                ●   first revision
                ●   → now Control Flow Structures available
                       (Why? I am currently not sure.)
Where to start?
●   for Beginner – Hugs (Haskell User's Gofer System):
        –   Successor of Gofer Interpreter (http://www.haskell.org/hugs/)
        –   Syntax closer to Miranda than to Haskell, but contains
              Haskell98 language specification
        –   really smart & easy installation in Mac using brew
Where to start?
●   for Developer - Haskell Platform – Batteries Included:
        –   Big “Toy” in big Package at
              (http://hackage.haskell.org/platform/)
        –   Contains Glasgow Haskell Compiler (GHC)
                 ●   THE one and only :)
                 ●   contains the full 2010 standard
                 ●   can also generate .cpp from .hs
        –   Contains Interpreter (GHCi)
                 ●   also Bytecode Interpreter like Hugs, but 2010
                       Standard
Sources
[1] Haskell-Tutorial: Learn you a Haskell (http://learnyouahaskell.com/,
2012/03/15)
[2] Haskell Introduction: Quicksort in C (
http://www.haskell.org/haskellwiki/Introduction, 2012/03/15)
[3] The Haskell 98 Foreign Function Interface Addendum (
http://www.cse.unsw.edu.au/~chak/haskell/ffi/, 2012/03/15)

More Related Content

What's hot

Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lispkyleburton
 
Beyond tf idf why, what & how
Beyond tf idf why, what & howBeyond tf idf why, what & how
Beyond tf idf why, what & howlucenerevolution
 
Advance LISP (Artificial Intelligence)
Advance LISP (Artificial Intelligence) Advance LISP (Artificial Intelligence)
Advance LISP (Artificial Intelligence) wahab khan
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...gjcross
 
Scala collections wizardry - Scalapeño
Scala collections wizardry - ScalapeñoScala collections wizardry - Scalapeño
Scala collections wizardry - ScalapeñoSagie Davidovich
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Chia-Chi Chang
 
INTRODUCTION TO LISP
INTRODUCTION TO LISPINTRODUCTION TO LISP
INTRODUCTION TO LISPNilt1234
 
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
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In ScalaKnoldus Inc.
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsKirill Kozlov
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arraysIntro C# Book
 
Proposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyondProposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyondBarry Feigenbaum
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
A brief introduction to lisp language
A brief introduction to lisp languageA brief introduction to lisp language
A brief introduction to lisp languageDavid Gu
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Bryan O'Sullivan
 

What's hot (19)

02. haskell motivation
02. haskell motivation02. haskell motivation
02. haskell motivation
 
Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lisp
 
(Ai lisp)
(Ai lisp)(Ai lisp)
(Ai lisp)
 
Beyond tf idf why, what & how
Beyond tf idf why, what & howBeyond tf idf why, what & how
Beyond tf idf why, what & how
 
Advance LISP (Artificial Intelligence)
Advance LISP (Artificial Intelligence) Advance LISP (Artificial Intelligence)
Advance LISP (Artificial Intelligence)
 
Why Haskell
Why HaskellWhy Haskell
Why Haskell
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...
 
Scala collections wizardry - Scalapeño
Scala collections wizardry - ScalapeñoScala collections wizardry - Scalapeño
Scala collections wizardry - Scalapeño
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
 
INTRODUCTION TO LISP
INTRODUCTION TO LISPINTRODUCTION TO LISP
INTRODUCTION TO LISP
 
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...
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
 
Proposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyondProposals for new function in Java SE 9 and beyond
Proposals for new function in Java SE 9 and beyond
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
A brief introduction to lisp language
A brief introduction to lisp languageA brief introduction to lisp language
A brief introduction to lisp language
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5
 

Similar to 01. haskell introduction

Haskell - Being lazy with class
Haskell - Being lazy with classHaskell - Being lazy with class
Haskell - Being lazy with classTiago Babo
 
03. haskell refresher quiz
03. haskell refresher quiz03. haskell refresher quiz
03. haskell refresher quizSebastian Rettig
 
Comparing Haskell & Scala
Comparing Haskell & ScalaComparing Haskell & Scala
Comparing Haskell & ScalaMartin Ockajak
 
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a MomentSergio Gil
 
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and FuturePushkar Kulkarni
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present FutureIndicThreads
 
Basic and logical implementation of r language
Basic and logical implementation of r language Basic and logical implementation of r language
Basic and logical implementation of r language Md. Mahedi Mahfuj
 
Introduction to Haskell: 2011-04-13
Introduction to Haskell: 2011-04-13Introduction to Haskell: 2011-04-13
Introduction to Haskell: 2011-04-13Jay Coskey
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programmingchriseidhof
 
TDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação FuncionalTDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação Funcionaltdc-globalcode
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsEelco Visser
 

Similar to 01. haskell introduction (20)

06. haskell type builder
06. haskell type builder06. haskell type builder
06. haskell type builder
 
Haskell - Being lazy with class
Haskell - Being lazy with classHaskell - Being lazy with class
Haskell - Being lazy with class
 
08. haskell Functions
08. haskell Functions08. haskell Functions
08. haskell Functions
 
03. haskell refresher quiz
03. haskell refresher quiz03. haskell refresher quiz
03. haskell refresher quiz
 
Comparing Haskell & Scala
Comparing Haskell & ScalaComparing Haskell & Scala
Comparing Haskell & Scala
 
10. haskell Modules
10. haskell Modules10. haskell Modules
10. haskell Modules
 
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a Moment
 
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and Future
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
 
Basic and logical implementation of r language
Basic and logical implementation of r language Basic and logical implementation of r language
Basic and logical implementation of r language
 
Introduction to Haskell: 2011-04-13
Introduction to Haskell: 2011-04-13Introduction to Haskell: 2011-04-13
Introduction to Haskell: 2011-04-13
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
Practical cats
Practical catsPractical cats
Practical cats
 
TDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação FuncionalTDC2016SP - Trilha Programação Funcional
TDC2016SP - Trilha Programação Funcional
 
Haskell 101
Haskell 101Haskell 101
Haskell 101
 
07. haskell Membership
07. haskell Membership07. haskell Membership
07. haskell Membership
 
C# programming
C# programming C# programming
C# programming
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 

Recently uploaded

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Recently uploaded (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

01. haskell introduction

  • 1. Introduction 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-ass language.”([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 (Memory)
  • 4. Haskell Features ● Pure Functional Programming Language ● Lazy Evaluation ● Pattern Matching and Guards ● List Comprehension ● Type Polymorphism
  • 5. Haskell Functions (1) ● 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} – more later... ● Example (2 params if type [Int] & Int, result Bool): isHead :: [Int] -> Int -> Bool isHead xs i = i==head xs
  • 6. Haskell Functions (2) ● You want an Interface? No Problem myMap :: (Float -> Int) -> [Int] -> [Int] ● first parameter of myMap is a function ● allows all functions, with Float parameter and result of type Int ● interface also individual extendable myMap :: (Float -> Bool -> Int) -> [Int] -> [Int] ● or include a sub-level interface myMap :: ((Int -> Float) -> Bool -> Int) -> [Int] -> [Int]
  • 7. 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
  • 8. 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
  • 9. 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]
  • 10. List Comprehension (1) ● Example Quicksort: quicksort [] = [] quicksort (x:xs) = quicksort [y | y <- xs,y<x] ++ [x] ++ quicksort[y | y <- xs,y>=x] – Who the f??k needs an array? ● Lists are dynamic, flexible & part of the language definition = deeply integrated – List expression (x:xs) ● x is head of list (value), ● xs is tail (list) ● also individual extendable (x:y:ys) – List Generator: [<value> | <pipe>, <match> ]
  • 11. List Comprehension (2) void qsort(int a[], int lo, int hi) Quicksort in C: ([2]) { int h, l, p, t; if (lo < hi) { l = lo; h = hi; p = a[hi]; do { while ((l < h) && (a[l] <= p)) l = l+1; while ((h > l) && (a[h] >= p)) h = h-1; if (l < h) { t = a[l]; a[l] = a[h]; a[h] = t; } } while (l < h); a[hi] = a[l]; a[l] = p; qsort( a, lo, l-1 ); qsort( a, l+1, hi ); } }
  • 12. List Comprehension (3) ● Example: double entries in list doubleList [] = [] doubleList (x:xs) = x:x:doubleList xs ● Multiplication of a list of values: Hugs> product [1..5] 120 ● or in a function: fac n = product [1..n]
  • 13. Type Polymorphism ● 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"
  • 14. Recursion (1) ● we have no loops → use Recursion: myMap :: Int -> [Int] -> [Int] myMap v [] = [] -- Recursion Anchor! myMap v (x:xs) = [v*x] ++ myMap v xs ● Recursion Anchor contains the break rule – endless loop = anchorless recursion isTrue :: Bool Bool isTrue b = b && isTrue b
  • 15. Recursion (2) ● 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
  • 16. History ● Haskell 1.0 (1990) – 1.4 ● Haskell 98 (1997) – quasi standard language definition – Foreign Function Interface included ([3]) ● Haskell Prime (2006 = ~) – Haskell 2010 (2009) ● first revision ● → now Control Flow Structures available (Why? I am currently not sure.)
  • 17. Where to start? ● for Beginner – Hugs (Haskell User's Gofer System): – Successor of Gofer Interpreter (http://www.haskell.org/hugs/) – Syntax closer to Miranda than to Haskell, but contains Haskell98 language specification – really smart & easy installation in Mac using brew
  • 18. Where to start? ● for Developer - Haskell Platform – Batteries Included: – Big “Toy” in big Package at (http://hackage.haskell.org/platform/) – Contains Glasgow Haskell Compiler (GHC) ● THE one and only :) ● contains the full 2010 standard ● can also generate .cpp from .hs – Contains Interpreter (GHCi) ● also Bytecode Interpreter like Hugs, but 2010 Standard
  • 19. Sources [1] Haskell-Tutorial: Learn you a Haskell (http://learnyouahaskell.com/, 2012/03/15) [2] Haskell Introduction: Quicksort in C ( http://www.haskell.org/haskellwiki/Introduction, 2012/03/15) [3] The Haskell 98 Foreign Function Interface Addendum ( http://www.cse.unsw.edu.au/~chak/haskell/ffi/, 2012/03/15)