Question 1. [Type Inference] The principal type: (t -> (int -> int)) -> (t -> int) Question 2. [CPS] (define merge (lambda (l m) (if (null? l) m (if (null? m) l (if (<= (car l) (car m)) (cons (car l) (merge (cdr l) m)) (cons (car m) (merge l (cdr m)))))))) ;; CPS: (define merge-cps-main (lambda (l m) (merge-cps l m (lambda (v) v)))) (define merge-cps (lambda (l m k) (if (null? l) (k m) (if (null? m) (k l) (if (<= (car l) (car m)) (merge-cps (cdr l) m (lambda (v) (k (cons (car l) v)))) (merge-cps l (cdr m) (lambda (w) (k (cons (car m) w))))))))) ;; First-order form: ;; Represent (lambda (v) v)) ;; as (), ;; and (lambda (v) (k (cons (car l) v))) ;; as (cons (car l) k) ;; and (lambda (w) (k (cons (car m) w))) ;; as (cons (car m) k) (define merge-fo-main (lambda (l m) (merge-fo l m '()))) (define merge-fo (lambda (l m k) (if (null? l) (apply-cont-fo k m) (if (null? m) (apply-cont-fo k l) (if (<= (car l) (car m)) (merge-fo (cdr l) m (cons (car l) k)) (merge-fo l (cdr m) (cons (car m) k))))))) (define apply-cont-fo (lambda (k x) (if (null? k) x (apply-cont-fo (cdr k) (cons (car k) x))))) ;; Imperative form: ;; Just four global variables: l m k x. (define l '*dummy) (define m '*dummy) (define k '()) (define x '*dummy) (define merge-imp-main (lambda () (merge-imp))) (define merge-imp (lambda () (if (null? l) (begin (set! x m) (apply-cont-imp)) (if (null? m) (begin (set! x l) (apply-cont-imp)) (if (<= (car l) (car m)) (begin (set! k (cons (car l) k)) (set! l (cdr l)) (merge-imp)) (begin (set! k (cons (car m) k)) (set! m (cdr m)) (merge-imp))))))) (define apply-cont-imp (lambda () (if (null? k) x (begin (set! x (cons (car k) x)) (set! k (cdr k)) (apply-cont-imp)))))