MASSACHVSETTS INSTITVTE OF TECHNOLOGY epartment of Electrical Engineering and Computer Se lence 001--Structure and Interpretation of Computer Programs Spring Semester, 2005 Quiz I- Sample Solutions Closed book - one sheet of notes Throughout this quiz, we have set aside space in which you should write your answers. Please try to put all of your answers in the designated spaces, as we will look only in this spaces when gradin Note that any procedures or code fragments that you write will be judged not only on correct function, but also on clarity and good programming practice Section Numb Tutor's name: PART Value grade grader Total 100
✁✂✄✄✂☎✆✝✄✞✟✟✄ ✠✡✄✟✠✟✝✟✞ ☛☞ ✟✞☎✆✡☛✌☛✍✎ ✏✑✒✓✔✕✖✑✗✕ ✘✙ ✞✚✑✛✕✔✜✛✓✚ ✞✗✢✜✗✑✑✔✜✗✢ ✓✗✣ ☎✘✖✒✤✕✑✔ ✄✛✜✑✗✛✑ ✥✦✧✧★✄✕✔✤✛✕✤✔✑ ✓✗✣ ✠✗✕✑✔✒✔✑✕✓✕✜✘✗ ✘✙ ☎✘✖✒✤✕✑✔ ✩✔✘✢✔✓✖✪ ✄✒✔✜✗✢ ✄✑✖✑✪✕✑✔ ✫ ✬✧✧✭ ✮✯✰✱ ✲ ✳ ✴✵✶✷✸✹ ✴✺✸✯✻✰✺✼✽ ✾✸✺✽✹✿ ❀✺✺❁ ✳ ✺✼✹ ✽❂✹✹✻ ✺❃ ✼✺✻✹✽ ✟❄✔✘✤✢❄✘✤✕ ✕❄✜✪ ❅✤✜❆✫ ❇✑ ❄✓❈✑ ✪✑✕ ✓✪✜✣✑ ✪✒✓✛✑ ✜✗ ❇❄✜✛❄ ❉✘✤ ✪❄✘✤✚✣ ❇✔✜✕✑ ❉✘✤✔ ✓✗✪❇✑✔✪✦ ✩✚✑✓✪✑ ✕✔❉ ✕✘ ✒✤✕ ✓✚✚ ✘✙ ❉✘✤✔ ✓✗✪❇✑✔✪ ✜✗ ✕❄✑ ✣✑✪✜✢✗✓✕✑✣ ✪✒✓✛✑✪✫ ✓✪ ❇✑ ❇✜✚✚ ✚✘✘❊ ✘✗✚❉ ✜✗ ✕❄✜✪ ✪✒✓✛✑✪ ❇❄✑✗ ✢✔✓✣✜✗✢✦ ✡✘✕✑ ✕❄✓✕ ✓✗❉ ✒✔✘✛✑✣✤✔✑✪ ✘✔ ✛✘✣✑ ✙✔✓✢✖✑✗✕✪ ✕❄✓✕ ❉✘✤ ❇✔✜✕✑ ❇✜✚✚ ❋✑ ●✤✣✢✑✣ ✗✘✕ ✘✗✚❉ ✘✗ ✛✘✔✔✑✛✕ ✙✤✗✛✕✜✘✗✫ ❋✤✕ ✓✚✪✘ ✘✗ ✛✚✓✔✜✕❉ ✓✗✣ ✢✘✘✣ ✒✔✘✢✔✓✖✖✜✗✢ ✒✔✓✛✕✜✛✑✦ ✡✂✁✞❍ ✄✑✛✕✜✘✗ ✡✤✖❋✑✔❍ ✟✤✕✘✔ ■✪ ✡✓✖✑❍ ✩✂❏✟ ✝✓✚✤✑ ✍✔✓✣✑ ✍✔✓✣✑✔ ✬✧ ✬ ❑ ▲ ✬✧ ▼ ✬ ✭ ✭ ✥ ✥ ✟✘✕✓✚ ✧✧
6.001, Spring Semester, 2005-Quiz I- Sample Solutions Part 1:(20 points) For each of the following expressions or sequences of expressions, state the value returned as the result of evaluating the final expression in each set, or indicate that the evaluation results in an error. If the result is an error, state in general terms what kind of error(e.g. you might write ng type of arg to procedure"). If the evaluation reti built-in proced write primitive procedure. If the evaluation returns a user-created procedure, write compound procedure If the expression does not result in an error, also state the "type"of the returned expression, using You may assume that evaluation of each sequence takes place in a newly initialized Scheme system Question 1 primitive procedure: number, number -, boolean Q Question 3. amova a )) 2: number
✁✂✂✄☎ ✆✝✞✟✠✡ ✆☛☞☛ ✌✍☛✞ ☎ ✎ ✂✂✏✑✒✓✟✔ ✕ ✖ ✆✗☞✝✘☛ ✆✙✘✓✍✟✙✠✌ ✬ ✚✵✛✻ ✜✢ ✣✤✥ ✷✺✰✼✻✽✦ ☞✘✔ ✑✓✛❄ ✘✙ ✕❄✑ ✙✘✚✚✘❇✜✗✢ ✑✧✒✔✑✪✪✜✘✗✪ ✘✔ ✪✑❅✤✑✗✛✑✪ ✘✙ ✑✧✒✔✑✪✪✜✘✗✪✫ ✪✕✓✕✑ ✕❄✑ ❈✓✚✤✑ ✔✑✕✤✔✗✑✣ ✓✪ ✕❄✑ ✔✑✪✤✚✕ ✘✙ ✑❈✓✚✤✓✕✜✗✢ ✕❄✑ ★✗✓✚ ✑✧✒✔✑✪✪✜✘✗ ✜✗ ✑✓✛❄ ✪✑✕ ✫ ✘✔ ✜✗✣✜✛✓✕✑ ✕❄✓✕ ✕❄✑ ✑❈✓✚✤✓✕✜✘✗ ✔✑✪✤✚✕✪ ✜✗ ✓✗ ✑✔✔✘✔✦ ✠ ✙ ✕❄✑ ✔✑✪✤✚✕ ✜✪ ✓✗ ✑✔✔✘✔ ✫ ✪✕✓✕✑ ✜✗ ✢✑✗✑✔✓✚ ✕✑✔✖✪ ❇❄✓✕ ❊✜✗✣ ✘✙ ✑✔✔✘✔ ✩✑✦✢✦ ❉✘✤ ✖✜✢❄✕ ❇✔✜✕✑ ✪✑✔✔✘✔❍ ❇✔✘✗✢ ✕❉✒✑ ✘✙ ✓✔✢✤✖✑✗✕ ✕✘ ✒✔✘✛✑✣✤✔✑✫✬✦ ✠ ✙ ✕❄✑ ✑❈✓✚✤✓✕✜✘✗ ✔✑✕✤✔✗✪ ✓ ❋✤✜ ✚✕◆✜✗ ✒✔✘✛✑✣✤✔✑✫ ❇✔✜✕✑ ✭✮✯✰✯✱✯✲✳ ✭✮✴✵✳✶✷✮✳✦ ✠ ✙ ✕❄✑ ✑❈✓✚✤✓✕✜✘✗ ✔✑✕✤✔✗✪ ✓ ✤✪✑✔◆✛✔✑✓✕✑✣ ✒✔✘✛✑✣✤✔✑✫ ❇✔✜✕✑ ✵✴✰✭✴✷✸✶ ✭✮✴✵✳✶✷✮✳✦ ✠ ✙ ✕❄✑ ✑✧✒✔✑✪✪✜✘✗ ✣✘✑✪ ✗✘✕ ✔✑✪✤✚✕ ✜✗ ✓✗ ✑✔✔✘✔ ✫ ✓✚✪✘ ✪✕✓✕✑ ✕❄✑ ✪ ✕❉✒✑✫ ✘✙ ✕❄✑ ✔✑✕✤✔✗✑✣ ✑✧✒✔✑✪✪✜✘✗✫ ✤✪✜✗✢ ✕❄✑ ✗✘✕✓✕✜✘✗ ✜✗✕✔✘✣✤✛✑✣ ✜✗ ✚✑✛✕✤✔✑✦ ✎✘✤ ✖✓❉ ✓✪✪✤✖✑ ✕❄✓✕ ✑❈✓✚✤✓✕✜✘✗ ✘✙ ✑✓✛❄ ✪✑❅✤✑✗✛✑ ✕✓❊✑✪ ✒✚✓✛✑ ✜✗ ✓ ✗✑❇✚❉ ✜✗✜✕✜✓✚ ✜❆✑✣ ✄✛❄✑✖✑ ✪❉✪✕✑✖✦ ✮✯✹✽✻✰✺✼ ✜ ✹ ✺ ✷✛✰✶✰✻✰✻✹ ✷✛✺✼✹✿✯✛✹✢ ✼✯✶✽✹✛✾ ✼✯✶✽✹✛ ✿❀ ✽✺✺✸✹✵✼ ✮✯✹✽✻✰✺✼ ✤ ✹ ❁❂❃❄❅❆❃ ❇ ❈❉ ❁❂❃❄❅❆❃ ❊ ❋❉ ❁❇ ●❍ ❁❊ ■ ❍❉❉ ❏✢ ✼✯✶✽✹✛ ✮✯✹✽✻✰✺✼ ❏ ✹ ❁❁❑▲▼◆❂▲ ❁▲ ❊ ◆❉ ❁❊ ◆ ▲❉❉ ❍ ❋ ❖❉ ✤✢ ✼✯✶✽✹✛
6.001, Spring Semester, 2005-Quiz I- Sample Solutions Question 6 ((lambda (a) (lambda (b) (+(.O1a)(.0b)) compound procedure: number number Question P. (define aur s, (define li al-air g) (define (lin air (1e((1im1-a1r2) air lial-air))) (1 2):rist numbert
✁✂✂✄☎ ✆✝✞✟✠✡ ✆☛☞☛ ✌✍☛✞ ☎ ✎ ✂✂✏✑✒✓✟✔ ✕ ✖ ✆✗☞✝✘☛ ✆✙✘✓✍✟✙✠✌ ▲ ✮✯✹✽✻✰✺✼ ✹ ❁❁❑▲▼◆❂▲ ❁ ▲❉ ❁❑▲▼◆❂▲ ❁◆❉ ❁❊ ❁✁✂✄☎ ▲❉ ❁✁✂✄☎ ◆❉❉❉❉ ✆❉ ✼✺✶✷✺✯✼✿ ✷✛✺✼✹✿✯✛✹✢ ✼✯✶✽✹✛ ✿❀ ✼✯✶✽✹✛ ✮✯✹✽✻✰✺✼ ✝ ✹ ❁❂❃❄❅❆❃ ▲✄✞ ✆❉ ❁❂❃❄❅❆❃ ❑✟✠▲❑❋▲✄✞ ✡❉ ❁❂❃❄❅❆❃ ❁☛✄✟✠ ▲✄✞❉ ❁❑❃☎ ❁❁❑✟✠▲❑❋▲✄✞ ❍❉❉ ❁❑❅✁☎ ▲✄✞ ❑✟✠▲❑❋▲✄✞❉❉❉ ❁☛✄✟✠ ●❉ ✣✜ ✤✦✢ ☞✰✽✻ ✌✼✯✶✽✹✛✍
6.001, Spring Semester, 2005-Quiz I- Sample Solutions Consider the following simple database of personnel information. The entire database is represented as a list of entries. Each entry is made using the following constructor (define (make-entry person salary position) (list per son salary position)) The person part of an entry is created using the con define make-person list) Note that values for names and positions are represented using strings, while salaries are represented using numbers. Here is an example database (list (make-entry (make-person smiths johns henrys 30000 presidents (make-entry (make-person sioness sannes maries heathers 60000 (make-entry (make-person smiths sf (make-entry (make-person soes sjanes elizabeths 38000 assistants make-entry (make-person groes maries SvIce-presidents)) ote that the family" name is always the first element of the person abstraction, but there can be arbitrarily many” glven” names. Part 2:(17 points) Question F: Draw a box-and-pointer diagram for the structure corresponding to test, where define test (make-entry (make-per son smiths johns henrys 30000 presidents))
✁✂✂✄☎ ✆✝✞✟✠✡ ✆☛☞☛ ✌✍☛✞ ☎ ✎ ✂✂✏✑✒✓✟✔ ✕ ✖ ✆✗☞✝✘☛ ✆✙✘✓✍✟✙✠✌ ▼ ☎✘✗✪✜✣✑✔ ✕❄✑ ✙✘✚✚✘❇✜✗✢ ✪✜✖✒✚✑ ✣✓✕✓❋✓✪✑ ✘✙ ✒✑✔✪✘✗✗✑✚ ✜✗✙✘✔✖✓✕✜✘✗✦ ✟❄✑ ✑✗✕✜✔✑ ✣✓✕✓❋✓✪✑ ✜✪ ✔✑✒✔✑✪✑✗✕✑✣ ✓✪ ✓ ✸✰✽✻ ✘✙ ✑✗✕✔✜✑✪✦ ✞✓✛❄ ✑✗✕✔❉ ✜✪ ✖✓✣✑ ✤✪✜✗✢ ✕❄✑ ✙✘✚✚✘❇✜✗✢ ✛✘✗✪✕✔✤✛✕✘✔❍ ❁❂❃❄❅❆❃ ❁▼▲❃❋❃❆☎✄✁ ☛❃✄✁✟❆ ✁▲❑▲✄✁ ☛✟✁❅☎❅✟❆❉ ❁❑❅✁☎ ☛❃✄✁✟❆ ✁▲❑▲✄✁ ☛✟✁❅☎❅✟❆❉❉ ✟❄✑ ✭✳✮✂✴✸ ✒✓✔✕ ✘✙ ✓✗ ✑✗✕✔❉ ✜✪ ✛✔✑✓✕✑✣ ✤✪✜✗✢ ✕❄✑ ✛✘✗✪✕✔✤✛✕✘✔❍ ❁❂❃❄❅❆❃ ▼▲❃❋☛❃✄✁✟❆ ❑❅✁☎❉ ✡✘✕✑ ✕❄✓✕ ❈✓✚✤✑✪ ✙✘✔ ✗✓✖✑✪ ✓✗✣ ✒✘✪✜✕✜✘✗✪ ✓✔✑ ✔✑✒✔✑✪✑✗✕✑✣ ✤✪✜✗✢ ✪✕✔✜✗✢✪✫ ❇❄✜ ✚✑ ✪✓✚✓✔✜✑✪ ✓✔✑ ✔✑✒✔✑✪✑✗✕✑✣ ✤✪✜✗✢ ✗✤✖❋✑✔✪✦ ✆✑✔✑ ✜✪ ✓✗ ✑✧✓✖✒✚✑ ✣✓✕✓❋✓✪✑❍ ❁❂❃❄❅❆❃ ✁▲▼☛❑❃❂▲☎▲ ❁❑❅✁☎ ❁▼▲❃❋❃❆☎✄✁ ❁▼▲❃❋☛❃✄✁✟❆ ✄✁▼ ❅☎☎ ✄ ✄✆✟☎❆ ✄ ✄☎❃❆✄✁✄❉ ✡✝✝✝✝ ✄☛✄❃✁❅❂❃❆☎✄❉ ❁▼▲❃❋❃❆☎✄✁ ❁▼▲❃❋☛❃✄✁✟❆ ✄✆✟❆❃✁✄ ✄▲❆❆❃✄ ✄▼▲✄❅❃✄ ✄☎❃▲☎☎❃✄✄❉ ■✝✝✝✝ ✄☎▲✠❃✄✄❉ ❁▼▲❃❋❃❆☎✄✁ ❁▼▲❃❋☛❃✄✁✟❆ ✄✁▼ ❅☎☎ ✄ ✄❄✄❃❂✄❉ ✆✆✝✝✝ ✄☎▲✠❃✄✄❉ ❁▼▲❃❋❃❆☎✄✁ ❁▼▲❃❋☛❃✄✁✟❆ ✄❂✟❃✄ ✄✆▲❆❃✄ ✄❃❑❅✞▲◆❃☎☎ ✄❉ ✡✟✝✝✝ ✄▲✁✁❅✁☎▲❆☎✄❉ ❁▼▲❃❋❃❆☎✄✁ ❁▼▲❃❋☛❃✄✁✟❆ ✄✄✟❃✄ ✄▼▲✄❅❃✄ ✄✆▲❆❃✄❉ ❍✠✝✝✝ ✄✡❅✠❃❋☛✄❃✁❅❂❃❆☎✄❉❉❉ ✡✘✕✑ ✕❄✓✕ ✕❄✑ ✫✙✓✖✜✚❉✫ ✗✓✖✑ ✜✪ ✓✚❇✓❉✪ ✕❄✑ ★✔✪✕ ✑✚✑✖✑✗✕ ✘✙ ✕❄✑ ✒✑✔✪✘✗ ✓❋✪✕✔✓✛✕✜✘✗✫ ❋✤✕ ✕❄✑✔✑ ✛✓✗ ❋✑ ✓✔❋✜✕✔✓✔✜✚❉ ✖✓✗❉ ✫✢✜❈✑✗✫ ✗✓✖✑✪✦ ✚✵✛✻ ✤✢ ✣✜☛ ✷✺✰✼✻✽✦ ✮✯✹✽✻✰✺✼ ☞✢ ✏✔✓❇ ✓ ❋✘✧◆✓✗✣◆✒✘✜✗✕✑✔ ✣✜✓✢✔✓✖ ✙✘✔ ✕❄✑ ✪✕✔✤✛✕✤✔✑ ✛✘✔✔✑✪✒✘✗✣✜✗✢ ✕✘ ✱✳✂✱✫ ❇❄✑✔✑ ❁❂❃❄❅❆❃ ☎❃✁☎ ❁▼▲❃❋❃❆☎✄✁ ❁▼▲❃❋☛❃✄✁✟❆ ✄✁▼ ❅☎☎ ✄ ✄✆✟☎❆ ✄ ✄☎❃❆✄✁✄❉ ✡✝✝✝✝ ✄☛✄❃✁❅❂❃❆☎✄❉❉
6.001, Spring Semester, 2005--Quiz I- Sample Solutions Question 7: Complete the entry abstraction by providing selectors for per son, for salary and for position. For example (person test) henry) solution (define per son car (define salary cadr) (define posit Question 8: Complete the per son abstraction by providing selectors for family-name and given-name s, Value: ("anne""marie""heather") (family-name (make-person "jones""anne ""marie""heather")) Value: Jones Thus the family name is always the first name in the entry, the given names are any names after solution: define family-name car) (define given-names cdr
✁✂✂✄☎ ✆✝✞✟✠✡ ✆☛☞☛ ✌✍☛✞ ☎ ✎ ✂✂✏✑✒✓✟✔ ✕ ✖ ✆✗☞✝✘☛ ✆✙✘✓✍✟✙✠✌ ✭ ✮✯✹✽✻✰✺✼ ☛✢ ☎✘✖✒✚✑✕✑ ✕❄✑ ✳✸✱✮ ✓❋✪✕✔✓✛✕✜✘✗ ❋❉ ✒✔✘❈✜✣✜✗✢ ✪✑✚✑✛✕✘✔✪ ✙✘✔ ✭✳✮✂✴✸✫ ✙✘✔ ✂✁✂✁✮ ✓✗✣ ✙✘✔ ✭✴✂✯✱✯✴✸✦ ☞✘✔ ✑✧✓✖✒✚✑❍ ❁☛❃✄✁✟❆ ☎❃✁☎❉ ✄☎▲❑✆❃ ✝ ❁✄✁▼ ❅☎☎ ✄ ✄✆✟☎❆ ✄ ✄☎❃❆✄✁✄❉ ✽✺✸✯✻✰✺✼✢ ❁❂❃❄❅❆❃ ☛❃✄✁✟❆ ✠▲✄❉ ❁❂❃❄❅❆❃ ✁▲❑▲✄✁ ✠▲❂✄❉ ❁❂❃❄❅❆❃ ☛✟✁❅☎❅✟❆ ✠▲❂❂✄❉ ✮✯✹✽✻✰✺✼ ✞✢ ☎✘✖✒✚✑✕✑ ✕❄✑ ✭✳✮✂✴✸ ✓❋✪✕✔✓✛✕✜✘✗ ❋❉ ✒✔✘❈✜✣✜✗✢ ✪✑✚✑✛✕✘✔✪ ✙✘✔ ✟✁✰✯✂✠✸✁✰✳ ✓✗✣ ✡✯✲✳✸✠✸✁✰✳✂✫ ✑✦✢✦ ❁✞❅✡❃❆❋❆▲▼❃✁ ❁▼▲❃❋☛❃✄✁✟❆ ✄✆✟❆❃✁✄ ✄▲❆❆❃✄ ✄▼▲✄❅❃✄ ✄☎❃▲☎☎❃✄✄❉❉ ✄☎▲❑✆❃ ✝ ❁✄▲❆❆❃✄ ✄▼▲✄❅❃✄ ✄☎❃▲☎☎❃✄✄❉ ❁❄▲▼ ❅❑✁❋❆▲▼❃ ❁▼▲❃❋☛❃✄✁✟❆ ✄✆✟❆❃✁✄ ✄▲❆❆❃✄ ✄▼▲✄❅❃✄ ✄☎❃▲☎☎❃✄✄❉❉ ✄☎▲❑✆❃ ✝ ✄✆✟❆❃✁✄ ✟❄✤✪ ✕❄✑ ✙✓✖✜✚❉ ✗✓✖✑ ✜✪ ✓✚❇✓❉✪ ✕❄✑ ★✔✪✕ ✗✓✖✑ ✜✗ ✕❄✑ ✑✗✕✔❉✫ ✕❄✑ ✢✜❈✑✗ ✗✓✖✑✪ ✓✔✑ ✓✗❉ ✗✓✖✑✪ ✓✙✕✑✔ ✕❄✜✪✦ ✽✺✸✯✻✰✺✼✢ ❁❂❃❄❅❆❃ ❄▲▼ ❅❑✁❋❆▲▼❃ ✠▲✄❉ ❁❂❃❄❅❆❃ ✞❅✡❃❆❋❆▲▼❃✁ ✠❂✄❉