Recursive Datatypes and Lists Types and constructors data Suit = - - PowerPoint PPT Presentation

recursive datatypes and lists
SMART_READER_LITE
LIVE PREVIEW

Recursive Datatypes and Lists Types and constructors data Suit = - - PowerPoint PPT Presentation

Recursive Datatypes and Lists Types and constructors data Suit = Spades | Hearts | Diamonds | Clubs Interpretation: Here is a new type Suit . This type has four possible values: Spades , Hearts , Diamonds and Clubs . Types and constructors


slide-1
SLIDE 1

Recursive Datatypes and Lists

slide-2
SLIDE 2

Types and constructors

Interpretation: “Here is a new type Suit. This type has four possible values: Spades, Hearts, Diamonds and Clubs.”

data Suit = Spades | Hearts | Diamonds | Clubs

slide-3
SLIDE 3

Types and constructors

This definition introduces five things:

– The type Suit – The constructors Spades :: Suit Hearts :: Suit Diamonds :: Suit Clubs :: Suit

data Suit = Spades | Hearts | Diamonds | Clubs

slide-4
SLIDE 4

Types and constructors

Interpretation: “Here is a new type Rank. Values of this type have five possible possible forms: Numeric n, Jack, Queen, King or Ace, where n is a value of type Integer”

data Rank = Numeric Integer | Jack | Queen | King | Ace

slide-5
SLIDE 5

Types and constructors

This definition introduces six things:

– The type Rank – The constructors Numeric :: ??? Jack :: ??? Queen :: ??? King :: ??? Ace :: ???

data Rank = Numeric Integer | Jack | Queen | King | Ace

slide-6
SLIDE 6

Types and constructors

This definition introduces six things:

– The type Rank – The constructors Numeric :: Integer → Rank Jack :: ??? Queen :: ??? King :: ??? Ace :: ???

data Rank = Numeric Integer | Jack | Queen | King | Ace

slide-7
SLIDE 7

Types and constructors

This definition introduces six things:

– The type Rank – The constructors Numeric :: Integer → Rank Jack :: Rank Queen :: Rank King :: Rank Ace :: Rank

data Rank = Numeric Integer | Jack | Queen | King | Ace

slide-8
SLIDE 8

Types and constructors

data Rank = Numeric Integer | Jack | Queen | King | Ace Type Constructor Type

slide-9
SLIDE 9

Types and constructors

Interpretation: “Here is a new type Card. Values of this type have the form Card r s, where r and s are values of type Rank and Suit respectively.”

data Card = Card Rank Suit

slide-10
SLIDE 10

Types and constructors

This definition introduces two things:

– The type Card – The constructor Card :: ???

data Card = Card Rank Suit

slide-11
SLIDE 11

Types and constructors

This definition introduces two things:

– The type Card – The constructor Card :: Rank → Suit → Card

data Card = Card Rank Suit

slide-12
SLIDE 12

Types and constructors

data Card = Card Rank Suit Type Constructor Type Type

slide-13
SLIDE 13

Types and constructors

Interpretation: “Here is a new type Hand. Values of this type have two possible forms: Empty or Add c h where c and h are of type Card and Hand respectively.”

data Hand = Empty | Add Card Hand

slide-14
SLIDE 14

Types and constructors

Alternative interpretation: “A hand is either empty or consists of a card

  • n top of a smaller hand.”

data Hand = Empty | Add Card Hand

slide-15
SLIDE 15

Types and constructors

This definition introduces three things:

– The type Hand – The constructors Empty :: ??? Add :: ???

data Hand = Empty | Add Card Hand

slide-16
SLIDE 16

Types and constructors

This definition introduces three things:

– The type Hand – The constructors Empty :: Hand Add :: ???

data Hand = Empty | Add Card Hand

slide-17
SLIDE 17

Types and constructors

This definition introduces three things:

– The type Hand – The constructors Empty :: Hand Add :: Card → Hand → Hand

data Hand = Empty | Add Card Hand

slide-18
SLIDE 18

Types and constructors

data Hand = Empty | Add Card Hand Type Constructor Type (recursion) Type Constructors

slide-19
SLIDE 19

Pattern matching

Define functions by stating their results for all possible forms of the input

size :: Hand → Integer

slide-20
SLIDE 20

Pattern matching

Define functions by stating their results for all possible forms of the input

size :: Hand → Integer size Empty = 0 size (Add card hand) = 1 + size hand

Interpretation: “If the argument is Empty, then the result is 0. If the argument consists of a card card on top

  • f a hand hand, then the result is 1 + the size
  • f the rest of the hand.”
slide-21
SLIDE 21

Pattern matching

size :: Hand → Integer size Empty = 0 size (Add card hand) = 1 + size hand

Patterns have two purposes: 1.Distinguish between forms of the input (e.g. Empty and Add) 2.Give names to parts of the input (In the definition of size, card is the first card in the argument, and hand is the rest of the hand.)

slide-22
SLIDE 22

Pattern matching

size :: Hand → Integer size Empty = 0 size (Add kort resten) = 1 + size resten Variables can have arbitrary names

slide-23
SLIDE 23

Construction/destruction

When used in an expression (RHS), Add constructs a hand:

aHand :: Hand aHand = Add c1 (Add c2 Empty)

When used in a pattern (LHS), Add destructs a hand:

size (Add card hand) = …

slide-24
SLIDE 24

Lists

– how they work

slide-25
SLIDE 25

Lists of arbitrary type

  • Can we generalize the Hand type to lists with

elements of arbitrary type?

  • What to put on the place of the ??

data List = Empty | Add ?? List

slide-26
SLIDE 26

Lists of arbitrary type

data List a = Empty | Add a (List a)

A parameterized type Constructors: Empty :: ??? Add :: ???

slide-27
SLIDE 27

Lists of arbitrary type

data List a = Empty | Add a (List a)

A parameterized type Constructors: Empty :: List a Add :: ???

slide-28
SLIDE 28

Lists of arbitrary type

data List a = Empty | Add a (List a)

A parameterized type Constructors: Empty :: List a Add :: a → List a → List a

slide-29
SLIDE 29

Constructing lists

  • List containing the numbers 1, 2 and 3:

myList1 :: List Integer myList1 = Add 1 (Add 2 (Add 3 Empty))

  • List containing the strings “apa”, “hund”:

myList2 :: List String myList2 = Add “apa” (Add “hund” Empty)

slide-30
SLIDE 30

Constructing lists

  • Cannot mix elements of different types:

myList3 = Add True (Add “bil” Empty) Error: Couldn't match expected type ‘Bool’ with actual type ‘[Char]’

slide-31
SLIDE 31

Lists of arbitrary type

data List a = Empty | Add a (List a)

A parameterized type Constructors: Empty :: List a Add :: a → List a → List a

slide-32
SLIDE 32

Built-in lists

The List type is just for demonstration, Haskell has an equivalent built-in list type that should be used instead: List a ≈ [a]

slide-33
SLIDE 33

Built-in lists

data [a] = [] | (:) a [a]

Constructors: [] :: [a] (:) :: a → [a] → [a]

Not a legal definition, but the built-in lists are conceptually defined like this

slide-34
SLIDE 34

Built-in lists

Instead of

Add 1 (Add 2 (Add 3 Empty))

we can use built-in lists and write

(:) 1 ((:) 2 ((:) 3 []))

  • r, equivalently

1 : 2 : 3 : []

  • r, equivalently

[1,2,3]

Special syntax for the built-in lists

slide-35
SLIDE 35

Some list operations

  • From the Data.List module (also in the

Prelude):

reverse :: [a] -> [a]

  • - reverse a list

take :: Int -> [a] -> [a]

  • - (take n) picks the first n elements

(++) :: [a] -> [a] -> [a]

  • - append a list after another

replicate :: Int -> a -> [a]

  • - make a list by replicating an element
slide-36
SLIDE 36

Some list operations

*Main> reverse [1,2,3] [3,2,1] *Main> take 4 [1..10] [1,2,3,4] *Main> [1,2,3] ++ [4,5,6] [1,2,3,4,5,6] *Main> replicate 5 2 [2,2,2,2,2]

slide-37
SLIDE 37

Strings are lists of characters

type String = [Char] Prelude> 'g' : "apa" "gapa" Prelude> "flyg" ++ "plan" "flygplan" Prelude> ['A','p','a'] "Apa"

Type synonym definition

slide-38
SLIDE 38

More on Types

  • Functions can have “general” types:

– polymorphism – reverse :: [a] → [a] – (:) :: a → [a] → [a]

  • Sometimes, these types can be restricted

– Ord a => … for comparisons (<, <=, >, >=, …) – Eq a => … for equality (==, /=) – Num a => … for numeric operations (+, -, *, …)

slide-39
SLIDE 39

Do’s and Don’ts

isBig :: Integer → Bool isBig n | n > 9999 = True | otherwise = False isBig :: Integer → Bool isBig n = n > 9999

guards and boolean results

slide-40
SLIDE 40

Do’s and Don’ts

resultIsSmall :: Integer → Bool resultIsSmall n = isSmall (f n) == True resultIsSmall :: Integer → Bool resultIsSmall n = isSmall (f n)

comparison with a boolean constant

slide-41
SLIDE 41

Do’s and Don’ts

resultIsBig :: Integer → Bool resultIsBig n = isSmall (f n) == False resultIsBig :: Integer → Bool resultIsBig n = not (isSmall (f n))

comparison with a boolean constant

slide-42
SLIDE 42

Do’s and Don’ts

fun1 :: [Integer] → Bool fun1 [] = False fun1 (x:xs) = length (x:xs) == 10 fun1 :: [Integer] → Bool fun1 xs = length xs == 10

repeated code necessary case distinction?

Do not make unnecessary case distinctions

slide-43
SLIDE 43

Do’s and Don’ts

fun2 :: [Integer] → Integer fun2 [x] = calc x fun2 (x:xs) = calc x + fun2 xs fun2 :: [Integer] → Integer fun2 [] = 0 fun2 (x:xs) = calc x + fun2 xs

repeated code right base case ?

Make the base case as simple as possible