Practice Questions #1 Note: most of this stuff is very easy, but I want you to make sure you know it all. I suggest that you sit down and write out what you think are the answers, and THEN check your answers against the answers I'll send out later. Try to do these all by hand, but it's okay to check your answers by running theexpressions through a real Scheme system. Be warned that this won't always work, for two reasons: 1. RScheme lets you get away with some things standard Scheme won't, and returns definite values for certain things that standard Scheme doesn't specify the result of. 2. At this writing, there's a bug in RScheme that keeps it from accepting some perfectly legal expressions, especially if or cond expressions with literals as the condition expressions. ----- Part I Fill in the blank. 1. What kind of object does cons return? ___________ Part II True or False (circle T for true, F for false) 2. T F the expression (cons 1 (cons 2 (cons 3 4))) returns a proper list 3. T F 0 (the integer 0) counts as false in Scheme conditionals (e.g., an if or cond) 4. T F 1 (the integer 1) counts as true in Scheme conditionals (e.g., an if or cond) 5. T F A pointer to a list is never eq? to a pointer to a pair. 6. T F A pointer to an integer is never eq? to a pointer to a pair. 8. T F A pointer to a list is always equal? to a pointer to some pair. 9. T F The car fields of the pairs in a list must all point to the same kind of object. 10. T F The cdr fields of the pointers in a proper list must all point to pairs or the empty list. 12. T F All objects in Scheme are actually represented as character strings. 13. T F All values in Scheme are conceptually pointers, but integers are often represented in a way that doesn't actually use pointers. 14. T F A tagged immediate value can be distinguished from a true pointer value using the predicate immediate?. 15. T F You can side-effect (change) a number using set!, and all other references to that number will see the new value for the number, e.g. pointers to 6 will now point to 7 if you evaluate the expression (set! 6 7). 16. T F if is a special form. 17. T F null? is a special form. 18. T F cond and set! are both special forms. 19. T F Variables in Scheme are bound directly to values, not to storage 20. T F set-car! creates a new pair object Part II Consider the following expressions. Under each expression, print the result that Scheme would print if you typed the expression at the Scheme prompt. If the result is undefined in standard Scheme (though maybe not in RScheme), just print #void. If evaluating the expression is an error, print ERROR. Assume that we recover to the normal top level after errors. Print all procedure objects as #, which is not necessarily how RScheme (or any other Scheme you use) prints them. 21. (cons 5 '()) 22. (eq? #f #f) 23. (equal? #f #t) 24. (eq? (cons 1 2) (cons 1 2)) 25. (equal? (cons 1 2) (cons 1 2)) 26. (eq? (cons 1 2) (cons 1 3)) 27. (if (begin 1 2 3) (begin 1 2 3) (begin 1 2 3)) 28. (car (cdr (cons 2 (cons 3 (cons 4 (begin '() '() '())))))) 29. (cons 3 (list 2 1 (list 0))) 30. (begin (begin (begin '1 '2 '3))) 31. (if '0 (begin '1 '2) (begin '3 '4)) 32. (quote 1) 33. '1 34. (quote (1 2 3)) 35. '(1 2 3) 35. (cond (1 2) (3 4)) 36. (cond (1 2 3) (4 5 6) (7 8 9 10)) 37. (cond (#f #t) (#t #f)) Part IV Answer the following questions like the ones in the previous part, but assume that the expressions are evaluated in order. That is, earlier expressions may affect the state of the system, determining what later expressions do. Assume that initially, foo, bar, baz, and quux are undefined and unbound. 38. (define foo 2) 39. (set! foo 3) 40. foo 41. (foo) 42. (set! bar 3) 43. (define bar '()) 44. bar 45. (bar) 46. (define (baz) 3) 47. (baz) 48. baz 49. (set! baz 3) 50. (baz) 51. (define (quux x) (if (< x 0) #t #f)) 52. (quux 3) 53. (begin (quux 3) (quux -2) (quux 5)) 54. (cons (quux -1) '()) 55. (cons (quux -1) (cons (quux 0) (cons (quux 1) '()))) 56. (list foo) 57. (quote foo) 58. (define (fact x) (if (= x 1) 1 (* x (fact (- x 1))))) 59. (fact 4) 60. (fact (+ 1 1)) 62. (fact 0) 63. There is no question 63. Part V 64. Consider the function fact in question #58. How many times does fact get called in evaluating the expression in question #59, i.e., (fact 4). 65. Consider the following function: (define (tot x) (if (null? x) 0 (+ (car x) (tot (cdr x))))) What is the result of (tot '(3 4 5))? 66. Consider the following function: (define (repeat x) (if (pair? x) (cons (car x) (cons (car x) (repeat (cdr x)))) '())) What is the result of (repeat '(#t #f #t #f)) ?