Home > Community > Forums > Custom IC SKILL > ANDing 2 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: *

 ANDing 2 lists 

Last post Sat, Mar 23 2013 3:31 AM by Andrew Beckett. 9 replies.
Started by blankman 27 Feb 2013 08:49 AM. Topic has 9 replies and 1106 views
Page 1 of 1 (10 items)
Sort Posts:
  • Wed, Feb 27 2013 8:49 AM

    • blankman
    • Top 100 Contributor
    • Joined on Wed, Apr 20 2011
    • Posts 70
    • Points 1,175
    ANDing 2 lists Reply
    Hi All, I have 2 lists below. I am looking to AND them together to get a 3rd list containing only the coordinates of bslist2 that correspond to the values of 't' in bslist1. What is the best way to go about this?

    Thanks,

    Brian.

    bslist1 = (nil nil nil nil nil t t t t)

    bslist2 = ((106.64 64.105) (106.64 69.895) (117.09 69.895) (117.09 63.54) (111.135 63.54) (111.135 67.755) (114.9 67.755) (114.9 66.35) (113.045 66.35))
    • Post Points: 35
  • Wed, Feb 27 2013 9:13 AM

    • skillUser
    • Top 10 Contributor
    • Joined on Fri, Sep 19 2008
    • Austin, TX
    • Posts 2,569
    • Points 15,600
    Re: ANDing 2 lists Reply

    Hi Brian,

    you can obtain the "AND" result by iterating over the two lists at the same time with foreach, and then if you want to filter away the nil results you can do that using "setof".  Here's an example:

    
    foreach(mapcar (a b) bslist1 bslist2 a&&b)
    => (nil nil nil nil nil
        (111.135 67.755) (114.9 67.755) (114.9 66.35) (113.045 66.35)
       )
     
    setof(item foreach(mapcar (a b) bslist1 bslist2 a&&b) item)
    => ((111.135 67.755) 
         (114.9 67.755) 
         (114.9 66.35) 
         (113.045 66.35)
       )
    

    By the way, I inserted a single quote ' before the open parentheses from your example of defining bslist1 and bslist2. The 'mapcar' allows us to return the result rather than the (first) original list.  With the foreach function you can iterate two variables over two lists, three variables over three lists and so on; the number of iterations is determined by the first list, so if the lists are not equal in length the first list should be the shortest or an error will ensue.

    Hopefully this answers your question?

    Regards,

    Lawrence.

    • Post Points: 5
  • Wed, Feb 27 2013 11:26 AM

    Re: ANDing 2 lists Reply
    foreach(mapcan (a b) blist1 blist2 a&&list(blist2))

    Regards,
    Andrew
    • Post Points: 5
  • Wed, Feb 27 2013 12:24 PM

    Re: ANDing 2 lists Reply

    Sorry, deliberate mistake (ok, typo from typing on a smart phone). Should have been:

     

    foreach(mapcan (a b) blist1 blist2 a&&list(b))
     
    Andrew 

     

    • Post Points: 20
  • Thu, Feb 28 2013 6:49 AM

    • blankman
    • Top 100 Contributor
    • Joined on Wed, Apr 20 2011
    • Posts 70
    • Points 1,175
    Re: ANDing 2 lists Reply

    Andrew, Lawrence, thanks for your help, works a charm.
    Brian.

    • Post Points: 5
  • Fri, Mar 22 2013 5:09 AM

    • blankman
    • Top 100 Contributor
    • Joined on Wed, Apr 20 2011
    • Posts 70
    • Points 1,175
    Re: ANDing 2 lists Reply

    Andrew & Lawrence,

    I have 2 lists below, essentially lists within a list. How would I go about comparing and extracting the corresponding points to true values in this case?

    bspartselobjvalidlist = ((nil nil nil nil nil nil t nil) (nil t nil nil))
    bspartselobjpointlist = (((-3.685 7.465) (-4.45 7.465) (-4.45 8.0) (-5.39 8.0) (-5.39 7.24) (-4.675 7.24) (-4.675 6.335) (-3.685 6.335)) ((-5.39 3.555) (-5.39 4.74) (-4.01 4.74) (-4.01 3.555)))

    Thanks,
    Brian.

    • Post Points: 35
  • Fri, Mar 22 2013 5:56 AM

    • dmay
    • Top 25 Contributor
    • Joined on Thu, Jul 17 2008
    • Allen, TX
    • Posts 393
    • Points 7,415
    Re: ANDing 2 lists Reply

    I'm not sure how you want to store your results, but here is one way to do it:

    procedure(myListAnd(list1 list2)
      let((pointList)
        foreach((subList1 subList2) list1 list2
            foreach((flag point) subList1 subList2
                when(flag
                    println(point)
                    pointList = cons(point pointList)
                )
            )
        )
        pointList
      ) ;let
    ) ;proc

    myListAnd(bspartselobjvalidlist bspartselobjpointlist)

    Derek

    • Post Points: 5
  • Fri, Mar 22 2013 8:33 AM

    • skillUser
    • Top 10 Contributor
    • Joined on Fri, Sep 19 2008
    • Austin, TX
    • Posts 2,569
    • Points 15,600
    Re: ANDing 2 lists Reply

    Hi Brian,

    Or you can wrap Andrew's example as a function and call it using the same foreach-mapcan structure:

    
    procedure(CCFandLists(blist1 blist2 "ll")
      foreach(mapcan (a b) blist1 blist2 a&&list(b))
    )
    
    foreach(mapcan (a b) bspartselobjvalidlist bspartselobjpointlist
      CCFandLists(a b)
    )
    => ((-4.675 6.335) 
        (-5.39 4.74)
    )
    

    So you can then either call the CCFandLists function directly similar to Andrew's earlier post (e.g. CCFandLists(bslist1 bslist2) ) when the lists are in that format, or you can call the function as in the above example when the lists are list-of-lists.  This way you are re-using the function...

    Regards,

    Lawrence.

    • Post Points: 20
  • Fri, Mar 22 2013 9:55 AM

    • blankman
    • Top 100 Contributor
    • Joined on Wed, Apr 20 2011
    • Posts 70
    • Points 1,175
    Re: ANDing 2 lists Reply

    Lawrence Derek thanks, I made an attempt at it since I posted and came up with the below. Working fine.

    Its insightful to see ye're solutions..

    Brian.

    foreach(elementb bspartselobjvalidlist
                    elementc=car(bspartselobjpointlist)
                    bspartselobjpointlist=cdr(bspartselobjpointlist)
                    result=foreach(mapcan (a b) elementb elementc a&&list(b))
                    bspartselobjvalidpointlist = cons( result bspartselobjvalidpointlist)
    )

    • Post Points: 20
  • Sat, Mar 23 2013 3:31 AM

    Re: ANDing 2 lists Reply

    Here's how I'd have done it - with a recursive function. This will then cope with any nested structure of lists:

    procedure(CCFandNestedLists(blist1 blist2)
      if(listp(car(blist1)) && car(blist1)
        foreach(mapcar (a b) blist1 blist2 CCFandNestedLists(a b))
        foreach(mapcan (a b) blist1 blist2 a && list(b))
      )
    )

    The lists will be returned in the same order; Brian's solution reverses them (which may not matter to you).

    Andrew.

    • Post Points: 5
Page 1 of 1 (10 items)
Sort Posts:
Started by blankman at 27 Feb 2013 08:49 AM. Topic has 9 replies.