20-CS-4003-001 Organization of Programming Languages Fall 2018

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures           Code

Lambda - Stream Networks

(define merge$
  (lambda (S1$ S2$)
    (if (null? S1$)
        (if (null? S2$)
            (if (< (car S1$) (car S2$))
                (cons (car S1$) 
                      (lambda () (merge$ ((cdr S1$)) S2$)))
                (cons (car S2$) 
                      (lambda () (merge$ S1$ ((cdr S2$))))))))))

prompt> (take 10 (merge$ (times$ 3 (inf-list$ 1))
                         (times$ 5 (inf-list$ 1)))))
;Value 34: (3 5 6 9 10 12 15 15 18 20)
 -  The procedure merge$ takes as input two streams of increasing numbers and produces a stream that contains all tokens from the input streams and all those tokens are in increasing order. The force-delay pattern is applied to this example, almost without thinking. Take the merge that applies to ordinary lists and replace (cdr lst) with ((cdr S$)) and place the procedure using this change inside a (lambda () ...). An example is shown to the left.