Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; (define f-imperative (y) (x) ; x is a local variable
- ; (begin
- ; (set x e)
- ; (while (p x y)
- ; (set x (g x y)))
- ; (h x y)))
- ; Problem 9
- ; 9b
- (define maxl* (l)
- (foldl max (car l) l))
- (check-expect (maxl* '(5 7 1 3 9 2)) 9)
- ; 9c
- (define gcd* (l)
- (foldl gcd 0 l))
- (gcd* '(-5 -10 -20))
- (check-expect (gcd* '(10 15 20)) 5)
- ; 9d
- (define lcm* (l)
- (foldl lcm (car l) l))
- (check-expect (lcm* '(2 5 8 10)) 40)
- ; 9e
- (define sum (l)
- (foldl + 0 l))
- (check-expect (sum '(1 2 3 4 5)) 15)
- ; 9f
- (define product (l)
- (foldl * 1 l))
- (check-expect (product '(1 2 3 4)) 24)
- ; 9g
- (define append (l1 l2)
- (foldr cons l2 l1))
- (check-expect (append '(1 2 3) '(4 5 6)) '(1 2 3 4 5 6))
- ; 9i
- (define reverse (l)
- (foldl cons '() l))
- (check-expect (reverse '(1 2 3 4 5)) '(5 4 3 2 1))
- ; Problem 10
- (define length (l)
- (let ((combine (lambda (x a) (+ 1 a))))
- (foldr combine 0 l)))
- (check-expect (length '(1 2 3 4 5)) 5)
- (define even? (n) (= 0 (mod n 2)))
- (define map (f xs)
- (let ((combine (lambda (y ys) (cons (f y) ys))))
- (foldr combine '() xs)))
- (check-expect (map ((curry +) 3) '(1 2 3 4 5 6)) '(4 5 6 7 8 9))
- (define filter (f xs)
- (let
- ((combine (lambda (y ys) (if (f y) (cons y ys) ys))))
- (foldr combine '() xs)))
- (check-expect (filter even? '(1 2 3 4)) '(2 4))
- (define exists? (p? xs)
- (let ((find (lambda (y a) (if (p? y) (+ 1 a) a))))
- (if (< 0 (foldr find 0 xs))
- #t
- #f)))
- (check-expect (exists? even? '(1 3 5 6)) #t)
- (check-expect (exists? even? '(1 3 5)) #f)
- (define all? (p? xs)
- (let ((find (lambda (y a) (if (p? y) a (+ 1 a)))))
- (if (< 0 (foldr find 0 xs))
- #f
- #t)))
- (check-expect (all? even? '(2 4 6 8)) #t)
- (check-expect (all? even? '(1 2 4)) #f)
- ; Problem 15
- ; Problem S
- (define quicksort (c l1)
- (if (null? l1)
- '()
- (let ((pivot (car l1)))
- (foldr cons (foldr cons (quicksort c (filter (lambda (x) (c x pivot)) l1))
- (filter (lambda (x) (= x pivot)) l1))
- (quicksort c (filter (lambda (x) (c pivot x)) l1))))))
- (quicksort > '(2 4 3 1 5))
- (quicksort < '(2 4 3 1 5))
- ; Problem A
- (define f-functional (y)
- (let* ((x e))
- (if (o not p)
- (h x y)
- (let* ((x (g x y)))
- (f-functional y)))))
- ; Problem 15
- (val emptyset (lambda (x) #f))
- (define member? (x s) (s x))
- (define add-element (x s)
- (lambda (n)
- (if (s x)
- #t
- (if (equal? n x)
- #t
- #f))))
- (define union (s1 s2)
- (lambda (x)
- (if (s1 x)
- #t
- (if (s2 x)
- #t
- #f))))
- (define inter (s1 s2)
- (lambda (x)
- (if (s1 x)
- (if (s2 x)
- #t
- #f)
- #f)))
- (define diff (s1 s2)
- (lambda (x)
- (if (s1 x)
- (if (s2 x)
- #f
- #t)
- #f)))
- (check-expect ((inter atom? number?) 1) #t)
- (check-expect ((inter atom? number?) #f) #f)
- (check-expect ((union atom? number?) #f) #t)
- (check-expect ((union atom? number?) '(1 2 3)) #f)
- (check-expect ((diff atom? number?) #f) #t)
- (check-expect ((diff atom? number?) 1) #f)
- (val mk-set-ops (lambda (eqfun)
- (list6
- (lambda (x) #f)
- (lambda (x s) (s x))
- (lambda (n)
- (if (s x)
- #t
- (if (equal? n x)
- #t
- #f)))
- (lambda (x)
- (if (s1 x)
- #t
- (if (s2 x)
- #t
- #f)))
- (lambda (x)
- (if (s1 x)
- (if (s2 x)
- #t
- #f)
- #f))
- (lambda (x)
- (if (s1 x)
- (if (s2 x)
- #f
- #t)
- #f)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement