Question: Write and fully demonstrate a Scheme function that implements a Tree Search And Replace operation. Your function should be named tsar, and have this interface:



Write and fully demonstrate a Scheme function that implements a Tree Search And Replace operation. Your function should be named tsar, and have this interface: (tsar subj srch repl) A "tree" is simply a Scheme list (i.e., a symbolic expression, or s-expr). The function returns a copy of the list subj, with every sublist equal to srch replaced by a copy of repl. The function returns its result; it does not display anything. Any of the arguments might be a list or an atom. For each replacement, instances of the atom RANDOM within repl should be replaced by a random number between 0 and 100 , the result of: (random 100). 'This will demonstrate that proper copies are made. You might want to add this "feature" last. For example: You are encouraged to define other functions, and call them from tsar. The only builtin comparison function that you are allowed to use is eq?, for comparing atoms. Do not use the builtin function equal?, which is too much like a function I want you to write. Likewise, do not use the builtin function copy-tree. You are required to use only a pure subset of Scheme: - no side-effecting functions, with an exclamation mark in their names (e.g., set-car! and set-cdr!) - no loops (e.g., do, foreach, and map) Historically, students often want to use the builtin function append. There are several reasons why you should not use append: - It doesn't really do what you want. Use cons. - It does not make a copy of its arguments, as required by parts of the assignment. Paraphrasing the reference manual: append doesn't modify the given arguments, but the return value may share structure with the final argument
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
