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
Get step-by-step solutions from verified subject matter experts
