1

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?

1
  • From the Stackoverflow help: Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. Commented May 19, 2017 at 10:59

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I figured it out on my own, eventually. But your answer is great

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.