Download presentation

Presentation is loading. Please wait.

Published byErnest Strode Modified over 2 years ago

1
CSE305 – Programming Languages Daniel R. Schlegel April 25, 2011 “Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration.” (Stan Kelly-Bootle)

2
JavaScript Syntax Is this valid? int fact(int n) { if (n === 0) { return 1; } else return n * fact(n - 1); }

3
JavaScript Syntax Is this valid? int fact(int n) { if (n === 0) { return 1; } else return n * fact(n - 1); } NO! function fact(n) { if (n === 0) { return 1; } else return n * fact(n - 1); }

4
JavaScript Syntax What does this return? function array(f, g) { //f and g are functions return [f(), g()]; }

5
JavaScript Syntax What does this return? What about this? function array(f, g) { //f and g are functions return [f(), g()]; } function array(f, g) { //f and g are functions return [f, g]; }

6
Assignment 5, Part 1 iterator ireverse(array a) { int n = a.length -1; for (int i=0; i<=n; i++) { yield a[n-i]; } void main { foreach x in ireverse(a) { foreach y in ireverse (b) { sum = sum + x*y; } Q: When you call ireverse(a) does it give you all the answers at once?

7
Assignment 5, Part 1 iterator ireverse(array a) { int n = a.length -1; for (int i=0; i<=n; i++) { yield a[n-i]; } void main { foreach x in ireverse(a) { foreach y in ireverse (b) { sum = sum + x*y; } Q: When you call ireverse(a) does it give you all the answers at once? NO! What does this do?

8
Assignment 5, Part 1 iterator ireverse(array a) { int n = a.length -1; for (int i=0; i<=n; i++) { yield a[n-i]; } void main { foreach x in ireverse(a) { foreach y in ireverse (b) { sum = sum + x*y; } Q: When you call ireverse(a) does it give you all the answers at once? NO! What does this do? 1.Get the next value of ireverse(a). 2.Set x to it.

9
We need to simulate this behavior! void main(){ List l1, l2; void thk(int x) { l2 := cons(x, l2); } l2 := nil; elements(l1, thk); print l2; } void elements(List l, void thk(int)){ while (l != nil) do { thk(l.val); l := l.next; } void main(){ List l1, l2; … l2 := nil; foreach x in elements(l1) do{ l2 := cons(x, l2); } print l2; } iterator int elements(List l){ while(l != nil) { yield l.val; l := l.next; }

10
Lazy Evaluation What’s the advantage?

11
Lazy Evaluation What’s the advantage? – Doesn’t find a result until that result is needed! – We can use functions that generate infinite lists. Does JavaScript have it built in?

12
Lazy Evaluation What’s the advantage? – Doesn’t find a result until that result is needed! – We can use functions that generate infinite lists. Does JavaScript have it built in? – No! Can we simulate it? If yes, how?

13
Lazy Evaluation What’s the advantage? – Doesn’t find a result until that result is needed! – We can use functions that generate infinite lists. Does JavaScript have it built in? – No! Can we simulate it? If yes, how? – Yes, using higher order functions.

14
How we usually do things… function f(x) { return [x+1, x*2, x*x]; } f(100); //[101, 200, 10000] Short running tasks Okay, so these are short running tasks… but what if we didn’t need all of this?

15
What about this… function f(x) { return [longtask(x), longertask(x), reallylongtask(x)]; } function g(n) { print(f(100)[n]); //Some value } Is there a problem with this? We never use f(100)[0] or f(100)[2], but they are still evaluated!

16
A lazy version function f(x) { thk1() = longtask(x); thk2() = longertask(x); thk3() = reallylongtask(x); return [thk1, thk2, thk3]; } function g(n) { print(f(100)[n]()); //Some value }

17
So now extend this to infinite lists… numsfrom = function(n){ return cons(n, numsfrom(n+1)); }; What happens when we run this?

18
So now extend this to infinite lists… numsfrom = function(n){ var thk = function(){return numsfrom(n+1);}; return [n,thk]; };

19
Final Notes JIVE Survey Today is the last recitation Good luck on the assignment and final! Also, I’m teaching CSE111 this summer… If you know anyone who needs to take it, let them know!

Similar presentations

OK

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on sources of energy for class 8th results Ppt on obesity prevention in children Ppt on object-oriented programming concepts polymorphism Ppt on electricity crisis in india Ppt on depth first search vs breadth Ppt on genetically modified crops Ppt on image fusion Ppt on role of individual in conservation of natural resources Ppt on spiritual leadership oswald Ppt on weather and climate for grade 5