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.

The only comparison function that can be used is eq?, it CANNOT use other comparison functions like equal? or copy-tree It CANNOT use other side-effecting functions like set-car! or set-cdr! It CANNOT use any form of looping, like do, foreach, or map It can also NOT use the append function

Example usage: (tsar z x y) z (tsar x x y) y (tsar x x x) x (tsar () x y) () (tsar (x x) x y) (y y) (tsar (x (x) z) x y) (y (y) z) (tsar (x (x) z) (x) (y y)) (x (y y) z) (tsar (x (x) ((x)) z) (x) (y y)) (x (y y) ((y y)) z) (tsar (x (x) ((x)) z) (x) ()) (x () (()) z) (tsar (x (x) ((x)) z) () (y y)) (x (x y y) ((x y y) y y) z y y) (tsar (x (x) ((x)) z) (x) (RANDOM y)) (x (74 y) ((46 y)) z) (tsar (x (x) ((x)) z) (x) (RANDOM RANDOM)) (x (76 76) ((63 63))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!