tailieunhanh - Lecture Programming languages (2/e): Chapter 5b - Tucker, Noonan
A language's types denote the kinds of values that programs can manipulate: simple types, structured types, and more complex types. This chapter presents the following content: Recursive data types, functions as types, type equivalence, subtypes, polymorphism and generics, programmer-defined types. | Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming; they make it digestible. Robin Milner Contents Type Errors Static and Dynamic Typing Basic Types NonBasic Types Recursive Data Types Functions as Types Type Equivalence Subtypes Polymorphism and Generics Programmer-Defined Types Strings Now so fundamental, directly supported. In C, a string is a 1D array with the string value terminated by a NUL character (value = 0). In Java, Perl, Python, a string variable can hold an unbounded number of characters. Libraries of string operations and functions. Structures Analogous to a tuple in mathematics Collection of elements of different types Used first in Cobol, PL/I Absent from Fortran, Algol 60 Common to Pascal-like, C-like languages Omitted from Java as redundant struct employeeType { int id; char name[25]; int age; float salary; char dept; }; struct employeeType employee; . = 45; Unions C: union Pascal: case-variant record Logically: multiple views of same storage Useful in some systems applications type union = record case b : boolean of true : (i : integer); false : (r : real); end; var tagged : union; begin tagged := (b => false, r => ); put(); -- error // simulated union type class Value extends Expression { // Value = int intValue | boolean boolValue Type type; int intValue; boolean boolValue; Value(int i) { intValue = i; type = new Type(); } Value(boolean b) { boolValue = b; type = new Type(); } Recursive Data Type data Value = IntValue Integer | FloatValue Float | BoolValue Bool | CharValue Char deriving (Eq, Ord, Show) data Expression = Var Variable | Lit Value | Binary Op Expression Expression | Unary Op Expression deriving (Eq, Ord, Show) type Variable = String type Op = String type State = [(Variable, Value)] Functions as Types Pascal example: function newton(a, b: real; function f: real): real; . | Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming; they make it digestible. Robin Milner Contents Type Errors Static and Dynamic Typing Basic Types NonBasic Types Recursive Data Types Functions as Types Type Equivalence Subtypes Polymorphism and Generics Programmer-Defined Types Strings Now so fundamental, directly supported. In C, a string is a 1D array with the string value terminated by a NUL character (value = 0). In Java, Perl, Python, a string variable can hold an unbounded number of characters. Libraries of string operations and functions. Structures Analogous to a tuple in mathematics Collection of elements of different types Used first in Cobol, PL/I Absent from Fortran, Algol 60 Common to Pascal-like, C-like languages Omitted from Java as redundant struct employeeType { int id; char name[25]; int age; float salary; char dept; }; struct employeeType employee; . .
đang nạp các trang xem trước