TEACH REVISE                                           A.Sloman Nov 1988
                                                Last updated 21 Oct 2000


         CONTENTS - (Use ENTER g to access required sections)

 -- Introduction
 -- How to copy this teach file into your directory
 -- Question 1: Logging in and out, files
 -- Question 2: Keys, control keys, keyboard
 -- Question 3: Procedures and files
 -- Question 4: Compile time and run time mishaps
 -- Question 5: More mishaps?
 -- Question 6: Analyse a buggy procedure definition
 -- Question 7: Lengths of lists
 -- Question 8: Boolean expressions
 -- Question 9: Fix and explain a buggy procedure
 -- Question 10: Output locals
 -- Question 11: Program libraries
 -- Question 12: Pop-11 data types
 -- Question 13: Matcher and database utilities
 -- Question 14: Loops
 -- Question 15: vars and lvars
 -- Question 16: Case-sensitive search in Ved
 -- Question 17: Case insensitive search in Ved
 -- Question 18: Searching and substituting with patterns in Ved
 -- Question 19: More on format of files and procedure definitions

-- Introduction -------------------------------------------------------
Below are some questions to help you revise what you have learned in the
first few weeks of the course. Sample answers will be supplied later on.

Whether your tutor requires this or not you should write out answers to
as many questions as you can. It is too easy to look at a question and
think you know the answer when in fact your grasp of the question is
very shallow. Only by writing out an answer and then later comparing
your answer with another (or getting help) will you really learn where
your understanding needs correction.

Feel free to compare your answers with those of other students. In fact,
a very good way to learn is for students first to write answers down
separately, then try to produce better versions working in groups of two
or more.

Feel free to find out the answers by trying things out on the computer.

-- How to copy this teach file into your directory --------------------

You can turn this teach file into a file (owned by you and located in
your own directory) called 'revise.p' by giving the following command,
which "re-names" the file:

    ENTER name revise.p

That is

    Press the ENTER key

    Type: name revise,p

    Press the RETURN key

The file will then be called 'revise.p' in your own directory. It will
be an ordinary VED file. You can subsequently access it by

    ved revise.p              from POP11, or
    ENTER ved revise.p        from inside VED

You can then type your answers into it directly. Alternatively use the
editor to make space for written answers, then print out the file and
write in your answers. You should delete these instructions before
printing it out! (See TEACH * PRINTING)

-- Question 1: Logging in and out, files -------------------------------

(1.a) What does 'logging in' mean?

(1.b) What does 'logging out' mean?

(1.c) What is a directory?

(1.d) What is a file?

(1.e) Why is it recommended that you use names ending with '.p' for
    files containing end Pop-11 program definitions, e.g. 'respond.p'?

    What difference does it make if you don't use such a name, e.g. if
    you call the file just 'respond'?


-- Question 2: Keys, control keys, keyboard ---------------------------

(6) Some keys on the keyboard transmit characters that can be printed,
some modify the signals sent by other keys, and some send their own
signals which are not printing characters. Give an example of each
type of key.

(7) Which key or key sequence, or command is used:

    a. to delete the last character typed in

    b. to delete the whole of the current line

    c. to delete the word to the left of the cursor

    d. to quit a VED file

    e. to compile one line of program

    f. to compile a procedure definition

    g. to compile a marked range

    h. to compile a whole file

    i. To exit from VED/Pop-11

-- Question 3: Procedures and files -----------------------------------

3.1. What are the differences between creating a new procedure and
creating a new file? (There are several differences.)


3.2. What do procedures and files have in common?


3.3. Explain what happens when a procedure definition (e.g. your
procedure RESPOND) is read in by the POP11 system.

    In particular, what happens to the POP11 dictionary?
    (a) that procedure had not previously been compiled in the
        current pop-11 session

    (b) the procedure had been compiled but is being recompiled
    after editing.

3.4. If you want to "tidy" or "justify" a procedure definition (produce
indentation showing the structure of the procedure) what command should
you use?

3.5 For the "compile procedure", or "justify procedure" command to work
in Ved, the word "define" at the beginning of the definition needs to be
in the right place. Where?


-- Question 4: Compile time and run time mishaps -----------------------

What is the difference between a compile time error and a run time
error?

Explain what is wrong with each of the following, and whether it
involves a compile time or a run time error:

    (a)     add [steve is happy];

    (b)     3 -> [x];

    (c)     [3] * 5 =>

    (d)     x -> 10;

    (e)     if x == 3 then x => endwhile;

    (f)     define fred(x, y;
                [silly] =>
            enddefine;


-- Question 5: More mishaps? ------------------------------------------

What do the following print out? Explain the mishap messages (if any)
if you can.

    (a)     1 + 2 * 3 =>

    (b)     vars x; [if] -> x; x =>

    (c)     vars if; [x] -> if; if =>

    (d)     vars x = sqrt(16); x*x =>


-- Question 6: Analyse a buggy procedure definition -------------------

The following procedure is meant to take a list containing a name and produce
as its result a list containing a greeting including the name. What is wrong
with it? (There is more than one thing wrong!)

        define greet([name]) -> x;
            [pleased to meet you name] -> x;
        enddefine;

(Hint: it will produce a compile time error. Why?)


-- Question 7: Lengths of lists ---------------------------------------

TEACH ARROW will help you with this question.

The procedure LENGTH when given a list produces as its result the number of
things in the list. When given a word, LENGTH returns the number of
characters in the word.

What are LENGTHs of the values of the following expressions?

    (a)     [a b c]

    (b)     [abc]

    (c)     [ [a b c] ]

    (d)     [ ]

    (e)     [list6]

    (f)     [6list]

    (g)     ["a"]

    (h)     [ % "a" % ]

    (i)     After the instruction [a b c] -> list;
            what are the lengths of these lists:
                [^^list ^^list]
                [^^list ^list]
                [^list ^list]
                [[^^list]]
                [[^^list] ^list]
                [list]


For the remaining questions you need to know that TL takes a list as input
and gives as its result a list containing all but the first element. HD takes
a list as input and gives the first element as its result.

What is the result of applying LENGTH to the result of each of the following:

    (j)     tl([a b c d])

    (k)     hd([cat on mat])

    (l)     hd([ [a b c] ])

    (m)     hd(tl([a b c]))

    (n)     tl([ [a b c] ])

-- Question 8: Boolean expressions ------------------------------------

Which of the following are true?

    (a)     1 + 2 * 3 > 1 * 2 + 3;

    (b)     [this list] = [this list]

    (c)     [this list] == [this list]

    (d)     [this list] matches [that list]

    (e)     [this list] matches [??that list]

    (f)     [1 + 2] matches [3]

    (g)     [ % 1 + 2 % ] matches [3]

    (h)     [ % 1 + 2 % ] == [3]

    (i) [A B] <> rev(tl([C D E])) matches rev([E D C] <> tl([A B]))

    (j) [A B] <> rev(tl([C D E])) matches rev([D E] <> tl([C B A]))

    (k) "x" = x

    (l) isword(x)

    (m) isword([])

    (n) isword([x])

    (o) isword(hd([x]))

    (p) isinteger("three")

    (q) isinteger(33 + 55)

    (r) isinteger(33 * 55)

    (s) isinteger(33 / 55)


-- Question 9: Fix and explain a buggy procedure ----------------------

Explain what is wrong with the following procedure definition,, correct
it and say what it then does.


    (a)     define smaller(numa, numb) -> ans;
              if numa > numb then
                  numb -> ans
              else
                  numa
            enddefine;

Now correct these tests:

    (b)     smaller(5, 6) =>

    (c)     smaller(10 6) =>

    (d)     smaller(4;4) =>

-- Question 10: Output locals -----------------------------------------

Q. What is an 'output local' variable?

Q. Explain the meaning of '-> zzz' in

    define xxx(yyy) -> zzz;

(See TEACH STACK, TEACH DEFINE)

Q. What happens if nothing inside the procedure definition assigns any
value to the output local (e.g. to zzz)?

Q. Suppose you have a procedure which takes a number as input and
returns three other numbers, its square, its cube and its fourth power,
defined with this header:

    define powers_of(x) -> (xsquare, xcube, xfourth);

If you run the procedure powers_of with a number such as 99 as input and
you want the results to be assigned to the variables p, q and r, where p
gets the squqre, q gets the cube, and r gets the fourth power, how would
you express this, i.e. complete the following

    vars p, q, r;
    powers_of(99) ->


Q. Try completing the definition of powers_of, and test your answer to
the previous question.






-- Question 11: Program libraries -------------------------------------

11.a What is the difference between these two Pop-11 commands.

    lib river
    uses river

11.b Which VED command can be used to examine a library file?

11.c What is the difference between an autoloadable library and a
     non-autoloadable library?

11.d What does the "ENTER sourcefile xxx" command do?
     (See HELP SOURCEFILE)

11.e. Why should the "uses" and "lib" commands appear at the beginning
of a file, not in the middle or at the end?


11.d. What is wrong with the following:


    define river_solve();

        lib river;

        start();
        .......
        .......
    enddefine;


-- Question 12: Pop-11 data types -------------------------------------

12.1. Try writing down from memory a list of all the different types of
objects a POP11 program can refer to.

Start with: words, numbers, ....


12.2. What are the different kinds of numbers in Pop-11. (See HELP MATH)


12.3. The Pop-11 concatenator <> (infix operator) can be used to
concatenate several different types of pop-11 objects, e.g. two lists,
two strings. Which others?


-- Question 13: Matcher and database utilities ------------------------

Explain what the following do.

    matches

    add

    remove

    present

    lookup

    flush

    -->

HINT:
They are all procedures: say what sorts of inputs they take, what sorts
of outputs they produce (if any) and what the relationship between input
and output is, and what side effects they have. If the procedure name is
an infix operator say so.

Which of the above procedures make use of MATCHES?

See HELP MATCHES, HELP DATABASE

Harder: Explain what "!" does when used before a pattern.


-- Question 14: Loops -------------------------------------------------

14.1. If you want a program to loop forever, obeying a sequence of
instructions, which words should go before and after the instructions?

14.2 Which instruction can be used to make Pop-11 jump to the next
instruction after the end of a loop?

14.3 Which instruction makes Pop-11 jump to the end of a procedure?

14.4 Explain the difference between the following, assuming "x" has
previousy been declared as a variable.

    for x in [a b c d e] x=> endfor;

    for x on [a b c d e] x=> endfor;

-- Question 15: vars and lvars ----------------------------------------

15.1 What is the difference between vars and lvars?

15.2 What sort of bug is avoided by making local variables in a
procedure lvars rather than vars variables?

15.3 What difference does "!" make to a pattern used with the matcher?

15.4 In the following procedure definition

    define silly(w, x, y) -> z;
        w * x * y -> z
    enddefine;

Some variables are automatically declared. Which are they? Are they
declared as "vars" or as "lvars".
Which one is the "output local" variable?

    See TEACH VARS_AND_LVARS

15.5 What kind of mistake can produce a compile time warning message of
the form:
;;; WARNING DECLARING PATTERN VARIABLE person AS LVARS


-- Question 16: Case-sensitive search in Ved --------------------------

16.1 How do you get Ved to search forward for "enddefine"

16.2 How do you get Ved to search backwards for "enddefine"

16.3 How do you get Ved to search forward for the same string as last
time, without re-typing the string?

16.4 How do you get Ved to search backward for the same string as last
time, without re-typing the string?

16.5 How do you get Ved to search forward for the word "the" in such
a way that it does not find the embedded occurrence in "these"?
(Non-embedded search)

16.6 How do you get Ved to search backward for the word "the" in such
a way that it does not find the embedded occurrence in "these"?

-- Question 17: Case insensitive search in Ved ------------------------

17.1 How do you get Ved to search forward for the next occurrence of
"the" or "The". I.e. you want a case insensitive search for a string
containing those letters in any combination of upper and lower case?

17.2 How do you get Ved to search backward for the next occurrence of
"the" or "The". I.e. you want a case insensitive search backwards.

17.3 How do you do a case insensitive search forwards again for the
last case insensitive search string you used.

17.4 How do you do a case insensitive search backwards again for the
last case insensitive search string you used.

17.5 How do you do a non-embedded case insensitive forward search, e.g.
you want to find "the" or "The" but not "these" or "These"?

17.6 How do you do a non-embedded case insensitive backward search, e.g.
you want to find "the" or "The" but not "these" or "These"?

For more on case insensistive search see HELP VED_SS


-- Question 18: Searching and substituting with patterns in Ved -------

VED allows you to search for strings that match a pattern, instead of
having to specify the exact string. The full variety of patterns that
can be used is quite complex, and makes use of what computer scientists
know as "regular expressions". If you are familiar with regular
expressions you can read TEACH * REGEXP and REF * REGEXP

Most users get by with a subset of pattern types, illustrated by the
following questions.

18.1 How do you search for 'define' at the beginning of a line, so that
it does not find 'define' in this line, but does find this:
define


18.2 How do you search for 'then' where it is the last thing on a
line?

18.3 How do you interactively search for occurrences of 'this' and
replace them with 'that' ?

18.4 How do you replace all occurrences of 'result' in a procedure with
'output'?

See TEACH * VEDSEARCH for more on these topics. Also REF * VEDSEARCH

For still more sophisticated searching facilities see
    TEACH * REGEXP
    REF   * REGEXP





-- Question 19: More on format of files and procedure definitions -----

19.1 Why should test instructions be put inside comments?
e.g.

/*

riv_setup([man fox chicken boat],[grain],[]);
database ==>

*/


19.2 In which order should the following occur in a file

a. test instructions
b. procedure definitions
c. procedure headers
d. file header
e. commands to load libraries
f. commands to load other files used by this one
g. comments
h. global variable declarations



19.3 How can you prepare an index of procedure definitions in a file?
See HELP VED_INDEXIFY


19.4 How can you search for a procedure called foo in the current file?


19.5 How can you search for the next procedure after the current one?


19.6 How can you search backwards for the procedure before the current
one?



--- $poplocal/local/teach/revise
--- Copyright University of Birmingham 2000. All rights reserved. ------