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

## Email

Recipients email * (separate multiple addresses with commas)

Message *

 Send yourself a copy

## Subscribe

Intro copy of the newsletter section here, some intro copy of the newsletter. Instruction of how to subscribe to this newsletter.

First Name *

Last Name *

Email *

Company / Institution *

 Send Yourself A Copy

# 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
• #### Tue, Dec 17 2013 6:42 AM

• richardyuan
• Joined on Tue, Nov 26 2013
• Shanghai, Shanghai
• Posts 32
• Points 475
Is there a function to get first several elements of a list?
 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?
 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