Principles ¡of ¡Programming ¡Languages ¡
h"p://www.di.unipi.it/~andrea/Dida2ca/PLP-‑14/ ¡
- Prof. ¡Andrea ¡Corradini ¡
Department ¡of ¡Computer ¡Science, ¡Pisa ¡
- Composite ¡data ¡types ¡(cont’d) ¡
¡
Lesson 24
1 ¡
Principles of Programming Languages - - PowerPoint PPT Presentation
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/Dida2ca/PLP-14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 24 Composite data
1 ¡
2 ¡
3 ¡
image of u is c, image of v is b
4 ¡
so we can select the ith component
5 ¡
lower bound upper bound
6 ¡
7 ¡
§ Function:
¡void print_vector (float v[], int n) {
// Print the array v[0], …, v[n-1] in the form “[… …]”. int i; printf("[%f", v[0]); for (i = 1; i < n; i++) printf(" %f", v[i]); printf("]"); } … print_vector(v1, 4); print_vector(v2, 10);
3-‑8 ¡
float v1[] = {2.0, 3.0, 5.0, 7.0}; float v2[10]; index range is {0, …, 3} index range is {0, …, 9} A C array doesn’t know its own length!
3-‑9 ¡
type Vector is array (Integer range <>) of Float; v1: Vector(1 .. 4) := (1.0, 0.5, 5.0, 3.5); v2: Vector(0 .. m) := (0 .. m => 0.0);
procedure print_vector (v: in Vector) is
begin put('['); put(v(v'first)); for i in v'first + 1 .. v'last loop put(' '); put(v(i)); end loop; put(']'); end; … print_vector(v1); print_vector(v2);
3-‑10 ¡
float[] v1 = {1.0, 0.5, 5.0, 3.5}; float[] v2 = {0.0, 0.0, 0.0}; … v1 = v2; index range is {0, …, 3} v1’s index range is now {0, …, 2} index range is {0, …, 2}
§ Method:
¡static void printVector (float[] v) { // Print the array v in the form “[… … …]”. System.out.print("[" + v[0]); for (int i = 1; i < v.length; i++) System.out.print(" " + v[i]); System.out.print("]"); } … printVector(v1); printVector(v2);
Enhanced ¡for: ¡ for (float f : v) System.out.print(" " + f) ¡
11 ¡
procedure foo (size : integer) is M : array (1..size, 1..size) of real; ... begin ... end foo; // C99: void foo(int size) { double M[size][size]; ... } M sp
Temporaries Pointer to M Dope vector Bookkeeping Return address Arguments and returns
fp
Local variables Variable-size part of the frame Fixed-size part
12 ¡
13 ¡
14 ¡
15 ¡
16 ¡
17 ¡
data Number = Exact Int | Inexact Float Each Number value consists of a tag, together with either an Integer variant (if the tag is Exact) or a Float variant (if the tag is Inexact).
18 ¡
¡pi = Inexact 3.1416 rounded :: Number -> Integer rounded num = case num of Exact i -> i Inexact r -> round r projection (by pattern matching) projection (by pattern matching)
19 ¡
Fortran ¡I ¡-‑-‑ ¡equivalence ¡statement ¡ integer i real r logical b equivalence (i, r, b) C ¡-‑-‑ ¡union ¡ union { int i; double d; _Bool b; };
20 ¡
type Form is (pointy, circular, rectangular); type Figure (f: Form := pointy) is record x, y: Float; case f is when pointy => null; when circular => r: Float; when rectangular => w, h: Float; end case; end record;
tag
discriminated-record construction tag test projection projection
21 ¡
22 ¡
inherits x and y from Point ¡ inherits x and y from Point ¡
23 ¡
area() method
area() method
24 ¡
it can refer to a Point, Circle, or Rectangle object calls the appropriate area() method
25 ¡
26 ¡
3-‑27 ¡
type Date is record y: Year_Number; m: Month; d: Day_Number; end record; dateA: Date := (2004, jan, 1); dateB: Date;
dateB := dateA; dateB.y := 2005; jan 1 2004 dateA ¡ ? ? ? dateB ¡ jan 1 2004 dateA ¡ jan 1 2004 dateB ¡ jan 1 2004 dateA ¡ jan 1 2005 dateB ¡
3-‑28 ¡
dateS = dateR; dateR.y = 2005; 1 1 2004 dateR ¡ 12 25 2004 dateS ¡ 1 1 2004 dateR ¡ dateS ¡ 1 1 2005 dateR ¡ dateS ¡
3-‑29 ¡
3-‑30 ¡
3-‑31 ¡
– efficient ¡(someEmes ¡intuiEve) ¡access ¡to ¡elaborated ¡objects ¡(as ¡in ¡C) ¡ – dynamic ¡creaEon ¡of ¡linked ¡data ¡structures, ¡in ¡conjuncEon ¡with ¡a ¡heap ¡ storage ¡manager ¡
3-‑32 ¡
3-‑33 ¡
3-‑34 ¡
deallocates that heap variable (dateP and dateQ are now dangling pointers)
struct Date {int y, m, d;}; struct Date* dateP, dateQ; dateP = (struct Date*)malloc(sizeof (struct Date)); dateP->y = 2004; dateP->m = 1; dateP->d = 1; dateQ = dateP; free(dateQ); printf("%d", dateP->y); dateP->y = 2005;
allocates a new heap variable makes dateQ point to the same heap variable as dateP ¡ fails fails
35 ¡
36 ¡
int **a == int *a[]
– Specifically, ¡a ¡declaraEon ¡allocates ¡an ¡array ¡if ¡it ¡specifies ¡a ¡size ¡for ¡the ¡ first ¡dimension, ¡otherwise ¡it ¡allocates ¡a ¡pointer ¡
int **a, int *a[] pointer ¡to ¡pointer ¡to int int *a[n], n-‑element ¡array ¡of ¡row ¡pointers int a[n][m], 2-‑d ¡array ¡
37 ¡
int *a[n], n-element array of pointers to integer int (*a)[n], pointer to n-element array of integers ¡
bad int (*a)[] bad
38 ¡
39 ¡
40 ¡
data IntList = Nil | Cons Int IntList recursive § Some IntList constructions:
¡Cons ¡2 ¡(Cons ¡3 ¡(Cons ¡5 ¡(Cons ¡7 ¡Nil))) ¡ § Actually, Haskell has built-in list types:
§ Some list constructions:
§ Built-in operator for cons
41 ¡
type IntNode;
type IntList is access IntNode; type IntNode is record head: Integer; tail: IntList; end record; mutually recursive mutually recursive
§ An IntList construction:
¡new ¡IntNode'(2, ¡ ¡ ¡new ¡IntNode'(3, ¡ ¡ ¡ ¡ ¡new ¡IntNode'(5, ¡ ¡ ¡ ¡ ¡ ¡ ¡new ¡IntNode'(7, ¡null))) ¡
42 ¡
class List<E> { public E head; public List<E> tail; public List<E> (E el, List<E> t) { head = h; tail = t; } } recursive
§ A list construction:
List<Integer> ¡list ¡= ¡ ¡new ¡List<Integer>(2, ¡ ¡ ¡new ¡List<Integer>(3, ¡ ¡ ¡ ¡ ¡new ¡List<integer>(5, ¡null)))); ¡
43 ¡