# Using functions in Maple

How do approach this problem?

Write a function points which takes a list L of non negative integers and returns a list of pairs $[x, y]$ where $1 ≤ x ≤$ the length of $L$ and $1 ≤ y ≤ L[x]$.

-
Why ask in a math forum instead of in a programming forum? – GEdgar Oct 15 '12 at 1:54

This is easily done as a nested loop, ie. one loop within another, where the one or both of the end-points of the inner loop will depend upon the current value of the outer loop.

You can use a pair of for-do loops, but then you have to be creative about where to store the list [x,y] that gets generated each time through the inner loop (or be inefficient in memory and repeatedly concatenate with a list of previous pairs). It's easier and efficient to accumulate instead an expression sequence of all the [x,y] pairs using a pair of nested calls to the seq command rather than a pair of for-do loops.

The inner loop can determine y, since that loop has to run from 1 to L[x] where a particular x would be known. The outer loop can determine x, since the restrictions on x don't involve y.

The length of a list L is obtained with the command nops(L).

Experiment with a pair of nested seq calls like the following, to get an idea of how they can form a longer sequence of results. Your goal is to nest such a pair in the right order and to find the particular ranges i=a..b and j=c..d where some of the a,b,c,d of the inner seq may depend upon i or j of the outer seq.

seq( seq( [i,j], i=4..j ), j=1..6 );

Notice that if the right end-point of a range is less that the left end-point then that range is empty: it produces nothing. This comes in handy. For example, the following produces NULL.

seq( i, i=4..2 );
-