CS 228 lecture -*- Outline -*- * translation from Scheme to C++ (or fundamental C++) (H&R Appendix A) ** plan go over how to translate Scheme to C++ translations using example problems for those who don't know Scheme, listen to what stuff means, the translations still may help some ** caveats writing a program is more than just vocabulary ("the wine is good but the meat is rotten" joke) pragmatics: how to use C++ in typical (idomatic) fashion impreative style iteration more often used than recursion, and iteration is expressed with "while" not tail-recurson ** overview ----------------------------- SCHEME TO C++ OVERVIEW Standard parts of a language: - primitives English: sounds, words Scheme: boolean, number, symbol, string, list, vector C++: int, long, float, double, char... struct, union, array - means of combination English: grammar, punctuation Scheme: if, cond, begin, recursion C++: if, switch, { }, while, for, ... - means of abstraction English: naming Scheme: lambda C++: declaration, function, class, ... --------------------- when learning a language, important to see how it does these things --------------------- MAIN NEW IDEAS IN C++ Type distinctions: Scheme: number C++: int, short int, long int unsigned int, unsigned short int, ... float, double, long double Scheme: char C++: char, signed char, unsigned char Type declarations: Scheme: (define square (lambda (x) (* x x))) C++: int square(int x) { return x * x; } Finiteness (square doesn't always work) ----------------------------- *** type distinctions no boolean type yet, although one is coming... **** scalar types (HR A.6) ------------------------------ SCALAR TYPES IN C++ Enumeration types: enum FlagColor {red, white, blue}; enum Answer {yes, no, maybe}; -------------------- not really a translation of symbols, although sometimes can be used for them strings also translate symbols -------------------- Integral types: NAME TYPICAL SIZE LITERALS char 1 byte 'c' '\t' '\n' short int 2 bytes int 2 or 4 228 0342 0x72 long int 4 or 8 -228L ----------------- (unlike C, 'c' is not an integer) use char only for character data use int unless you know better the above are only typical (ref Ellis & Stroustrup) char might or might not be signed, if you care, use signed char or ... ----------------- Unsigned Versions: unsigned int 2 or 4 228U unsigned long 4 or 8 228UL Floating point types: float 4 bytes 2.28e2F double 8 bytes 228. .228e3 long double 12 or 16 -2280e-1L ------------------------------ use double unless you know better, it's favored in the syntax Nothing like rational is primitive all the primitives are supported directly by machines (although the floating point types might not be on impoverished micros) Idea of a machine-oriented language: language reflects machine language lets you control machine precisely like a stick shift, microwave with fine scale **** structured types (HR A.7) -------------------------- STRUCTURED TYPES IN C++ Product types: Strings "a char string? \'yes\'\n" Arrays int myArray[5]; float myFA[228]; Records struct person { int age; char name[50]; }; ---------------------- draw pictures of these arrays are 0-indexed just like vectors in Scheme strings look the same as in Scheme, but have different semantics... records are like Scheme association lists ---------------------- Sum (coproduct) types: Unions union float_or_int { int i; float f; }; ---------------------- note the symmetry: products = same time, different space coproducts = different time, same space this kind of union is dangereous, we'll discuss safe ways of using it later. We'll study these in more detail later. For now, let's look at using the scalar types with means of combination, expressions...