Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel 2024 In 7 Days

Authors: Alan Dinkins

1st Edition

B0CJ3X98XK, 979-8861224000

Students also viewed these Databases questions