CS152
Chris Pollett
Nov 3, 2021
var dummy : symbol_table_index; ... dummy := insert_in_symbol_table(bar);whereas, in C we can use casting and do:
foo_index = insert_in_symbol_table(foo); ... (void) insert_in_symbol_table(bar); /*better yet, casting is implied if omitted */
enum suit {CLUBS, DIAMONDS, HEARTS, SPADES} suit_test;In C, the enums are implemented as int's that increase in the order of the enum. So DIAMONDS has value 1.
let a = [1,2,3,4,5]; let two_three = &a[1..2];
type list T = | Nil : list T | Cons : hd:T -> tl:list T -> list TI.e., the type of list T consists of the elements the empty list Nil as well as elements cons(hd, tl) where hd in T and tl is in list T. Lists are common in functional languages like Scheme. The above ML-like declaration could be implemented in Rust as:
enum List<T> { Nil, Cons(T, Box<List<T>>) }
struct __attribute__ ((__packed__)) element { char name[2]; int atomic_number; double atomic_weight; _Bool metallic; } s;Then `s` would be stored as 15 bytes, the first 2 bytes for name, the next 4 for atomic_number, followed by 8 bytes for atomic_weight (IEEE 754 double precision format), followed by 1 byte for metallic.
int a[3][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} , {8, 9, 10, 11} };
type R1 = record a, b : integer end; type R2 = record a : integer; b : integer end; type R3 = record b : integer; a : integer end;
type str = array [1..10] of char; type str = array [0..9] of char;Many language would say these two types are different as they begin at different offset, but Ada and Fortran would say they are the same.
type student = record name, address : string age : integer end; type school = record name, address : string age : integer end; x : student; y : school; ... x := y; //can we make a school a student?
typedef old_type new_type;
type celsius_temp = real; type fahrenheit_temp = real; var c : celsius_temp; f : fahrenheit_temp; ... f := c; (* this should probably be an error *)
subtype mode_t is integer range 0..2**16-1; ... type celsius_temp is new integer; type fahrenheit_temp is new integer;
a := expression // expect a to be of same type as expression a + b // we expect the types of a and b to be // such that adding makes sense foo(arg_1, ..., arg_n) //we expect types of arg_i to be // those of the parameters of foo.
int n = (int) b;