# 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]$.

-

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

## 1 Answer

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 );
-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 4, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": false, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.8942129611968994, "perplexity_flag": "middle"}