Finished 12.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
#lang pl broken
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user