# lets talk a bit more about lists: # at fist, here is a range, which is just a sequence:2..3; NiM7IiIjIiIk1..10;NiM7IiIiIiM110..1Warning, inserted missing semicolon at end of statement, 10..1;NiM7IiM1IiIi# hmm, we cannot have a "downward range"# this is how we create a sequence:s := seq(i,i=1..10);NiM+SSJzRzYiNiwiIiIiIiMiIiQiIiUiIiYiIiciIigiIikiIioiIzU=# same thing, but now as a lists := [seq(i,i=1..10)];NiM+SSJzRzYiNywiIiIiIiMiIiQiIiUiIiYiIiciIigiIikiIioiIzU=# 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)]; NiM3JyIiJiIiJSIiJCIiIyIiIg==# yet another way ListTools[Reverse]([seq(i,i=1..5)]); NiM3JyIiJiIiJSIiJCIiIyIiIg==# 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)]);NiM3JyIiIyIiJCIiJSIiJiIiJw==# 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)]); NiM3JyIiIyIiJCIiJSIiJiIiJw==# 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);NiM3JyIiJSIiJiIiJyIiKCIiKQ==# 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 downwardsmap( proc(a,n) return n + 1 - a; end, [seq(i,i=1..5)], 5);NiM3JyIiJiIiJSIiJCIiIyIiIg==with(StringTools): Warning, the assiged name Group now has a global bindingError, missing operator or `;`Map( Capitalize, "This is a test." ); NiNRMFRISVN+SVN+QX5URVNULjYiMap( LowerCase, %); NiNRMHRoaXN+aXN+YX50ZXN0LjYiconvert("Test string", 'bytes'); NiM3LSIjJSkiJCwiIiQ6IiIkOyIiI0tGJkYnIiQ5IiIkMCIiJDUiIiQuIg==convert([65,66,67], 'bytes'); NiNRJEFCQzYiconvert("abcdefghijklmnopqrstuvwxyz", 'bytes'); NiM3PCIjKCoiIykqIiMqKiIkKyIiJCwiIiQtIiIkLiIiJC8iIiQwIiIkMSIiJDIiIiQzIiIkNCIiJDUiIiQ2IiIkNyIiJDgiIiQ5IiIkOiIiJDsiIiQ8IiIkPSIiJD4iIiQ/IiIkQCIiJEEiconvert("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 'bytes'); NiM3PCIjbCIjbSIjbiIjbyIjcCIjcSIjciIjcyIjdCIjdSIjdiIjdyIjeCIjeSIjeiIjISkiIyIpIiMjKSIjJCkiIyUpIiMmKSIjJykiIygpIiMpKSIjKikiIyEqconvert(" -=!@#$%^&*()_", 'bytes'); NiM3MCIjSyIjWCIjaCIjTCIjayIjTiIjTyIjUCIjJSoiI1EiI1UiI1MiI1QiIyYqconvert(%,'bytes'); NiNRL34tPSFAIyQlXiYqKClfNiI=# 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"