?he 'POPSUMMARY''Page %'
.fo 'Aaron Sloman'%'Dec 1977'


.ce 2
PROGRAMMING IN POP11 : BRIEF SUMMARY
----------- -- ----- - ----- -------

Arithmetic:
.br
----------

   The following operations are available
   +	add two numbers
   -	subtract two numbers, or negate one number.
   *	multiply two numbers
   /	divide first number by second
   //	divide first number by second, produce
      	remainder and quotient.

   >	test two numbers: TRUE if first greater than second.
   <	test two numbers: TRUE if first less than second.
   >=	test two numbers: TRUE if first greater than or
      	equal to second.
   =<	test two numbers: TRUE if first less than or
      	equal to second.

   =	test any two objects. TRUE if they are identical.


Print-arrow, for printing out results
.br
-----------
   =>

   Examples:

 	: 33+66 =>	
 	  I.e.: print sum of 33 and 66.
.tp6
 	: 234+22  >  33*66  =>
 	  I.e.: is 234 bigger than 33 times 66?
 	  If so, print out 1(for true) or 0(for false).

 	: X+5 < Y =>
 	  I.e.:  is the sum of X and 5 less than Y?
 	  X and Y should have numbers as values.

 	: 99+5 = 95+9  =>	
 	  I.e.:  is 99+5 equal to 95+9?
 	  If so, print 1 otherwise 0.

 	: X = Y  =>		
 	  I.e.:  does X have the same value as Y?

 	: X =>		
 	  I.e.:  print out the value of X.


.tp6
Declaring variables:
.br
--------- ---------

   Type VARS followed by variables then semi-colon.
 	Example:
 	: VARS LIST1 LIST2 X Y Z;
 	  I.e.: this declares five variables.


Assignments.
.br
-----------

   ->	assign to, or "goes to"

   E.g. - to give the variable X the value 33 do:

 	: 33 -> X;

   to give the variable Y twice the value of X do:

 	: X + X -> Y;
or
 	: (X + X) -> Y;

   to assign a list of numbers to LIST1 do something like:

 	: [ 1 2 3 4 5 ]  -> LIST1;

   To assign a list of words to LIST2:

 	: [CAT DOG MOUSE ELEPHANT] -> LIST2;

   To assign a list like LIST1 but in reverse order to X, do:

 	: REV(LIST1)  ->X;
 	: X  =>		
 	  I.e.: now print out value of X.


Defining functions.
.br
-------- ---------

Type FUNCTION 
.br
then <name of function>
.br
then <formal parameters in parentheses, separated by commas>
.br
then semi-colon
.br
then type action to be performed
 	(may include conditionals, loops, etc.)
.br
then type END;

   Example - a function, named DOUBLESUM which takes two numbers, and
      produces a new number got by doubling the sum of the two.

 	: FUNCTION DOUBLESUM (NUM1, NUM2);
 	:  2 * (NUM1 + NUM2)
 	: END;

   Another example - a function named SQUARE which will take a number,
      called SIDE in the function, and will draw a square of the
      appropriate size:
.tp10

 	: FUNCTION SQUARE (SIDE);
 	:  REPEAT 4 TIMES
 	:	DRAW(SIDE);
 	:	TURN(90);
 	:  CLOSE;
 	: END;


Executing (calling, running, applying) a previously defined function.
.br
---------

 	: SQUARE(5);	
 	  I.e.: execute the function square with
 	  5 as argument. i.e. side gets the value 5.

 	: DOUBLESUM(3,99) =>	
 	  I.e.: execute the function doublesum,
 	  with 3 and 99 as arguments. I.e NUM1
      	  gets the value 3 and NUM2 the value 99.
      	  the result is printed out by =>


Loops: instructions to do something repeatedly.
.br
-----
 1.
   UNTIL <condition> THEN <action> CLOSE;
      This means, check if the <condition> is true, and if not
      keep on repeating the <action> until it becomes true.
      for example, to print out all the numbers from 3 to 99 do:

 	: VARS NUM;
 	: 3 -> NUM;
 	: UNTIL	NUM > 99
 	: THEN	PR(NUM):
 	:	NUM + 1 -> NUM;
 	: CLOSE;

      To print out the words "THE" "CAT" "SAT" "ON" "THE" "MAT",
      make a list of the words, then keep printing out elements
      of the list and chopping one off until the list is [],
      thus:

 	: VARS LIST;
 	: [THE CAT SAT ON THE MAT ]  -> LIST;
 	: UNTIL	LIST = []
 	: THEN	PR(HD(LIST));	
 	  I.e.: print head of list;
 	:	TL(LIST) -> LIST;
 	: CLOSE;

 2.
   REPEAT <number> TIMES <action> CLOSE;
      After the word REPEAT you can have a number, e.g. 4,
      or a variable whose value is anumber, e.g. REPEAT N TIMES...
      or a more complex expression which calculates a number,
      e.g. REPEAT 66+53 TIMES....
      The <action> will be done the specified number of times.
      Example: to print out  10 blank lines, do

 	      : REPEAT 10 TIMES PR(NEWLINE) CLOSE;

      See also the definition of function SQUARE, above.

 3.
   LOOPIF <condition> THEN <action>  CLOSE;
      This means, keep on doing the <action> over
      and over again, so long as the condition remains TRUE.
      Example, to print  out positive integers from 66 in descending
      order do:

.tp6
 	: VARS NUM;
 	: 66 -> NUM;
 	: LOOPIF	NUM > 0
 	: THEN		PR(NUM);
 	:		NUM - 1 -> NUM;
 	: CLOSE;

 4.
   FOR <init> STEP <step> TILL <condition> THEN
      <action>
   CLOSE;

     This is equivalent to:
      <init>;

      UNTIL <condition> THEN <action>; <step> CLOSE;

     In other words, do the initialisation, then, until the condition
     evaluates to TRUE, repeatedly do the action followed by the step.
     For example, to print out all the numbers from LO to HI:

 	: FOR	LO->X
 	: STEP	X+1 ->X
 	: TILL	X > HI
 	: THEN	PR(X)
 	: CLOSE;

or,
to apply the function FOO to every element of the list [A B C
D];

 	: FOR [A B C D]	-> L
 	: STEP	TL(L) -> L
 	: TILL	L=[]
 	: THEN	FOO(HD(L))
 	: CLOSE;


.tp6
Conditionals. These can have several forms:
.br
------------
  (1)	IF <condition> THEN <action> CLOSE;

  (2)	IF <condition> THEN <action1> ELSE <action2> CLOSE;
     if the <condition> is true, then <action1> will be executed,
     otherwise <action2> will be executed.

  (3)	IF <condition1> THEN <action1>
   ElSEIF <condition2> THEN <action2>
   ELSEIF <condition3> THEN <action3>
      .............
      .............
   ELSE <finalaction>
   CLOSE;
      This says try <condition1> then <condition2> etc in turn until
      one is found which is TRUE, in which case execute the
      corresponding <action>.  If none of the conditions comes out
      TRUE then do the thing following ELSE, i.e. 
      <finalaction>
      Note that you dont have to have the ELSE <finalaction> bit. See.
      (1).
       You can include as many ELSEIF clauses as you like.

  (4)	UNLESS <condition> THEN  <action> CLOSE;
       this is equivalent to:
       IF NOT(<condition>) THEN <action> CLOSE;

   Note: the words NOT, AND and OR are available for use in
   formulating complex conditions.

   Examples:
     To test whether the value of X is bigger than the value of Y,
     and print out the bigger value do:

 	: IF	X > Y
 	: THEN	X =>
 	: ELSE	Y =>
 	: CLOSE;

     What will the following do?

 	: IF		X > Y
 	: THEN		X =>
 	: ELSEIF	Y > X
 	: THEN		Y =>
 	: ELSE		"SAME" =>
 	: CLOSE;

 	: IF	2 < X AND X < 6
 	: THEN	TRUE
 	: ELSE	FALSE
 	: CLOSE;

      If LIST1 and LIST2 are two lists and you want to print out
       the one which is shorter you could do:
.tp4

 	: IF	LENGTH(LIST1) < LENGTH(LIST2)
 	: THEN	LIST1 =>
 	: ELSE	LIST2 =>
 	: CLOSE;

      If N and M are two numbers, and you wish to assign the bigger
       one to the variable MAX then do:

 	: IF	M > N
 	: THEN	M
 	: ELSE	N
 	: CLOSE -> MAX;

Now try using these ideas. 


Tracing
.br
-------

To trace some functions, type TRACE, followed by the
names of the functions.  example:

 	: TRACE SQUARE DOUBLESUM;

will cause the two functions to be traced whenever they are
executed.



See also the following demos:
SYNTAX, SYSVARS, LISTSUMMARY.

