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.

fricas
(1) -> <spad>
SI ==> SingleInteger
LSI ==> List SI
PSI ==> PrimitiveArray SI
fricas
)abbrev package TTT TTT
TTT : with (
   list_map1 : (LSI, SI -> SI) -> LSI;
     ++ list_map1([e1, ..., en], f) returns
     ++ \spad{[f(e1), ..., f(e_n)]}
   list_inc : LSI -> LSI;
     ++ list_inc([e1,...,en]) returns \spad{[e1+1,...,en+1]}
     ++ Note: we create a fresh list
   arr_inc : PSI -> PSI;
     ++ arr_inc([e1,...,en]) returns \spad{[e1+1,...,en+1]}
     ++ Note: we create a fresh array
   list_fill : (LSI, SI) -> LSI;
     ++ list_fill(l, a) returns list having all elements equal
     ++ to \spad{a} of the same length as \spad{l}
   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;
     ++ list_add2 is the same as list_add1 but using
     ++ simple iteration
   list_add3 : (LSI, LSI) -> LSI;
     ++ list_add2 is the same as list_add1 but uses anonymous
     ++ function
   list_filter : (LSI, SI -> Boolean) -> LSI;
     ++ list_filter(l, f) returns list of elements \spad{e} of
     ++ \spad{l} for which \spad{f(e)} is true
   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
     ++ \spad{a} to appropriate element of \spad{l}
     ++ Probably useless, but demonstrates itertion
 ) == add
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]
-- helper for list_add1 add_SI(x1 : SI, x2 : SI) : SI == x1 + x2
list_add1(l1 : LSI, l2 : LSI) : LSI == list_map2(l1, l2, add_SI)
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</spad>
fricas
Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/8834476310057333648-25px001.spad
      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 TTT;inc_SI is replaced by add_SIel1 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 TTT;add_SI is replaced by add_SI 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.03 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 "\\spad{list_map1}(\\spad{l1},{} \\spad{l2},{} \\spad{f}) creates list from values obtained by applying \\spad{f} to corresponding elements of \\spad{l1} and \\spad{l2} Note: we truncate result to length of shorter list,{} excess elements of \\spad{l1} or \\spad{l2} are ignored." --------(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 "\\spad{list_add2} is the same as \\spad{list_add1} but using simple iteration" --------(list_add3 ((List (SingleInteger)) (List (SingleInteger)) (List (SingleInteger))))--------- --->-->TTT((list_add3 ((List (SingleInteger)) (List (SingleInteger)) (List (SingleInteger))))): Improper initial operator in comments: is "\\spad{list_add2} is the same as \\spad{list_add1} but uses anonymous function" --------(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 01 DEC 2024 02:14:39 PM):
; wrote /var/aw/var/LatexWiki/TTT.NRLIB/TTT.fasl ; compilation finished in 0:00:00.024 ------------------------------------------------------------------------ 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)

\label{eq1}\left[ - 2, \: 4, \: 0 \right](1)
Type: List(SingleInteger?)
fricas
list_inc([-1, 2, 0])

\label{eq2}\left[ 0, \: 3, \: 1 \right](2)
Type: List(SingleInteger?)
fricas
a := construct([-1, 2, 0])$PrimitiveArray(SingleInteger)

\label{eq3}\left[ - 1, \: 2, \: 0 \right](3)
Type: PrimitiveArray?(SingleInteger?)
fricas
arr_inc(a)

\label{eq4}\left[ 0, \: 3, \: 1 \right](4)
Type: PrimitiveArray?(SingleInteger?)
fricas
list_fill([-1, 2, 0], 42)

\label{eq5}\left[{42}, \:{42}, \:{42}\right](5)
Type: List(SingleInteger?)
fricas
list_map2([-1, 2, 0], [17, 1, 3], (x, y) +-> 42)

\label{eq6}\left[{42}, \:{42}, \:{42}\right](6)
Type: List(SingleInteger?)
fricas
list_add1([-1, 2, 0], [17, 1, 3])

\label{eq7}\left[{16}, \: 3, \: 3 \right](7)
Type: List(SingleInteger?)
fricas
list_add2([-1, 2, 0], [17, 1, 3])

\label{eq8}\left[{16}, \: 3, \: 3 \right](8)
Type: List(SingleInteger?)
fricas
list_add3([-1, 2, 0], [17, 1, 3])

\label{eq9}\left[{16}, \: 3, \: 3 \right](9)
Type: List(SingleInteger?)
fricas
list_filter([17, 1, 3], x +-> x = 1)

\label{eq10}\left[ 1 \right](10)
Type: List(SingleInteger?)
fricas
filter_pos1([-1, 2, 0])

\label{eq11}\left[ 2 \right](11)
Type: List(SingleInteger?)
fricas
filter_pos2([-1, 2, 0])

\label{eq12}\left[ 2 \right](12)
Type: List(SingleInteger?)
fricas
filter_pos3(a)

\label{eq13}\left[ 2 \right](13)
Type: PrimitiveArray?(SingleInteger?)
fricas
b := construct([-1, 2, 0, 4])$PrimitiveArray(SingleInteger)

\label{eq14}\left[ - 1, \: 2, \: 0, \: 4 \right](14)
Type: PrimitiveArray?(SingleInteger?)
fricas
setelt3(b, [1, 2])
Type: Void
fricas
b

\label{eq15}\left[ 1, \: 2, \: 0, \: 2 \right](15)
Type: PrimitiveArray?(SingleInteger?)




  Subject:   Be Bold !!
  ( 15 subscribers )  
Please rate this page: