Recall function quote we learned in class. This function produces a logical representation of the code given as parameter. The serialized code that results from quote is known as a datum, or a quoted term. In the following exercises, the quoted term shall not include boolean expressions and conditionals. A quoted expression will include numbers, define, lambda, and function application For the sake of simplicity, there is no need to recursively check the syntactic validity (eg, you do not need to check the if the body of a lambda is syntactically valid). For instance, given a lambda are the parameters symbols? Does the body of a lambda has expected number datums as we discussed in class? You do not need to check the semantic validity of the datum (eg, check if a variable is defined).
(a) Function lambda? takes a datum and returns a boolean whether or not the quoted term is a lambda.
(check-true (lambda? (quote (lambda (x) x))))
(check-false (lambda? (quote 3)))
You can check if a datum is a list of symbols with a combination of functions symbol?² and andmap:³
(check-true (andmap symbol? (quote (x y z)))) (check-false (andmap symbol? (quote (x 3 z))))
(b) Function lambda-params takes a quoted lambda and returns the list of parameters (symbols) of the given function declaration.
(check-equal? (list 'x) (lambda-params (quote (lambda (x) y))))
(c) Function lambda-body takes a quoted lambda and returns a list of terms of the given lambda. (check-equal? (list 'y) (lambda-body (quote (lambda (x) y))))
(d) Function apply? takes a datum and returns a boolean whether or not the quoted term is a function application
(check-false (apply? (quote (lambda (x) x))))
(check-true (apply? (quote (x y))))
(e) Function apply-func takes a quoted function application expression and returns the function being called
(check-equal? 'x (apply-func (quote (x y))))
(f) Function apply-args takes a quoted function application expression and should return the arguments (expressions) of the function being called
(check-equal? (list 'y) (apply-args (quote (x y))))
(g) Function define? takes a datum and returns a boolean whether or not the quoted term is a define
(check-true (define? (quote (define x 3))))
(h) Function define-basic? takes a datum and returns a boolean whether or not the quoted term is a basic definition, according the specification we learned in class. (check-true (define-basic? (quote (define x 3))))
(i) Function define-func? takes a datum and returns a boolean whether or not the quoted term is a function definition, according to the specification we learned in class.
(check-true (define-func? (quote (define (x) 3))))