From ecfbf910cdd6bea5e1efc14dbaa59ea7be9b9368 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 1 Mar 2026 14:15:07 -0500 Subject: [PATCH] Finished 12. --- 12-ypp/13.rkt | 1 + 12-ypp/main.rkt | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 12-ypp/13.rkt diff --git a/12-ypp/13.rkt b/12-ypp/13.rkt new file mode 100644 index 0000000..4fec36c --- /dev/null +++ b/12-ypp/13.rkt @@ -0,0 +1 @@ +#lang pl broken diff --git a/12-ypp/main.rkt b/12-ypp/main.rkt index 4fec36c..19a4e67 100644 --- a/12-ypp/main.rkt +++ b/12-ypp/main.rkt @@ -1 +1,74 @@ #lang pl broken + +(define (Z f) + ((λ (x) (f (λ (v) ((x x) v)))) + (λ (x) (f (λ (v) ((x x) v)))))) + +(rewrite (define/rec (f x ...) E) => + (define f + (let ([g (Z (λ (f) + (λ (_) ; Ignore this arg. + (λ (x ...) + (let ([f (f #f)]) ; #f is bogus. + E)))) )]) + (g #f)))) ; #f is bogus. + +; ackermann : Nat Nat -> Nat +(define/rec (ackermann m n) + (cond [(zero? m) (+ n 1)] + [(zero? n) (ackermann (- m 1) 1)] + [else (ackermann (- m 1) (ackermann m (- n 1)))])) + +(test (ackermann 3 3) => 61) + +(rewrite (letfuns ([(f x) E] ...) B) => + (let ([g (Z (λ (funs) + (λ (fname) + (match fname + ['f (λ (x) + (let ([f (funs 'f)] ...) + E))] + ...))))]) + (let ([f (g 'f)] ...) + B))) + +(test (letfuns ([(even? n) (if (= n 0) #t (odd? (- n 1)))] + [(odd? n) (if (= n 0) #f (even? (- n 1)))]) + (even? 123)) => #f) + +(rewrite (letfunss ([(f x ...) E] ...) B) => + (let ([g (Z (λ (funs) + (λ (name) + (match name + ['f (λ (x ...) + (let ([f (funs 'f)] ...) + E))] + ...))))]) + (let ([f (g 'f)] ...) + B))) + +(define balanced? + (letfunss ([(balanced? l) + (and (not (null? l)) + (symbol=? (first l) 'left) + (symbol=? (last l) 'right) + (check l 0))] + [(check l amount) + (cond [(null? l) (= amount 0)] + [(symbol=? (first l) 'left) + (check (rest l) (add1 amount))] + [(symbol=? (first l) 'right) + (and (> amount 0) + (check (rest l) (sub1 amount)))] + [else (check (rest l) amount)])] + [(last l) + (cond [(null? (rest l)) (first l)] + [else (last (rest l))])]) + balanced?)) + +(test (balanced? '()) => #f) +(test (balanced? '(left right)) => #t) +(test (balanced? '(left left right right)) => #t) +(test (balanced? '(left right right)) => #f) +(test (balanced? '(left right right)) => #f) +(test (balanced? '(left fasdkfhaskdjhfkajshdfkajhsdkfjhaskdjfhasdhf right)) => #t)