Maxima – 24 – Tipi di dati e strutture – liste – 5

Continuo da qui, copio dal Reference Manual, PDF scaricabile da qui, sono a p.77.

pop(list)
pop removes and returns the first element from the list list. The argument list must be a mapatom that is bound to a nonempty list. If the argument list is not bound to a nonempty list, Maxima signals an error.

push (item, list)
push prepends the item item to the list list and returns a copy of the new list. The second argument list must be a mapatom that is bound to a list. The first argument item can be any Maxima symbol or expression. If the argument list is not bound to a list, Maxima signals an error.

To remove the first item from a list, see pop.

(%i1) ll: [];
(%o1)                                 []
(%i2) push (x, ll);
(%o2)                                 [x]
(%i3) push (x^2+y, ll);
                                        2
(%o3)                             [y + x , x]
(%i4) a: push ("string", ll);
                                            2
(%o4)                         [string, y + x , x]
(%i5) pop (ll);
(%o5)                               string
(%i6) pop (ll);
                                         2
(%o6)                               y + x
(%i7) pop (ll);
(%o7)                                  x
(%i8) ll;
(%o8)                                 []
(%i9) a;
                                            2
(%o9)                         [string, y + x , x]

rest(expr, n)
rest(expr)
Returns expr with its first n elements removed if n is positive and its last - n elements removed if n is negative. If n is 1 it may be omitted. The first argument expr may be a list, matrix, or other expression. When expr is an atom, rest signals an error; when expr is an empty list and partswitch is false, rest signals an error. When expr is an empty list and partswitch is true, rest returns end.

Applying rest to expression such as f(a,b,c) returns f(b,c). In general, applying rest to an nonlist doesn’t make sense. For example, because ’^’ requires two arguments, rest(a^b) results in an error message. The functions args and op may be useful as well, since args(a^b) returns [a,b] and op(a^b) returns ^.

See also firstn and lastn.

(%i1) rest(a+b+c);
(%o1)                                b + a
(%i2) rest(a+b+c,2);
(%o2)                                  a
(%i3) rest(a+b+c,-2);
(%o3)                                  c

reverse(list)
Reverses the order of the members of the list (not the members themselves). reverse also works on general expressions, e.g. reverse(a=b); gives b=a.

rreduce(F, s)
rreduce(F, s, s_{n + 1})
Extends the binary function F to an n-ary function by composition, where s is a list.

rreduce(F, s) returns F(s_1, ... F(s_{n - 2}, F(s_{n - 1}, s_n))).

When the optional argument s{ n + 1} is present, the result is equivalent to rreduce(F, endcons(s_{n + 1}, s)).

The function F is first applied to the rightmost list elements, thus the name “rreduce”.

rreduce without the optional argument.

(%i4) rreduce(f, [1, 2, 3]);
(%o4)                            f(1, f(2, 3))
(%i5) rreduce(f, [1, 2, 3, 4]);
(%o5)                         f(1, f(2, f(3, 4)))

rreduce with the optional argument.

(%i6) rreduce (f, [1, 2, 3], 4);
(%o6)                         f(1, f(2, f(3, 4)))

rreduce applied to built-in binary operators. / is the division operator.

(%i7) rreduce ("^", args({a, b, c, d}));
                                        d
                                       c
                                      b
(%o7)                                a
(%i8) rreduce ("/", args({a, b, c, d}));
                                      a c
(%o8)                                 ---
                                      b d

sort(L, P)
sort(L)
sort(L, P) sorts a list L according to a predicate P of two arguments which defines a strict weak order on the elements of L. If P(a, b) is true, then a appears before b in the result. If neither P(a, b) nor P(b, a) are true, then a and b are equivalent, and appear in the result in the same order as in the input. That is, sort is a stable sort.

If P(a, b) and P(b, a) are both true for some elements of L, then P is not a valid sort predicate, and the result is undefined. If P(a, b) is something other than true or false, sort signals an error.

The predicate may be specified as the name of a function or binary infix operator, or as a lambda expression. If specified as the name of an operator, the name must be enclosed in double quotes.

The sorted list is returned as a new object; the argument L is not modified.

sort(L) is equivalent to sort(L, orderlessp).

The default sorting order is ascending, as determined by orderlessp. The predicate ordergreatp sorts a list in descending order.

All Maxima atoms and expressions are comparable under orderlessp and ordergreatp.

Operators < and > order numbers, constants, and constant expressions by magnitude. Note that orderlessp and ordergreatp do not order numbers, constants, and constant expressions by magnitude.

ordermagnitudep orders numbers, constants, and constant expressions the same as <, and all other elements the same as orderlessp.

sort sorts a list according to a predicate of two arguments which defines a strict weak order on the elements of the list.

(%i9) sort([1, a, b, 2, 3, c], 'orderlessp);
(%o9)                         [1, 2, 3, a, b, c]
(%i10) sort([1, a, b, 2, 3, c], 'ordergreatp);
(%o10)                        [c, b, a, 3, 2, 1]

The predicate may be specified as the name of a function or binary infix operator, or as a lambda expression. If specified as the name of an operator, the name must be enclosed in double quotes.

(%i11) L : [[1, x], [3, y], [4, w], [2, z]];
(%o11)                 [[1, x], [3, y], [4, w], [2, z]]
(%i12) foo(a, b) := a[1] > b[1];
(%o12)                       foo(a, b) := a  > b
                                           1    1
(%i13) sort(L, 'foo);
(%o13)                 [[4, w], [3, y], [2, z], [1, x]]
(%i14) infix(">>");
(%o14)                                >>
(%i15) a >> b := a[1] > b[1];
(%o15)                        (a >> b) := a  > b
                                           1    1
(%i16) sort(L, ">>");
(%o16)                 [[4, w], [3, y], [2, z], [1, x]]
(%i17) sort(L, lambda([a, b], a[1] > b[1]));
(%o17)                 [[4, w], [3, y], [2, z], [1, x]]

sort(L) is equivalent to sort(L, orderlessp).

(%i18) L : [a, 2*b, -5, 7, 1 + %e, %pi];
(%o18)                   [a, 2 b, - 5, 7, %e + 1, %pi]
(%i19) sort(L);
(%o19)                   [- 5, 7, %e + 1, %pi, a, 2 b]
(%i20) sort(L, 'orderlessp);
(%o20)                   [- 5, 7, %e + 1, %pi, a, 2 b]

The default sorting order is ascending, as determined by orderlessp. The predicate ordergreatp sorts a list in descending order.

(%i21) L : [a, 2*b, -5, 7, 1 + %e, %pi];
(%o21)                   [a, 2 b, - 5, 7, %e + 1, %pi]
(%i22) sort(L);
(%o22)                   [- 5, 7, %e + 1, %pi, a, 2 b]
(%i23) sort(L, 'ordergreatp);
(%o23)                   [2 b, a, %pi, %e + 1, 7, - 5]

All Maxima atoms and expressions are comparable under orderlessp and ordergreatp.

(%i1) L : [11, -17, 29b0, 9*c, 7.55, foo(x, y), -5/2, b + a];
                                                        5
(%o1)         [11, - 17, 2.9b1, 9 c, 7.55, foo(x, y), - -, b + a]
                                                        2
(%i2) sort(L, orderlessp);
                       5
(%o2)         [- 17, - -, 7.55, 11, 2.9b1, b + a, 9 c, foo(x, y)]
                       2
(%i3) sort(L, ordergreatp);
                                                         5
(%o3)         [foo(x, y), 9 c, b + a, 2.9b1, 11, 7.55, - -, - 17]
                                                         2

Operators < and > order numbers, constants, and constant expressions by magnitude. Note that orderlessp and ordergreatp do not order numbers, constants, and constant expressions by magnitude.

(%i4) L : [%pi, 3, 4, %e, %gamma];
(%o4)                       [%pi, 3, 4, %e, %gamma]
(%i5) sort(L, ">");
(%o5)                       [4, %pi, 3, %e, %gamma]
(%i6) sort(L, ordergreatp);
(%o6)                       [%pi, %gamma, %e, 4, 3]

ordermagnitudep orders numbers, constants, and constant expressions the same as <, and all other elements the same as orderlessp.

(%i7) L : [%i, 1+%i, 2*x, minf, inf, %e, sin(1), 0, 1, 2, 3, 1.0, 1.0b0];
(%o7)  [%i, %i + 1, 2 x, minf, inf, %e, sin(1), 0, 1, 2, 3, 1.0, 1.0b0]
(%i8) sort(L, ordermagnitudep);
(%o8)  [minf, 0, sin(1), 1, 1.0, 1.0b0, 2, %e, 3, inf, %i, %i + 1, 2 x]
(%i9) sort(L, orderlessp);
(%o9)  [0, 1, 1.0, 2, 3, %e, %i, %i + 1, inf, minf, sin(1), 1.0b0, 2 x]

Nota mia di cui non so bene la risposta: il predicato si può quotare con ' ma anche no; sembra, da appurare in futuro.

(%i1) L : [3, 5, 7, 1, 2, 4, 6, 8, 0];
(%o1)                     [3, 5, 7, 1, 2, 4, 6, 8, 0]
(%i2) sort(L, orderlessp);
(%o2)                     [0, 1, 2, 3, 4, 5, 6, 7, 8]
(%i3) sort(L, 'orderlessp);
(%o3)                     [0, 1, 2, 3, 4, 5, 6, 7, 8]

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: