Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Questions?  Hand back Midterm  Extra Credit Opportunity: CSET Colloquium Talks ◦ May 23 rd 4-5:30pm, PV206 (Priscilla Oppenheimer, Cisco Systems) ◦

Similar presentations


Presentation on theme: " Questions?  Hand back Midterm  Extra Credit Opportunity: CSET Colloquium Talks ◦ May 23 rd 4-5:30pm, PV206 (Priscilla Oppenheimer, Cisco Systems) ◦"— Presentation transcript:

1  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.  Homework #3 (chapter 6) due on Monday, May 14th  Discuss Final Project & Sign-Up  Functional Language vs. Applicative Language  Chapter 5: Names & Scoping

2  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.

3  Questions?  Extra Credit Opportunity: ◦ Project Symposium: May 21 st ◦ Talk: May 23 rd 4-5:30pm, PV206 (Priscilla Oppenheimer, Cisco Systems) ◦ Talk: May 30 th 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++

4  Lab5 deadline has been extended to next Tuesday, May 15 th. ◦ Extra credit for those who have already completed.  Lab6 deadline has been extended to Tuesday, May 22 nd. ◦ Extra credit if done by next Tuesday, May 15 th.  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)

5  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]

6 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)

7 F# Function Parameter(s) Return Values

8 let rec countAll x list = match list with | [] -> 0 | head::tail -> if head=x then 1 + …. else 2 countAll ‘X’ [‘X’;’Y’;’X’]

9 let rec reverse list = [] @ [3;2;1] reverse [1;2;3]

10 let rec replace l ist x pos = :: //use :: to build the list back up [‘-’;’O’;’X’] replace [‘-’;’-’;’X’] 2 ‘O’

11  Not really a general-purpose programming language.  Interpreted not compiled.

12  Local { var i, j; }

13  Instance { 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)

14 When an object invokes a script, the scope of variables in the script is dynamically bound

15  global { var I, j; …. global.field[I, j] = 0; } Global variables are available to all object instances

16 Initialization in C++98 Containers require another container: int vals[]={10, 20, 30}; //init from another container const vector 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?

17 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 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};

18 Uniform Initialization Syntax  You can use it everywhere: Point2 makePoint() { return {0, 0}; } //return expression;calls Point2 ctor void f(const vector & v); f({val1, val2, 10, 20, 30});

19 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.

20 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 larr = {1, 2, 4, 5}; //Error

21 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 args const Point2 p3{5, 10, 20}; //error,too many args vector v {1,a,2,b,3}; //calls vector’s ctor

22 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 cv = {1, 2, 3};

23 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

24 Uniform Initialization Syntax  And “T var = expr” syntax can’t call explicit constructors: class Widget { public: explicit Widget(int); …}; Widget w1(10); //okay, direct init Widget w2{10}; //ditto Widget w3 = 10; //error, because of explicit Widget w4 = {10}; //ditto  Develop the habit of using brace initialization without “=“

25 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 (2.5)}; //Okay in both

26 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); //Okay Widget w2{i}; //error Widget w3(u); //Okay Widget w4{u}; //Okay

27 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 v {99, -8, x-y}; myType w {a[0], a[1], 25, 6};  Available for more than just initialization, e.g. vector 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.

28 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

29 Initializer Lists #include //in std namespace string getName(int ID); Class Widget { public: Widget(initializer_list il) { values.reserve(il.size()); for (auto v:il) values.push_back(getName(v)); } private: vector values; }; Widget w {1, x, 25, 16};

30 Initializer Lists  std::intializer_list parameter may be used with other parameters: class Widget { public: Widget(string& name, double d, initializer_list il); …}; string name(“Buffy”); Widget w {name, 0.5, {5, 10, 15}}; =>Note the nested brace sets.

31 Initializer Lists  They may be templatized:  Only homogeneous initializer lists allow type deduction to succeed: class Widget { public: template Widget(initializer_list il); …}; Widget w1 {-55, 25, 16}; // T = int Widget w2 {-55, 2.5, 16}; //Error

32 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 vs); //#2..}; double d1, d2; Widget w {d1, d2}; //calls #2

33 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 ss); //#2..}; double d1, d2; Widget w {d1, d2}; //tried to call #2 but //failed. Call #1

34 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 ); //#1 Widget(initializer_list ); //#2 Widget(initializer_list ); //#3..}; Widget w2 {1,0f, 2.0, 3.0}; //calls #2, float=>double string s; Widget w3 {s, “Init”, “lists”}; //calls #3 Widget w4 {1, 2.0, 3}; //Error if #2 if not //available

35 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)

36 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])?

37 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 2 3 “ROW Major” Store the first index first

38 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


Download ppt " Questions?  Hand back Midterm  Extra Credit Opportunity: CSET Colloquium Talks ◦ May 23 rd 4-5:30pm, PV206 (Priscilla Oppenheimer, Cisco Systems) ◦"

Similar presentations


Ads by Google