SlideShare a Scribd company logo
1 of 11
Geoff Holmes
 Week 6 problem
 Overview
 Functions
 Types
 HUGS
 Guards
 Lists
 Recursion
 Tuples
Introduction to Functional
Programming in Haskell
11/13/2022 2
Week 6 problem – Haskell IO
 Write a Haskell function that takes a list of monthly sales figues typed in
at the keyboard and prints them and the average monthly sales in a
table. For example:
> sales [10, 20, 30, 40, 60, 30, 10, 6, 78, 84, 12, 5]
Month Sales
January 10
February 20
March 30
April 40
May 60
June 30
July 10
August 6
September 78
October 84
November 12
December 5
Average 32.08
11/13/2022 3
Functional Programming in Haskell
 What to expect:
 No statements, assignments
 Program is a set of function definitions and one
expression
 Strong type checking
 Useful built in functions to manipulate lists and
strings (basic data structures)
 Interpreted language
11/13/2022 4
Haskell functions – yours or built-in
 head [1, 2, 3] – returns 1
 tail [1, 2, 3] – returns [2, 3]
 minimum [5 ,4, 1, 3, 2] – returns 1
 To build your own you need to provide:
 Function identifier
 Number and types of arguments
 Return type
 Expression that produces the return value
11/13/2022 5
Haskell – a first function
 square x returns x*x
 square :: Int -> Int (ident = square, takes Int arg,
returns Int arg)
 square x = x * x (exp that returns value)
11/13/2022 6
Basic Types
 Constants and types all start with capital letter
 Int, Float, Char, Bool, Integer, Rational
 2^3, 2.0/3.6, ‘a’, True, (2::Integer)^100, 1%2 + 1%3
 Int and Integer have +,-,*,div,mod
 Float and Rational have / (floor, ceiling, etc.)
11/13/2022 7
The HUGS interpreter
 Put all function definitions into a file and load the file into HUGS
double :: Int -> Int
double x = 2 * x
square :: Int -> Int
square x = x * x
rectArea :: Int -> Int -> Int
rectArea b h = b * h
isEven :: Int -> Bool
isEven x = (mod x 2) == 0
11/13/2022 8
Guards and Recursion
 To provide a switch among options we use a guard |, as in:
smallest :: Int -> Int -> Int
smallest a b
| a < b = a
| b < a = b
 Recursive functions are very common, eg. doublelist:
doublelist :: [Int] -> [Int]
doublelist a
| a == [] = []
| otherwise = [double (head a)] ++ doublelist (tail a)
11/13/2022 9
Lists
 [type] for any type
 String is [Char]
 “geoff” == [‘g’,’e’,’o’,’f’,’f’]
 [ ] is the empty list [ 2 .. 5 ] = [2,3,4,5]
 List comprehensions let you apply functions to a generated
list, eg [ 2 * n | n <- [1,2,3] ] - returns [2,4,6]
11/13/2022 10
Recursive Lists
remove :: Int -> [Int] -> [Int]
remove x xs
| xs == [] = []
| head xs == x = tail xs
| otherwise = head xs : remove x (tail xs)
selectionSort :: [Int] -> [Int]
selectionSort a
| a == [] = []
| otherwise = minimum a : selectionSort (remove (minimum a) a)
11/13/2022 11
Tuples
 Used for records – grouping related data together
 (“fred smith”, 124, True) – (String, Int, Bool)
 Can supply tuples to functions and create types:
type Point = (Float, Float)
makePoint :: Float -> Float -> Point
makePoint x y = (x,y)
type Student = (String, Int, Bool)
type class = [Student]

More Related Content

Similar to Intro.ppt

R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environmentYogendra Chaubey
 
The Ring programming language version 1.8 book - Part 23 of 202
The Ring programming language version 1.8 book - Part 23 of 202The Ring programming language version 1.8 book - Part 23 of 202
The Ring programming language version 1.8 book - Part 23 of 202Mahmoud Samir Fayed
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code LabColin Su
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsVasil Remeniuk
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query LanguageJulian Hyde
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdfAshaWankar1
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional ProgrammingLuka Jacobowitz
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Pedro Rodrigues
 
R Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB AcademyR Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB Academyrajkamaltibacademy
 
The Ring programming language version 1.5.4 book - Part 22 of 185
The Ring programming language version 1.5.4 book - Part 22 of 185The Ring programming language version 1.5.4 book - Part 22 of 185
The Ring programming language version 1.5.4 book - Part 22 of 185Mahmoud Samir Fayed
 
Morel, a data-parallel programming language
Morel, a data-parallel programming languageMorel, a data-parallel programming language
Morel, a data-parallel programming languageJulian Hyde
 
Python 培训讲义
Python 培训讲义Python 培训讲义
Python 培训讲义leejd
 
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a MomentSergio Gil
 

Similar to Intro.ppt (20)

Python Training
Python TrainingPython Training
Python Training
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
scripting in Python
scripting in Pythonscripting in Python
scripting in Python
 
The Ring programming language version 1.8 book - Part 23 of 202
The Ring programming language version 1.8 book - Part 23 of 202The Ring programming language version 1.8 book - Part 23 of 202
The Ring programming language version 1.8 book - Part 23 of 202
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
C# programming
C# programming C# programming
C# programming
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami Patterns
 
Python basics - for bigginers
Python basics - for bigginersPython basics - for bigginers
Python basics - for bigginers
 
Python basics
Python basicsPython basics
Python basics
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query Language
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional Programming
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)
 
R Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB AcademyR Programming Tutorial for Beginners - -TIB Academy
R Programming Tutorial for Beginners - -TIB Academy
 
The Ring programming language version 1.5.4 book - Part 22 of 185
The Ring programming language version 1.5.4 book - Part 22 of 185The Ring programming language version 1.5.4 book - Part 22 of 185
The Ring programming language version 1.5.4 book - Part 22 of 185
 
Practical cats
Practical catsPractical cats
Practical cats
 
Morel, a data-parallel programming language
Morel, a data-parallel programming languageMorel, a data-parallel programming language
Morel, a data-parallel programming language
 
Python 培训讲义
Python 培训讲义Python 培训讲义
Python 培训讲义
 
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a Moment
 

Recently uploaded

Efficient spin-up of Earth System Models usingsequence acceleration
Efficient spin-up of Earth System Models usingsequence accelerationEfficient spin-up of Earth System Models usingsequence acceleration
Efficient spin-up of Earth System Models usingsequence accelerationSérgio Sacani
 
COMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demeritsCOMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demeritsCherry
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learninglevieagacer
 
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRingsTransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRingsSérgio Sacani
 
Use of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptxUse of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptxRenuJangid3
 
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRLGwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRLkantirani197
 
Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.Cherry
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.Cherry
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsSérgio Sacani
 
FAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceFAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceAlex Henderson
 
Role of AI in seed science Predictive modelling and Beyond.pptx
Role of AI in seed science  Predictive modelling and  Beyond.pptxRole of AI in seed science  Predictive modelling and  Beyond.pptx
Role of AI in seed science Predictive modelling and Beyond.pptxArvind Kumar
 
FS P2 COMBO MSTA LAST PUSH past exam papers.
FS P2 COMBO MSTA LAST PUSH past exam papers.FS P2 COMBO MSTA LAST PUSH past exam papers.
FS P2 COMBO MSTA LAST PUSH past exam papers.takadzanijustinmaime
 
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsKanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsDeepika Singh
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusNazaninKarimi6
 
PODOCARPUS...........................pptx
PODOCARPUS...........................pptxPODOCARPUS...........................pptx
PODOCARPUS...........................pptxCherry
 
Cyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCherry
 
Digital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxDigital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxMohamedFarag457087
 
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort ServiceCall Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort Serviceshivanisharma5244
 
Thyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate ProfessorThyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate Professormuralinath2
 
Pteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecyclePteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecycleCherry
 

Recently uploaded (20)

Efficient spin-up of Earth System Models usingsequence acceleration
Efficient spin-up of Earth System Models usingsequence accelerationEfficient spin-up of Earth System Models usingsequence acceleration
Efficient spin-up of Earth System Models usingsequence acceleration
 
COMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demeritsCOMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demerits
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learning
 
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRingsTransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
 
Use of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptxUse of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptx
 
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRLGwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
 
Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
 
FAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceFAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical Science
 
Role of AI in seed science Predictive modelling and Beyond.pptx
Role of AI in seed science  Predictive modelling and  Beyond.pptxRole of AI in seed science  Predictive modelling and  Beyond.pptx
Role of AI in seed science Predictive modelling and Beyond.pptx
 
FS P2 COMBO MSTA LAST PUSH past exam papers.
FS P2 COMBO MSTA LAST PUSH past exam papers.FS P2 COMBO MSTA LAST PUSH past exam papers.
FS P2 COMBO MSTA LAST PUSH past exam papers.
 
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsKanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virus
 
PODOCARPUS...........................pptx
PODOCARPUS...........................pptxPODOCARPUS...........................pptx
PODOCARPUS...........................pptx
 
Cyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptx
 
Digital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxDigital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptx
 
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort ServiceCall Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
 
Thyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate ProfessorThyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate Professor
 
Pteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecyclePteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecycle
 

Intro.ppt

  • 1. Geoff Holmes  Week 6 problem  Overview  Functions  Types  HUGS  Guards  Lists  Recursion  Tuples Introduction to Functional Programming in Haskell
  • 2. 11/13/2022 2 Week 6 problem – Haskell IO  Write a Haskell function that takes a list of monthly sales figues typed in at the keyboard and prints them and the average monthly sales in a table. For example: > sales [10, 20, 30, 40, 60, 30, 10, 6, 78, 84, 12, 5] Month Sales January 10 February 20 March 30 April 40 May 60 June 30 July 10 August 6 September 78 October 84 November 12 December 5 Average 32.08
  • 3. 11/13/2022 3 Functional Programming in Haskell  What to expect:  No statements, assignments  Program is a set of function definitions and one expression  Strong type checking  Useful built in functions to manipulate lists and strings (basic data structures)  Interpreted language
  • 4. 11/13/2022 4 Haskell functions – yours or built-in  head [1, 2, 3] – returns 1  tail [1, 2, 3] – returns [2, 3]  minimum [5 ,4, 1, 3, 2] – returns 1  To build your own you need to provide:  Function identifier  Number and types of arguments  Return type  Expression that produces the return value
  • 5. 11/13/2022 5 Haskell – a first function  square x returns x*x  square :: Int -> Int (ident = square, takes Int arg, returns Int arg)  square x = x * x (exp that returns value)
  • 6. 11/13/2022 6 Basic Types  Constants and types all start with capital letter  Int, Float, Char, Bool, Integer, Rational  2^3, 2.0/3.6, ‘a’, True, (2::Integer)^100, 1%2 + 1%3  Int and Integer have +,-,*,div,mod  Float and Rational have / (floor, ceiling, etc.)
  • 7. 11/13/2022 7 The HUGS interpreter  Put all function definitions into a file and load the file into HUGS double :: Int -> Int double x = 2 * x square :: Int -> Int square x = x * x rectArea :: Int -> Int -> Int rectArea b h = b * h isEven :: Int -> Bool isEven x = (mod x 2) == 0
  • 8. 11/13/2022 8 Guards and Recursion  To provide a switch among options we use a guard |, as in: smallest :: Int -> Int -> Int smallest a b | a < b = a | b < a = b  Recursive functions are very common, eg. doublelist: doublelist :: [Int] -> [Int] doublelist a | a == [] = [] | otherwise = [double (head a)] ++ doublelist (tail a)
  • 9. 11/13/2022 9 Lists  [type] for any type  String is [Char]  “geoff” == [‘g’,’e’,’o’,’f’,’f’]  [ ] is the empty list [ 2 .. 5 ] = [2,3,4,5]  List comprehensions let you apply functions to a generated list, eg [ 2 * n | n <- [1,2,3] ] - returns [2,4,6]
  • 10. 11/13/2022 10 Recursive Lists remove :: Int -> [Int] -> [Int] remove x xs | xs == [] = [] | head xs == x = tail xs | otherwise = head xs : remove x (tail xs) selectionSort :: [Int] -> [Int] selectionSort a | a == [] = [] | otherwise = minimum a : selectionSort (remove (minimum a) a)
  • 11. 11/13/2022 11 Tuples  Used for records – grouping related data together  (“fred smith”, 124, True) – (String, Int, Bool)  Can supply tuples to functions and create types: type Point = (Float, Float) makePoint :: Float -> Float -> Point makePoint x y = (x,y) type Student = (String, Int, Bool) type class = [Student]