1.7 Various Other Patterns

1.7.1 Control Patterns

Betaenv contains three predefined control patterns: forTo, cycle and loop. They are respectively defined as:

forTo: (* for inx in [min,max] do inner *)
  (# min,max,inx: @integer
  enter (min,max) 
  do ... inner;
     ...
  #);

cycle: (* executes inner forever *)
  (# do l:(#... inner; ...#) #);

loop: (* control pattern for while- and repeat-loop *)
  (# while:< booleanValue(# do ... inner; ... #);
     until:< booleanValue(# do ... inner; ... #);
  do ... inner; ...
  #)

Recall that control patterns are procedure patterns that are to be used as superpattern:s. The first example illustrates the use of forTo in an inserted item:

do ...    
   (3,17) -> forTo(# do inx*inx -> putint; newline #);
   ...

It will cause printing of the values 32, 42, ..., 172. The next example illustrates the use of cycle in the definition of another control pattern.

countCycle: cycle 
  (* increments inx and executes inner forever *)
  (# inx: @integer
  do inx + 1 -> inx;
  inner
  #);

Finally, the following loop example reads a sequence of integers from standard input until either a non-positive integer is read or the sum of integers exceeds 1000:

loop(# while::< (# do getint->i; i>0->value #);
       until::< (# do sum>1000->value #);
       sum, i: @integer
    do i+sum->sum
    #)

1.7.2 Input/Output Patterns

1.7.2.1 Screen and keyboard

Standard input/output is available through dynamic references to objects that are instances of the pattern stream (see later). These streams are automatically opened. Abbreviations for the most often used input/output operations are defined (e.g. put for screen.put and get for keyboard.get).

(# j: @integer
do (for i: 5 repeat
        '? '-> puttext; getint -> j;
        j*j -> putint; newline
   for) 
#)

1.7.2.2 Single character input - iget.bet

The iget.bet library implements immediate character input (i.e. non-buffered input). This is through the iget operation, which returns a single character. iget returns as soon as a character is typed.

1.7.3 Command Line Arguments

It is possible to let BETA programs access the command line arguments through the noOfArguments and arguments patterns. NoOfArguments returns the number of text atoms on the command line, including the program name. The text atoms are numbered from 1 to noOfArguments. The program name is obtained by 1 -> arguments, the first argument is obtained by 2 -> arguments, etc.

The following example displays the number of arguments of a command line followed by the arguments.

(# 
do 'This program was called with ' -> puttext;
   noOfArguments -> putint; ' argument(s):' -> putline;
   (for i: noOfArguments repeat
        i -> arguments -> puttext; ' ' -> put;
   for); 
   newline;
#)

1.7.4 Object Pool

The objectPool is for keeping track of unique instances of patterns. A call of the form

objectPool.get(# type::< T #) -> obj[]

will return an instance of T. The first call will create an instance of T. Subsequent calls will return this instance again. The objectPool is useful in systems where many fragments must refer to the same unique instance of a pattern T. ObjectPool also defines a scan operation which may be used for scanning the objects in the pool. For more operations, please consult the interface descriptions later.

1.7.5 Concurrency - SystemEnv

The current version of the Mjølner System includes an experimental implementation of concurrency. The complete environment for concurrency is defined in the systemEnv library, described in a later chapter. However, some concurrency facilities are necessarily defined in the betaenv library. Please refer to the later chapter on the systemEnv library for details.

1.7.6 External Language Interface

External and cStruct

Betaenv contains the shortInt, external and cStruct patterns for interfacing into facilities written in other languages, such as C and Pascal. Please refer to the later chapter on the external library for details.


Basic Libraries - Reference Manual
© 1990-2004 Mjølner Informatics
[Modified: Friday April 6th 2001 at 12:43]