Question: (Using the programming language Scheme) - Define a procedure remove-last that removes the last top-level occurrence of a given element item in a list Is.

(Using the programming language Scheme) - Define a procedure remove-last that removes the last top-level occurrence of a given element item in a list Is. Test your procedure on:

;(remove-last 'a '(b a n a n a s)) ==> (b a n a n s) ;(remove-last 'a '(b a n a n a)) ==> (b a n a n) ;(remove-last 'a '()) ==> ()

Below is my solution with a helper. I first reverse the list and then remove the first element, but I'm not sure how to reverse the final output of the list.

(define remove-last (lambda (item ls) (cond ((null? ls) '()) ((h-remove-last item (reverse ls))))))

(define h-remove-last (lambda (item ls) (cond ((null? ls) '()) ((equal? (car ls) item) (cdr ls)) (else (cons (car ls) (h-remove-last item (cdr ls)))))))

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!