> | # lets talk a bit more about lists:
# at fist, here is a range, which is just a sequence: |
> | 2..3;
|
> | 1..10; |
> | 10..1 |
Warning, inserted missing semicolon at end of statement, 10..1;
> | # hmm, we cannot have a "downward range" |
> | # this is how we create a sequence: |
> | s := seq(i,i=1..10); |
> | # same thing, but now as a list |
> | s := [seq(i,i=1..10)]; |
> | # we cannot have a range counting downwards.
# for this we seed to use lists. |
> |
|
> | # the following piece of code produces a list whose elements are decreasing. |
> | [seq(5+1-i,i=1..5)];
|
> | # yet another way
|
> | ListTools[Reverse]([seq(i,i=1..5)]);
|
> | # alternatively, we could have loaded the package
# by using with(listtools); |
> | # let's use the map command.
# The map command applies a function to all elements of a list. # For example, we may increase each element in the list by one # as follows (here we use the function i -> i+1, I hope # you still remember this notation) |
> | map( i -> i+1, [seq(i,i=1..5)]); |
> | # We may also write the function as a procedure,
# using the proc command: |
> | map( proc(a) return a + 1; end, [seq(i,i=1..5)]);
|
> | # a procedure always starts with proc()
# in the parenthesis, we may put one or more arguments (or parameter) # for the procedure as input. # the procedure body goes right to the command end which closes the procedure. # we may have a return command inside the procedure, # then the procedure is treated as a function, # namely the function which returns the value from the return command. # So, in this case, we have a procedure which takes as arguments # an integer a and computes a + 1 as return value. # That's all folks! |
> | # Let's say we want to add some quantity to all emntries of the list,
# but we want to pass the value of that quantity as a parameter # to the procedure. # This is how we do it, we just put that parameter as an additional # argument to the map command. |
> | map( proc(a,n) return a+n; end, [seq(i,i=1..5)], 3); |
> | # Here 3 is the additional parameter which gets passed as n to out procedure.
# In this case, we just want to add 3 to all list elements. |
> |
> | # OK, now we have another way of creating a list with the numbers downwards |
> | map( proc(a,n) return n + 1 - a; end, [seq(i,i=1..5)], 5); |
> |
> | with(StringTools):
Warning, the assiged name Group now has a global binding |
Error, missing operator or `;`
> | Map( Capitalize, "This is a test." );
|
> | Map( LowerCase, %);
|
> | convert("Test string", 'bytes');
|
> | convert([65,66,67], 'bytes');
|
> | convert("abcdefghijklmnopqrstuvwxyz", 'bytes');
|
> | convert("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 'bytes');
|
> | convert(" -=!@#$%^&*()_", 'bytes');
|
> | convert(%,'bytes');
|
> |
> |
> | # HOMEWORK |
> | # hw 1:
|
> | # Create a list with the numbers n, n-2, n-4, n-6,...2 for any given even n
# a) using a for loop with appending to a list # b) using the seq command # c) using seq and map command using a function (i -> something) # d) using seq and map command using a procedure |
> |
|
> | # hw 2: |
> | # what is the following procedure doing?
|
> | par := proc(n,k)
local i; if n=0 then return([[]]) fi; map( proc(i,n) op(map( proc(a,b) [b,op(a)]: end, par(n - i,i), i)); end, ListTools[Reverse]([ seq(i, i = 1 .. min(n,k)) ]), n); end: |
> |
> | # hw 3
# a) Create a 8 by 8 matrix whose (i,j) th entry is the result of par(i,j). # b) Create a 8 by 8 matrix while (i,j) th entry is the length of the list # in the (i,j) th entry of the matrix from a). |
> |
> | # hw 4
# write some code which takes as input a string and # which reverses the order of the characters. # i.e. "hallo" should become "ollah" |
> |
> |