15
Types Types (2) (2)

Types(2). 2 Recursive Problems One or more simple cases of the problem have a straightforward, nonrecusive solution The other cases can be redefined

Embed Size (px)

Citation preview

Page 1: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

TypesTypes (2)(2)

Page 2: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

2

Recursive Problems

One or more simple cases of the problem have a straightforward, nonrecusive solution

The other cases can be redefined in terms of problems that are closer to the simple case

By applying this redefinition process every time the recursive function is called, eventually the problem is reduced entirely to simple csaes, which are relatively easy to solve.

if this is a simple case solve itelse redefine the

problem using recursion

Page 3: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

3

Example

int multiply (int m, int n){

int ans;if (n==1)

ans = m;else

ans = m + multiply ( m , n-1 )return (ans);

}

Page 4: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

4

Tracing multiply Function

m is 6n is 33==1 false multiply (6,2)ans is 6 +Return (ans)

m is 6n is 22==1 false multiply (6,1)ans is 6 +Return (ans)

m is 6n is 11==1 is trueans is 6Return (ans)

multiply (6,3)

18

12

6

Page 5: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

5

Recursive Data Type (1)

Recursive data type uses itself in its declaration

Recursive data types are important to recursive algorithms

Used to represent data whose size and structure is not known in advance

Examples: lists and trees

Page 6: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

6

Recursive Data Type (2)Struct CharList

{ char data; Sturct CharList next;

/* illegal C*/ }; Needs a unlimited storage space (illegal C) In the analogy with recursive functions

Int factorial (int n) { return n * factorial (n-1); } /* illegal C*/

No base case (test to stop the recursion)

Page 7: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

7

Recursive Data Type (3) The solution: using pointers to allow

manual dynamic allocationStruct CharListNode { char data; Sturct CharListNode *next; /* legal C*/

}; Each individual element in a CharListNode

has a fixed size Elements can be linked together to form a

list of arbitrary size

Page 8: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

8

Type Equivalence The assignment statement serves to

replace the current value of a variable with a new value specified as an expression. The variable (or the function) and the expression

must be of identical type. Name Equivalence

Two types are equivalent if they have the same name

Structure Equivalence Two types are equivalent if they have the same

structure

Page 9: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

9

Example: Name equivalent

c and d have the same type a and c haven’t the same type

Structure equivalent a, b, c, d have the same type d and e haven’t the same

type Field names are different

struct complex {

float re, im;

};

struct polar {

float x, y;

};

struct y {

float re, im;

};

struct complex c, d;

Struct y a, b;

struct polar e;

int f[5], g[10];

Page 10: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

10

Subtypes

A subtype is a type that has certain constraints placed on its values or operations.

Page 11: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

11

Ada Examplesubtype one_to_ten is Integer range 1 .. 10;

type Day is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

subtype Weekend is Day range Saturday .. Sunday;

type Salary is delta 0.01 digits 9 range 0.00 .. 9_999_999.99;

subtype Author_Salary is Salary digits 5 range 0.0 .. 999.99;

Page 12: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

12

Java example

An object s of class S can be assigned to object t of class T

t = s Either S and T are the same class Or S is a subclass of T Widening Conversion

Page 13: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

13

Polymorphism and Generics (1)

Polymorphism Comes from Greek Means: having many forms

A function or operation is polymorphic if it can be applied to any one of several related types and achieve the same result

Operators overloading is a kind of polymorphism

An advantage of polymorphism is that it enables code reuse.

Page 14: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

14

Polymorphism and Generics (2)

To enables code reuse Ada introduced the concept of generics or template

A generic function is a template that can be instantiated at a compile time with concrete types and operators Binding of arguments to a type is

deferred from programming time to compile time.

Page 15: Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined

15

Polymorphism and Generics (3)

C++ provides generics or templates The implementation mechanism

Actual parameters are textually substituted for generic parameters

Resulting text is then compiled