1. what does a macro do? 2. when should you use a macro? 3. review solution to policy checker macro from homework 5 ------------------------------------------------------------------------ 4. (this was a question on the C03 final exam) We have been using cond all semester to write conditionals. Most languages include an if-statement for conditionals with only two cases. The expression (if test then-stmt else-stmt) should run the test, performing the then-stmt if the test returns true, otherwise performing the else-stmt. In this question, we want to implement such a construct. 4-1. Should if be implemented as a function, as a macro, or as either? Justify your answer. 4-2. Implement if. Your answer should be consistent with your answer to 4-1. 4-3. Assume you had augmented your implementation of if with a check that the test expression evaluates to a boolean before you execute the then or else part (assume that Scheme cond doesn't perform this check for you). Assume you typed the following code into the definitions window: (define (myfunc x) (if 3 x (+ x 5))) Would the error (that the test 3 isn't a boolean) get detected when you hit execute, when you enter a call like (myfunc 7) from the prompt, neither, or both? (Answer this based on how you implemented if in 4-2. ------------------------------------------------------------------------ 5. Write a travel-db macro to convert the trip-planner data into the given define-structs (define-struct flight (from to lasts times)) (define-struct city (name activities)) (define-struct travel-database (cities flights)) (define trip-planner (travel-db (cities (boston (activities museums dining)) (austin (activities music outdoors)) (new-york (activities museums music dining))) (flights (boston new-york (duration 50) (times 900 1100 1300 1500 1700)) (austin boston (duration 200) (times 1100 1430))))) ------------------------------------------------------------------------ 6. Write the following function over travel databases ;; city-search : symbol symbol travel-database -> list[symbol] ;; takes a starting city, an activity and a travel database and returns ;; a list of city names with the activity and direct flights from the ;; starting city ------------------------------------------------------------------------ 7. Write form and line macros to convert the given tax form format into the following define-structs. (define-struct query-line (number question)) (define-struct compute-line (number func line-nums)) (define-struct form (title lines)) (define ez-tax-form (form ez-form (line 1 "Enter income") (line 2 "Enter interest") (line 3 "Total income" compute (+ 1 2)) (line 4 "Enter deductions") (line 5 "Gross income" compute (- 3 4)))) ------------------------------------------------------------------------ 8. Write a tax form interpreter for the above form language using the following helper functions ;; store-line-value : number number -> void ;; stores value (second arg) for line number (first arg) in a database ;; lookup-line-value : number -> number or false ;; takes a line number and retrieves value stored for that number or false ;; if nothing stored ;; apply : (arg1 ... argn -> ans) list[arg] -> ans ;; sends list of arguments to a function. For example ;; (apply + (list 1 2 3)) returns 6