Quiz 3 review and Recursion in AAQZ4
Table of Contents
1. Quiz 3
-
a: (list 7 “apple” 2) b: (list 4 4 4)
what is (length (cons a b))
= 1 + (length b)
= 1 + 3
-
a : (list 9 “hello”) b : (list “a” 44 “b”)
what is (second (cons a b))
= (first (rest (cons a b)))
= (first b)
= “a”
-
’(24 () v)
(cons 24 (cons ’() (cons v ’())))
- (define (whistly [l : (Listof Booleab)]) : String (match l [’() ’()] [(cons f r) … (cons f (whistly r)) …]))
- Match case for langauge
<exp>::= <real num> | {sub <expr> <rea num>}
Abstract Syntax Definitions Given
(define-type Exp (U ListNum Sub))
(struct LitNum ([n : Real]))
(struct Sub ([e : Exp] [n : Real])
(define (parse [c : Sexp]) : Exp (match c [(? real? n) (LitNum n)] [(list 'sub x (? real? n)) (sub (parse x) n)]))
2. Recursion in AAQZ4
{bind [fact = {(n) > {if {<
n 0 {* n {fact {+ n
1}}}}}}]}
fact is not in scope here but what if we pass in fact…
:{bind [fact = {(self n) > {if {<
n 0 {* n {self
self {+ n 1}}}}}}]}
{fact fact 4}}