Add 1 to the middle element of a list
Posted: Fri Nov 09, 2018 10:12 am
Hi. Can I please get some help?
"Write a function MID-ADD1 that adds 1 to the middle element of a
three-element list.
For example, (MID-ADD1 ’(TAKE 2
COOKIES)) should return the list (TAKE 3 COOKIES). Note: You
are not allowed to make MID-ADD1 a function of three inputs. It
has to take a single input that is a list of three elements."
Here's what I came up with:
(defun mid-add1 (l) (+ 1 (car (cdr l))))
This only returns the modified element. How can I get the modified list?
"Write a function MID-ADD1 that adds 1 to the middle element of a
three-element list.
For example, (MID-ADD1 ’(TAKE 2
COOKIES)) should return the list (TAKE 3 COOKIES). Note: You
are not allowed to make MID-ADD1 a function of three inputs. It
has to take a single input that is a list of three elements."
Here's what I came up with:
(defun mid-add1 (l) (+ 1 (car (cdr l))))
This only returns the modified element. How can I get the modified list?