Reputation: 1211
I'm trying to learn LISP. I got my way around functions and I wanted to test myself with some.
I was trying to write a function that can remove an element from a list in a given index.
This seems pretty straightforward, but I can't manage to do it.
Example: I have the list (20 8 13 10) and I want to remove the number at index 2.
How would I go about something like this?
Upvotes: 1
Views: 2518
Reputation: 48745
It's very easy. This is the base case:
(remove-nth 0 '(2 3)) ; => (3)
And the default case:
(remove-nth 1 '(1 2 3)) ; ==
(cons 1 (remove-nth 0 '(2 3)))
The only thing left for you to do is to actually implement it!
There is a third case. What if the list is nil
? In the strictest sense you cannot do the job and you should signal an error or perhaps there isn't anything to do so it's ok to then have it as a base case that evaluates to '()
so that (remove-nth 5 '(1 2)) ; ==> (1 2)
Upvotes: 2