Skip to content

Commit

Permalink
more CPS practie in CPS final
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hewner committed Oct 22, 2024
1 parent 1ed38ea commit c8e1b28
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
Binary file modified ClassMaterials/CPSFinal/26-CPSWrapup_set-bang.pptx
Binary file not shown.
43 changes: 43 additions & 0 deletions ClassMaterials/CPSFinal/super-return-solution.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#lang racket

(require "chez-init.rkt")

(define leftmost-even
(lambda (nlist) ; like an slist but for numbers
(cond [(null? nlist) #f]
[(number? (car nlist))
(if (even? (car nlist))
(car nlist)
(leftmost-even (cdr nlist)))]
[else
(let ((car-result (leftmost-even (car nlist))))
(if car-result
car-result
(leftmost-even (cdr nlist))))])))

(define-datatype continuation continuation?
[init-k]
[step1 (nlist list?) (k continuation?) (return-k continuation?)])

(define apply-k
(lambda (k v)
(cases continuation k
[init-k () v]
[step1 (nlist k return-k)
(leftmost-even (cdr nlist) k return-k)]
)))


(define leftmost-even-cps
(lambda (nlist k return-k) ; like an slist but for numbers
(cond [(null? nlist) (apply-k k #f)]
[(number? (car nlist))
(if (even? (car nlist))
(apply-k return-k (car nlist))
(leftmost-even-cps (cdr nlist) k return-k))]
[else
(leftmost-even-cps (car nlist) (step1 nlist k return-k) return-k)])))

(define leftmost-even-2
(lambda (lst)
(leftmost-even-cps lst (init-k) (init-k))))
43 changes: 43 additions & 0 deletions ClassMaterials/CPSFinal/super-return.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#lang racket

(require "chez-init.rkt")

(define leftmost-even
(lambda (nlist) ; like an slist but for numbers
(cond [(null? nlist) #f]
[(number? (car nlist))
(if (even? (car nlist))
(car nlist)
(leftmost-even (cdr nlist)))]
[else
(let ((car-result (leftmost-even (car nlist))))
(if car-result
car-result
(leftmost-even (cdr nlist))))])))

(define-datatype continuation continuation?
[init-k]
[step1 (nlist list?) (k continuation?)])

(define apply-k
(lambda (k v)
(cases continuation k
[init-k () v]
[step1 (nlist k)
(let ((car-result v))
(if car-result
(apply-k k car-result)
(leftmost-even (cdr nlist) k)))]
)))


(define leftmost-even-cps
(lambda (nlist k) ; like an slist but for numbers
(cond [(null? nlist) (apply-k k #f)]
[(number? (car nlist))
(if (even? (car nlist))
(apply-k k (car nlist))
(leftmost-even-cps (cdr nlist) k))]
[else
(leftmost-even-cps (car nlist) (step1 nlist k))])))

0 comments on commit c8e1b28

Please sign in to comment.