Definitions

Definitions have the following forms.

def x = E end

This definition indicates that x names the value of expression E.

def A B = E end

This is equivalent to

  def A = B -> E end.
This only makes sense if B is an identifier, but it does not require A to be an identifier. You can use this rule more than once.

For example, you can perform conversions.

def x y z = a end
  ⇒ def x y = z -> a end
  ⇒ def x = y -> z -> a end
  = def x = y -> (z -> a) end

The last step follows from the fact that -> associates to the right.


Examples of definitions

A number

Definition

  def twenty = 20 end

defines identifier twenty to name number 20.


A squaring function

Function sqr returns the square of its parameter. You can define it by

  def sqr = x -> x*x end
or by
  def sqr x = x*x end


Maximum

  def max x y =
    case 
        x > y => x
      | else  => y
    end
  end


List concatenation

The following is a definition of the list concatenation function, cat. For example, expression (cat [2,4] [6,8]) yields result [2,4,6,8].

   def cat x y =
     case
       isNull x => y
     | else => head x : cat (tail x) y
     end
   end

After doing replacements, the definition of cat is equivalent to the following.

   def cat =
    x ->
     y ->
      case
        isNull x => y
      | else     => head x : cat (tail x) y
      end
   end

You can understand cat by looking at an evaluation by substitution. Recall that [a,b] = a:b:[ ]. It is necessary to add parentheses to ensure that precedence rules do not change the structure of expressions.

 cat(a:b:[], c:d:[]) 
   = a : cat(b:[]) (c:d:[])
   = a : b : cat([]) (c:d:[])
   = a : b : c : d : []


Function composition

Here is a function composition function. (compose f g) is a function which, when applied to x, produces f(g(x)).

   def compose f g x = f(g x)
   end

You can use function composition to build a function.

   def 
     fourthPower x = (compose sqr sqr) x
   end

You can also create a function in a more direct way.

   def 
     fourthPower = (compose sqr sqr)
   end

An action

Function showSquare, below, ignores its parameter. It yields an action that, when run, will read an integer and print the square of that integer.

  def showSquare a =
    readInt ~> (x -> print (x*x))
  end

Hello

Identifier main names an action that, when run, prints

Hello

  def main =
    printList ['H','e','l','l','o','\n'] 
  end