Programs and Actions

A program is a sequence of one or more definitions, one after another.

When a program is run, the language implementation evaluates the definition of main.

If main yields an action then the language implementation performs that action.

If main yields a result r that is not an action then the implementation prints the result, as if the result had been action print r.

Use the following rules for printing.

  1. To print a number, show it in its usual (decimal) form. Note that it might be negative. (There are no negative integer constants, but the program can compute negative numbers.)

  2. Show true as true and false as false.

  3. Show a character as it would appear as a character constant in a program. For example, show character 'x' as 'x'.

  4. Show the empty list as [ ]. Show list x:y by showing x, then writing a colon, then showing y.

  5. Show a function simply as, literally, (a function).

  6. If the answer is an action, perform the action. In general, an action can do something and has a result. Actions are performed as follows.

    readChar

    Read a character from the standard input and return that character.

    readInt

    Read an integer from the standard input and return that integer.

    produce v

    Just return v.

    print v

    Print value v. If v is an action, just print (an action). Return 0.

    printList x

    Expression x must yields a list. Print the members of list x, one after another. Do not show brackets or commas. If a member of x is a character, just print the character, without quote marks. Return 0.

    A ~> B

    Perform action A, getting result r. B must be a function. Apply that function to r, getting result b, which must be an action or it is an error. Run that action. The result returned by B is the result of A ~> B.


A sample program

   def main =
     cat a b     
   end

   def a = [2, 4, 6] end
   def b = [8, 10] end

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

prints

 2:4:6:8:10:[]


Another sample program

  def main = 
    printList ['H','o','w',' ',
               'm','a','n','y',' ',
               's','e','c','o',
               'n','d','s','?'];
    readInt ~> 
     (n ->
      let m = n/60 in
       let h = n/3600 in
        let secs = n - 60*m in
         let mins = m - 60*h in
          printList 
           [h, ':', mins, ':', secs]
         end
        end
       end
      end
     )
  end

reads a number then prints it as hours, minutes, seconds. For example, if it reads 1000 then it prints

0:16:40