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]