home *** CD-ROM | disk | FTP | other *** search
- g := func (x);
- return x * x - 5 * x + 6;
- end func;
-
- prime := func (x);
- return forall k in [2..x-1] | x mod k /= 0;
- end func;
-
- set_of_primes := func (n);
- return { x : x in [2..n] | prime (x) };
- end func;
-
- inverse := func (r, a);
- return { [x, y] : x,y in a | [y, x] in r};
- end func;
-
- rand := func (n);
- return arb ({1..n});
- end func;
-
- toss := func (n);
- return [ rand(6) + rand(6) : k in [1..n] ];
- end func;
-
- sample := func (n, p);
- return [ arb(p) : k in [1..n] ];
- end func;
-
- nodups := func (t);
- return #t = #{ k : k in t };
- end func;
-
- squares := { x*x : x in {1..20} | x mod 5 = 0};
-
- t := [2, 3, 5, 2, 3];
-
- u := [1..20];
-
- primes50 := set_of_primes (50);
-
- primes100 := set_of_primes (100);
-
- a1 := {1..5};
- r1 := { [1,1], [1,2], [2,3], [3,2], [2,1] };
- r2 := r1 + { [1,4] };
-
- results := toss (36);
-
- tup := [2, 3, 4, 2, 4];
-
- set := { k : k in tup };
-
- tup2 := [2, 4, 7, 6];
-
- answer := func (n);
- if n = 1 then
- return " {[x,x+2]:x in primes100 | x+2 in primes100}; ";
- elseif n = 2 then
- print " sym_closure := func (r, a); ";
- print " return r + inverse (r, a); ";
- return " end func; ";
- elseif n = 3 then
- return " not nodups (sample (35, {1..365})); ";
- else
- return " Enter answer(n), where n in {1..3}. ";
- end if;
- end func;
-
- $ Example of a func which accepts two funcs as input and returns
- $ a func.
-
- compose := func (g, f);
- local h;
- h := func (x);
- return g(f(x));
- end func; $ This completes the definition of h.
- return h;
- end func; $ This completes the definition of compose.
-
- $ Alternatively we could have defined compose as follows:
- $ compose := func (g, f);
- $ return func (x);
- $ return g(f(x);
- $ end func;
- $ end func;
-
- $ Example of use of compose.
-
- add1 := func (x);
- return x + 1;
- end func;
-
- cube := func (x);
- return x*x*x;
- end func;
-
- new_func := compose (cube, add1);
-
- $ new_func (x) = cube (add1 (x)) = (x + 1) ** 3
-