Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Scheme Scheme Scheme Scheme Scheme Scheme Scheme Scheme Scheme Scheme Scheme Sch

ID: 3732024 • Letter: S

Question

Scheme Scheme Scheme Scheme Scheme Scheme Scheme

Scheme Scheme Scheme Scheme Scheme Scheme Scheme Scheme

3. (3 points) The rev function takes two lists as arguments. It returns a list that is the concatenation of the reverse of the first list with the second st. For example, (rev '(1 2 3) (4 5)) should return (3 214 (a) (2 points) Define the rev function by using case analysis and recursion (b) (1 point) Now we want to define a reverseList function, which reverses a list. Define reverseList based on the rev function

Explanation / Answer

Please find my answer:

Ans. a) Here we will use simple car and cdr methods.

as we know that car method takes a list and returns the first element of list.

And the cdr method takes a list as input and returns the list after removing the first element from it.

A single iteration of this function rev can be thought as removing one element from the first list and appending it to the front of the second list. For appending we are using cons.

Hence the function can be defined as

(define (rev list1 list2)
(if (null? list1) list2 (rev (cdr list1) (cons (car list1) list2))))

Here we used the null? funciton which checks if the given list is null or not. and if is the common conditional statement. The first parameter of if is the condition and if it is true then it will return/execute its second parameter and if the first condition is false it will return/execute the second parameter.

Here in the second parameter we recursively call the rev function using car and cdr as described previously.

b) We can use rev to define reverseList function as shown below,

(define (reverseList list) (rev list '()))

Where we passes second list as a NULL list.

P.S: If you like our answer please give a thumb up. Alternatively if you think the answer is not good, then please give us a feed back. Your feedback will surely help us to help you in future.

Please rate my answer

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote