Home > Community > Forums > Custom IC SKILL > how to simultaneously traverse varaiable number of 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: *

 how to simultaneously traverse varaiable number of lists? 

Last post Fri, May 26 2006 10:16 AM by archive. 8 replies.
Started by archive 26 May 2006 10:16 AM. Topic has 8 replies and 2375 views
Page 1 of 1 (9 items)
Sort Posts:
  • Fri, May 26 2006 10:16 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,910
    how to simultaneously traverse varaiable number of lists? Reply

    If I want to traverse, lets's say for example, 3 lists at one time, I would use:

    list1 = '(1 2 3)
    list2 = '(4 5 6)
    list3 = '(7 8 9)
    foreach((x y z) list1 list2 list3
    printf("%L %L %L\n" x y z)
    )
    But, how do you do this, when you don't know how many lists that you will need to traverse? Is this easily done in SKILL? Or, will I have to brute force it using elemental list access, like so?
    listOfLists = '((1 2 3) (4 5 6) (7 8 9))
    for(j 1 length(car(listOfLists))
    string = ""
    foreach(l listOfLists
    string = sprintf(nil "%s %L" string nthelem(j l))
    )
    printf("%s\n" string)
    )
    Do you know a better way?  Please remember that I do not know the number of lists in the listOfLists a priori (although I do know the lists are all the same length); otherwise, I would use the structure from the first example ...

    Thanks!


    Originally posted in cdnusers.org by m27315
    • Post Points: 0
  • Fri, May 26 2006 11:02 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,910
    RE: how to simultaneously traverse varaiable number of lists? Reply

    The best thing here would be to use mapc and apply, as follows

    apply('mapc
    `(lambda((@rest entries)
    let((string)
    string=""
    foreach(entry entries string=sprintf(nil "%s %L" string entry))
    printf("%s\n" string)
    ) ; let
    ); lambda
    ,@listOfLists
    ) ; end of backquote list
    ) ; apply

    The backquote is being used as a readable way of building the arguments to the mapc - which would be a function object (created with lambda in this case) and the rest of the arguments which would be the list members of listOfLists. You
    could have just used cons instead:


    apply('mapc
    cons(lambda((@rest entries)
    let((string)
    string=""
    foreach(entry entries string=sprintf(nil "%s %L" string entry))
    printf("%s\n" string)
    ) ; let
    ); lambda
    listOfLists
    ) ; cons
    ) ; apply

    They do the same thing - but backquote tends to be used more with macros, for example.

    Regards,

    Andrew.


    Originally posted in cdnusers.org by adbeckett
    • Post Points: 0
  • Fri, May 26 2006 11:04 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,910
    RE: how to simultaneously traverse varaiable number of lists? Reply

    Apologies, I should have used the "Quote" button at the top - my code has had the indentation stripped as a result. It should have been:

    apply('mapc
    `(lambda((@rest entries)
    let((string)
    string=""
    foreach(entry entries string=sprintf(nil "%s %L" string entry))
    printf("%s\n" string)
    ) ; let
    ); lambda
    ,@listOfLists
    ) ; end of backquote list
    ) ; apply


    and:

    apply('mapc
    cons(lambda((@rest entries)
    let((string)
    string=""
    foreach(entry entries string=sprintf(nil "%s %L" string entry))
    printf("%s\n" string)
    ) ; let
    ); lambda
    listOfLists
    ) ; cons
    ) ; apply

    Regards,

    Andrew.


    Originally posted in cdnusers.org by adbeckett
    • Post Points: 0
  • Fri, May 26 2006 11:13 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,910
    RE: how to simultaneously traverse varaiable number of lists? Reply

    Posted By adbeckett on 5/26/2006 11:04 AM
    Apologies, I should have used the "Quote" button at the top - my code has had the indentation stripped as a result. It should have been:

    ...
    Hi Andrew,

    I am not sure if this forum supports BBcode... I had to switch to the HTML view tab and enclose my preformatted code in
    tags.

    I really wish this forum supported BBcode.  A preview function would be really nice.

    back to topic in the next post ... :-)


    Originally posted in cdnusers.org by m27315
    • Post Points: 0
  • Fri, May 26 2006 11:20 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,910
    RE: how to simultaneously traverse varaiable number of lists? Reply

    Sorry, the quoting seems to have gone wrong with the forum software. I'll give up and let you figure out the indentation yourself...

    Andrew.


    Originally posted in cdnusers.org by adbeckett
    • Post Points: 0
  • Fri, May 26 2006 12:05 PM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,910
    RE: how to simultaneously traverse varaiable number of lists? Reply

    Posted By m27315 on 5/26/2006 11:13 AM

    I am not sure if this forum supports BBcode... I had to switch to the HTML view tab and enclose my preformatted code in
    tags.

    Ahh!  I wrote out BLOCKQUOTE and PRE tags, but they got dropped!  And, I could not tell before posting, because I cannot find a Previw function.  Apparently, there is no way to edit existing posts either, so I can't clean it up.  Oh, well...

    Anyway, thanks again Andrew for the good tip.  I had tried something similar to the build entries list, but i did not make use of the apply function, so mapc kept complaining that the first argument was not a list of unquoted symbols.  ... Anyway, that's a very good trick!

    Many thanks!!


    Originally posted in cdnusers.org by m27315
    • Post Points: 0
  • Tue, May 30 2006 9:10 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,910
    RE: how to simultaneously traverse varaiable number of lists? Reply

    Posted By adbeckett on 5/26/2006 11:02 AM
    The best thing here would be to use mapc and apply, as follows ... The backquote is being used as a readable way of building the arguments to the mapc - which would be a function object (created with lambda in this case) and the rest of the arguments which would be the list members of listOfLists. You could have just used cons instead ... They do the same thing - but backquote tends to be used more with macros, for example.
    Do you think there are any performance penalties/advantages associated with any of these techniques for large lists and more complex lambda functions?


    Originally posted in cdnusers.org by m27315
    • Post Points: 0
  • Tue, May 30 2006 9:39 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,910
    RE: how to simultaneously traverse varaiable number of lists? Reply

    Well, many of the foreach forms actually use lambda inside to build a unnamed function object. That compilation is normally only done once - it is smart enough to avoid compiling at run time.

    The backquote is a macro - so in essence it ends up being the same as cons in this case. I don't think there would be an major performance penalties here.

    The best way to find out though is to use the SKILL profiler (part of the SKILL Development environment). This will allow you to figure out where the bottlenecks in the code are.

    Andrew.


    Originally posted in cdnusers.org by adbeckett
    • Post Points: 0
  • Tue, May 30 2006 9:41 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,910
    RE: how to simultaneously traverse varaiable number of lists? Reply

    sounds good - many thanks!


    Originally posted in cdnusers.org by m27315
    • Post Points: 0
Page 1 of 1 (9 items)
Sort Posts:
Started by archive at 26 May 2006 10:16 AM. Topic has 8 replies.