11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

22 • Chapter 2: Procedures, Variables, and Extending Maple> element(s, c);FAIL> element(s, c);[α, x]Example 4Instead of writing a new procedure for each Cartesian product you study,you can write a procedure, CartesianProduct, that returns such a procedure.CartesianProduct creates a list, s, of its arguments, which mustbe sets, and then initializes the array, c, of counters and defines the subprocedureelement. Finally, the element subprocedure is invoked insidea proc structure.> CartesianProduct := proc()> local s, c, element;> s := [args];> if not type(s, list(set)) then> error "expected a sequence of sets, but received",> args ;> end if;> c := array( [0, 1$(nops(s)-1)] );>> element := proc(s::list(set), c::array(1, nonnegint))> local i, j;> for i to nops(s) do> c[i] := c[i] + 1;> if c[i] return [ seq(s[j][c[j]], j=1..nops(s)) ] ;> end if;> c[i] := 1;> end do;> c[1] := 0;> FAIL;> end proc;>> proc()> element(s, c);> end proc;> end proc:Again, you can find all six elements of {α, β, γ} × {x, y}.> f := CartesianProduct( {alpha, beta, gamma}, {x,y} );

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!