home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / proglang / isetl.arj / MATH.STL < prev    next >
Encoding:
Text File  |  1989-10-05  |  2.2 KB  |  101 lines

  1.  g := func (x);
  2.      return x * x - 5 * x + 6;
  3.  end func;
  4.  
  5.  prime := func (x);
  6.      return forall k in [2..x-1] | x mod k /= 0;
  7.  end func;
  8.  
  9.  set_of_primes := func (n);
  10.      return { x : x in [2..n] | prime (x) };
  11.  end func;
  12.  
  13.  inverse := func (r, a);
  14.      return { [x, y] : x,y in a | [y, x] in r};
  15.  end func;
  16.  
  17.  rand := func (n);
  18.      return arb ({1..n});
  19.  end func;
  20.  
  21.  toss := func (n);
  22.      return [ rand(6) + rand(6) : k in [1..n] ];
  23.  end func;
  24.  
  25.  sample := func (n, p);
  26.      return [ arb(p) : k in [1..n] ]; 
  27.  end func;
  28.  
  29.  nodups := func (t);
  30.      return #t = #{ k : k in t };
  31.  end func;
  32.  
  33.  squares := { x*x : x in {1..20} | x mod 5 = 0};
  34.  
  35.  t := [2, 3, 5, 2, 3];
  36.  
  37.  u := [1..20];
  38.  
  39.  primes50 := set_of_primes (50);
  40.  
  41.  primes100 := set_of_primes (100);
  42.  
  43.  a1 := {1..5};
  44.  r1 := { [1,1], [1,2], [2,3], [3,2], [2,1] };
  45.  r2 := r1 + { [1,4] };
  46.  
  47.  results := toss (36);
  48.  
  49.  tup := [2, 3, 4, 2, 4];
  50.  
  51.  set := { k : k in tup };
  52.  
  53.  tup2 := [2, 4, 7, 6];
  54.  
  55.  answer := func (n);
  56.      if n = 1 then
  57.          return "       {[x,x+2]:x in primes100 | x+2 in primes100};      ";
  58.      elseif n = 2 then
  59.          print  "       sym_closure := func (r, a);                       ";
  60.          print  "           return r + inverse (r, a);                    ";
  61.          return "       end func;                                         ";
  62.      elseif n = 3 then
  63.          return "       not nodups (sample (35, {1..365}));               ";
  64.      else
  65.          return "       Enter answer(n), where n in {1..3}.               ";
  66.      end if;
  67.  end func;
  68.        
  69. $ Example of a func which accepts two funcs as input and returns
  70. $ a func.
  71.  
  72. compose := func (g, f);
  73.     local h;
  74.     h := func (x);
  75.         return g(f(x));
  76.     end func;  $ This completes the definition of h.
  77.     return h;
  78. end func;      $ This completes the definition of compose.
  79.  
  80. $  Alternatively we could have defined compose as follows:
  81. $  compose := func (g, f);
  82. $      return func (x);
  83. $          return g(f(x);
  84. $      end func;
  85. $  end func;
  86.  
  87. $ Example of use of compose.
  88.  
  89. add1 := func (x);
  90.     return x + 1;
  91. end func;
  92.  
  93. cube := func (x);
  94.     return x*x*x;
  95. end func;
  96.  
  97. new_func := compose (cube, add1);
  98.  
  99. $ new_func (x) = cube (add1 (x)) = (x + 1) ** 3
  100.  
  101.