Created
February 18, 2026 20:53
-
-
Save kmicinski/5c97a9faabc30a602a6a1ef57a345f2c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #lang racket | |
| ;; CIS352 -- Feb 18, 2026 | |
| ;; Consider watching this lecture video on foldr: | |
| ;; https://www.youtube.com/watch?v=WUAI_v110NQ&list=PLXaqTeMx01E_eK1ZEpKvKL5KwSaj7cJW9&index=15 | |
| (define (count-evens? l) | |
| (foldr (lambda (next-elt acc) (if (even? next-elt) (+ 1 acc) acc)) | |
| 0 | |
| l)) | |
| (define f (lambda (next-elt acc) (if (even? next-elt) (+ 1 acc) acc))) | |
| (count-evens? '(1 2 3 4)) | |
| (f 1 (f 2 (f 3 (f 4 0)))) | |
| ;; result is a list | |
| (define (filter f l) | |
| ;; acc is a list | |
| (foldr (lambda (next-elt acc) (if (f l) (cons next-elt acc) acc)) | |
| '() | |
| l)) | |
| #;(define (filter f l) | |
| ;; acc is a list | |
| (foldr (lambda (next-elt acc) (cons next-elt acc)) | |
| '() | |
| l)) | |
| ;; (foldr f z (cons x0 (cons x1 ... (cons xn '())))) | |
| ;; = (cons x0 (cons x1 ... (cons xn '()))) | |
| ;; return the number of elements in l equal? to x | |
| (define (count l x) | |
| (foldr (lambda (next-elt acc) (if (equal? x next-elt) (+ 1 acc) acc)) | |
| 0 | |
| l)) | |
| #;(define (foldr f z l) | |
| (if (empty? l) | |
| z | |
| (f (first l) (foldr f z (rest l))))) | |
| #;(define (foldl f a l) | |
| (if (empty? l) | |
| a | |
| (foldl f (f a (first l)) (rest l)))) | |
| (define (print-and-return x acc) | |
| (displayln x) | |
| x) | |
| (foldr print-and-return 0 '(1 2 3)) | |
| (foldl print-and-return 0 '(1 2 3)) | |
| (foldl cons '() '(1 2 3)) | |
| (foldr cons '() '(1 2 3)) | |
| (define (reverse l acc) | |
| (if (empty? l) | |
| acc | |
| (reverse (rest l) (cons (first l) acc)))) | |
| (define (append l0 l1) | |
| (if (empty? l0) | |
| l1 | |
| (cons (first l0) (append (rest l0) l1)))) | |
| (define (filter-tail f l) | |
| (define (h l acc) | |
| (cond [(empty? l) acc] | |
| [(f (first l)) (h (rest l) (append acc (list (first l))))] | |
| [else (h (rest l) acc)])) | |
| (h l '())) | |
| (define (ignore _) (void)) | |
| ;; return #t iff the lst contains x | |
| (define (member? lst x) | |
| (foldr | |
| (lambda (next-elt acc) ;; acc is a boolean? | |
| (or acc (equal? next-elt acc))) | |
| #f | |
| lst)) | |
| (define tester (lambda (next-elt acc) ;; acc is a boolean? | |
| (or acc (equal? next-elt acc)))) | |
| (member? '(1 2 3) 5) | |
| (foldr tester #f '(1 2 3)) | |
| (foldr tester #f '(1 5 3)) | |
| #t | |
| ;; (add-if f l) takes a function f, which is a predicate (i.e., a classifier, a function | |
| ;; that returns #t / #f) and l is a list of numbers. add-if produces the sum of numbers | |
| ;; that satisfy f. | |
| ;;(add-if even? '(2 4 5 1 3 2)) ;; 8 | |
| (define (add-if f l) | |
| (foldr | |
| (lambda (next-elt acc) | |
| (if (f next-elt) | |
| (+ next-elt acc) | |
| acc)) | |
| 0 | |
| l)) | |
| ;; (foldr g 0 (cons 5 (cons 10 '()))) | |
| ;; = (g 5 (foldr g 0 (cons 10 '()))) ;; (fold-2) | |
| ;; = (g 5 (g 10 (foldr g 0 '()))) ;; (fold-2) | |
| ;; = (g 5 (g 10 0)) ;; (fold-1) | |
| ;; = (g 5 (+ 10 0)) ;; definition of g | |
| ;; = (g 5 10) ;; computing + | |
| ;; = (+ 5 10) ;; definition of g | |
| ;; = 15 ;; computing + | |
| ;; (g x acc) = (+ x acc) | |
| ;; Consider: what would be the execution of this | |
| (foldr (lambda (x acc) (if (equal? x 5) (+ acc 3) acc)) | |
| 10 | |
| '(1 2 3)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment