cst223 week 6 monday
DESCRIPTION
CST223 Week 6 Monday. Questions? Hand back Midterm Extra Credit Opportunity: CSET Colloquium Talks May 23 rd 4-5:30pm, PV206 (Priscilla Oppenheimer, Cisco Systems) May 30 th 4-5:30pm, PV206 (David Lowe, xtranormal.com) Homework #2 (chapter 5) due on Wednesday in class. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/1.jpg)
Questions? Hand back Midterm Extra Credit Opportunity: CSET Colloquium Talks
◦ May 23rd 4-5:30pm, PV206 (Priscilla Oppenheimer, Cisco Systems)
◦ May 30th 4-5:30pm, PV206 (David Lowe, xtranormal.com) Homework #2 (chapter 5) due on Wednesday in
class. Homework #3 (chapter 6) due on Monday, May 14th Discuss Final Project & Sign-Up Functional Language vs. Applicative Language Chapter 5: Names & Scoping
CST223 Week 6 Monday
![Page 2: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/2.jpg)
Recursion is just one of the ways to apply the function to all elements.
Functional languages like Lisp, Scheme, Haskell use recursion as the fundamental control structure.
A variation of functional languages called Applicative languages don’t use recursion.
“Apply to all” or “Apply to a range” is implied.
C++’s built-in STL algorithms are applicative functions.
Functional ≠ Recursion
![Page 3: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/3.jpg)
Questions? Extra Credit Opportunity:
◦ Project Symposium: May 21st
◦ Talk: May 23rd 4-5:30pm, PV206 (Priscilla Oppenheimer, Cisco Systems)
◦ Talk: May 30th 4-5:30pm, PV206 (David Lowe, xtranormal.com) Final Project Sign-up Schedule changes Lab5 – F# -in-class exercise #6 Lab6 – GameMaker comments Homework #2 (chapter 5) due today Homework #3 (chapter 6) due on Monday in class. Topics:
◦ Overview of Chapter 6: Data Types◦ Smart Pointers for C++
CST223 Week 6 Wednesday
![Page 4: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/4.jpg)
Lab5 deadline has been extended to next Tuesday, May 15th. ◦ Extra credit for those who have already completed.
Lab6 deadline has been extended to Tuesday, May 22nd. ◦ Extra credit if done by next Tuesday, May 15th.
Lab7 will not be assigned until Week 8. Please work on your final project during Week 7 if
you are already done with Lab6. Final Project Presentations start on Wednesday,
May 30, and will continue during dead week (June 4 & June 6)
Schedule Changes
![Page 5: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/5.jpg)
Using this function:let divides m n = if m%n=0 then true else false
Write a F# function to remove all multiples of n from a list:
let rec removeMultiple n list Example: removeMultiple 2 [1;2;3;4;5] => [1;3;5]
In-Class Exercise #6
![Page 6: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/6.jpg)
let divides m n = if m%n=0 then true else false
let rec removeMultiple n list = match list with | [] -> [] | head::tail-> if (divides head n) then (removeMultiple n list.Tail) else head::(removeMultiple n list.Tail)
![Page 7: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/7.jpg)
F# Function
Parameter(s)
Return Values
![Page 8: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/8.jpg)
let rec countAll x list = match list with | [] -> 0 | head::tail -> if head=x then 1 + …. else
2
countAll ‘X’ [‘X’;’Y’;’X’]
![Page 9: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/9.jpg)
let rec reverse list = []
@
[3;2;1]
reverse [1;2;3]
![Page 10: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/10.jpg)
let rec replace l ist x pos =
:: //use :: to build the list back up
[‘-’;’O’;’X’]
replace [‘-’;’-’;’X’] 2 ‘O’
![Page 11: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/11.jpg)
Not really a general-purpose programming language.
Interpreted not compiled.
GameMaker
![Page 12: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/12.jpg)
Local
Scoping of variables{ var i, j;
}
![Page 13: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/13.jpg)
Instance
Scoping of variables{ var i, j;
…. field[I, j] = 0;
}
Variables are dynamically allocated, dynamically typed and dynamically type-checked (it knows when an array subscript is out of range)
![Page 14: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/14.jpg)
When an object invokes a script, the scope of variables in the script is dynamically bound
![Page 15: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/15.jpg)
global
Scoping of variables{ var I, j;
…. global.field[I, j] = 0;
}
Global variables are available to all object instances
![Page 16: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/16.jpg)
Initialization in C++98Containers require another container:int vals[]={10, 20, 30};
//init from another containerconst vector<int> cv(vals, vals+3);
Member and heap arrays are impossible:class Widget {
public: Widget(): data(???){} private: const int data[5]; //init? } const float *pData=new const float[4]; //init?
![Page 17: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/17.jpg)
New Brace Initialization Syntax
const int val1 {5};const int val2 {5};int a[] {1, 2, val1, val1+val2};const Point p1 {10, 20}; const Point2 p2 {10, 20};const vector<int> cv {a[0], 20, val2};class Widget{ public: Widget():data{1, 2, a[3], 4, 5}{} private: const int data[5];};const float * pData = new const float[4] {1.5, val1-val2, 3.5, 4.6};
![Page 18: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/18.jpg)
Uniform Initialization Syntax You can use it everywhere:
Point2 makePoint() { return {0, 0}; } //return expression;calls Point2 ctor
void f(const vector<int>& v);
f({val1, val2, 10, 20, 30});
![Page 19: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/19.jpg)
Uniform Initialization Syntax Semantics differ for aggregates and non-aggregates:
Aggregates (e.g. arrays and structs) Initialize members/elements beginning to end
Non-aggregates: Invoke a constructor.
![Page 20: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/20.jpg)
Brace-Initializing Aggregates Initialize members/elements from beginning to end:
Too many initializers => error Too few initializers => remaining objects are value-initialized: Built-in types initialized to 0. User-defined types with constructors are default-constructed. UDTs without constructors: members are value-initialized.
struct Point1 {int x,y;}; const Point1 p1 = {10}; //same as (10, 0)const Point1 p2 = {1, 2, 3}; //Error
Std::array<long, 3> larr = {1, 2, 4, 5}; //Error
![Page 21: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/21.jpg)
Brace-initializing non-aggregates
Invoke a constructor: class Point2 { public: Point2(int x, int y);};int a,b;
const Point2 p1{a, b}; //same as p1(a, b)const Point2 p2{10}; //error, too few argsconst Point2 p3{5, 10, 20}; //error,too many args
vector<int> v {1,a,2,b,3}; //calls vector’s ctor
![Page 22: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/22.jpg)
Uniform Initialization Syntax Use of “=“ with brace initialization typically
allowed:
const int val1 = {5};const int val2 = {5};int a[] = {1, 2, val1, val2};struct Point1 {…};const Point1 p1 = {10, 20};class Point2 {…};
const Point2 p2 = {10, 20};const vector<int> cv = {1, 2, 3};
![Page 23: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/23.jpg)
Uniform Initiazlization Syntax But not always:
class Widget { public: Widget(): data = {1, 2, 3, 4, 5} {} //error
private: const int data[5]; };
const float *pData = new const float[4] = {1.5, 2.0, 3.0, 4.6}; //error
Point2 makePoint() { return = {0, 0}; } //error
![Page 24: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/24.jpg)
Uniform Initialization Syntax And “T var = expr” syntax can’t call explicit
constructors:
class Widget { public: explicit Widget(int);…};
Widget w1(10); //okay, direct initWidget w2{10}; //dittoWidget w3 = 10; //error, because of explicitWidget w4 = {10}; //ditto
Develop the habit of using brace initialization without “=“
![Page 25: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/25.jpg)
Uniform Initialization Syntax Uniform initialization syntax a feature addition, not
a replacement. Almost all initialization code valid in C++98 remains
valid. Rarely a need to modify existing code. Sole exception: implicit narrowing.
C++98 allows it via brace initialization, C++0x doesn’t:
struct Point {int x, y;};
Point p1 {1.2, 5}; //Okay in C++98, but //error in C++0x
Point p2 {1, static_cast<int>(2.5)}; //Okay in both
![Page 26: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/26.jpg)
Uniform Initialization Syntax Direct constructor calls and brace initialization thus
differ subtly:
class Widget { public: Widget(unsigned u);…};
int i;unsigned u;Widget w1(i); //OkayWidget w2{i}; //errorWidget w3(u); //OkayWidget w4{u}; //Okay
![Page 27: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/27.jpg)
Uniform Initialization Syntax A mechanism to generalize array aggregate initialization:
Available to all user-defined types int x, y; int a[] {x, y, 7, 22, -13, 44}; vector<int> v {99, -8, x-y}; myType w {a[0], a[1], 25, 6}; Available for more than just initialization, e.g. vector<int> v {}; //init v = {1, 2, 3}; //assignment v.assign({1, 2, 3}); //assign v.insert(v.end(), {99, 88, -1});
=> Any function can use an “initializer” list.
![Page 28: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/28.jpg)
Uniform Initialization Syntax Approach startlingly simple:
Brace initialization lists convertible to std::initializer_list objects.
Functions can declare parameters of this type. std::initializer_list stores initializer values
in an array and offer these member functions: Size begin end
![Page 29: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/29.jpg)
Initializer Lists#include <initializer_list> //in std namespace
string getName(int ID);
Class Widget { public: Widget(initializer_list<int> il) { values.reserve(il.size()); for (auto v:il) values.push_back(getName(v)); } private: vector<string> values; };
Widget w {1, x, 25, 16};
![Page 30: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/30.jpg)
Initializer Lists std::intializer_list parameter may be
used with other parameters:
class Widget { public: Widget(string& name, double d, initializer_list<int> il);…};string name(“Buffy”);Widget w {name, 0.5, {5, 10, 15}};
=>Note the nested brace sets.
![Page 31: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/31.jpg)
Initializer Lists They may be templatized: Only homogeneous initializer lists allow type
deduction to succeed:
class Widget{ public:
template<typename T> Widget(initializer_list<T> il); …};
Widget w1 {-55, 25, 16}; // T = int Widget w2 {-55, 2.5, 16}; //Error
![Page 32: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/32.jpg)
Initializer List and Overload Resolution
When resolving constructor calls, initializer_list parameters are preferred for brace-deliminted arguments:
class Widget { public: Widget(double v1, double v2); //#1 Widget(initializer_list<double> vs); //#2 ..};
double d1, d2; Widget w {d1, d2}; //calls #2
![Page 33: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/33.jpg)
Initializer List and Overload Resolution
initializer_list parameters are always preferred over other types
class Widget { public: Widget(double v1, double v2); //#1 Widget(initializer_list<string> ss); //#2 ..};
double d1, d2; Widget w {d1, d2}; //tried to call #2 but //failed. Call #1
![Page 34: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/34.jpg)
Initializer List and Overload Resolution
Given multiple initializer_list candidates, best match is determined as long as it’s not a narrowing conversion:
class Widget { public: Widget(initializer_list<int>); //#1 Widget(initializer_list<double>); //#2 Widget(initializer_list<string>); //#3 ..};
Widget w2 {1,0f, 2.0, 3.0}; //calls #2, float=>double string s;
Widget w3 {s, “Init”, “lists”}; //calls #3Widget w4 {1, 2.0, 3}; //Error if #2 if not
//available
![Page 35: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/35.jpg)
Uniform Initialization Summary
Brace initialization syntax now available everywhere.
Implicit narrowing not allowed. std::intializer_list parameters allow
“initialization” lists to be passed to functions. Not actually limited to initialization (e.g. vector::assign)
![Page 36: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/36.jpg)
int twoD [3][4];
0 1 2 4
5 6 7 8
9 10 11 12
C’s array
Row Major
How to find twoD[i][j] (eg. [2][3])?
![Page 37: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/37.jpg)
int threeD[2][3][4];
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
16 17 18 19
20 21 22 23
0 4 8
1
12 16 20
23
“ROW Major”Store the first index first
![Page 38: CST223 Week 6 Monday](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681615c550346895dd0e707/html5/thumbnails/38.jpg)
int threeD[2][3][4]; How to find threeD[i][j][k] (eg. [1][2][3])?
0 2 4
1 3 5
“Column Major”
Store each slice/plane first