Docsity

Lecture Notes on CPSC 326: Heap Data Structures and Algebraic Data Types (Spring 2013), Study notes of Programming Languages

Partial preview of the text

Download Lecture Notes on CPSC 326: Heap Data Structures and Algebraic Data Types (Spring 2013) and more Study notes Programming Languages in PDF only on Docsity!

Today ...

  • Heap data structure (for HW 5)
  • Algebraic data types

HW

  • Homework 4 out
  • Reading 5 out

Note on where with guards vs. let

f x | g1 = e | g2 = e where ...

f x | g1 = let ... in ... | g2 = let ... in ...

The Heap data structure

Often used for

  • Implementing “priority queues” and “heapsort” (an O(n log n) sort)

Based on a binary tree ...

  • each “node” contains a value, plus a left and right child

Min Heap main idea ...

  • each node has a smaller value than its children ... heap constraint
  • Q: what value does the root have? ... the smallest value

2 4 5

10 8 9 This is a heap

3 8 4

7 9 8 This is a not a heap … why?

4 6 7

9 8 This is a not a heap … why?

  • a “complete” binary tree (filled left-to-right)
    • insert:
      1. add node to next open location
      2. “trickle up” as needed to maintain heap constraint

2 4 5 10 8 9

Insert 1

1

2 4 1 10 8 9

Trickle up

5

1 4 2 10 8 9

Trickle up

5

Algebraic data types

We can define new data types in Haskell

  • New data types are defined using the data keyword
  • For example, a simple book “record” of book ids, titles, and authors

data BookInfo = Book Integer String [String] deriving (Show)

The definition

  • BookInfo is a type constructor ... types are always capitalized
  • Book is a value (data) constructor ... also capitalized
  • everything after Book and up to deriving are fields
    • each field here is given as an existing type
  • deriving says BookInfo is a member of the Show typeclass
    • Haskell takes care of the Show implementation here

Once defined, we can use our new type ...

Prelude> :load books

  • Our new type is defined in books.hs

Main*> Book 035 "Neuromancer" ["Gibson"] Book 035 "Neuromancer" ["Gibson"]

  • We use the value constructor (Book) to create a value
  • Here we see Show at work ... the value is printable!

Main> let b1 = Book 035 "Neuromance" ["Gibson"] Main> :type b b1 :: BookInfo

  • Our Book value is of type BookInfo

Main*> :type Book Book :: Integer -> String -> [String] -> BookInfo

  • a value constructor is just another function!
  • that happens to create a value of the corresponding type

Type and value constructors are independent

  • so far we’ve used different names (BookInfo vs. Book)
  • convention is to use the same name for both

data Book = Book Integer String [String] deriving (Show)

data Magazine = Magazine Integer String [String] deriving (Show)

Type Synonyms

The type keyword creates type synonyms

  • a type synomym creates a new name for an existing type

type ID = Integer type Title = String type Authors = [String]

  • can help give “meaning” to fields

data Book = Book ID Title Authors deriving (Show)

data Magazin = Magazine ID Title Authors deriving (Show)

Type synonyms are structural

  • Authors and [String] are the same type
  • whereas Book and Magazine are different types

Q: What type synonym have we already used in Haskell?

  • type String = [Char]

Type synonyms can name complex structures

type BookRecord = (Book, Review, Retailer)

  • a triple of type Book, Review, and Retailer