Home > Community > Forums > Custom IC SKILL > Merging Lists

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 Merging Lists 

Last post Fri, Sep 11 2009 5:57 PM by SBrickles. 2 replies.
Started by SBrickles 09 Sep 2009 06:22 PM. Topic has 2 replies and 890 views
Page 1 of 1 (3 items)
Sort Posts:
  • Wed, Sep 9 2009 6:22 PM

    • SBrickles
    • Not Ranked
    • Joined on Thu, Jul 17 2008
    • Milpitas, CA
    • Posts 3
    • Points 45
    Merging Lists Reply

    I am writing some code which uses lists.
    I need a way to be able merge two lists into one such that

    list1:

    ( x "foo"
         (
            ( a "a" "func1()" )
            ( b "b" "func2()" )
            ( c "c" (
                        ( d "d" "func3()" )
                      )
         )
    )

    and list2: 

    ( x "foo"
         (
            ( e "e" "func4()" )
            ( c "c" (
                        ( f "f" "func5" )
                      )
         )
    )

    merge so that you end up with:

    ( x "foo"
         (
            ( a "a" "func1()" )
            ( b "b" "func2()" )
            ( c "c" (
                        ( d "d" "func3()" )
                        ( f "f" "func5" )
                      )
            ( e "e" "func4()" )
         )
    )

    The code needs to be able to merge at multiple levels depending on
    whether the  'key' element (the car of the list at any level) is the same value or not.

    If the item in the second list is of the form (a "a" "func()" ) then it should overwrite the
    value when it gets merged with the first list.  So in the example above if the second list
    had  ( a "a" "func8()" ) in it then it would have overwritten the ( a "a" "func1()" ) in the first list
    - but only if it was at the same level of hierachy  as in the first list.

    Any ideas ?

    Stephen 

    • Post Points: 20
  • Thu, Sep 10 2009 1:57 AM

    Re: Merging Lists Reply

    Stephen,

    Try the code below. In the comments at the top, I corrected your examples - because there were some missing close parentheses. Note that in order to make the top level look the same as the lists further down in the list hierarchy, I added a call to list() around each of list1 and list2 - that keeps them the same.

    If you use the destructive version (the one without the copy) then you can simply look at list1 once the function has finished, and it will be updated. If you use the version with the SBcopyDeep(), you can just take the car() of the result of SBmergeLists(). In fact you can always take the car() of the SBmergeLists.

    The code works by using a recursive function - I think it captures your requirements.

    /*
    list1=
    '( x "foo"
         (
            ( a "a" "func1()" )
            ( b "b" "func2()" )
            ( c "c" (
                        ( d "d" "func3()" )
                      )
               )
         )
    )
    
    list2=
    '( x "foo"
         (
            ( a "a" "func8()" )
            ( e "e" "func4()" )
            ( c "c" (
                        ( f "f" "func5" )
                      )
               )
         )
    )
    
    ; if you don't mind a destructive change
    SBmergeLists(list(list1) list(list2))
    ; if you want to protect against side effects caused by doing
    ; destructive change
    SBmergeLists(SBcopyDeep(list(list1)) list(list2))
    
    */
    
    procedure(SBcopyDeep(l)
      foreach(mapcar el l
        if(listp(el) SBcopyDeep(el) el)
      )
    )
    
    procedure(SBmergeLists(l1 l2)
      let((match)
        foreach(l2el l2
          match=assoc(car(l2el) l1)
          if(match then
    	if(listp(caddr(match)) then
    	  SBmergeLists(caddr(match) caddr(l2el))
            else
              rplaca(cddr(match) caddr(l2el))
            )
          else
            rplacd(last(l1) list(l2el))
          )
        )
        l1
      )
    )
    

    Best Regards,

    Andrew.

     

    • Post Points: 20
  • Fri, Sep 11 2009 5:57 PM

    • SBrickles
    • Not Ranked
    • Joined on Thu, Jul 17 2008
    • Milpitas, CA
    • Posts 3
    • Points 45
    Re: Merging Lists Reply

    Andrew - Many many thanks for your help and prompt response on this.
    It completely solved my problem !!

    I made the following modification to the SBmergeLists procedure to allow a list
    to be replaced by an item:

    procedure( SBmergeLists(l1 l2)
    let( ( match )
    foreach( l2el l2
    match = assoc( car( l2el ) l1 )
    if( match then
    if( listp( caddr( match ) ) then
              if( listp( nth( 2 l2el ) ) then
    SBmergeLists( caddr( match ) caddr( l2el ) )
    else
    rplaca( member( match l1 ) l2el )
    )
    else
    rplaca( cddr( match ) caddr( l2el ) )
    )
    else
    rplacd(last( l1 ) list( l2el ) )
    )
    )
    l1
    )
    )

    This allows the case below to work as well:

    list1=
    '( x "foo"
    (
    ( a "a" "func1()" )
    ( b "b" "func2()" )
    ( c "c" (
    ( d "d" "func3()" )
    )
    )
    )
    )

    list2=
    '( x "foo"
    (
    ( c "c" "func4()" )
    )

    Resulting in:

    pp( list1 )
    ( x "foo"
    (
    ( a "a" "func1()" )
    ( b "b" "func2()" )
    ( c "c" "func4()" )
    )
    )

    The opposite case of turning an item into a list works fine as well.

    Again, many thanks,

    Stephen.

     
    • Post Points: 5
Page 1 of 1 (3 items)
Sort Posts:
Started by SBrickles at 09 Sep 2009 06:22 PM. Topic has 2 replies.