How it is possible to receive list consisting only from members of
initial list with even or odd indexes? For example there is a list
containing 7 Cadence database members. How to receive list containing
first, third, fifth and seventh members?
mapcar(lambda((items) list(car(items) caddr(items) nth(4 items) nth(6 items))) myList)
In reply to eDave:
Ок and if quantity of members of list equally N and me it is necessary to receive list containing every second?
In reply to Slawa:
My solution, don't know if this is what your exactly looking for.
let( ( myList listLegth evenIndexList oddIndexList )myList = list( "one" "two" "three" "four" "five" "six" "seven" )listLegth = length( myList )for( n 1 listLegth cond( ( evenp( n ) evenIndexList = nconc( evenIndexList list( nthelem( n myList ) ) ) ) ( oddp( n ) oddIndexList = nconc( oddIndexList list( nthelem( n myList ) ) ) ) ))printf( "evenIndexList: %L\n" evenIndexList )printf( "oddIndexList: %L\n" oddIndexList ))
In reply to berndf:
Many thanks Berndf it that is necessary.
How about the following?
myList = '(1 2 3 4 5 6 7 8)
=> (1 2 3 4 5
6 7 8
idx = 0
setof(elem foreach(mapcar item myList evenp(idx++)&&item) elem)
=> (1 3 5 7)
This iterates over the list using foreach mapcar and the result would look like (1 nil 3 nil ...) and then the setof filters this list to remove any 'nil' values. The input list does not have to be numeric, only the variable idx is required to be numeric. Change the evenp predicate to oddp if the even indexes are needed.
In reply to skillUser:
This variant too approaches also it a bit easier offered above.