Home > Community > Forums > Custom IC SKILL > list substitution problems

## Email

Recipients email * (separate multiple addresses with commas)

Message *

 Send yourself a copy

## Subscribe

Intro copy of the newsletter section here, some intro copy of the newsletter. Instruction of how to subscribe to this newsletter.

First Name *

Last Name *

Email *

Company / Institution *

 Send Yourself A Copy

# list substitution problems

Last post Thu, Jul 23 2009 2:05 AM by Andrew Beckett. 3 replies.
 Started by sparksu 23 Jul 2009 12:13 AM. Topic has 3 replies and 913 views
• #### Thu, Jul 23 2009 12:13 AM

• sparksu
• Joined on Thu, May 14 2009
• Beijing, China
• Posts 17
• Points 295
list substitution problems
 I have a long list here:listA=list(P1 P2 P3 ... Px-1 Px Px+1 ... Pn)for a given Px, I want to substitute Px with 2 new elements Px1 Px2  :ListB=list(P1 P2 P3 ... Px-1 Px1 Px2 Px+1 ... Pn)I know how to do it using "reverse()" function.But based on SKILL Language User Guide, reverse() can generate a lot of memeory.Is there a more efficient way to do this? I don't care if it is destructive or nondestructive.Here is my code using reverse():;---------------------------------------------------------------------------------------------------------------------------------listB2=rplaca(member(Px listA) Px2)                                     ;listB2=>list(Px2 ... Pn)listB1=reverse(rplaca(member(Px2 reverse(listA) Px1)             ;listB1=>list(P1 P2 P3 ... Px-1 Px1)listB=append(listB1 listB2)                ;ListB=>list(P1 P2 P3 ... Px-1 Px1 Px2 Px+1 ... Pn);---------------------------------------------------------------------------------------------------------------------------------
• Post Points: 20
• #### Thu, Jul 23 2009 1:10 AM

Re: list substitution problems
 match=member(Px listA)listB=  if(eq(match listA) then    constar(Px1 Px2 cdr(listA))  else    rplaca(match Px1)    rplacd(match cons(Px2 cdr(match)))    listA  )You also should consider whether a list is the right data structure for your needs - I presume whatever you are doing needs to be ordered, otherwise you could have just stuck Px2 on the beginning. Or used a table.Regards,Andrew.
• Post Points: 20
• #### Thu, Jul 23 2009 1:56 AM

• sparksu
• Joined on Thu, May 14 2009
• Beijing, China
• Posts 17
• Points 295
Re: list substitution problems
 Thanks, Andrew.Actually, listA is the points of a path object. It should remain their original order.Further more, if i want to break listA into 2 seperate sublistslistA1=list(P1 P2 P3 ... Px-1)listA2=list(Px ... Pn)What should I do to get listA1 and listA2?Can I just break the pointer (or called link) between Px-1 and Px?Thanks again for your quick reply.
• Post Points: 20
• #### Thu, Jul 23 2009 2:05 AM

Re: list substitution problems
 Something like this: match=nilforeach(map rem listA  when(cadr(rem)==Px match=rem))listA2=cdr(match)rplacd(match nil)listA1=listANote this doesn't handle the case where the match is at the beginning of the list - but that's cheap to check for and handle, as I did in the previous response. Potentially you could make the foreach stop as soon as it finds a match by doing:prog(()  foreach(map rem listA    when(cadr(rem)==Px      match=rem      return()    )  ))The surrounding prog allows you to use return() to exit the foreach early. Regards,Andrew.
• Post Points: 5