1 / 19

CSE305 – Programming Languages Daniel R. Schlegel April 25, 2011

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 ) . JavaScript Syntax. Is this valid?. int fact( int n) {

kiril
Télécharger la présentation

CSE305 – Programming Languages Daniel R. Schlegel April 25, 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE305 – Programming LanguagesDaniel R. SchlegelApril 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 iteratorireverse(array a) { int n = a.length -1; for (inti=0; i<=n; i++) { yield a[n-i]; } } void main { foreach x inireverse(a) { foreach y inireverse (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 iteratorireverse(array a) { int n = a.length -1; for (inti=0; i<=n; i++) { yield a[n-i]; } } void main { foreach x inireverse(a) { foreach y inireverse (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 iteratorireverse(array a) { int n = a.length -1; for (inti=0; i<=n; i++) { yield a[n-i]; } } void main { foreach x inireverse(a) { foreach y inireverse (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? Get the next value of ireverse(a). Set x to it.

  9. We need to simulate this behavior! • void main(){ • List l1, l2; … • l2 := nil; • foreach x in elements(l1) do{ • l2 := cons(x, l2); • } • print l2; • } • iteratorint elements(List l){ • while(l != nil) { • yield l.val; • l := l.next; • } • } • 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; • } • }

  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){ varthk = 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!

More Related