Up vote 1 down vote favorite share g+ share fb share tw.
Two questions: 1. I want a function to insert an element inside a list in-place (in any position but the start of the list, see question 2 for the reason) such that: CL> (defun insert-in-place (the-list after-position new-element) .... ) => ... CL> (setf testy-list (list 'a 'b 'c 'd)) => ... CL> testy-list => ('A 'B 'C 'D) CL> (insert-in-place testy-list 1 'BOOOO) => ... CL> testy-list => ('A 'B 'BOOOO 'C 'D) 2. I think that inserting an element into the start of the list in-place is impossible through a function because the args are passed by value, so since the first cons cell of the list is passed, it is passed by value and it is a copy and so changing its car only changes a copy car, not the original, although the following cons cells are shared and change in place is possible.
Am I correct? Common-lisp link|improve this question asked Dec 8 '10 at 12:46Paralife1,0091616 88% accept rate.
1) here it is: (defun insert-after (lst index newelt) (push newelt (cdr (nthcdr index lst))) lst) (insert-after '(a c d) 0 'b) => (A B C D) 2) destructive modification of cons cells: (setf testy-list '(a bar)) (defun modify (list) (setf (first list) 'foo)) (modify testy-list) testy-list => (FOO BAR) This sets the car of the first cons cell to 'foo.
Thanks. About question 2: When you call (modify test-list) what exactly is being passed to modify? The 1st cons cell of test-list as a value or its reference?
I mean you answered that I am not correct but I cant find the flaw in my argument about question 2... – Paralife Dec 8 '10 at 14:22 Also I ended up doing this on my own: (rplacd (nthcdr position lst) (cons elem (nthcdr (+ 1 position) lst)))) but yours is better. Actually I wanted to setf the nthcdr, but in clisp that I use, nthcdr is not setfable. I wonder if it is worth to make it setfable.
See my other question: stackoverflow.com/questions/4387967/… – Paralife Dec 8 '10 at 14:24 About question 2: when you call (modify testy-list), you pass a cons cell itself ("by reference"). – npoektop Dec 8 '10 at 15:23.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.