Home > Community > Forums > Custom IC SKILL > Is there a function to get first several elements of a list?

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: *

 Is there a function to get first several elements of a list? 

Last post Tue, Dec 17 2013 9:29 AM by Andrew Beckett. 1 replies.
Started by richardyuan 17 Dec 2013 06:42 AM. Topic has 1 replies and 171 views
Page 1 of 1 (2 items)
Sort Posts:
  • Tue, Dec 17 2013 6:42 AM

    • richardyuan
    • Top 500 Contributor
    • Joined on Tue, Nov 26 2013
    • Shanghai, Shanghai
    • Posts 32
    • Points 475
    Is there a function to get first several elements of a list? Reply

    I need to get part of a list. Is there a function in skill? It's better return a list.

    I tried some coding, but failed:

    a = list((0:0) (1:1) (2:2) (3:3) (4:2) (5:0))

    b = nthcdr(3 a) 

    c = remove(b a)

    As I suppose, c should be a list of ((0:0) (1:1) (2:2)). But actually c is equal to a. 

    What's wrong?

     

    • Post Points: 20
  • Tue, Dec 17 2013 9:29 AM

    Re: Is there a function to get first several elements of a list? Reply

    You can do this destructively (which means it is quite efficient) using the following:

    rplacd(nthcdr(2 a) nil)

    a =>  ((0 0) (1 1) (2 2))

    If you wanted a non-destructive list range function, you could implement it like this:

    procedure(CCFlistRange(lst start end)
      let((tail)
        tail=copy(nthcdr(start lst))
        rplacd(nthcdr(end-start tail) nil)
        tail
      )
    )

    Whether this would be more efficient than doing something like:

    procedure(CCFlistRange(lst start end)
      let(((count 0))
        setof(elem lst prog1(count>=start && count<=end count++))
      )
    )

    Would need some profiling and testing with various list sizes.

    Regards,

    Andrew.

     

    • Post Points: 5
Page 1 of 1 (2 items)
Sort Posts:
Started by richardyuan at 17 Dec 2013 06:42 AM. Topic has 1 replies.