CONS INDEX

CONS _ _ _ _ _ _ _ _ _ _ _ _ operator

The cons operator adds a new element to the beginning of a list. Its operation is identical to the symbol dot (dot). It can be used infix or prefix.

syntax:

cons(<item>,<list>) or <item> cons <list >

<item> can be any REDUCE scalar expression, including a list; <list> must be a list.

examples:



liss := cons(a,{b}); 

  {A,B} 



liss := c cons liss; 

  {C,A,B} 



newliss := for each y in liss collect cons(y,list x);
 


  NEWLISS := {{C,X},{A,X},{B,X}} 



for each y in newliss sum (first y)*(second y);
 


  X*(A + B + C)

If you want to use cons to put together two elements into a new list, you must make the second one into a list with curly brackets or the list command. You can also start with an empty list created by {}.

The cons operator is right associative: a cons b cons c is val id if c is a list; b need not be a list. The list produced is {a,b,c}.