LISP code for Q#2 [total of ½ pt extra credit] For question 2 I pointed at writi
ID: 3864410 • Letter: L
Question
LISP code for Q#2 [total of ½ pt extra credit]
For question 2 I pointed at writing a wrapper function to deal with the “parts” designation. If instead, you handle the “parts” designation for each of the three functions: findItem (a), findSize (b) and findColor (c) in that function (i.e. aside from helper functions such as getItemNum, getSize or getColor you don’t write any additional functions for each) you will receive ½ point extra credit.
2). The interesting thing about LISP is that not only is it typeless, but it easily confuses code with data. For instance, we can quickly create a database of parts with the following list:
(parts ((item 1001) (shoes (color brown) (size 10) (cost 20)))
((item 2011) (skirt (color blue) (size 4) (cost 10)))
((item 2120) (pants (color white) (size 32) (cost 30))) ((item 2121) (pants (color brown) (size 34) (cost 30)))
)
If I want a variable to represent it, I simply stick this before and after:
> (set ‘inventory
> ‘(parts …
> )
i.e. by quoting the parts list, I tell LISP that the word parts is an atom and not a function.
How would I access my inventory? Write the following functions. When writing code, only use these functions: defun, cond, car, cdr, list, cons, append, equal, and null. (DO NOT USE setq or set.)
API (output below)
a) [1 pts] one that returns an entry by item number
> (findItem 1001 inventory)
b) [1 pts] one that returns first entry of a given size
> (findSize 32 inventory)
c) [2 pts] one that returns all items with a given color
> (findColor ‘brown inventory)
Some sample runs to help understand how function should work. Note inventory parts list will change (i.e. your functions need to work with different, longer list) so don’t write function depending on the one given.
> (set ‘inventory ‘(parts ((item 1001) (shoes (color brown) (size 10)
(cost 20))) ((item 2011) (skirt (color blue) (size 4) (cost 10))) ((item 2120) (pants (color white) (size 32) (cost 30))) ((item 2121)
(pants (color brown) (size 34) (cost 30)))))
>
> (findItem 1001 inventory)
> ((item 1001) (shoes (color brown) (size 10) (cost 20)))
>
> (findSize 32 inventory)
> ((item 2120) (pants (color white) (size 32) (cost 30)))
>
> (findColor ‘brown inventory)
> (((item 1001) (shoes (color brown) (size 10) (cost 20)))((item 2121)
(pants (color brown) (size 34) (cost 30))))
>
API (output below)
a) [1 pts] one that returns an entry by item number
> (findItem 1001 inventory)
b) [1 pts] one that returns first entry of a given size
> (findSize 32 inventory)
c) [2 pts] one that returns all items with a given color
> (findColor ‘brown inventory)
Explanation / Answer
and transform it into this,
After these transformations are done, the compiler can then go on to simply produce object code (simply?), using all the various other techniques for doing so. While other compilers do common subexpression elimination (and RABBIT didn't), they do it using other mechanisms, typically by analyzing expression parse trees directly. This transformation is a bit more general and can operate on anything that doesn't have side effects, whether a traditional numeric expression or not. Steele goes on to say,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.