20-CS-4003-001 Organization of Programming Languages Fall 2017
Lambda

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures           Code

Lambda - Connect Streams

(define times$
  (lambda (m S$)
    (if (null? S$)
        '()
        (cons (* m (car S$))
              (lambda () (times$ m ((cdr S$))))))))
 -  The procedure times$ produces a stream whose tokens are m times the tokens of the integer input stream S$. The term ((cdr S$)) produces a pair containing the next token of S$ and a procedure to compute all the remaining tokens after the next one. But without the (lambda () ...), (times$ m ((cdr S$))) would then eagerly compute all the tokens of the stream and reach maximum recursion depth in the case of an infinite stream. Thus, the next token is "forced" by the ((cdr S$)) then delayed by the (lambda () ..,).
   prompt> (take 10 (times$ 3 (inf-list$ 1)))
   ;Value 33: (3 6 9 12 15 18 21 24 27 30)