Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Racket streams memoize their elements?

Tags:

stream

racket

Does Racket use memoization when computing large amounts of numbers from an infinite stream? So, for example, if I printed out (aka, computed and displayed) the first 400 numbers on the infinite stream of integers: (1 2 3 ... 399 400) And right after I asked to print the first 500 numbers on this infinite stream. Would this second set of computations use memoization? So the first 400 numbers would not be computed again?

Or does this functionality need to be coded by the user/obtained from libraries?

like image 277
farid99 Avatar asked Dec 02 '22 18:12

farid99


2 Answers

The built-in racket/stream library uses lazy evaluation and memoization to draw elements from a stream:

(require racket/stream)

(define (print-and-return x)
  (displayln "drawing element...")
  x)

(define (in-range-stream n m)
  (if (= n m)
      empty-stream
      (stream-cons (print-and-return n) (in-range-stream (add1 n) m))))

(define s (in-range-stream 5 10))

(stream-first s)
(stream-first s)
(stream-first (stream-rest s))

The expressions passed to stream-cons are not evaluated until requested either with stream-first or stream-rest. Once evaluated, they are memoized. Notice that despite the four stream operations performed on s, only two `"drawing element..." messages are displayed.

like image 161
Jack Avatar answered Mar 27 '23 04:03

Jack


You can use the memoize package.

GitHub source: https://github.com/jbclements/memoize/tree/master

raco pkg install memoize

Using it is as simple as replacing define with define/memo. To quote its example:

(define (fib n)                                     
  (if (<= n 1) 1 (+ (fib (- n 1)) (fib (- n 2)))))  

> (time (fib 35))                                   
cpu time: 513 real time: 522 gc time: 0             
14930352                                            

> (define/memo (fib n)                              
    (if (<= n 1) 1 (+ (fib (- n 1)) (fib (- n 2)))))

> (time (fib 35))                                   
cpu time: 0 real time: 0 gc time: 0                 
14930352      

Also, it is generally quite easy to implement memoization yourself using a Racket hash-table.

like image 43
Greg Hendershott Avatar answered Mar 27 '23 04:03

Greg Hendershott