• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. ANDing 2 lists

Stats

  • Locked Locked
  • Replies 9
  • Subscribers 143
  • Views 15185
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

ANDing 2 lists

blankman
blankman over 12 years ago
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))
  • Cancel
  • skillUser
    skillUser over 12 years ago

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 12 years ago
    foreach(mapcan (a b) blist1 blist2 a&&list(blist2))

    Regards,
    Andrew
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 12 years ago

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

     

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

     

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • blankman
    blankman over 12 years ago

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

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • blankman
    blankman over 12 years ago

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • dmay
    dmay over 12 years ago

    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

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • skillUser
    skillUser over 12 years ago

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • blankman
    blankman over 12 years ago

    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)
    )

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 12 years ago

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information