Funcs.

  1. A func is an ISETL value that may be applied to zero or more values passed to it as arguments. It then returns a value specified by the definition of the func. Because it is a value, an ISETL func can be assigned to an identifier, passed as an argument, etc. Evaluation of an ISETL func can have side-effects determined by the statements in the definition of the func. Thus, it also serves the purpose of what is often called a procedure.

  2. A func is the computational representation of a function, as a map is the ordered pair representation, and a tuple is the sequence representation. Just as tuples and maps may be modified at a point by assignment, so can funcs. However, if the value at a point is structured, you may not modify that at a point as well.

    
         x := func(i); return char(i); end; 
    
    x(97) := "b";
    x(97)(1) := "abc";

    x

    may be modified at a point. The assignment to

    x(97)

    is legal. However, the following assignment is not supported at this time, because you are trying to modify the structure of the value returned.

  3. A number of functions have been pre-defined as funcs in ISETL. A list of their definitions is given later in this document. These are not keywords and may be changed by the user. They may not be modified at a point, however.

  4. It is possible for the user to define her/his own func. This is done with the following syntax,

    
        func(list-of-parameters);
    
    local list-of-local-ids;
    value list-of-global-ids;
    statements;
    end

    1. The declaration of local-ids may be omitted if no locals are needed. The declaration of global-ids represents global variables whose current values are to be remembered and used at the time of function invocation; these may be omitted if not needed. The list-of-parameters may be empty, but the pair of parentheses must be present.

    2. Parameters and local-ids are local to the func. See below for a discussion of scope.

    3. The syntax described above is for an expression of type func. As with any expression, it has an existence as a value, but no name. Thus, the definition will typically be part of an assignment statement, or passed as a parameter. As a very simple example, consider:

      
       cube_ plus := func(x,y);
      
      return x**3 + y;
      end;

      After having executed this input, ISETL will evaluate an expression such as

      cube$\displaystyle \_$ plus(2, 5);

      as 13.

    4. Parameters are passed by value. If there are too many arguments, the extra arguments are ignored. If there are too few arguments, the extra parameters are assigned the value

      OM

      .

    5. Scope is lexical (static) with retention. Lexical means that references to global variables are determined by where the func was created, not by where it will be evaluated. Retention means that even if the scope that created the func has been exited, its variables persist and can be used by the func.

      By default, references to global variables will use the value of the variable at the time the function is invoked. The

      value

      declaration causes the value of the global variable at the time the func is created to be used.

    6. Here is a more complicated example of the use of func. As defined below,

      compose

      takes two functions as arguments and creates their functional composition. The functions can be any ISETL values that may be applied to a single argument; e.g. func, tuple, smap.

      
       compose := func (f,g); 
      
      return func (x);
      return f(g(x));
      end;
      end;
      twice := func (a);
      return 2*a;
      end;
      times4 := compose(twice,twice);

      Then the value of

      times4(3)

      would be 12. The value of

      times4

      needs to refer to the values of

      f

      and

      g

      , and they remain accessible to

      times4

      , even though

      compose

      has returned.

    7. Finally, here are some examples of functions modified at a point, and functions that capture the current value of a global.

      
       f := func (x);        
      
      return x + 4;
      end ;
      gs := [ func(x); value N; return x+3*N; end :
      N in [1..3] ];
      f(3) := 21;

      After this is executed,

      f (1)

      is 5,

      f (2)

      is 6, but

      f (3)

      is 21.

      gs(2)(4)

      is 10 (

      4 + 3*2

      ).