login  home  contents  what's new  discussion  bug reports help  links  subscribe  changes  refresh  edit

## Sequence Iteration

The following package demonstrate various form of iteration over list and arrays, both using loops and using higher order functions. Note that Spad loop

    for i in 0..5 for e in l repeat


is a parallel iteration, stepping i and e in sync stopping when one of iterators runs out.

SI ==> SingleInteger
LSI ==> List SI
PSI ==> PrimitiveArray SI
)abbrev package TTT TTT
TTT : with (
list_map1 : (LSI, SI -> SI) -> LSI;
++ list_map1([e1, ..., en], f) returns
list_inc : LSI -> LSI;
++ Note: we create a fresh list
arr_inc : PSI -> PSI;
++ Note: we create a fresh array
list_fill : (LSI, SI) -> LSI;
++ list_fill(l, a) returns list having all elements equal
list_map2: (LSI, LSI, (SI, SI) -> SI) -> LSI;
++ list_map1(l1, l2, f) creates list from values obtained
++ by applying f to corresponding elements of l1 and l2
++ Note: we truncate result to length of shorter list,
++ excess elements of l1 or l2 are ignored.
list_add1 : (LSI, LSI) -> LSI;
++ list_add1(l1, l2) returns list of sums of corresponding
++ elements of l1 and l2 using list_map2 and a helper
++ Note: we truncate result to length of shorter list,
++ excess elements of l1 or l2 are ignored.
list_add2 : (LSI, LSI) -> LSI;
++ simple iteration
list_add3 : (LSI, LSI) -> LSI;
++ function
list_filter : (LSI, SI -> Boolean) -> LSI;
++ list_filter(l, f) returns list of elements \spad{e} of
filter_pos1 : LSI -> LSI;
++ filter_pos1(l) returns list of positive elements of l
++ usinig simple iteration
filter_pos2 : LSI -> LSI;
++ filter_pos2(l) is like filter_pos1, but uses list_filter
filter_pos3 : PSI -> PSI;
++ filter_pos3(l) is like filter_pos1, but for arrays
setelt3 : (PSI, LSI) -> Void
++ setelt3(a, l) sets every third element of
++ Probably useless, but demonstrates itertion
list_map1(l : LSI, f : SI -> SI) : LSI ==
[f(el) for el in l]
-- helper for list_inc
inc_SI(el : SI) : SI == el + 1
list_inc(l : LSI) : LSI ==
list_map1(l, inc_SI)
arr_inc(pa : PSI) : PSI ==
[pa(i) + 1 for i in 0..(#pa - 1)]
list_fill(l : LSI, x : SI) : LSI ==
list_map1(l, (y : SI) : SI +-> x)
list_map2(l1 : LSI, l2 : LSI, f : (SI, SI) -> SI) : LSI ==
[f(el1, el2) for el1 in l1 for el2 in l2]
add_SI(x1 : SI, x2 : SI) : SI == x1 + x2
list_add1(l1 : LSI, l2 : LSI) : LSI ==
list_add2(l1 : LSI, l2 : LSI) : LSI ==
[el1 + el2 for el1 in l1 for el2 in l2]
list_add3(l1 : LSI, l2 : LSI) : LSI ==
list_map2(l1, l2, (x1 : SI, x2 : SI) : SI +-> x1 + x2)
list_filter(l : LSI, f : SI -> Boolean) : LSI ==
[el for el in l | f(el)]
filter_pos1(l : LSI) : LSI ==
[el for el in l | 0 < el]
pos_SI(x : SI) : Boolean == 0 < x
filter_pos2(l : LSI) : LSI ==
list_filter(l, pos_SI)
filter_pos3(pa : PSI) : PSI ==
[pa(i) for i in 0..(#pa - 1) | 0 < pa(i)]
setelt3(pa : PSI, l : LSI) : Void ==
for i in 0..(#pa - 1) by 3 for el in l repeat
pa(i) := el
   Compiling FriCAS source code from file
using old system compiler.
TTT abbreviates package TTT
------------------------------------------------------------------------
initializing NRLIB TTT for TTT
compiling into NRLIB TTT
compiling exported list_map1 : (List SingleInteger,SingleInteger -> SingleInteger) -> List SingleInteger
Time: 0 SEC.
compiling local inc_SI : SingleInteger -> SingleInteger
Time: 0 SEC.
compiling exported list_inc : List SingleInteger -> List SingleInteger
Time: 0 SEC.
compiling exported arr_inc : PrimitiveArray SingleInteger -> PrimitiveArray SingleInteger
Time: 0.01 SEC.
compiling exported list_fill : (List SingleInteger,SingleInteger) -> List SingleInteger
Time: 0 SEC.
compiling exported list_map2 : (List SingleInteger,List SingleInteger,(SingleInteger,SingleInteger) -> SingleInteger) -> List SingleInteger
Time: 0 SEC.
compiling local add_SI : (SingleInteger,SingleInteger) -> SingleInteger
Time: 0 SEC.
compiling exported list_add1 : (List SingleInteger,List SingleInteger) -> List SingleInteger
Time: 0 SEC.
compiling exported list_add2 : (List SingleInteger,List SingleInteger) -> List SingleInteger
Time: 0 SEC.
compiling exported list_add3 : (List SingleInteger,List SingleInteger) -> List SingleInteger
Time: 0 SEC.
compiling exported list_filter : (List SingleInteger,SingleInteger -> Boolean) -> List SingleInteger
Time: 0 SEC.
compiling exported filter_pos1 : List SingleInteger -> List SingleInteger
Time: 0 SEC.
compiling local pos_SI : SingleInteger -> Boolean
TTT;pos_SI is replaced by less_SI0x
Time: 0 SEC.
compiling exported filter_pos2 : List SingleInteger -> List SingleInteger
Time: 0 SEC.
compiling exported filter_pos3 : PrimitiveArray SingleInteger -> PrimitiveArray SingleInteger
Time: 0 SEC.
compiling exported setelt3 : (PrimitiveArray SingleInteger,List SingleInteger) -> Void
Time: 0 SEC.
(time taken in buildFunctor:  0)
;;;     ***       TTT REDEFINED
;;;     ***       TTT REDEFINED
Time: 0 SEC.
Cumulative Statistics for Constructor TTT
Time: 0.01 seconds
finalizing NRLIB TTT
Processing TTT for Browser database:
--->-->TTT(constructor): Not documented!!!!
--------(list_map1 ((List (SingleInteger)) (List (SingleInteger)) (Mapping (SingleInteger) (SingleInteger))))---------
--------(list_inc ((List (SingleInteger)) (List (SingleInteger))))---------
--------(arr_inc ((PrimitiveArray (SingleInteger)) (PrimitiveArray (SingleInteger))))---------
--------(list_fill ((List (SingleInteger)) (List (SingleInteger)) (SingleInteger)))---------
--------(list_map2 ((List (SingleInteger)) (List (SingleInteger)) (List (SingleInteger)) (Mapping (SingleInteger) (SingleInteger) (SingleInteger))))---------
--->-->TTT((list_map2 ((List (SingleInteger)) (List (SingleInteger)) (List (SingleInteger)) (Mapping (SingleInteger) (SingleInteger) (SingleInteger))))): Improper first word in comments: list_map1
--------(list_add1 ((List (SingleInteger)) (List (SingleInteger)) (List (SingleInteger))))---------
--------(list_add2 ((List (SingleInteger)) (List (SingleInteger)) (List (SingleInteger))))---------
--->-->TTT((list_add2 ((List (SingleInteger)) (List (SingleInteger)) (List (SingleInteger))))): Improper initial operator in comments: is
--------(list_add3 ((List (SingleInteger)) (List (SingleInteger)) (List (SingleInteger))))---------
--->-->TTT((list_add3 ((List (SingleInteger)) (List (SingleInteger)) (List (SingleInteger))))): Improper initial operator in comments: is
--------(list_filter ((List (SingleInteger)) (List (SingleInteger)) (Mapping (Boolean) (SingleInteger))))---------
--------(filter_pos1 ((List (SingleInteger)) (List (SingleInteger))))---------
--------(filter_pos2 ((List (SingleInteger)) (List (SingleInteger))))---------
--------(filter_pos3 ((PrimitiveArray (SingleInteger)) (PrimitiveArray (SingleInteger))))---------
--------(setelt3 ((Void) (PrimitiveArray (SingleInteger)) (List (SingleInteger))))---------
--->-->TTT(): Missing Description
; compiling file "/var/aw/var/LatexWiki/TTT.NRLIB/TTT.lsp" (written 21 FEB 2014 07:55:28 AM):
; /var/aw/var/LatexWiki/TTT.NRLIB/TTT.fasl written
; compilation finished in 0:00:00.064
------------------------------------------------------------------------
TTT is now explicitly exposed in frame initial
TTT will be automatically loaded when needed from
/var/aw/var/LatexWiki/TTT.NRLIB/TTT

Try it out:

fricas
list_map1([-1, 2, 0], x +-> x*2)
 (1)
Type: List(SingleInteger?)
fricas
list_inc([-1, 2, 0])
 (2)
Type: List(SingleInteger?)
fricas
a := construct([-1, 2, 0])$PrimitiveArray(SingleInteger)  (3) Type: PrimitiveArray?(SingleInteger?) fricas arr_inc(a)  (4) Type: PrimitiveArray?(SingleInteger?) fricas list_fill([-1, 2, 0], 42)  (5) Type: List(SingleInteger?) fricas list_map2([-1, 2, 0], [17, 1, 3], (x, y) +-> 42)  (6) Type: List(SingleInteger?) fricas list_add1([-1, 2, 0], [17, 1, 3])  (7) Type: List(SingleInteger?) fricas list_add2([-1, 2, 0], [17, 1, 3])  (8) Type: List(SingleInteger?) fricas list_add3([-1, 2, 0], [17, 1, 3])  (9) Type: List(SingleInteger?) fricas list_filter([17, 1, 3], x +-> x = 1)  (10) Type: List(SingleInteger?) fricas filter_pos1([-1, 2, 0])  (11) Type: List(SingleInteger?) fricas filter_pos2([-1, 2, 0])  (12) Type: List(SingleInteger?) fricas filter_pos3(a)  (13) Type: PrimitiveArray?(SingleInteger?) fricas b := construct([-1, 2, 0, 4])$PrimitiveArray(SingleInteger)
 (14)
Type: PrimitiveArray?(SingleInteger?)
fricas
setelt3(b, [1, 2])
Type: Void
fricas
b
 (15)
Type: PrimitiveArray?(SingleInteger?)

 Subject:   Be Bold !! ( 14 subscribers )