A function in Scheme to replace all occurences of an element in a list with another element?

A simple version might be designed like this: (define (replace old-element new-element list) ; if the list is null, just return null ; else if the car of the list is equal to old-element ; run replace on the rest of the list, and cons new-element onto it ; else ; run replace on the rest of the list, and cons the car onto it) (I left the details up to you, since you gotta learn by doing. ) Remember, in Scheme the most natural way to do things will usually be to assemble a new list piece-by-piece from your old list, not to try to make little changes one at a time to your old list Note that you could also use map to do this much more concisely.

A simple version might be designed like this: (define (replace old-element new-element list) ; if the list is null, just return null ; else if the car of the list is equal to old-element ; run replace on the rest of the list, and cons new-element onto it ; else ; run replace on the rest of the list, and cons the car onto it) (I left the details up to you, since you gotta learn by doing. ) Remember, in Scheme the most natural way to do things will usually be to assemble a new list piece-by-piece from your old list, not to try to make little changes one at a time to your old list. Note that you could also use map to do this much more concisely.

1 The syntax here is bad, it should be (define (replace old new list) ...). As for the actual implementation -- don't do this, use map. – Eli Barzilay Dec 18 '09 at 4:08 2 I assumed that he is working from a guide or textbook that has gotten nowhere near map yet.

– mquander Dec 18 '09 at 4:17 Any such book would have provided a similar kind of guiding, so it is either not needed because the book covers it, or because map is available. – Eli Barzilay Dec 18 '09 at 5:18 Fair enough, that's probably true. – mquander Dec 18 '09 at 5:26.

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.

Related Questions