-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Hewner
committed
Oct 22, 2024
1 parent
1ed38ea
commit c8e1b28
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))]))) | ||
|