Docsity

Programming Languages and Translators - Study Guide | CS 440, Study notes of Programming Languages

Toggle sidebar
bg1
CS 440 — Types January 30, 2009
1 Introduction and Objectives
Even more powerful than the idea of recursion is the idea of abstraction—which is a lot easier to
implement if you can make your own types. Your goal after this lecture is to...
•Be able to describe the record type and the disjoint type.
•Be able to give an example of how each one is used.
•Be able to draw a memory diagram that is the result of allocating a variable of a disjoint
type.
•Be able to use pattern-matching syntax to deconstruct a disjoint type.
•Be able to describe how data-structures in a functional language “recycle” data from previous
versions of the structure when a modification has been made.
2 Record Types
2.1 Defined
Example: Complex Numbers
•Complex numbers have the form a+bi, where i≡√−1
•Addition: (a+bi)+(c+di)=(a+c)+(b+d)i
•Multiplication: (a+bi)×(c+di) = ac −bd + (ad +bc)i
1cadd (a,b) (c,d) =(a +c, b +d)
2cmul (a,b) (c,d) =(a ∗c-b∗d,
3a∗d+b∗c)
We could use tuples to represent complex numbers, like this. (What are the types of these
functions?) Why might this be a bad idea?
Record Type Definitions
data Name =Name {field :: type [,field :: type . . . ] }
Mattox Beckman Page 1 Illinois Institute of Technology
pf3
pf4
pf5

Partial preview of the text

Download Programming Languages and Translators - Study Guide | CS 440 and more Study notes Programming Languages in PDF only on Docsity!

1 Introduction and Objectives

Even more powerful than the idea of recursion is the idea of abstraction—which is a lot easier to implement if you can make your own types. Your goal after this lecture is to...

  • Be able to describe the record type and the disjoint type.
  • Be able to give an example of how each one is used.
  • Be able to draw a memory diagram that is the result of allocating a variable of a disjoint type.
  • Be able to use pattern-matching syntax to deconstruct a disjoint type.
  • Be able to describe how data-structures in a functional language “recycle” data from previous versions of the structure when a modification has been made.

2 Record Types

2.1 Defined

Example: Complex Numbers

  • Complex numbers have the form a + bi, where i ≡
  • Addition: (a + bi) + (c + di) = (a + c) + (b + d)i
  • Multiplication: (a + bi) × (c + di) = ac − bd + (ad + bc)i

1 cadd (a,b) (c,d) = (a + c, b + d) 2 cmul (a,b) (c,d) = (a ∗ c - b ∗ d, 3 a ∗ d + b ∗ c)

We could use tuples to represent complex numbers, like this. (What are the types of these functions?) Why might this be a bad idea?

Record Type Definitions

data Name = Name { field :: type [, field :: type... ] }

1 data Complex = Complex { re :: Float, im :: Float } 2 3 cadd’ x y = Complex { re = re x + re y, 4 im = im x + im y }

  • Fields are accessed with dot notation.
  • Note that field names must be unique.

Activity I

  1. Write a function to multiply complex numbers. Use the type definition we had before.
  2. What would the type definition for a phone book entry look like? Assume we want to store a name, address, and phone number, all as strings.

3 Disjoint Types

3.1 Defined

Simple Type Definitions

data Name = Name [type · · ·] [| Name [type · · ·] · · ·]

  • Note: Constructor names must be capitalized.
  • Constructor names also must be unique.

1 data Contest = Rock | Scissors | Paper 2 data Velocity = MetersPerSecond Float 3 | FeetPerSecond Float

  • A recursive type without a base case is dangerous, but using Haskell, it might even make sense.

Activity Write the code to take a Mylist and return a Haskell list.

Type Constructors and Memory

  • When a type constructor is invoked, it causes memory to be allocated.
    • Writing an integer
    • Writing [] or Nil
    • Using : or Cons
  • Writing down a variable does not cause memory to be allocated.

x = 4 -- allocates 4 n = [] -- allocates empty list n2 = n -- does NOT allocate memory l = x:n -- A cons cell is allocated, but not the 4 or the empty list

What is the memory diagram for this code?

How about this code?

x = 4 n = Nil n2 = n l = Cons x n

Example: length Write the function to take the length of a Mylist.

Now try writing Reverse. Make sure it’s O(n) time.

Parameters Haskell supports parametric polymorphism, like templates in C++ or generics in Java.

1 data Glist a = GCons a (Glist a) 2 | GNil 3 deriving Show

Activity

  • This technique is called functional-style updating.
  • Advantage: multiple versions of the data-structure can exist. This allows multiple versions, and consistent multiprocessing. Used to implement multi-level undo.
  • Disadvantage: it takes more memory.

3.3 A Very Useful Type

A very common type definition

data Maybe a = Just a | Nothing

1 2 getItem key [] = Nothing 3 getItem key ((k,v):xs) = 4 if key == k then Just v 5 else getItem key xs

*Main> getItem 3 [(2,"french hens"), (3,"turtle doves")] Just "turtle doves" *Main> getItem 5 [(2,"french hens"), (3,"turtle doves")] Nothing