Download presentation

Presentation is loading. Please wait.

Published byBilly Hazleton Modified over 2 years ago

1
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can be found at: www.csc.liv.ac.uk/~grant/Teaching/COMP205/

2
Nested Definitions maxOf3 :: Int -> Int -> Int -> Int maxOf3 x y z = maxOf2 u z where u = maxOf2 x y "Local" definitions can be made using the where keyword: maxOf3 x y z = maxOf2 (maxOf2 x y) z which is equivalent to:

3
The Fibonnacci Sequence -- nth number in the Fibonnacci sequence fib n = fib1 where (fib1, fib2) = fibs n -- (nth, (n+1)th) in Fib. seq. fibs 0 = (1,1) fibs n = (f2, f1 + f2) where (f1,f2) = fibs (n-1)

4
... Or fib n = fib1 where (fib1, fib2) = fibs n fibs n | n <= 0 = (1,1) | n > 0 = (f2, f1 + f2) where (f1,f2) = fibs (n-1)

5
The F sequence where F = Fibonacci -- file: fib.hs fib n = fib1 where (fib1, fib2) = fibs n fibs m | m < 1 = (1,1) | 0 < m = (f2, f1 + f2) where (f1,f2) = fibs (m-1),

6
The F sequence where F = Fibonacci -- file: fib.hs fib n = f1 where (f1, f2) = fibs n fibs n | n < 1 = (1,1) | 0 < n = (f2, f1 + f2) where (f1,f2) = fibs (n-1),

7
Local is Hidden Main> :l fib.hs... Main> fibs 5 ERROR - Undefined variable "fibs" Main>

8
Summary Key topics: Local definitions ( where ) Next: Lists

9
Lists bread milk butter onions orange juice t-bone steaks chocolate Attendance Sheets League tables top tens the Fibonnacci sequence...

10
Lists in Haskell A list is a sequence of values, in a particular order

11
Lists in Haskell A list is a sequence of values, in a particular order Lists are homogeneous: all the elements in a list (in Haskell) have the same type

12
Lists in Haskell A list is a sequence of values, in a particular order Lists are homogeneous: all the elements in a list (in Haskell) have the same type Lists are dynamic: unlike arrays in imperative languages, the length of a list is not constrained (and Haskell allows infinite lists)

13
Notations for Lists Haskell has three notations for lists: constructors: 1 : 1 : 2 : 3 : 5 : 8 : [] "square brackets" notation: [1, 1, 2, 3, 5, 8] strings (only for lists of chars): ['h', 'e', 'l', 'l', 'o'] = "hello"

14
Functions on Lists Prelude> length [1, 1, 2, 3, 5, 8] 6 Prelude> length "" 0 Prelude> [1, 1, 2, 3] ++ [5, 8] [1, 1, 2, 3, 5, 8] Prelude> [] ++ [1] ++ [2] [1,2] Length: Concatenation (append):

15
More Functions on Lists Prelude> head [1, 2, 4] 1 Prelude> Heads and tails

16
More Functions on Lists Prelude> head [1, 2, 4] 1 Prelude> tail [1, 2, 4] [2,4] Prelude> Heads and tails

17
More Functions on Lists Prelude> head [1, 2, 4] 1 Prelude> tail [1, 2, 4] [2,4] Prelude> head [] ERROR:... Prelude> Heads and tails

18
More Functions on Lists Prelude> head [1, 2, 4] 1 Prelude> tail [1, 2, 4] [2,4] Prelude> head [] ERROR:... Prelude> tail [] ERROR:... Heads and tails

19
Yet More Functions on Lists Prelude> take 3 "catflap" "cat" Prelude>

20
Yet More Functions on Lists Prelude> take 3 "catflap" "cat" Prelude> drop 2 ['i','n','t','e','n','t'] "tent" Prelude>

21
Yet More Functions on Lists Prelude> take 3 "catflap" "cat" Prelude> drop 2 ['i','n','t','e','n','t'] "tent" Prelude> reverse 'p':'o':'o':'l':[] "loop" Prelude>

22
Yet More Functions on Lists Prelude> take 3 "catflap" "cat" Prelude> drop 2 ['i','n','t','e','n','t'] "tent" Prelude> reverse 'p':'o':'o':'l':[] "loop" Prelude> zip [1,2,3,4,5] "cat" [(1,'c'),(2,'a'),(3,'t')]

23
List Constructors I A list is either: empty or an element of a given type together with a list of elements of the same type in BNF: [a] ::= [] | a : [a]

24
List Constructors II "[]" and ":" are constructors for lists: they are primitive operators (i.e., not evaluated) all lists are built from these operators (and elements of the "parameter type")

25
Constructors and Pattern-Matching Constructors can be used in Pattern-Matching: isempty [] = True isempty anythingElse = False length [] = 0 length (x : xs) = 1 + length xs head [] = error "head []" head (x:xs) = x

26
Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : [])

27
Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : []) { x 1, xs 2 : 4 : [] }

28
Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : []) { x 1, xs 2 : 4 : [] } 1 + length (2 : 4 : [])

29
Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : []) { x 1, xs 2 : 4 : [] } 1 + length (2 : 4 : []) { x 2, xs 4 : [] }

30
Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : []) { x 1, xs 2 : 4 : [] } 1 + length (2 : 4 : []) { x 2, xs 4 : [] } 1 + 1 + length (4 : [])

31
Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + length xs length (1 : 2 : 4 : []) { x 1, xs 2 : 4 : [] } 1 + length (2 : 4 : []) { x 2, xs 4 : [] } 1 + 1 + length (4 : []) { x 4, xs [] } 1 + 1 + 1 + length []

32
Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + (length xs) length (1 : 2 : 4 : []) { x 1, xs 2 : 4 : [] } 1 + length (2 : 4 : []) { x 2, xs 4 : [] } 1 + 1 + length (4 : []) { x 4, xs [] } 1 + 1 + 1 + length [] { }

33
Evaluating Recursive Functions length [] = 0 length (x : xs) = 1 + (length xs) length (1 : 2 : 4 : []) { x 1, xs 2 : 4 : [] } 1 + length (2 : 4 : []) { x 2, xs 4 : [] } 1 + 1 + length (4 : []) { x 4, xs [] } 1 + 1 + 1 + length [] { } 1 + 1 + 1 + 0

34
length without Pattern-Matching length xs | xs == [] = 0 |otherwise = 1 + length (tail xs) length can be defined without pattern-matching, but the definition is more difficult to read:

35
Summary Key topics: Lists (3 notations) Constructors Pattern-matching Recursion Next: Recursion and List Comprehensions

Similar presentations

OK

Functional Programming Lecture 4 - More Lists Muffy Calder.

Functional Programming Lecture 4 - More Lists Muffy Calder.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Best backgrounds for ppt on social media Ppt on types of sentences Ppt on petroleum industry in india download Ppt on finite difference method Show ppt on french revolution Ppt on layer 3 switching configuration Maths ppt on linear equation in two variables Ppt on condition based maintenance certification Ppt on viruses and bacteria structure Ppt on hard work is the key to success