lec14.pdf - Streams Laziness and Memoization CS3100 Fall 2019 Review Previously Modular Programming Namespacing Abstraction Code Reuse Structures

lec14.pdf - Streams Laziness and Memoization CS3100 Fall...

This preview shows page 1 - 5 out of 21 pages.

Streams, Laziness and Memoization CS3100 Fall 2019 Review Previously Modular Programming Namespacing, Abstraction, Code Reuse Structures, Signatures, Functors This lecture Streams: Programming with infinite data structures Laziness: Call-by-need evaluation Recursive values In OCaml, we can define recursive functions. we can also define recursive values In [1]: Out[1]: val ones : int list = [1; <cycle>] ( * Infinite list of ones * ) let rec ones = 1 :: ones
Image of page 1
In [2]: Even though the list is infinite , the data structure uses finite memory. Infinite data structures Infinite data structures are not just an intellectual curiosity. Infinite sequences such as primes and fibonacci numbers. Streams of input read from file or socket. Game trees which may be infinite Every possible move leads to branch in the tree. Imagine game trees where a piece could chase the other around forever. Limitations of cyclic structures Suppose we want to convert the infinite list zero_ones to string, the obvious solutions don't work. Out[2]: val zero_ones : int list = [0; 1; <cycle>] ( * Infinite list of alternating 0s and 1s * ) let rec zero_ones = 0 :: 1 :: zero_ones
Image of page 2
In [3]: List to Streams We can start with the list type type 'a list = Nil | Cons of 'a * 'a list and make a stream type. In [4]: There is no Nil since the streams are infinite. Doesn't quite work Stack overflow during evaluation (looping recursion?). Raised by primitive operation at file "list.ml", line 88, c haracters 20-23 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 Called from file "list.ml", line 88, characters 32-39 C ll d f fil "li t l" li 88 h t 32 39 Out[4]: type 'a stream = Cons of 'a * 'a stream let zero_ones_string = List . map string_of_int zero_ones type 'a stream = Cons of 'a * 'a stream
Image of page 3
In [5]: In [6]: In [7]: Pausing the execution We need a way to pause the execution rather than recursively applying to the rest of the list. Use thunks : unit -> 'a functions. Out[5]: val zero_ones : int stream = Cons (0, Cons (1, <cycle>)) Out[6]: val to_string : int stream -> string stream = <fun> Stack overflow during evaluation (looping recursion?). Raised by primitive operation at file "[6]", line 1, charac ters 55-67 Called from file "[6]", line 1, characters 55-67 Called from file "[6]", line 1, characters 55-67 Called from file "[6]", line 1, characters 55-67 Called from file "[6]", line 1, characters 55-67 Called from file "[6]", line 1, characters 55-67 Called from file "[6]", line 1, characters 55-67 Called from file "[6]", line 1, characters 55-67 Called from file "[6]", line 1, characters 55-67
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 21 pages?

  • Fall '19

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors