11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.1 Syntax and Semantics • 57modules) and procedure parameters are the same as the rules for nestedprocedures.Parameterized Modules Modules do not take explicit parameters. Youcan write a generic module that could be specialized by providing one ormore parameters.For example, here is a module for arithmetic modulo 6.> z6 := module()> export add, mul;> add := ( a, b ) -> a + b mod 6;> mul := ( a, b) -> a * b mod 6;> end module:> z6:-add( 5, 4);3> z6:-mul( 2, 3);0You can write a generic module for arithmetic modulo any positiveinteger n, and then specialize it for any integer that you need. This ispossible as a result of the standard lexical scoping rules. You must writea constructor procedure for the module that accepts the value of n asan argument. Here is a generic version of the z6 example.> MakeZn := proc( n::posint )> module()> export add, mul;> add := ( a, b ) -> a + b mod n;> mul := ( a, b ) -> a * b mod n;> end module> end proc:To generate a module that does arithmetic modulo 7, call the constructorMakeZn with the number 7 as its argument.> z7 := MakeZn( 7 );z7 := module() export add , mul ; end module> z7:-add( 3, 4 );0

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

Saved successfully!

Ooh no, something went wrong!