LISP - Problem 4 Solution for Problem 3:

(define omitlast (l)
         ((atom l) nil)
         ((null (cdr l)) ())
         (t (cons (car l) (omitlast (cdr l))))

LISP - Problem 4

Design, and implement a LISP function named replacelast that will take a list (identified as the second argument) and replace the last item in that list by the first argument. That is, (replacelast 'x '(a b c)) would return (a b x); (replacelast '(f) '(a)) will return ((f)).

You may check out the validity of your implementation at any time using the EVALUATE button and editing the function definition as necessary. We recommend that you copy the function definition each time you edit it, so that you can reconstruct and edit it as you go along. Once you have completed your implementation of this function, click on the DONE button to submit your solution to the grader and score recorder. A new problem will then be displayed.

Last updated 2001/06/25
© J.A.N. Lee, 2001.