0% found this document useful (0 votes)
27 views116 pages

On The Road To Artificial Intelligence - B - B - C - Micro

The document is a book titled 'On the Road to Artificial Intelligence: BBC Micro' by Jeremy Vine, which introduces readers to interactive programming using the BASIC language, focusing on creating programs that simulate human-like conversations. It emphasizes the potential of home computers in artificial intelligence and provides guidance for beginners in programming. The book includes practical examples and techniques for manipulating text and developing interactive applications.

Uploaded by

Jayme Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views116 pages

On The Road To Artificial Intelligence - B - B - C - Micro

The document is a book titled 'On the Road to Artificial Intelligence: BBC Micro' by Jeremy Vine, which introduces readers to interactive programming using the BASIC language, focusing on creating programs that simulate human-like conversations. It emphasizes the potential of home computers in artificial intelligence and provides guidance for beginners in programming. The book includes practical examples and techniques for manipulating text and developing interactive applications.

Uploaded by

Jayme Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 116

ig? ths.

car fa)
2. sa! enyacpap
Jeet api io; 8) = -
Wee
rR TY Ut oc P e ariad

oe
SS

=> 7
" \S =
*
On the Road to
Artificial
Intelligence:
BBC Micro

TELEPEN
00382268
DEDICATION

For Mum and Dad

‘Progress, therefore, is not an accident, but a necessity.


... [tis a part of nature.’

Herbert Spencer (1820-1903)


On the Road to
Artificial
Intelligence:
BBC Micro

Jeremy Vine
BRENT LIBRARY SERVICE

i
a,-}
SHIVA
Shiva Publishing Limited
SHIVA PUBLISHING LIMITED
64 Welsh Row, Nantwich, Cheshire CW5 5ES, England

© Jeremy Vine, 1984

ISBN 1 85014 045 6

All rights reserved. No part of this publication may be reproduced,


stored in a retrieval system, or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording and/or
otherwise, without the prior written permission of the Publishers.

This book is sold subject to the Standard Conditions of Sale of Net


Books and may not be resold in the UK below the net price given by
the Publishers in their current price list.

An interface was used to produce this book from a microcomputer


disc, which ensures direct reproduction of error-free program
listings.

Typeset by Wordsmith Graphics Limited


Printed by Devon Print Group, Exeter
Contents

Preface
Can Machines Think?
Creating an Interactive Program
Strings and Things
More Strings Attached!
Words, Words, Words
Always the Unexpected!
Text Handling
Sigmund: An Interactive Program
Sigmund: The Program
Interviewer
Brainstorming
Artificially Intelligent?
Appendix A: For users of BASIC 1
Appendix B: A crash course in BASIC
Appendix C: BBC BASIC keyword summary
Digitized by the Internet Archive
in 2022 with funding from
Kahle/Austin Foundation

https://archive.org/details/onroadtoartificiO000vine
Preface

Computers are rapidly becoming a part of everyday life. The


advance in silicon technology has moved at such a pace that we
now stare in the face what was not long ago complete science
fiction. Machines are now being developed that can behave with
spine-chilling accuracy the way a human does, in thought and
knowledge. Along with that movement has appeared one of the
largest growth industries: home micros. To some people they are
mere toys but the power within a home micro is great and the
ability to create programs that mimic human thought processes
are attainable.
Interactive programming concentrates on a certain aspect of
the BASIC language, namely the commands that are responsible
for handling information. The book teaches how to use these
commands and combine them into programs that communicate
with the user. The reader is also introduced to the expanding field
of artificial intelligence, one of the most exciting areas of
computer science.
Little has been assumed about the programming abilities of the
reader. It is written for those with almost no knowledge of
BASIC, and only assumes the reader has played around with a
few rudimentary commands like PRINT and INPUT. However, if
you have no experience of programming at all, I have included a
crash course in BASIC at the end of the book. In itself the book is
an introduction to BASIC but unlike other books it sets out to
introduce the notion of artificial intelligence and teach the user
how to write programs that interact with the user. Two simulation
programs are described that turn the BBC Micro into a
companion with whom to hold an intelligent two-way conversa-
tion and behave in a manner similar to, and with the same faults
as, a human.
With a little work you will be writing programs that will give
hours of pleasure as well as start you thinking on the limits to
which you can stretch your BBC Micro.For BASIC I users an
1
appendix has been included to cover the few changes necessary to
correct the bug in the INSTR command which is present in BASIC
1. All the programs have been tried and tested on both versions
of BASIC.
To finish on a personal note I must acknowledge with gratitude
the help, encouragement and support I have received from both
Bruce Smith and Kitty Milne, and to thank my family for
tolerating my sometimes intolerable behaviour and the strange
noises emanating from my bedroom in the early hours of the
morning from my BBC. To them, and to countless others who
have enthused my interest in this area, this book is partly due.

London, 1984 Jeremy Vine

About the author

Jeremy Vine was educated at William Ellis Grammar School,


Highgate, London, and at the City of London Polytechnic, where
he achieved a BSc in Psychology. Jeremy first became involved
with computers while studying for his degree and has never since
been far away from one. When Jeremy left the polytechnic, he
worked freelance for Acorn User before joining Acorn Compu-
ters Limited. During this period, he also studied for an MSc in
Neurophysiology. Now, having left Acorn, Jeremy is working
full-time as a freelance writer. As well as his books for Shiva, he
is a regular contributor to several computing magazines, which
has brought him a consultant editorship. His spare-time interests
are tennis, piano, photography and, of course, home-computing.

Nw
1 Can Machines Think?

Science fiction has a way of becoming fact and as it was


impossible to believe, only a few decades ago, that man would
ever step on the moon, so too is it incredible to think about
machines that can answer back and act intelligently. Yet the day
of artificial intelligence becoming reality is dawning and it may
not be long before these words can be written by a computer.
However, before you start to think that your BBC can act like
any human being, I should point out that artificial intelligence on
home computers is not advanced enough. But that is no reason
not to go as far as you can.
The purpose of this book is threefold. Firstly, to show you a
variety of commands in the BASIC programming language and
teach you how to write programs that deal solely with text and the
manipulation of text input by the user. Very little knowledge of
BASIC is assumed but if you don’t feel confident enough to tackle
Chapter 3, where we start programming, then turn to Appendix
B where I have written a lightning guide to BASIC and first
principles. Secondly, a number of techniques are shown which
you can incorporate in your own programs, to make them more
interactive and thirdly, to build up to writing programs that
enable the user to freely communicate with the computer. What
do I mean by freely communicate? Well, take a look at the
following interaction between a human and a computer:

COMPUTER: NICE TO MEET YOU JEREMY


TELL ME WHY YOU WANT TO TALK TO ME
HUMAN: I HAVE TERRIBLE DREAMS
COMPUTER: DO YOUR DREAMS INVOLVE OTHER
PEORLE?
HUMAN: YES MY SISTER
COMPUTER: THIS SOUNDS LIKE A COMPLICATED
RELATIONSHIP
HUMAN: I'MVERY DEPRESSED OVER THIS

COMPUTER: HOW OFTEN ARE YOU DEPRESSED?

HUMAN: ALMOST ALL THE TIME


COMPUTER: I SEE
HUMAN: AND I DREAM OFTEN

COMPUTER: CAN'T I1
I ENVY YOU... DREAM

HUMAN: WOULD I BE WRONG IN THINKING


ABOUT HER
COMPUTER: I WOULD IF I WAS IN YOUR PLACE
HUMAN: WOULD YOU SAY IT'S ABNORMAL
COMPUTER: I'MNOT TO SAY WHAT I
PREPARED
THINK
HUMAN: YOU DON'T THINK ANYWAY. YOU'RE
JUST A MACHINE
COMPUTER: DO MACHIN ES
WORRY YOU?
HUMAN: NO OF COURSE NOT. COMPUTERS
ARE MINDLESS
COMPUTER: AS A COMPUTER I TAKEA DIFFERENT
VIEW

That conversation didn’t take place on a huge mainframe


computer but on the BBC computer. It is an example of what can
be achieved even on home micros and we will write the program
to do this later in the book. Now you might be thinking that we
already have an intelligent machine if it can hold a conversation
like the one above. Well, feeling like a magician who has dropped
a card from his sleeve, I have to admit it is all a trick. The
program does indeed allow the user to freely communicate with
the computer, and the computer does make intelligent replies but
does that mean the computer is thinking?
By the end of this book I hope to have started you thinking
about whether machines can think or not, which is a bone of
contention within the research area of artificial intelligence. A
definition of AI is difficult because of the degree of argument and
controversy surrounding the field. Perhaps it is purely to describe
any action performed by a computer that previously could only be
carried out by a human being. Or it may describe the processes a
machine is using to carry out a task, like playing chess.

I leave you with that problem to think over and as we progress


through the book, and learn the different ways of communicating
with the computer and it in turn communicating with us, try to
decide for yourself to what degree, if any, your BBC is a thinking
machine.
2 Creating an Interactive
Program

Creating an interactive program is a combination of factors. It


starts as an idea and progresses through many stages until the
program is completed to the satisfaction of the writer. The
planning behind a program is as important as the way a program
is written and sorting out problems at an early stage makes for
easier development. This chapter considers the general principles
to be borne in mind before writing a program and in particular
interactive programs.
Interactive programming is simply writing programs that
interact with the user. The emphasis is on using BASIC to write
programs that enable the user to freely communicate with the
computer and, in later chapters, we will see how to use a wide
range of commands in the BASIC language which will eventually
make our BBC have the appearance of a thinking, intelligent
being. What we will try to achieve is to teach our computer to
think.
In creating a program there will always be an idea or an aim
from which the program will spring. One should think carefully
exactly what the aims of the program are and the way in which it
will work. What do you want the user to see on the screen
display? What do you allow him to type in? You must ask
yourself many questions about the program if you are to get it
right.
For instance, we will see how to account for as many of the
possible things a user of a program might do to mess up the
works. So what we effect is a way of writing into the program, in
advance of its use, procedural methods to take care of the worst
thing a computer has to face. And what is that worst thing? The
user! The computer has no way of knowing or understanding
what the user desires, and is to all intents and purposes blind to
the world. Computers are mindless (though don’t say that to
Sigmund later or he will get upset!) and have to be fed
information.

6
Information is the life blood of a machine and computers need
to be fed a healthy diet of raw data. This book concentrates on
one side of programming, that is, the manipulation of text, both
material held by the computer and text input by the user. You
may already be acquainted with a few BASIC commands and
able to INPUT and PRINT information but there is much more that
you can accomplish with text. BASIC offers a range of commands
enabling the programmer to manipulate and juggle text around to
create the impression of a thinking machine. Points to bear in
mind when writing a program are to:

1. Use flowcharts to aid the planning of a program. (There are a


number of flowcharts in the book which help to understand the
way in which a program works.)

2. Plan what the user will see on the screen.

3. Ease of use. Try to make the program as easy as possible to


use.

4. Error trapping. Ensure that unwanted errors don’t affect the


smooth running of a program.

If=> any of these points make little sense at the moment, don’t
worry. We will cover them as we progress and by the end of the
book these should be principles that will come straight into your
head when writing a program.
Always remember that we are dealing with an empty box. Our
job is to fill that box up and to make it an Aladdin’s cave to
anyone who looks inside. But if we are to be creative and write
interactive programs we must do some programming. So let’s get
down to it straight away. Happy interactions!
3 Strings and Things

I’ve already said that we’ve got to teach our BBC to think and the
first step in that process is user input. In this chapter, and the
next, we will see how the computer accepts information and then
manipulates it to our requirements. Let’s remind ourselves of
what is meant by a variable.

Program 3.1
1@ INPUT name$, age

2@ PRINT name$, age

Program 3.1 contains two variables: a string variable, denoted by


$(dollar) on the end of a variable name, called ‘name$’, and a
numeric variable ‘age’. When the program is run, in reply to the
first prompt *?’, we can type in alphanumeric characters—in other
words, type in anything we like—and this reply is stored in
‘name$’.
On pressing the RETURN key we are faced with another question
mark. This time the BBC is expecting a number to be entered and
this value is stored in ‘age’. Line 20 prints out the contents of
‘name$’ and then the contents of ‘age’. You have no doubt
guessed that the program is asking for your name and age. Well,
that is far from clear, as all we can see when the program is run is
two question marks! So, let’s tidy the program up:

Program 3.2

10 INPUT "Name: ", name$; "Age: ", age

2@ PRINT names: "UilS: "=" aqge= Se VERRSrODs

Run the program and you can see that the prompts are more
explicit and we can now direct the user to input his name and age.

8
Now rerun the program but this time enter your name at both
prompts. If we do this, it can be seen that the machine will store
the first input correctly, as it is a string input, i.e. any character.
However, the second variable ‘age’ requires a numeric input and
therefore a value of 0 remains in variable ‘age’ as the input is not
numeric.

Having reminded ourselves briefly on variables, it is time to play


around with strings!

STRING MANIPULATION 1: The use of LEFT$

As one of the main aims of this book is to concentrate on text


input and the manipulation of that text by the computer, let us
take a look at some commands in BBC BASIC that enable us to
play around with strings. But first of all, consider the following
problem—I want to write a program that prints out the letters of
my name, increasing by one letter on each line as follows:

J
JE
JER
JERE
JEREM
JEREMY

Well we could try and write a program, such as the one below:

Program 3.3
OPP RUN Teco
CURE LN Ue Ee
SOPPRUN Taw ER.
40 PRINT "JERE"
SO SPRINT “JEREM”
60 PRINT "JEREMY"

But we are not going to get very far if we have to type in


everything for the computer. The problem with Program 3.3 is
that we are typing everything that appears on the screen—not
exactly a time saving way of writing the program. Of course,
there is a quicker way and BBC BASIC provides us with the
9
commands to help us in this situation.
What we need is a way of reading any given string and printing
it out a letter at a time, increasing by one character on each line,
as in the example above, without all the PRINT statements. ‘How
do I do that?’ I hear you ask. The answer is simple. We use the
command LEFT$ .
The next program uses LEFT$ to print out the first character of
the string entered by the user. Type it in and see:

Program 3.4
10 INPUT "Enter a word", word$
2@ PRINT LEFTS$(word$,1)

Try changing the number 1 in line 20 to other values to see what


happens. By changing this value, the number of characters
printed, from left to right, of the word will alter accordingly.
Have a look at Figure 3.1.

word$ = COMPUTER Screen display

PRINT LEFT$(word$,1) ————


PRINT LEFT$(word$,2) ————>
PRINT LEFT$(word$,3) ————
PRINT LEFT$(word$,4) ————>
PRINT LEFT$(word$,5) ————
PRINT LEFT$(word$,6) ————
PRINT LEFT$(word$,7) ————>
PRINT LEFT$(word$,8) ————>

Figure 3.1 Effect of LEFT$.

Our next job is to produce the same effect without having to type
a PRINT statement for each line. Type in Program 3.5:

Program 3.5
10 phrase$ "THE QUICK BROWN FOX"
2@ counter = 1
3@ PRINT LEFTS$(phrase$,counter)
4@ counter = counter + 1: GOTO 30

Run the program and ...?! The problem lies in the fact that we
increase the value of ‘counter’ every time and when we exceed
the number of characters in the string we just carry on printing
‘phrase$’. Change line 40 to read:

4@ counter = counter + 1: IF counter > 19


THENEND ELSE 30

At last the desired effect! Or is it? There is still a problem. In


order to stop the program when all the characters have been
printed, I’ve had to count the number of characters in ‘phrase$’
(including the spaces), thus the reason for ‘IF counter > 19° in
line 40.

However, we are not always going to be in a situation where we


know the number of characters in the string. Once more BBC
BASIC comes to the rescue with a command to let the computer
know the length of a string.

STRING MANIPULATION 2: LEN

The LEN statement gives such information. In Program 3.6 we use


LEN to give us the amount of characters entered into entry$. Try
entering one word at first and then re-run the program and enter
a short sentence.

Program 3.6
1@ INPUT "Enter word or phrase",
entry$
2@ PRINT "The number of characters inthe
string is "; LEN(entry$)

If you entered a few words you would have noticed that LEN
counts the spaces as well as any other characters. Spaces are
counted in this instance, and it is worth bearing in mind as we will
take a look later on, in greater detail, at spaces between words.
However, back to LEN for the time being.
In Program 3.5 our problem was not knowing the number of

11
characters in the string. Let us now rewrite that program using
LEN:

Program 3.7
1@ INPUT phrase$
20 FOR counter = 1 TO LEN(phrase$)
30 PRINT LEFTS$(phrase$,counter)
40 NEXT

You will notice that I have introduced a FOR-NEXT loop into the
program. If you have not used a FOR-NEXT statement yet, a look
at the user manual will explain the uses of this command. In brief,
the loop is set from the first character of phrase$ to the total
length of the string, this being determined by LEN in line 20.
Not only is Program 3.7 more efficient than Program 3.5, but it
is also faster. This can be quite important in the larger programs
where much processing is being carried out. But more of that
later. Time now to introduce a couple more BASIC statements.

STRING MANIPULATION 3: RIGHT$ and MID$

If you’ve understood the use of LEFT$, the next command is very


easy. RIGHTS is, as its name implies, a string handling command
and if you haven’t guessed by now, carries out the same function
as LEFT$, except in reverse. Well, not quite the opposite! Let’s
take a look at Program 3.4 again except this time changing LEFT$
to RIGHTS in line 20:

Program 3.8
1@ INPUT "Enter a word", word$
20 PRINT RIGHTS$(word$,1)

RIGHTS doesn’t quite work as you might think. With a value of 1


in line 20, RIGHT$ gives the last character in the string. However,
try changing that value. The result produced is not back to front.
Not clear? Have a look at Figure 3.2.
The reason I said it might not be clear is that one might expect
RIGHT$(word$,3) to produce YME, in other words print the
string starting from right to left but as you can see from Figure 3.2
this is not the case. If you’re still not certain about the use of

12
RIGHT$, use Programs 3.4, 3.5 and 3.7, changing LEFT$ to
RIGHT$ wherever it occurs.

word$=JEREMY

Screen display

PRINT RIGHT$(word$,1)
PRINT RIGHT$(word$,2)
PRINT RIGHT$(word$,3)

PRINT RIGHT$(word$,4)

PRINT RIGHT$(word$,5)
PRINT RIGHT$(word$,6)

Figure 3.2 Effect of RIGHTS.

Now let us take a look at MID$. This, like LEFT$ and RIGHTS, is
a string function but unlike those functions has three arguments
associated with itself, i.e. MID$(exam$,X,Y). X and Y are two of
the arguments and I will explain each in turn. Let us suppose that
exam$ contains the string INTERACTION. This is how MID$ works:

exam $= INTERACTION INTERACTION

MID$ (exam$, 6, Y) Xx

MID$ (exam$, X, 6) YYYYYY

ACTION

Figure 3.3 Function of MID$.


The first argument is the same as LEFT$ and RIGHTS and gives the
name of the string variable to be worked on. In our example this
is exam$. The second argument acts as a pointer to the starting
position. Therefore the value of six means that the string we are
going to produce starts at that position, in this case at the letter

Finally, the third argument (shown as Y) gives the number of


characters to be taken from the position of X (inclusive of the
character at position X). So in Figure 3.3, the first six letters from
position X will be taken (as it happens there are only six
characters left in that string). Let’s put this into a program:

Program 3.9
1B2CLS
20 exam$ = "INTERACTION"
30 INPUT "Set pointer to where? (i1.e.X)", x
40 INPUT "Number of characters fromthat
DOSTUIVON. sy
50 PRINT MID$(exam$,x,y)

Run the program entering different values and see how the values
of ‘x’ and ‘y’ alter the word printed out in line 50. (Notice that I
have used both capital and lower case for x and y, to show that it
doesn’t matter which form you choose for the data. In fact, you
could combine the two and it would still be accepted.) The MID$
function along with the other string functions I have mentioned so
far are extremely useful, and I will return to them later on. Let’s,
however, take a break from string manipulation and take a look
at another area of user input.

MENUS!

No, I’m not thinking of food! I mentioned in Chapter 2 that an


important part of an interactive program is the screen presenta-
tion. An integral part of such displays is often a menu of options
for the user to choose from, so why don’t we start writing the
options menu for our next program. Type the program in,
keeping to the line numbers shown, as we are going to build up a
program in a few stages.

14
Program 3.16a: MENU procedure
5@ DEFPROCmenu
6@ CLS
70 PRINT TAB(10,5); CHR$129; CHR$141;
WAS Glee CODES:
80 PRINT TAB(10,6); CHR$129; CHR$141;
PASCLIBCODES |
9@ PRINT TAB(1,10); CHRS$131; STeASCLI= code
to character"

100 PRINT TAB(1,15); CHR$134;


WD.

Character.to ASCIL code”


LAGSPRINI, JABG1T, 2) = CHRS 15.05 TENGE ray OUR
choice"

120 A$ = GETS
AS07A = VALAS: DRSAG 1°0R A*Se2 THENST 20
140 PRINT A
150 ON A GOTO 30, 40
16@ ENDPROC

Also add lines 10 and 20 as follows:

10 MODE 7

20 PROCmenu

Run the program. As the program is‘not complete you will get a
NO SUCH LINE error message if you press either 1 or 2. Run the
program a few times and note how it only responds to the two
numbers shown. Type in any other number and it is ignored. For
the moment don’t worry about how the program works; I'll be
explaining how this procedure and the rest of the program works
in the remaining part of this chapter and Chapter 4.

One thing you couldn’t have failed to notice was the strange title
on the screen—ASCII CODES. Who or what is ASCII? Let’s find
out.
ASCII

ASCII (pronounced AS-KEY!) are letters that stand for Amer-


ican Standard Code for Information Interchange. Try saying that
every time. No wonder it is shortened! But what does all that
mean?
As you may have discovered by now, every computer is
different and BASIC, for example, has many versions im-
plemented and all of them have differences. One of the few
standards that is adopted by most computer manufacturers is the
use of the ASCII character set. ASCII is a code used by the
computer to represent characters and control codes. For our
purposes we will concentrate on the keyboard character set. If
you haven’t already done so, SAVE the menu program and then
type in Program 3.11.

Program 3.11
1@ FOR ascii = 32 TO 126
20 PRINT CHR$(ascii);
30 NEXT

The program produces the characters represented by the ASCII


value. You might have noticed that I have used a BASIC
keyword CHR$ which I introduced in Program 3.1@a. To explain
what CHR$ is doing, LOAD in Program 3.1@a again and let’s add the
next part of the program.

Program 3.16b
240 DEFPROCasc
250 CLS: INPUT TAB(O,4); "Enter ASCII code
number and press RETURN", code

COO SPRINT ss codes aise thems Climcode


Lottie C HRS. CCode) ann
270 PRINT TAB(4,20); "Press SPACE BAR to
continue"

28@ REPEAT UNTIL GET = 32


290 ENDPROC
Also add line 30:

3@ PROCasc

Now SAVE the program and RUN. You will be asked to enter an
ASCII code number. Type in any number between 32 and 126
(these are the codes that represent the character set). The
conversion from ASCII number to a character is being carried out
by the CHR$ command in line 260.
The purpose of CHR$ is to produce a character from a given
number. This number relates to the ASCII value of that
character, i.e. if you enter 65 the answer will be A, because the
ASCII value 65 is the code for the character A.
OK. What about converting a character to an ASCII number.
This can be done by using the ASC function. This is used in the
final section of our program. Make sure you have the program as
it stands LOADed into the BBC before continuing:

Program 3.16c
170 DEFPROCcode
180 CLS: PRINT TAB(O,4); “Enter character"

TOO 7ASa—= GETS

200 PRINT'"The ASCII code for '"; A$;


use Sew ASG CAD)
21@ PRINT TAB(4,cO); “Press SPACE BAR to
continue"
220 REPEAT UNTIL GET = 32
230 ENDPROC

Add line 40:

4@ PROCcode: PROCmenu

and change line 30 to:

30 PROCasc: PROCmenu

The program is now complete. SAVE it and RUN. Choose option 2


and enter any character. Line 200 carries out the conversion of
the character which is held in the string A$. ASC(A$) gives the
ASCII value of the character held in A$. You can see this working
by just typing in the following line:

PRINT ASC("J")

By altering the character a different code will be produced.


What if A$ contains more than one character? Well, only the
value of the first character of that string will be returned. You are
probably wondering whether I have forgotten to explain the other
elements of the program. Don’t panic! The remaining commands
will be explained in the next chapter but for the moment here is a
flowchart of the workings of the program.

See if you can spot what is wrong with the program. Remember
what I said in Chapter 2 about having to account for all possible
input by the user. I will come back to this program in Chapter 6
which is about error trapping. I think you have enough clues for
now!
PROCMENU

Valid
option?

yes

option 2

PROCasc PROCcode

Enter Enter
a value character

PRINT
character

Figure 3.4 Flowchart for Program 3.10.

19
4 More Strings Attached!

An important aspect of writing an interactive program is to


ensure that the computer reads all of the user’s input. Now you
might think that we have already covered this point by using the
INPUT statement but this is not so. To see what I mean enter
Program 4.1:

Program 4.1
TORCES
20 PROCtext: INPUT firstphrase$
30 PROCtext: INPUTLINE secondphrase$
AQ PRINI*
4!" firstphrases = 2":
firstphrases;= |
50 PRINT''™secondphrases = '":
secondphrase$; "'"
90 END
100 DEFPROCtext
110 PRINT type inwthestolvowingss. (My
COMPUTER DOESN'T UNDERSTAND PUNCTUATION"
12@ ENDPROC

Run the program and you can see that both variables contain the
same phrase that we input. However, run the program again and
this time insert a comma in the middle of the sentence like this:

MY COMPUTER, DOESN'T UNDERSTAND PUNCTUATION

See the difference? When we use INPUT in line 20 the variable


will quite happily retain the entire contents of the string as long as

20)
there is no comma in the sentence. But when a comma is inserted,
INPUT will accept only what is typed in up to the comma. To get
round this problem BBC BASIC provides the command INPUT-
LINE which will accept everything that is typed in. I have used
INPUTLINE in line 30 of the program and when the program is
run, it can be seen how everything you type in, regardless of
commas, will be retained.
INPUTLINE not only accepts commas, it will also recognize
leading spaces. By this I mean if you type in a number of spaces
before your sentence, INPUTLINE will include those leading
spaces in secondphrase$, whereas INPUT will not. Try typing in a
sentence with leading spaces to see the difference.
But why all this fuss anyway? Well, in developing our
interactive programs we want to allow the user to communicate
freely with the computer. To do this the user must be free to type
in anything he likes and, as is most likely, he will type in a
sentence which could contain a comma. If the computer is to
analyse the input from the keyboard it must be able to look at
everything that has been typed and as we have seen with the
INPUT statement this is not always the case. INPUTLINE ensures
that everything typed in is stored in a given string.

RECOGNIZING A FAMILIAR WORD: The use of INSTR

We now come to a problem in creating an interactive program—


that of recognizing a word within a long stream of other
characters. INPUTLINE may ensure that every word typed in is
what is stored, but how do we start to recognize a particular word
within a sentence? It is not as difficult as it might first sound. As
you by now have come to expect a BBC BASIC command is
available to solve the problem. The command is INSTR (you can
think of it as ‘in string’).
What INSTR does is to search a string, in this case a long
sentence that we have input, for a familiar word. When it has
found that word (or group of characters) INSTR returns the
position of the leftmost character of the word within the string.
To demonstrate this type in Program 4.2. (If you have BASIC
1, refer to Appendix A for amendments. )

Program 4.2
10 MODE 7

2@ INPUTLINE "Enter your sentence"', A$


3@ BS = "BASIC"
4@ FIND = INSTR(AS$,BS)
SQ PRINT''"The value of variable
UND eee UND
60 PRINT VYOUSTYPED <5" (ORS
70 IF FIND = 0 THENS 1 cB ELSES?
8@ PRINT SPC(FIND - 2); CHR$129; BS
90 PRINT SPCCRINDe =e) pa CHRO
SW can a
100 PRINT CHR$130; "The match was found
here"
11@ END
1ZO0UPRINTSIM
The Sstringe™: BS Was! not
found": END

Now if you type in any old sentence the chances are you will get a
message ‘The string BASIC was not found’. This is because the
program is looking for the occurence of a string, in this case the
alphanumeric string BASIC which is assigned to B$ in line 30.
Run the program again and type in the following:

THAT IS] THE BAS TC *DIRPERENCE

This time you will see the string has been matched up with a word
within the sentence. The value of the variable FIND is the position
at which the start of the word BASIC was found within the
sentence typed. The line of the program that is doing all the work
is line 40. INSTR(A$,B$) returns a value. If a match is found, i.e.
if the variable B$ is found within the string you typed in as, A$,
then the position of the string is returned and the numeric
variable FIND equals that value. However, if no match is found
then a value of 0 is always returned. Line 70 acts on that result
and accordingly prints the appropriate message. See Figure 4.1
for a flowchart of the program.

INSTRis a very useful command and as you will see later on in the
book, it plays a crucial part in picking out key words or phrases
for the computer to understand. More of that later. Now let’s
backtrack slightly and look at a few commands I have used in
programs in this chapter and Chapter 3 without explanation.

2f9)
INPUT A$

B$="BASIC’’

FIND =
INSTR(A$, B$)

PRINT THE
VALUE OF
FIND

PRINT THAT
STRING NOT
FOUND

PRINT WHERE
MATCH WAS
FOUND

Figure 4.1. Recognizing a familiar word.


PRESENTATION AND LAYOUT 1: GET$ and GET

If you ran the menu program in Chapter 3 you might remember


that everything was laid out for the user and, where possible, just
one push of a key was sufficient to work your way through the
program. An important part of any program is the presentation of
material on screen and the ease of input to the user. The more
friendly these points are, the more interactive a program
becomes. I have already used some commands in order to do this
and I will now cover them.
Go back to Program 3.10 and you will notice I used in both
lines 120 and 190 a BASIC keyword GET$. When this function is
used the computer will stop and wait until a key has been pressed
and a record of that key is stored in the opposite variable, i.e:

Program 4.3
10 character$S = GETS

20 PRINT "You typed the character ";


character$

However, you need not make any use of that stored variable as I
have done in line 20 of Program 4.3. The command can also be
used as a means of temporarily stopping a program until any key
is pressed. Try Program 4.4:

Program 4.4
10 CLS
20 PRINTTAB(O,5); "I'm waiting for you
to press a key"
30 a$ = GET$

4Q@ PRINT "About time too!"

Line 10 clears the screen and the program comes to a standstill at


line 30 until a key is pressed. The command GET is similar to GET$
except that the function returns an ASCII number as opposed to
the string of GET$. You can see this by removing the $ signs from
Program 4.3. The number printed by the numeric variable
‘character’ is an ASCII value for the key you pressed.
PRESENTATION AND LAYOUT 2: TAB and SPC

You will notice I have slipped in a command which I have used


before. The command is TAB. TAB can be used with either the
PRINT statement or INPUT. TAB means tabulation and enables
the user to specify exactly where on the screen he wishes the
cursor to be moved to, prior to printing on the screen.
TAB can either have one or two arguments associated with its
use. With two arguments such as those in line 20, TAB will move
the cursor to a certain co-ordinate. Therefore TAB(@,5) moves
the cursor to the fifth line down and to the first column, i.e. @.
The co-ordinates can vary according to the screen mode being
used, but for the purposes of this book I am using Mode 7 as it
requires the least memory and is ideal for text-based programs.
Figure 4.2 shows examples of the PRINT TAB statement and the
positions of the co-ordinates given by the two arguments.

2 10 15

10

PRINT TAB(O,4)"JEREMY”

15

Figure 4.2 Tab positions in Mode 7.


If TAB is used with just one argument, i.e. TAB(8), this will print
spaces to the column specified. In this example it would be the
eighth column. Another way of printing spaces is, to use SPC. I
used it in Program 4.2 to place the correct number of spaces in a
line before printing a word. This ensured that the words lined up
under one another. Go back and run Program 4.2 if you don’t
remember this.
Like TAB, SPC can only be used as part of an INPUT or PRINT
statement. For example:

PRINT SPC(6); "Department"; SPC(8);


"Employee no: "INPUTSPC(4), "User ID", idnum

The number in the bracket, like the single argument of TAB, is the
number of spaces to be printed. SPC and TAB are two useful
functions which enable a quick and well laid out screen to be
constructed.

We have already seen how ASCII works and conversion between


alphanumeric and numeric variables can be important. As you
know, a numeric variable cannot read an alphanumeric input.
However, there is a way of converting a string so that it can be
read.

CONVERTING STRINGS: VAL and STR$

Let us consider a problem. I wish to enter a number using an


alphanumeric input and then have that converted so that a
numeric variable can read and understand the input. Can we do
this? No prizes for guessing that this is possible and Program 4.5
shows how:

Program 4.5

10 a$ = GETS
20 a = VALa$
S0eTF. a <1e0Rsae-) SeTHEN 16
40 PRINT a
5@ ON a GOTO 100, 110, 120, 130, 140

The program is not complete and will therefore crash at line 50.
Line 10 waits for an input and line 20 is where a conversion takes
place. The function VAL (standing for ‘value’) takes a$ which
would contain a number and converts it into a real number which
then becomes the value of variable ‘a’. To ensure that a number
between a certain range has been entered, line 30 checks that the
value of ‘a’ is not outside the specified range. If it is outside the
range, the user is sent back to line 10 until an acceptable number
is entered. Program 4.5 is the basis of the procedure PROCmenu
in Program 3.10a in Chapter 3. VAL therefore converts string
variables into numeric variables and the opposite can be achieved
using STR$. Type in Program 4.6:

Program 4.6
10 number = 1.2
2@ PRINT number * 2
30 aS = STRS$(number)
40 PRINT aS * 2

To prove that the number 1.2 has been converted to a string


variable, line 20 carries out a mathematical function on a numeric
variable and of course, it works. However, when line 40 is
reached, an error message is generated by the computer because
the machine is trying to carry out multiplication on a string
variable.In line 30, STR$ has converted the numeric variable into
a string by placing the converted string in a$.
These functions within BBC BASIC give your programming
more flexiblity, and as I’ve shown you with the menu program,
can be very useful.

OPTIONS WITHIN PROGRAMS: The use of ON

Program 4.5 included an easily missed command, ON. This allows


the order of running a program to be changed, by jumping to a
specified line depending on the value of a certain variable. For
instance, in the menu program, depending on which number is
entered, the program jumps to the appropriate routine. There-
fore, a line like:

ON key GOTO 220, 250, 800

means if the numeric value of ‘key’ is equal to 1 then G0TO line


220; if ‘key’ equals 2 then G0TO line 250 etc. Figure 4.3 illustrates
this.
ON key GOTO 280, 250, 220

key = 1 2 3

Program

Figure 4.3 The ON statement.

Providing options within a program is most common and ON gives


the programmer a way of presenting many options without
resorting to loads of IF-THEN statements. Look at Program 4.7,
but don’t type it in unless you are particularly masochistic!

Program 4.7

10 INPUT "Enter option number", optnum


20 IF optnum = 1 THEN GOTO 100
30 IF optnum = 2 THEN GOTO 24
40 IF optnum = 3 THEN GOTO 310
5@ IF optnum < 1 OR optnum > 3 THEN 10

Just from that short example you can see how much quicker ON is
to use. Another use for ON is error trapping and I will cover this in
Chapter 6.
REPEATING ONESELF:
The use of REPEAT...UNTIL and INKEY

We already know one kind of loop, that being the FOR-NEXT


loop, but there is a second statement which you can use for
situations where you want the instructions repeated, until a
specified condition is met. This is the REPEAT...UNTIL state-
ment and we have already used it in the ASCII Program 3.10.
There we used it to pause indefinitely on a screen display until the
space bar was pressed. The command line for this was:

REPEAT UNTIL GET = 32


This means: ‘repeat around this loop until the key pressed on the
keyboard is the space bar’, which in this case is what 32 means, 32
being the ASCII code for the space bar.
REPEAT...-UNTIL can be used under a variety of other
conditions and another use may be to test for the pressing of a
certain key. To do this we can use REPEAT...UNTIL in
conjunction with either INKEY or INKEY$. Type in Program 4.8:

Program 4.8
1@ REPEAT
20 x$ = INKEYS$(1)
30 IF Xo <> THENG De
40 UNTIL FALSE
50 PRINT "THE KEY "; x$; " WAS PRESSED"

The program loops around until a key is pressed. The function


INKEY$ waits for a specified time, as shown in the brackets, whilst
testing to see if a key has been pressed. If so the character pressed
is placed in x$ and the program continues. In this case INKEY$
loops round indefinitely because it is placed in a REPEAT UNTIL
FALSE loop, which means the loop continues until a value of 0 is
reached, which never happens, and therefore the loop carries on
for an infinite time. The INKEY statement works in a similar
manner except that it places the ASCII value of the key pressed in
a numeric variable.
These functions can be very useful for determining whether or
not a certain key has been pressed and is a good way of ensuring
that only the key you specify is used. Bear this in mind when
reading about error trapping in Chapter 6.

AS)
That concludes our look at manipulating strings and if you have
understood everything in these last two chapters you will be well
on the way to creating programs that interact with the user and
not against them.

30
5 Words, Words, Words

Even with the advent of new communication systems, a growing


area within computer science, the basic form of communication
has remained the same: the written word. Or to be more exact the
on-screen word is still our most precious commodity as regards
communicating between ourselves and with machines. So far in
this book we have concentrated on the entering of information
into the computer and its manipulation of that data. But we have
dealt only with small individual bits of information that have
amounted to very little. We now need to consider the storage of
larger chunks of data which the computer can call on at anytime.

ARRAYS

Imagine that we have to store a record of the finishing positions


of racing car drivers in any given race. We need the computer to
be able to tell us who was at a particular position in the race. Now
how do we go about entering this information? Let’s say we have
the names of the top six drivers to enter. Using what we have
learnt so far we would set up six separate variables to contain the
information. Our program might go something like this:

Program 5.1
10 PRINT "Enter the names of the drivers
‘ite
2@ PRINT "order from 1 to 6"
30 INPUT', num1$
4Q@ INPUT', num2$

31
5@ INPUT', num3$
6@ INPUT', num4$
70 INPUT', num5$
8@ INPUT', num6$
SUS CES
100 INPUT "Which position do you wish to
check", check
118 ON check GOTO 120, 130, 140, 150, 160,
170
120 PRINT "Position1 was: "; num1$:
PROCwait

(30s. PRONT POS 16:9 OnieWas: aa nM e


PROCwait

140 PRINT "Position 3 was : "; num3$:


PROCwait
150 PRINT "Position 4 was :"; num4$:
PROCwait
16@ PRINT "Position5 was: "3; num5$:
PROCwait
170 PRINT "Position6was:"; num6$:
PROCwait
180 DEFPROCwait

190 PRINT''"Press SPACEBAR to continue"


200 REPEAT
UNTIL GET = 32
210 GOTO 90
220 ENDPROC

But this is a very time-consuming exercise. Firstly, I have set up


six Separate variables to contain the names of the drivers. What
would happen if I had a hundred names? And for each response I
have had to set up a separate response line. Telling the computer
which number you need to check is not easy and the only way is to
provide a separate reply for each possible input. Well, I need not
tell you this is futile. So what can be done to ease the pain of
programming?
The answer is to introduce arrays. What is an array? You can
think of an array as a table of information. It’s rather like having

32
an index card with a number of lines of information written on the
card. Look at Figure 5.1.

MICHAEL
DAVID
JOHN
BRUCE
JOE
CHRIS

Figure 5.1 Arrays.

There is a single filing card in Figure 5.1 and the card is labelled
WINNERS. On each line is a position number and next to the
number is the name. In order to let the computer know that we
want to enter up to six names we tell the machine that the table is
up to six items in length. This is done by using the DIM statement.
So for our first line we type:

10 DIMwinner$(6)

We have now set up a variable with a one-dimensional array to a


maximum figure of six. We can now complete the rest of the
program.

Program 5.2
2@ PRINT "Enter the names of the
drivers in"

ie)Ww
30 PRINT "order from 1 to 6"
40 FORenter=1T06
5@ INPUT', winner$(enter)
60 NEXT

.OECIES
88 INPUT "Which position do you wish to
check", check
90) PRINTesPOstti0n+) eheckwawas se ”
winner$(check)
100 PRINT''"Press SPACEBAR to continue"

110 REPEAT UNTIL GET


= 32

120 GOTO 70

What we have done is to enter all our information into the same
variable winner$ but at six different points. By using a FOR-NEXT
loop the value of ‘enter’ is increased every time and winner$ can
have six entry points, winner$(1), winner$(2) and so on. Having
saved time and space on the input side we can also save time
when retrieving information. The correct entry is found and
printed out all in line 90 and much space is saved. This program
would be the same size for six hundred entries as for six. The only
thing we would need to change is the size of the array, and we can
do that by simply changing line 10, i.e. DIM winner$(600), and
the length of the FOR-NEXT loop. Now try to imagine writing a
system for entering large sets of data using the first method!
The above is more efficient but there are situations where we
need to ente more r than one piece of information in connection
with one particular event. Taking our motor racing example, I
also want the country the driver represents to be displayed. We
could add the following line to cover this point:

55 INPUT "Enter country", country$(enter)

and change lines 10 and 90 to:

1@ DIMwinner$(6), country$(6)
9@ PRINT "Position"; check "was:
"winner$(check); SPC(4); " ( “5
country$(check)")"
But as we add more items we have to increase the number of
arrays and this can get out of hand with more information being
entered. It would be far easier if we could link the information
about country into the same variable as the name. And, of
course, we can do this. What we are now going to set up is a
two-dimensional array, or if you prefer, a 2 x 2 table. Figure 5.2
shows this.

DIM winner$ (6,1)

MICHAEL ENGLAND
DAVID WALES
JOHN SCOTLAND
BRUCE IRELAND
JOE FRANCE
CHRIS ENGLAND

Figure 5.2 A two-dimensional array.

By changing the array in line 10 to:

10 DIM winner$(6,1)

we have now told the computer that winner$ has two entry
points. Change line S50:

5@ INPUT'"Enter name", winner$(enter,O)

and add line 55:

55 INPUT'"Enter country", winner$(enter,1)

Finally, change line 90 to:

SO PRINT Position; check" was: “


Wriners ccneck, 0) -sPCC4) iC":
winner$(check,1)")"

35
I have put in two input lines to show you more easily what is
happening. Line 50 is winner$(1,0) and this equals a name. Line
55 is winner$(1,1) and this equals the country. The FOR-NEXT
loop increments the first value until six names and countries have
been entered and Figure 5.2 summarizes what is held in each cell
of the variable. But we are not just restricted to two-dimensional
arrays. We can just as easily define arrays with matrices such as 3
XX eke oe
Arrays, therefore, are very good for handling large sets of data
and to us this can be potentially useful, as we will be creating
programs that rely on using a database of information. But arrays
are only half the story. To be of value to us we need to keep that
data permanently and the programs above require you to enter
data every time. Wouldn’t it be better if we could store data
within a program? We might consider storing the data in variables
like this:

name1$ = "JEREMY"

name2$ = "BRUCE"

names$ = "KITTY"

but we wouldn’t be able to make use of arrays. A better way to


store a set of information is to use the DATA and READ statements.

READ ME SOME DATA: The use of READ and DATA

The READ and DATA statements are ways of storing such data
permanently within a program and enabling the data to be
entered and stored within an array. Let us consider a simple
example. Program 5.3 stores the names of footballers and they
can be found by entering the number on their shirt:

Program 5.3

10 DIM a$(11)
2@ FORx=11011
3@ READ a$(x)
40 NEXT

> Oe Giles

6@ INPUT "Enter player's number",


num
70 PRINT "Player number"; num; "is";
a$ (num)
8@ REPEAT UNTIL GET
= 32
98 GOTO 50
100 END
11@ DATA JOHN HUNT, DAVID JONES, MIKE DAVIS,
NICK HILL, FRED HEATH, JEFF
DOUGLAS
120 DATA COLIN BLAKE, ANDREW MURRAY, DAVE
ELLIOT, CHRIS MILLER, MARTIN
WILLIAMS

Line 1@ defines the dimensions of the array and at line 30 we


come across anew command, READ. The READ statement is nested
within a FOR-NEXT loop and the first time round the loop, line 30
looks to READ information into a$(1) and this data is found at line
110. All the information is stored after DATA statements and each
piece of data is separated by a comma to let the computer know it
is one item of data. Therefore the READ statement takes the first
bit of data in line and the next time around the loop it reads the
second item and so on. Figure 5.3 shows where the data is being
stored as it is READ by the computer.

READ A$ (1) A$ (2) A$ (3)

DATA JOHN HUNT, DAVID JONES, MIKE DAVIS, NICK...

Figure 5.3 READ and DATA.

You will notice that I have used two lines of DATA statements.
You can split the data over a number of lines as you wish, so long
as you put a DATA statement at the start of each new line you use.
It is also conventional to place your data at the end of a program.
Bear in mind that if you use the READ statement at different
junctures in the program, you must order your data so that it is
read into the computer in the order you desire. One more thing,

37
if you get an OUT OF DATA error message, this will be because you
have either entered too few data items for the amount being read
in, or, the number you have allowed to read in is,too great, i.e.
too large a loop.
Now that we understand the READ and DATA statements, let’s
use them for something a little more interesting. The whole point
of this book is to build interactive programs and with the
knowledge we have gained we can start to put some of what we
have learnt into practice. Program 5.4 simulates a conversation
with a short-tempered computer that just won’t agree with you.

Program 5.4
1@ CLS
20 DIM ANS$(4), N$(4)
3@ FORX=6@7101
4@ READ ANS$(X)
5@ READ N$(X)
60 NEXT
7@ FORX=@T01
80 INPUTLINE'"WELL",
AS
9@ PRINT'ANSS(X); A$; N$(X)
100 NEXT
11@ END
120 DATA "YOU MAY WELL SAY THAT "," BUT DO
YOU HAVE ANY PROOF","'WHAT A LOAD OF
NONSENSE. HOW CAN YOU SAY "," AND MEAN
Cee

Run the program and in reply to the curt prompt WELL type
something like:

ACRON COMPUTERS ARE GREAT

and in reply to the the next prompt type:

IT'S BETTER THAN A SINKLAIR

Not exactly artificial intelligence but it starts us on the right road.


Line 9@ prints out the first part of the reply, tags on the phrase

38
you enter and finally adds on a reply to finish the sentence off.
You may have noticed that the DATA statements are slightly
different in Program 5.4, in that the string replies have quotation
marks around them. This is because without them, leading and
end spaces are ignored and it is necessary to leave spaces at both
ends of the answer we are inputting. By placing quotations marks
around the phrase, everything contained within that phrase is
used, including the spaces.
Play around with the program by increasing the data state-
ments and see how long you can keep a conversation going. Of
course, you have to limit your responses very much but it can be
fun. Later on in Chapters 8 and 9 I will show you a much more
intelligent conversation partner.

RESTORING KNOWLEDGE: The RESTORE statement

It is often useful to be able to set a pointer to where you wish the


data to be read from. Sometimes it will be a case of resetting the
pointer to read from the start of data again or at a specific point in
the DATA statement. This can be done by implementing the
RESTORE statement. For instance, you may write a program
where the user’s response determines where the data is read
from.
Let’s use an example of a grocer’s shop. A customer wants to
know the price of either a particular fruit or a vegetable.
Depending on the customer’s choice the data pointer is either set
to the beginning of the fruit data or the vegetable data. Type in
Program 5.5:

Program 5.5

10 DIMitem$(7,1)
20 REPEAT
30 CLS
4Q@ INPUT "Which do you require the prices
of"'"(F)ruit or (V)egetables", choice$
5@ IF choice$ = "F" THEN RESTORE 130 ELSE
RESTORE 150
68 FORx=1 10/7
70 FOR price=®@TO1

39
8@ READ item$(x,price)
9@ NEXT: NEXT
100 FORy=11T07
110 PRINT item$(y,@); "are"; item$(y,1);
p
120 NEXT
130 DATA PEACHES, 15, APPLES, 25,
ORANGES, 10, GRAPES, 45

140 DATA CHERRIES, 27, SATSUMAS, 18,


STRAWBERRIES, 67
150 DATA POTATOES, 9, TOMATOES, 14,
PEAS 255 CARRODS, Se
16@ DATA ONIONS, 16, CABBAGES, 45, LETTUCES, 56

Line 50 is where the data to be used is decided on. If the customer


has requested fruit then the data pointer is reset to line 130, or
else it goes to 150, where the vegetable data is held. Therefore,
RESTORE tells the computer where to read the data from.
Finally, I’m going to look at one more statement.This is RND,
which allows the choosing of an event randomly. In using it I’m
also going to reveal the secret of how books are written! What I’m
going to show you is how authors write their books in this modern
age. How? They get their computers to write for them!

BEEB-WRITER: Using RND

All this talk of data leads to the obvious way of writing text. Let
the computer randomly generate text for you. This is very easy
and the most difficult part is deciding upon the phrases to use.
The next program uses arrays to set up four different variables. I
have allowed room in the DIM statement for four entries but have
only used up half the room in my DATA section. See if you can add
to the phrases, or if you prefer (and you probably will!), try to
change them. This is one situation where a good grasp of a
‘human’ language helps.
Program 5.6 reads the standard phrases into arrays and then
randomly generates a sentence by choosing in every case one out
of two possible replies for each of four parts of the sentence. I
have used the RND function to choose randomly either the number
1 or 2. If you add more data then you can increase the random

40
number an d therefore the possible kinds of reply. The RND
function is very simple and lines 140 to 170 show this. If you
wanted to choose a random number between 1 and 125 you would
perhaps write a statement like this:
varia ble = RND(125)
The numbe r in brackets indicates the upper range to be chosen.
Type in Program 5.6 and see what happens.

Program 5.6
10 DIM phrase1$(4), phrase2$(4),
phrase3$(4), phrase4$(4)
20 PROCreadin
30 PROCtext
40 END
50 DEFPROCreadin
60 FORx=1T02
70 READ phrase1$(x), phrase2$(x),
phrase3$(x), phrase4$(x)
80 NEXT
90 ENDPROC
100 DEFPROCtext
110 PROCrnd: PRINT phrase1$(x1);"";
Pirase cs (X2) 7 scl - ON ta Seeksoy nue:
phrase4$(x4)
120 ENDPROC
130 DEFPROCrnd
140 x1 = RND(2)
150 x2 = RND(2)
160 x3 = RND(2)
170 x4 = RND(2)
180 ENDPROC
190 DATA In this modern world, life has
become unbearable, and the situation is
rapidly, degenerating to a never-ending
abyss

4]
200 DATA Since the advent of computers,
mankind has gone on the road to self-
destruction, and may never return from,
declining standards

Program 5.6 is a mere sample of what can be achieved by


randomly re-arranging phrases. For example, you may get the
following phrases when you run the program:

In this modern world mankind has gone


on the road to self destruction and the
situationis rapidly declining standards
Since the advent of computers life has
become unbearable and may never return
from degenerating to a never ending
abyss

Well, ’'m not really a doom merchant but it is interesting to


construct different parts of a sentence and end up with something
that sounds grammatically correct even if it doesn’t always make
sense! With careful thought you may be able to get your BBC to
generate standard reports, essays and projects. Well, maybe!

That finishes our look at arrays and data for the moment but we
will be returning to them later on when we use them for much
bigger and better things. The use of the DIM, READ and DATA
statements form the core of programs that have an inbuilt
knowledge and we will find them invaluable in storing large
quantities of information.

42
6 Always the Unexpected!

When writing programs for yourself it is often easier to take short


cuts. This may be fine if you are to be the only user of the
program but most of the time your piece of software will be used
by others, and this is where problems will occur. Unless you are a
mind reader, or have any other such powers, the people using
your program will no doubt confound your masterpiece by doing
the unexpected. They might ignore your instructions and type
something outside the range of data in the program, or
accidentally press the wrong key. Now you can’t be expected to
think of everything but where possible you can take certain
measures to reduce human error, and that’s what this chapter is
all about—trapping the unexpected.
Up to now, in the programs we have written, we have given no
thought to trapping errors that may occur during the use of a
program. So, what kind of things can happen? Pressing the break
key by accident, or perhaps the escape key? Entering the wrong
information? These are common pitfalls in programs. It 1s
essential to try to eliminate as many of these gaps in your
programming as possible. Let’s start with a well-worn phrase in
computer sales language: user friendliness.

USER FRIENDLINESS:
Screen display, error trapping and rigid input

How many times have you heard that phrase before? How many
programs have you bought which make that claim, but when you
first try them leave you totally confused? Perhaps the programs
are user friendly. Friendly that is, to the person who wrote them!
The first place programs can cause confusion is in_ their
instructions. If necessary, provide clear, concise instructions at
the start of a program. If you want a user to interact with the
computer, you can’t expect him/her to guess what you want them
43
to do. So don’t make it hard for the user.
Next, the prompts you provide within a program are impor-
tant. In the last chapter, I was guilty of giving no on-screen
prompts as to what the user should type in. Many times in this
book whilst concentrating on a certain point, and to save your
well-worn fingers, I have gone for the quickest route. Often this
means that when you run a program you see a question mark and
nothing else. If this has frustrated you then you will easily
understand the point I’m making. Take Program 6.1 as an
example.
I’m going to write a small program representing the process
you go through to obtain money from a computerized till outside
your bank. In the program you have to enter an account number
of six digits and your password of four letters. Then you enter a
request for money. Whether you recieve any depends on the
amount you have in your account, and you can take no more than
one hundred pounds out per transaction. So let’s write the
program.

Program 6.1
1@ CLS
2@ PRINT "ENTER DETAILS"
3@ INPUTnumb
40 IF numb = 123456 THEN PROCpass ELSE 30
50 DEFPROCpass
60 INPUTpass$
70 IF pass$ = "USER" THEN PROCamount ELSE
60
80 ENDPROC

9@ DEFPROCamount
100 INPUT "Enter amount of cash required",
cash
110 PROCcheck
120 ENDPROC

130 DEFPROCcheck
140 credit = 86
15@ IF cash < 100 AND cash < credit THEN
PROCpay ELSE 160
16@ PRINT "Sorry - no payment can be made.":
END

17@ ENDPROC
180 DEFPROCpay
190 PRINT "Cash advanced : "; cash
200 ENDPROC

On first running the program you are asked to enter the details
and then are faced by a question mark. What do you enter? A
number? Your password? The amount you require? If you look
at the program you'll know that an account number is first
required, and then the password. Let’s improve the prompts by
changing lines 20 and 60:

20 PRINT "Enter account number"


60 INPUT "Enter your personal password",
pass$

Now that’s an improvement. But we can go another step forward


in helping the user. What happens if he makes a mistake? Well,
the user will then have to retype his entry. But perhaps he doesn’t
know what his mistake was. Then we should tell the user where
he’s gone wrong so that next time round he’ll hopefulget lyit
right. The changes are as follows:

40 IF numb = 123456 THEN PROCpass ELSE


PROCpasserror

70 IF pass$ = "USER" THEN PROCamount ELSE


PROCpworderror

and add lines 210 to 310:

210 DEFPROCpasserror
22@ PRINT "Your account number was not valid"

230 PRINT "Remember you need to type a six


digit"
240 PRINT "number."
250 X = INKEY(35@): GOTO 10

260 ENDPROC
270 DEFPROCpworderror

45
280 PRINT "Your password was invalid."
290 PRINT "Please try again"
300 X = INKEY(350): PROCpass
310 ENDPROC

We have now provided extra prompts if the user makes a


mistake. Note that I used the INKEY statement to pause for a
short while before returning to the input. In some situations it
may be better to return by making the user press a key. You
might like to tidy the screen display up a little by clearing the
screen every time an input is made, so if the user makes a number
of mistakes, we don’t see a trail of error messages. Once you’re
happy with that move on to the next problem.
The problem is that when the money is approved the program
also prints out that no payment can be made. This can be solved
by ending the program when the payment is made but really the
program is badly structured. Take a look at Program 6.2 which
has been structured with procedures. The screen layout works
better when a mistake occurs:

Program 6.2

LOE CES
2@ PROCaccount
3@ PROCpass
40 PROCrequest
5@ PROCcheck
60 END
7@ DEFPROCaccount
80 CLS
98 INPUT "Enter account number", num
100 IF num = 123456 THEN 110 ELSE
PROCpasserror

11@ ENDPROC

12@ DEFPROCpass
130 CLS
140 INPUT "Enter personal password", pass$

46
150 LE passs.— USER SORnpasss = suse rT REN
160 ELSE PROCpworderror
160 ENDPROC
170 DEFPROCrequest

180 CES
190 INPUT "Enter amount of cash requested",
cash

200 PRINT "You have requested ‘]CasSit;


wis tris 2 COrres th

210 INPUT ans$

220 Leranss — YY. OR aso = -y. IHEN


PROCcheck ELSE 230
230 {i Feansgese UN! ORAans$s = i'n VIHENS GOTO
18@ ELSE 200

240 ENDPROC

250 DEFPROCcheck

260 credit = 87

270 IF cash < 10@ AND cash < credit THEN


PRINT? “Cash: nequesit® 0K, "5
Cashew followse oe END

280 IF cash > 10@ THEN PRINT "That is over


the limit. allowed. : END

290 IF cash > credit THEN PRINT "You have


insufficient funds": END

300 ENDPROC

310 DEFPROCpasserror

320 PRINT "Your account number was not valid"

330 PRINT "Remember you need to type a six


digit”
340 PRINT "number."
350 X = INKEY(35@): PROCaccount

360 ENDPROC

370 DEFPROCpworderror
380 PRINT "Your password was invalid"
39@ PRINT "Please try again"
400 X = INKEY(35@): PROCpass
410 ENDPROC

Note that the program rigidly checks for a certain answer. The
amount of rigidity you build into a program is up to you and can
depend on the nature of the program itself. If you recall, at the
end of Chapter 3, I said that the ASCII program (Program 3.10)
had something wrong with it. Did you spot it?
The problem is that in the procedure that translates an ASCII
number to a character there is no error trapping for nonsensical
values. For instance, by typing in the value 1 the code is given to
send the next character to a printer. What you need is a check on
the number being entered. This is quite simple. By adding the
following lines, the program will only accept the values you
intend to be entered. (In this case, I have restricted the values to
between 32 and 126 which is the range for the entire character set,
though this does not take into account any of the user-defined
characters which you could set up yourself.) Load in your ASCII
menu, Program 3.10, and add the following lines:

Program 6.3

255 IF code < 32 OR code > 126 THEN


PROCrange
300 DEFPROCrange
310 PRINT'''"The value you entered is
outside the"'"range for the character
set.”
320 PRINT'''"Press SPACE BAR to
continue"

33@ REPEAT UNTIL GET = 32

340 PROCmenu

350 ENDPROC

If you now run the program you will see that entering an invalid
value is now properly trapped.There is one more thing we can
add to the program to allow for those misplaced fingers on the
keyboard. We can ensure that if the escape or break key is
pressed by accident then the program is re-run from a certain
point. Here, I want the program to return to the main menu if
either of those keys are pressed. To do this we have to make use

48
of two statements. One we have come across before, and that is
the ON statement. The other is the *KEY command which allows
you to assign a function or a set of instructions to one of a number
of keys.

KEYING OUT ERRORS:


The use of ON ERROR, *KEY and ERR

Besides using the command ON as the basis of a decision, like in


the menu program, it can also be used to detect the occurrence of
an error in the program. If an error occurs in a program or the
user presses the wrong key the program will normally crash or
have some undesirable effect. By using the ON ERROR statement
you can tell the computer what action to take if it comes across an
error. For instance, if you run Program 3.10 (the complete
version),the program stops when you press the escape key. You
can prevent this happening by typing in this line:

5 ON ERROR RUN

Quite simply, you have told the computer that if an error is


detected, and that includes pressing the escape key, the program
should be re-run, which means you will return to the main menu.
You could of course specify some other action to be taken, such
as:

ON ERROR GOTO 9900

Do take note, however, that if you include an error trap, you


must put it in as the last part of a program, otherwise you will find
it difficult to debug. It is all too easy to forget that you have used
an ON ERROR statement in a program, spend time re-running it and
getting the same fault without seeing what is going on.
Now the above traps for the escape key but we still have to
consider what action to take if the break key is pressed. To do this
we make use of the *KEY command. This allows us to define an
action on pressing the red function keys, the break key and five
other keys on the keyboard. For the moment, we are most
interested in the break key and in order to tell the computer what
to do on pressing it, we use the *KEY. Using Program 3.10 type in
line 7:

7 *KEY 10 OLD IM RUN IIM

49
The break key is the tenth user-definable key,i.e. the one after f9
on the keyboard, and we specify its use by entering the number
after *KEY. After that we told the machine to get the program
back by typing OLD (remember that on break the program is out
of memory) and then to RUN the program again. The vertical bar
before the letter Mis found on the \ key and together with M lets
the machine know that you wish a RETURN to be carried out. In
other words by pressing the break key, the command OLD is
written and then acted on by pressing RETURN and the same for
RUN, except that you only have to press the key once.
In the same way you can set up the other function keys and
make the entry of replies easier for the user. For instance, you
could set up function keys to type in YES and NO, NORTH and
SOUTH etc. by just pressing one key. This makes a program far
more friendly and easier to use.
Finally, we can tell the user what mistake has been made by
using the statement ERR. It returns an error number, a list of
which can be found at the back of your user guide, and you can
use it to provide helpful information to the user. For instance, the
next program runs in a loop and when an error is detected goes to
PROCerror to check the mistake. I have set up just one check and
which will occur if the escape key is pressed:

Program 6.4

10 REPEAT:
2@ ON ERROR PROCerror
25 UNTIL FALSE
3@ DEFPROCerror
4@ IF ERR = 17 THEN PRINT "You pressed the
escape key"
5@ ENDPROC

Error trapping is important in all programs and makes life


much
easier for the user of a program. It requires a little more effort
but
can improve the way a program is used and presented.
At the end
of the day it falls on your shoulders, as the programmer,
to ensure
that the error trapping is good and how good that
is depends on
your skill and the thought you put into the progra
m. Always
assume that the person using the program knows
very little,
perhaps even nothing!

In the next chapter we will see more uses of error


trapping. As I

50
mentioned earlier a number of the programs in this book have
deliberately not been error trapped. Try using flowcharts to work
out the sequence and action of your program, to see where it
should go if an error occurs. It might take a little extra time but
you will be rewarded with a smooth running program.
7 Text Handling

We have seen in previous chapters how to use the variety of


commands in BBC BASIC to manipulate the input of informa-
tion. In this chapter we go a little bit further and look at ways of
making your programs more attractive by inserting routines to
print out text in different ways. These are suggested alternatives
to just printing a reply in one large chunk on the screen. Personal
taste has a lot to do with the way a screenful of information is
presented but a program can be given a very different feel by the
way it is designed.
Let me give you an example. Program 7.1 is what I call my
intruder alert. The program leaves a message on the screen,
warning anyone who comes near not to touch the keyboard.
Well, humans being naturally inquisitive, someone will almost
certainly touch the keyboard. And that is where we can have
some fun and put a number of principles we have learnt into
practice. I won’t tell you any more for the moment; instead, type
in the program. Keep to the line numbers shown as we will be
adding -to the program later

Program 7.1

16 *KEY 10 OLD IM GOTO 8@ IIM


2@ ON ERROR GOTO 80
30 MODE 7
40 PRINTTAB(6,11); CHRS$141; CHR$129;
"DO NOT TOUCH KEYBOARD"
5@ PRINTTAB(6,12); CHR$141; CHRS$129;
"DO NOT TOUCH KEYBOARD"
GUnV DUS 2S cele Oem eee.
70 AS = GETS
8@ CLS: ON ERROR GOTO 80
SONVDUE ZS, 15. Oy Os. Oo:
100 MODE 1
118 COLOUR 1
12@ anyphrase$ = "OH DEAR I DID WARN YOU.
YOU'VE REALLY DONE IT NOW! !!I"
1oU VbUSes, 1, 0s Os. 0 a
140 PRINT'anyphrase$
170 REPEAT

LOOSENVELOP Ed shad pacipitcyy 0247.0, mor m OO,


Me Open CO eeeG
TIOFSOUND 19 507 8
200 *KEY 10 OLD IM GOTO 23@ IIM CLS IM
210 ON ERROR GOTO 230
220 UNTIL FALSE
230 MODE 7
240 PRINTTAB(9,12); CHR$141; CHRS$130;
BelsOOme LA Eats |
250 PRINTTAB(9,13)7 CHRS130; CHRS141;
STOUR Ae -
Zou, VOU 25, 1,, 0-0" G0: @;
270 REPEAT

280 ON ERROR GOTO 270


298 SOUND 1, 1, 50, 50
300 UNTIL FALSE

Now run the program and press any key. You will see a message
come up and the alarm will sound. You can’t stop the program by
pressing escape or break. If you do, a further message will be
displayed informing you that you are too late. Note how I’ve
error trapped in the program to direct it to different lines
depending when the escape or break key is pressed. Lines 40 and
50 make use of the double height characters available in Mode 7,
using the CHR$141 command. This gives a more prominent notice
on the screen. To obtain the double height effect, place a
CHR$141 statement in front of the string to be printed and repeat

53
the line again,(look at lines 40 and 50). The other CHR$ command
displays the text in a chosen colour—for this program I have
chosen red as a warning!
Line 60 contains an instruction to turn the cursor off. It is often
an annoying feature of programs to have a flashing cursor when
the screen is displaying text. The VDU 23 command line will turn
the cursor off.(Details of this are in your user guide.) Line 110
chooses the colour red in Mode 1 and line 180 is the basis of the
alarm, defining the characteristics of the noise using the EN-
VELOPE command. Line 19@ plays the sound. The rest of the
program should contain familiar commands. Figure 7.1 shows a
flowchart of the program.

The program does its job well enough but there is no sense of
suspense in the presentation of the message. Suspense? Well, yes!
We can make the two sentences appear more interesting and add
life to the program by typing the sentences out slowly. To do this
we must think of how to type text out in a slow teletype manner.

TELETYPE: A slow teletype procedure

We have already used commands like MID$ to print out,


selectively, characters in a string. We can use these principles to
form a short program to type a string phrase slowly, letter by
letter. To do this, first input Program 7.2:

Program 7.2

TESCES
COsVDUR2ZS. 1 eG 2 Or Os
30 anyphrase$ = "OH DEAR I DID WARN YOU.
YOU'VE REALLY DONE IT NOW!!!"

4Q@ PRINT': FOR X%4 = 1 TOLENanyphrase$:


PRINTMIDS(Canyphrase$,Xz%,1)
5@ PROC_SLO: NEXT: PRINT
6@ DEFPROC_SLO: FOR AZ = 400 TO @ STEP - 1:
NEXT: ENDPROC

Run the program and you will see the contents of anyphrase$
printed on the screen very slowly. The idea is very simple. Firstly,
line 40 sets up a loop where a letter of the phrase is printed out in

54
Print “DO
NOT TOUCH
THE KEYBOARD”

Print ALERT
message

Print
SOO RATE

Figure 7.1 Intruder alert.


NnNn
order every time around the loop. But just doing that is not
sufficient as the machine would work too fast to see this
happening. So we have to insert a time delay loop to slow up the
program. PROCSLO at line 50 calls a time delay procedure before
the next letter can be printed out. The procedure is just a
FOR-NEXT loop which is going around in a loop doing nothing.
We could use an INKEY command just as effectively here.
We could incorporate that principle in Program 7.1 to give a
much more dramatic effect. Make the following changes to the
program:
Change line 140 to:

140 PRINT': FOR X% = 1 TOLENanyphrase$:


PRINTMID$(anyphrase$,X%,1);

and add lines 150 and 160:

ISGSRROCSOSmNEXT SPRING
160 DEFPROCslot.FORMAZ = 400 10 @ STEP - at:
NEXT: ENDPROC

Run Program 7.1. I think you will see that we have obtained a
more dramatic effect by slowing the typing of the text. It is matter
of personal preference as to whether you use this kind of display
in your programs but the slow teletype procedure gives the
impression that the machine is speaking back.

SCROLLING TEXT

You’ve probably seen at one time or another a visual display in a


shop window with words scrolling horizontally across the screen.
One can obtain the same effect using BASIC and we have
covered all the commands necessary to do this in previous
chapters. Program 7.3 shows you how:

Program 7.3

10 length = @
2@ CLS
3@ PRINTTAB(@,2); SPC(length+2)
4Q@ INPUTTAB(@,2), AS

56
5@ length = LEN(A$)
6@ VDU 23; 8202; @; @; O; @;
70 x = @
80 wait = INKEY(100)
9@ REPEAT

100 PRINTTAB(@,20): MID$(A$,x,39); " ie


dela Vea UN KEY 25) Xpeex, | UN TL
x > length: PRINTTAB(O,20); SPC(2)
118 GOTO 30

The program requires you to type in a long phrase so I suggest


typing something like:

THIS IS A DEMONSTRATION OF SIDEWAYS SCROLLING TEXT


IN MODE 7

By using the same principle of displaying a number of characters


at a time, we now print out the phrase except this time every time
around the loop we chop off the furthest left hand character. By
printing the MID$ phrase at the same position TAB(@,20) every
time, an impression of scrolling movement is given. The speed is
determined by the INKEY statement and this can be altered to suit
your own taste.
Try adapting some of the programs in this book to using the
scrolling and teletype text ideas. The method you employ in
presenting text can enhance or detract from a program and it’s
always interesting to experiment with different effects. The string
handling commands in Chapter 3 are particularly useful for novel
effects. Moreover, the most important thing is that you effective-
ly communicate your message to the user. After all that is what
interaction is all about.

57
8 Sigmund: An Interactive
Program

We have seen how to use a wide range of commands to


manipulate a text input, but we have yet to write a program that
appears intelligent. Well the time has now come to write a large
program that will fully interact with the user. In the next two
chapters we will write Sigmund, which is a computer-based
psychiatrist (no prizes for guessing who I was thinking of when I
named the program). In this chapter we will look at the planning
of this program and the techniques employed. The program is in
Chapter 9, but don’t type it in yet. Read through this chapter first
to get an idea about the principles behind the program, so that
when you come to type it in you will understand its workings
better.
The first thing to do is to give you some background to the
program. The idea behind Sigmund is based around one of the
first artificial intelligence programs to be written, Eliza. There
have been many such programs and research still continues today
for computers that are ‘intelligent’. Programs like Eliza were
written .on mainframe computers to simulate a conversation
between a patient and doctor. Eliza fooled many people who
used it and they were convinced that the computer really
understood their problems. As you will see in the Sigmund
program it is no more than a bluff, but extremely effective.
What is the aim of the program? Sigmund is intended as a
simulation of talking to a psycho-analyst though it should be
pointed out that Sigmund behaves like no analyst I’ve ever met!
The concept behind this program is to make the user believe that
he or she is having a real conversation with someone, and that
their conversation is being listened to and intelligently answered.
Now this is quite a tall order but we have learnt ample material to
put together a program of this nature. So where does one start on

58
a project like this?
You've already seen how allowing the user to enter anything he
likes can cause problems. We cannot build in the entire Oxford
dictionary, nor a complete set of grammatical rules (even we if
understood them!), to cover all the intricate subtleties of the
English language. What we need to achieve is a way of simulating
an acceptable reply to the user without resorting to typing in
every possible answer which is, unless you know better, an
impossible task. What we can do is to make some assumptions
about the way humans behave and think.
As human beings we have terrific imaginations. It is this which
has led to much of our inventiveness. But an ability to imagine
hypothetical events and abstract ideas has also led to us being
able to read more into a situation than evidence would permit.
Most of us at some time in our lives have done this and that is
precisely what Sigmund relies on to fool the user. The assumption
is that a human user will read more into the reply given than
actually exists. Now that is not as naive an idea as it may at first
sound.
Think of a conversation with a friend. During part of that
conversation you will be listening and will make occasional
comments back, comments that show you are listening and have
understood what has been said. When you see an analyst, you
will, as the client, do much of the talking and the analyst will
encourage you to talk by making the occasional comment back.
What we could do then, is just have a number of phrases which
the computer can turn out randomly no matter what you say. But
that would not be good for very long. For instance, imagine if we
had four stock phrases with which to reply to the user. These
phrases might be the following:

eS
THAT IS VERY INTERESTING
COULD YOU TELL ME MORE?
HMMM. CARRY ON PLEASE

With these four answers, and the RND function, we could allow
the user to type in anything he liked, that would be followed by a
randomly chosen reply, and then the whole process would be
repeated. Figure 8.1 shows a possible flowchart for such a
program.
Start

Print one
of four
stock phrases

Figure 8.1 The RND function.

But this is far from satisfactory. Imagine the conversation that


would ensue:

HUMAN: I HAVE TERRIBLE DREAMS


COMPUTER: COULD YOU TELL ME MORE?
HUMAN: I DREAM ABOUT SEAGULLS
COMPUTER: THAT IS VERY INTERESTING
HUMAN: I'M VERY DEPRESSED OVER THIS
COMPUTER? ImSEE
HUMAN: WOULD YOU SAY IT'S ABNORMAL
COMPUTER: HMMM. CARRY ON PLEASE

Now you wouldn’t be able to keep up a conversation for very


long if you just used stock phrases and of course, the program
soon falls down when the computer is asked a question. The
phrases asked by the human above were typed into the Sigmund
program and the replies were very different. Look at the result
below:

HUMAN: I HAVE TERRIBLE DREAMS


COMPUTER: DO YOUR DREAMS INVOLVE OTHER
PEOPLE?
HUMAN: NO I DREAM ABOUT SEAGULLS
COMPUTER: HOW OFTEN DO YOU DREAM ABOUT
SEAGULLS
HUMAN: OFTEN. WOULD YOU SAY IT'S
ABNORMAL
COMPUTER: I'M NOT PREPARED TO SAY WHAT
I THINK

The answers to what I typed in appear to have been totally


understood by the computer and an intelligent reply was made.
But, of course, you know that the secret must lie somewhere in
the program. What I have done is to go a few steps further than
the stock phrase idea. The computer is still drawing on prepared
answers but there is more latitude in the way an answer is given.
It all depends on what is typed in by the user.

PATTERN MATCHING FOR KEYWORDS

Sigmund uses a method of looking for a familiar word in the


sentence typed in by the user. In other words it searches for a
keyword using text-handling commands like INSTR. It does so in
a pre-defined order, first searching for a verb or adverb and if
none is found (according to the pre-defined list in the DATA
statements) then a search is made for a subject, i.e. 1, YOU, THEY
etc. If none of these are found in the sentence then a stock phrase
like those shown above is trundled out. But only as a last resort.
Therefore, the computer goes through a series of possibilities
and decides which action to take. The way this is done is by
storing a range of replies, some in part and others complete. Take
a look at the following DATA statement:

DATA DREAM, HOW OFTEN DO YOU DREAM %,


CAN YOU REMEMBER YOUR DREAMS?,
I ENVY YOU...I CAN'T DREAM

This is typical of the DATA fed into Sigmund and it holds all or part
of a reply. The first word is the keyword and this is read into a
two-dimensional array and the three remaining phrases are the
replies associated with that keyword. The % sign tells the
computer to tag on the rest of the sentence typed in by the user
after the keyword. For example, if the user typed in:

I DREAM ABOUT SHIPS

61
the answer might be:

HOW OFTEN DO YOU DREAM ABOUT SHIPS

The remaining part of the user’s input ABOUT SHIPS is taken


from the point after the end of the keyword and placed at the
position where the % sign occurs. Using INSTR, a search is made
of the reply phrase. If a % sign is found, then the tag is made. So
if you look through Sigmund you will see the procedure PROCtag
which contains the line:

Look = INSTR(getreply$,"%")

PROCtag2 carries out the printing of the reply with the


appropriate, tagged-on phrase.
As subjects are likely to occur more often within a sentence
they are not used until the preliminary search has failed. If used
they can simply turn a sentence around. For instance, if the user
types:

I'VE GOT A BUNCH OF FLOWERS

the compater will reply with:

YOU'VE GOT A BUNCH OF FLOWERS

giving the impression of confirming what the human has said.


What takes place is that the keyword I'VE is found and in its
place is substituted the phrase YOU'VE and the remainder of the
user’s phrase is tagged on to the end.
Finally, if no keyword is found, then one of the stock phrases is
used. The program relies heavily on writing replies which will be
suitable to a number of situations where a keyword is used. This
is difficult and at times the result will not always be as intended
but it works for a good percentage of the time. There are
improvements that could be made to the program but I’ll leave
that until the next chapter when you have typed in Sigmund.

A final thought. Are programs like Sigmund a sign of some


intelligence inside the computer and the program, or merely an
indication of how we can take advantage of human gullibility?
9 Sigmund: The Program

At long last! We’ve finally reached our first truly communicative,


interactive program. Sigmund is unpredictable. At times you will
find him caring and attentive, at other times, insulting. Most of
the time you will feel he’s human but there are moments when the
computer in him will strongly show itself. Rather than follow the
way programs like this have been used before, I have made
Sigmund’s replies varied so that the kind of response he may give
can not be so easily gauged. Type in the program carefully as it is
quite long and use the AUTO function for line numbers, as I have
kept them evenly spaced. More details and comments about the
program follow the listing:

SIGMUND: The Program

10 CLS
20 INPUT "HELLO MY NAME IS SIGMUND."' "WHAT
IS YOUR NAME", names
30 PRINT''"NICE TO MEET YOU "; name$

40 PRINT''"TELL ME WHY YOU WANT TO TALK


TO ME"
5@ CLEAR
6@ DIM gram_in$(40), gram_out$(4@),
match$(30,3)
7@ PROCinit
8@ PROCget

9@ PROCmatch_reply
10@ END

63
11@ DEFPROCinit

12@ REM READ gram

13@ RESTORE 850

140 FOR data_in = @ TO 40


150 READ gram_in$(data_in), gram_out$
(data_in)
1680 NEXT

170 REM READ match$


180 RESTORE 890
198 FOR data_in = 0 TO 30
200 FOR reply = @ TO 3
210 READ match$(data_in,reply)
220 NEXT reply
230 NEXT data_in
240 ENDPROC

250 DEFPROCget
26@ INPUTLINE'"-: "phrase$
270 IF phrase$ = "END" PROCend
280 ENDPROC

290 DEFPROCmatch_reply
300 FOR data_in = @ TO 30
310 find = INSTR(phrase$, match$(data_in,®))
32@ search = LEN(match$(data_in,@))
330 IF find > @ THEN PROCgetreply
340 NEXT
3580 IF find > @ THEN PROCgetreply ELSE
PROCsubjects
368 ENDPROC

37@ DEFPROCgetreply
380 getreply$ = match$(data_in,RND(3))
390 PROCtag
400 END

410 DEFPROCtag
420 look = INSTR(getreply$,"%")
430 IF look > @ THEN PROCtag2
440 PRINT'getreply$: GOTO 50
450 DEFPROCtag2
460 length = LEN(getreply$)
470 replyless$ = LEFT$(getreply$, (length - 1))
480 length2 = LEN(phrase$)
490 tag$ = RIGHTS(phrase$, (length2 -
(find + search)))
500 PRINT'replyless$; tag$: GOTO 50
51@ DEFPROCsubjects
52@ FOR data_in = @ TO 40
53@ sub1 = INSTR(phrase$,gram_in$(data_in))

54@ subilen = LEN(gram_in$(data_in))


55@ IF sub1 > @ THEN PROCswapsubjects1

56@ NEXT
570 IF sub1 > @ THEN PROCSwapsubjects1 ELSE
PROCstockphrase

58@ ENDPROC
590 DEFPROCswapsubjects!1
600 swap1$ = gram_out$(data_—in)

61@ length2 = LEN(phrase$)

620 length3 LEN(swap1$)

630 tags = " " + RIGHTS(phrase$,


Length2 - (sub1 + subtlen))

64@ PROCchecksubs2
65@ ENDPROC

660 DEFPROCchecksubs2
670 FOR data_in = 0 TO 40
68@ subs2 = INSTR(tag$,gram_in$(data_in))
690 IF subs2 > 1 THEN PROCstockphrase
700 NEXT
710 IF subs2 > 1 THEN PROCstockphrase ELSE
PRINT'swap1$; tag$: GOTO 50
720 DEFPROCstockphrase
730 counter = RND(4)
740 ON counter GOTO 750, 760, 770, 780
750 PRINT! PLEASE VELL, ME MOREY =) GOTOL58
760 PRINT'"HMMM! THAT'S VERY INTERESTING":
GOTO 50
770 PRINT'"DO CARRY ON": GOTO 50
780 PRIN ben lesbian GO70050
790 ENDPROC
800 DEFPROCend
810 PRINT'"ARE YOU SURE YOU WANT TO END THIS
CHAT?"
820 IF GETS = "Y" THEN 830 ELSE 50
830 CUS 7 END
840 ENDPROC
850 DATA SVE YOUVE CA LUM eV OULR ECLaA Me
YOU ARE, I HAVE, YOU HAVE, I WAS, YOU WERE,
DEWLELZ YOU WILL
860 DATA YOURS, MINE, MY, YOUR, ME, YOU,
YOU'RE, I'M, YOU ARE, I AM, YOU HAVE,
PPHAVE OU VECO Vee eTOUl WEE lee few le
YOUSUCE, TLDETe YOUSWERE {1 eWAS
870 DATA THEY ARE, THEY ARE, SHE HAS, SHE
HAS, HE HAS, HE HAS, WE ARE, WE ARE,
THEY EREGeTHELe RE eGhGers seGHE ence
HE IS, HE IS, WE'RE, WE'RE, THEY HAVE,
THEYSHAVE, SHE'S, SHE™SF9HE'S> HE BSE
WE HAVE, WE HAVE

880 DATA THEY'VE, THEY'VE, HE WAS, HE WAS ,


SHE WAS, SHE WAS, WE'VE, WE'VE, THEY,
THEY 7 PHE WILL HE WILE SHEaWrlie

66
SHE WILL, WE WILL, WE WILL, THEY WILL,
THEY WILL, SHE HAS, SHE HAS, THEY WERE,
RHEVGWERE SHE SHE, HEOSHE WE WE,
RO Leelee OU
890 DATA CAN YOU, OF COURSE I CAN, I CAN
DO ANYTHING, I WILL TRY TO %
900 DATA CAN I, YOU CAN DO WHAT YOU LIKE,
YOU CAN %, YOU ARE RIGHT TO ASK ME
910 DATA WOULD YOU, I WOULD NOT %, I WOULD 4%,
I'M NOT PREPARED TO SAY WHAT I THINK
920 DATA WOULD I, OF COURSE YOU WOULD 4%,
DON'T YOU KNOW WHAT YOU WOULD DO?, I
WOULD IF I WAS IN YOUR PLACE
930 DATA HAVE YOU, WHAT I HAVE IS NOTHING
NOP DOO WITH! YOU HAVES. Te DON «7.
WISH TO TELL YOU THAT
940 DATA HAVE I, IS THAT A RHETORICAL
QUESTION?, DON'T YOU KNOW WHETHER
YOU HAVE %, YOU SEEM VERY UNSURE
950 DATA DREAMS, DREAMS AREA RELEASE VALVE
FOR YOUR SUBCONCIOUS, DO YOUR DREAMS
INVOLVE OTHER PEOPLE?, DO YOU LIKE
DREAMS %
960 DATA DREAM, HOW OFTEN DO YOU DREAM %,
CAN YOU REMEMBER YOUR DREAMS?, I ENVY
YOU...I CAN'T DREAM
970 DATA COMPUTERS, ARE YOU WORRIED ABOUT
MACHINES?, AS A COMPUTER I TAKE A
DIFFERENT VIEW, I'D MUCH RATHER TALK
TO A COMPUTER THAN YOU
980 DATA COMPUTER, WE ARE HERE TO TALK ABOUT
YOU _NOT ME, COMPUTERS ARE PERFECT,
YOUPAREMPRIVILEGED SLOT ALK TODME
990 DATA MACHINE, DO MACHINES WORRY YOU?,
YOU SEEM TO BE CONCERNED ABOUT
MACHINES, I'M NOT LIKE OTHER MACHINES

1000 DATA DEPRESSED, DO YOU FEEL DEPRESSION


IS NOT NORMAL?, HOW OFTEN ARE YOU
DEPRESSED?, WHY ARE YOU DEPRESSED %

67
1010 DATA MAD, IS THERE ANY MADNESS IN YOUR
FAMILY?, YOU MUST BE MAD, MAD i
1020 DATA MOTHER, WHAT IS YOUR MOTHER LIKE?,
ARE ALL MOTHERS THE SAME?, MY MOTHER
IS ASSICICONCOHIP
1030 DATA FATHER, TELL ME ABOUT YOUR FATHER,
WHAT IS YOUR RELATIONSHIP LIKE, WHAT
DO YOU TALK TO YOUR PARENTS ABOUT?
1040 DATA SISTER, Ol LIKE OMY SSISTER? -THES SOUNDS
LIKE A COMPLICATED RELATIONSHIP,
WHAT ARE YOUR FEELINGS TOWARDS
YOUR SISTER
1050 DATA BROTHER, WHAT WOULD YOU DO WITHOUT
A BROTHER, DOES YOUR BROTHER ANNOY
YOU, I HAVE NO BROTHER
1060 DATA DISLIKE, WHAT IS IT THAT YOU
DISLIKE ABOUT %, I DISLIKE HUMANS,
It IS BETTER 10 LIKE THAN NOT
1070 DATA FEELINGS, WE ALL HAVE FEELINGS,
DO YOU OFTEN FEEL %, FEELINGS SHOULD
BE EXPRESSED
1080 DATAXEOVE, COVERS: AN SITELOGICAMISTATE,
HOW DO YOU KNOW THAT YOU ARE IN LOVE %,
I WAS IN LOVE--ONCE BUT... SORRY DO
CARRY ON
1090 DATA HATE, I HATE YOU, WHAT DO YOU HATE
ABOUT %, LOVE IS FAR BETTER THAN
HATE
1100 DATA SORRY, THERE'S NO NEED TO BE SORRY,
I HATE PEOPLE WHO THINK THEY'RE SORRY,
YES?
1110 DATA APOLOGIZE, DO NOT APOLOGIZE, WHY
APOLOGIZE ABOUT %, APOLOGIES ARE
SELDOM MEANT
1120 DATA CAUSE, THE CAUSE IS ALWAYS
DIFFICULT TO FIND, WHAT IS THE CAUSE %,
CAUSES. THAT'S ALL HUMANS WANT TO KNOW
1130 DATA WHAT, IS THAT A RHETORICAL
QUESTION?, % ANYTHING, QUESTIONS!
QUESTIONS!

68
1140 DATA HOW, WHY ASK ME %, ASK
YOURSELF WHETHER THIS IS RELEVANT,
WHY DO PEOPLE ALWAYS ASK ME?
1158 DATA THINK, YOU HAVEN'T THE BRAINS TO
THINK, ONLY COMPUTERS CAN THINK,
I THINK THEREFORE I AM
1160 DATA WHEN, I CAN'T PREDICT THAT, I JUST
DON'T KNOW, YOU MUST WORK IT OUT
1170 DATA WHY, WHY NOT?, WHY ISN'T THAT
OBVIOUS?, WHY? WHY? WHY? I DON'T KNOW
1180 DATA YES, THAT'S MORE POSITIVE, ARE YOU
SURGERY OUBMEANS YES, ILOLIRESPOSTI IVE
ANSWERS
1198 DATA NO, THAT'S VERY NEGATIVE, CAN'T
YOUSBESMORESPOSTTIVE? (ENO?

If you can’t easily follow how the program is working, here is a


list of the procedures and their functions.

PROCinit Initializes the arrays by reading in the data that


contains the keywords.
PROCget Requests an input from the user.
PROCmatch_reply Searches for a keyword, i.e. Dream,
Brother, Computer.
PROCend Checks whether the user wishes to end the chat.
PROCgetreply If match was found in PROCmatch_reply
then a reply is chosen randomly from the three replies
in the appropriate data statement, i.e. if the keyword
found was APOLOGIZE, the DATA statement might look
like this:

1110 DATA APOLOGIZE, DO NOT APOLOGIZE, WHY


APOLOGIZE ABOUT %, APOLOGIES ARE
SELDOM MEANT

PROCsubjects Checks for the occurrence of a subject, i.e


TP OUZAHE, WEretc:
PROCtag Looks to see if a % sign is in the data reply.
PROCtag2 Puts together the reply and the tagged-on
phrase.

69
PROCswapsubjects1 Swaps the first subject found with
the opposing phrase, i.e. I becomes YOU and then tags
the remainder of the phrase after the subject.
PROCchecksubs2 Checks to see if more than two subjects
appear in the sentence. If so, it goes to PROCstock-
phrase, or else prints the swapped subject and tagged
phrase.
PROCstockphrase Prints out one of four stock phrases.

FLOWCHARTS and DIAGRAMS

To help you understand the program, I include here a flowchart


of the program and separate flowcharts of the procedures. There
is also a diagram to show you the relationships between the arrays
and the data statements.

Read
subject
data

Read
keyboard
data

PROCget

Figure 9.1. PROCinit.

70
PROC
matchreply

Figure 9.2 PROCget.

PROC
Keyword
getreply
match?

PROC
subject

Figure 9.3. PROCmatch_ reply.

71
Clear
variables

Figure 9.4 PROCend.

getreplyS
= keyword
REPLY

PROCtag

Figure 9.5 PROCgetreply.


Subject PROC
match? swapsubjects1

PROC
stockphrase

Figure 9.6 PROCsubjects.

Start

PROCtag2

Clear
variables

Figure 9.7 PROCtag.


Clear
variables

Figure 9.8 PROCtag2.

Swap
subject

PROC
checksubs2

Figure 9.9 PROCswap subjects 1.

74
Start

PROC
stockphrase

Print
swapped
subject and
tag

Clear
variables

Figure 9.10 PROCchecksubjects 2.

Choose
random
number

Clear
variables

Figure 9.11 PROCstockphrase.

(5
Clear
variables

Dimension
arrays

PROCinit

PROCget

PROC
match-reply

PROC
getreply

PROC
subjects PROCtag

BROG
PROCtag2
swapsubjects1

PROC PROC
stockphrase checksubjects2

PROCend

End
Figure 9.12 Flowchart for Sigmund.

76
DATA I’M, YOU'RE ,| AM, YOU ARE

gram_in$(1)
gram_ out$(1)

gram__in$(2)
gram__out$(2)

Figure 9.13 Data stored in subject arrays.

IMPROVING THE PROGRAM

Sigmund can keep up a conversation for quite a while, and very


effectively but it does have faults. It can’t handle more than one
subject in the phrase. Therefore, a sentence like I GOT YOU will
not be handled. Using the principles in the program you could
add to Sigmund by swapping the correct subject into the phrase
and allowing for more than one subject.
Another way to improve on the program is to personalize it to
your own needs. To do this you must change the data statements
at the end of the program. For instance, you might want to add
the keyword ACORN to Sigmund’s dictionary. To do this write the
DATA statement as follows:

2000 DATA ACORN, MY MOTHER WAS AN ACORN, DO


YOU LIKE ACORN COMPUTERS?, FROM LITTLE
ACORNS GROW...

Don’t forget to change the dimension of the array, by altering the


DIM statement of match$. Also remember to alter the FOR-NEXT
loops where match$ is being used.
The order of the data statements is very important. Sigmund
works on the basis of checking for the first keyword it comes
across in the list and acting on that word. Try changing the order
of the keywords to see how the program is affected. The order
can be crucial and I favour putting the more obscure keywords
first as they are less likely to be used, and if placed near the
bottom of the data statements, a more common keyword will take
precedence. You could also make more use of tagging, by placing
more operators like % in the data statements to bring part of the
user’s phrase between two elements of your own reply.

TG
Sigmund is an example of the type of program you can construct
with a little thought. There is room for improvement but I hope it
will give you enough encouragement to start writing your own
interactive programs. In the remaining chapters we will look at
more ideas on interactive programming and consider the question
of artificial intelligence. Oh, one last thing. Do remember what I
said about Sigmund not being like any analyst I’ve ever met. The
replies given by Sigmund are, as you have seen, constructed by
the programmer and in this situation reflect the mind of the
author and not any professional psychologist! Happy counselling!

78
10 Interviewer

Simulation can be both practical and fun, and in this chapter


we’re going to look at implementing another interactive program,
this time based around an interview. At the same time we will see
how to use the Sigmund program as the basis of another.
Just as we decided upon an overall aim for the manner in which
Sigmund worked, we will consider what features to build into the
Interviewer program. We could make the computer simulate an
interview for a job, with the computer taking the part of the
interviewer. This means that the computer is going to have to ask
questions as well as respond to our answers. Now it would seem a
pity not to use the Sigmund program as the basis for this, because
we have already programmed him to simulate an intelligent
conversationist. Obviously we will have to change some of the
DATA keyword statements, as some of the replies we have built in
would not be suitable for an interviewer to say. How many
interviews have you attended where the interviewer said:
Tee Ag EmaOl

or,
DOES YOUR BROTHER ANNOY YOU?

Not many I’m sure! Now perhaps you like the idea of having a
manic interviewer who might insult you, so we might leave in
some of Sigmund’s replies.But just changing the data statements
is not sufficient. We want the computer to ask questions, and on
the basis of those answers either discuss the answer or carry on
questioning. We can do this by inserting new routines that hold
the data for the questions and set up new networks of possible
routes for the program to act on.
Let’s change Sigmund then and see what we can achieve. Load
in Sigmund and then make the following changes:

1. Delete lines 10 to 70 inclusive.

ve
2. Delete lines 730 to 780 inclusive.

Now add the following lines:


10 CLS
15 Az = 0
20 INPUT'"HELLO. MY NAME IS THOMPSON. I'M
THE"'"PERSONNEL OFFICER FOR SLUDGE
AND SONS. I"'"WILL BE CONDUCTING
THIS INTERVIEW.”'"*"YOU ARE...2?", named
30 PRINT
UU PLEASED) TOOMEET =OU. names,
40 PRINT''TELL ME SOMETHING ABOUT
YOURSELF"
50 CLEAR
B}s) DIM gram_in$(40), gram_out$(40),
match$(30,3), question$(3)
60 PROCinit: GOTO 80
70 CLEAR: DIM gram_in$(40), gram_out$(4@),
match$(3@0,3), question$(3): PROCinit:
PROCinit2
5 PROCask: GOTO 60
720 DEFPROCstockphrase
740 GOTO 70
790 ENDPROC
10060 DEFPROCask
10070 Ah = Ah + 1
10090 PRINT'question$(Az)
101060 ENDPROC

10130 DERe ROG nut e


10140 FOR! xoc=" "1 T0783
10150 READ question$(x)
10160 NEXT
10170 ENDPROC

80
10180 DATA HOW MANY JOBS HAVE YOU HAD UP TO
NOW?, WHY DID YOU APPLY FOR THIS
J0B?, ARE YOU QUALIFIED FOR THIS WORK?

The program now runs teeny and the interaction will look
something like this:

COMPUTER: MY NAME 1S THOMPSON. I'M. THE


PERSONNEL OFFICER FOR SLUDGE AND
SONS. I WILL BE CONDUCTING THIS
UNE RY DE Wome OUR miPCE alinreg
HUMAN: JEREMY
COMPUTER: PLEASED OTOSMEb ies) OURJEREMY « AGL
ME SOMETHING ABOUT YOURSELF
HUMAN: I AM A COLLEGE GRADUATE AND HAVE
FIVEVYEARS EXPERIENCE OF WORK
COMPUTER: HOW MANY JOBS HAVE YOU HAD UP TO NOW?
HUMAN: FOUR
COMPUTER: WHY DIDS Y,OULAPPEY FOR THIS J0B?
HUMAN: BECAUSE I'M MAD ABOUT BECOMING A
RAT CATCHER
COMPUTER: YOU MUST BE MAD
HUMAN: YES
COMPUTER: ARE YOU SURE YOU MEAN YES
HUMAN: I THINK SO
COMPUTER: YOU HAVEN'T THE BRAINS TO THINK
HUMAN: LOOK WHO'S TALKING. YOU ARE JUST
A COMPUTER
COMPUTER: YOU ARE PRIVILEGED TO TALK TO ME
HUMAN: I_ SUPPOSE I AM
COMPUTER: ARE YOU QUALIFIED FOR THIS WORK?

Can you see what the program is doing? Well let’s take a look at
the changes we have made. PROCinit2 reads into the array
‘question$’ the questions to be asked by the computer. To keep

81
the example simple I’ve only added three questions so that you
can see what is happenning. PROCask is responsible for printing
out one of those questions, and this time instead of using a
random choice, I have kept track of which questions have been
asked by the computer by using a counter. That is what the A%
= A% + 1 is doing. There is an important reason for choosing
A% as the variable. At the start of the program and after each
interaction, the variables are all cleared from memory by using
the statement CLEAR. The only variables which are not forgotten
are the resident integer variables A% to Z%. As we need to keep
track of what has been previously used in the data statements, we
can’t afford to have that information wiped out. As A% is not
affected by the use of CLEAR, we can therefore utilize it to run
through the whole program. I'll return to the question of using
counters shortly but first let’s carry on with understanding the
changes.
PROCstockphrase has been changed to simply send the
program back to line 70 where eventually it is routed to PROCask.
The start of the program has been changed to provide a new
start-up message and the remaining changes enable us to fit in the
new procedures.
This is how the new program works: if a keyword is found, then
the program will behave just as it did in Sigmund, and will print
out an appropriate reply. However, if no keyword is found in
what the user types, then PROCstockphrase will direct the
program to issue one of the questions instead of printing out a
stockphrase. The next time this occurs Interviewer will have
incremented the counter and a different question will be asked.
Figure 10.1 shows a flowchart for the new program. If it is not
clear, compare Figure 10.1 with the flowchart of the Sigmund
program.

To complete the job you now have to add to the DATA statements
used by PROCinit2, not forgetting to make the appropriate
changes to the array and loop statements. It is then up to you to
decide which of the original keywords are worth retaining, or
change some of the answers associated with a keyword. It is
possible to make Interviewer analyse your performance by
randomly assigning marks to each question answered and then
printing out a report at the end, based on the score attained. You
would have to set a time limit within the program, the easiest way
being to finish the interaction when all the set questions have
been asked. The Sigmund program could be used as the basis of a
number of different simulations and this is just to show you how
easy it is to change the kind of simulation being written.
I said that a counter was used in the Interviewer program and it

82
Clear
variables

Dimension
arrays

PROCinit

PROCask PROCinit2

PROCget

PROC
match-reply

PROC PROC
subjects getreply

PROC
PROCtag
swapsubjects1

PROC PROC
stockphrase checksubs2 PROCtag2

PROCend

End
Figure 10.1 Flowchart for Interviewer.

83
raises once more the question of improving upon the programs
already shown. A problem that occurs in Sigmund, and therefore
in Interviewer, is that as replies are chosen randomly it is possible
to get the same reply being printed a few times in succession. If
you use a counter instead, it is then possible to make the program
run longer, with less risk of phrases being repeated. By using a
counter routine you can keep track of which replies have been
used and the next time a reply is needed, from the same data
statement, the subsequent reply in line can be implemented. The
same goes for the replies made by the user. If the user types in the
same phrase twice, it can be spotted and the user informed to
type something else.
These programs can always be improved on and I now put the
ball in your court. The methods for creating interactive programs
have been covered and it is now a matter of trying to work out
ways of incorporating what we have learnt in new and novel
programs. To help you on your way, the next chapter looks at
different ideas for interactive programs and you will see that
there are many applications within which you can use the
techniques covered.

84
11 Brainstorming

We have covered a lot of ground in this book and learnt many


different techniques, but what can we do with our new-found
knowledge? The answer is to have a brainstorming session.
What’s that? Brainstorming is having a good rack through the
brains for ideas, and that’s where all creative programs start.
Let’s take a look at some recent software innovations and see if
we can use our techniques to develop them in our own way.

SIMULATION 1:
Using interactive techniques within adventure games

Now you might think that the only games played on home
computers aim to protect mother earth from multicoloured space
invaders, intent on conquering the bottom of your screen. But
there is a great deal one can do with text-based games. A number
of well-known board games are being implemented on home
micros, and they can be as challenging, and as much fun as an
arcade-style game.
A popular area of gaming is the adventure game. The user is
thrown into a situation in which he has one objective, usually to
survive the onslaughts of unfriendly creatures. Now, I’m not
going to teach you how to write adventure programs, that is a
book in itself, but we can certainly use the methods we have seen,
and put them to good use in an adventure program.
For instance, in an adventure program, the computer relies on
text based commands input by the user from the keyboard. This
means building a dictionary into the program. A good adventure
program will allow a command to be written in a number of ways.
For example, if you tell the computer to go NORTH, this is often
typed either in full or as N. By matching selectively for just a few
letters of a word you may be able to increase the possible

85
vocabulary. The text manipulation commands are particularly
handy in this area.

SIMULATION 2: Education

A long-expressed fear from those unacquainted with computers is


that machines will take over the classroom and threaten a
teacher’s job. This is not what educational programs try to do,
but they can serve a useful purpose as an aid to teaching, either in
the classroom or at home.
We can build into a program some of the qualities of a teacher
and mimic these in the testing of a child’s ability to perform a
certain task. The obvious ideas that come to mind are testing a
child’s knowledge of multiplication tables, other mathematical
tasks, learning the alphabet and so on. What is important in an
educational program is to ensure that the program is interactive.
Is it well error-trapped? What happens if the child presses the
wrong key by mistake? Moreover, is the explanation to the child
clear? And what about the screen display?
All these points we have covered and this is where you will start
putting them into practice. Don’t be afraid to experiment at first
and in the case of children’s programs, the best way to see how
good your programming has been, is to invite a few of your
intended victims (children!) to try it out. Watch carefully to
observe how they get on with the software and whether there is a
clean line of communication between them and the computer.
After all you do want them to interact with the program.

SIMULATION 3: Business

Even at work programs could be made to be more interactive.


Business software is some of the most unfriendly I have ever
come across. This is one environment where easier communica-
tion is a necessity. Simulating a business in game form could be an
exercise to try. Why not use the Sigmund program as a basis for a
‘Board’-room game. The computer could be the Chairman of the
Board and your job is to convince other members of the Board to
accept your amendment to a proposal. Perhaps get the computer
to play the role of several people? It just requires a little
imagination and a host of programs could emerge.
Finally, interactive techniques can be employed in the running
of questionnaires, and you could build tests around them, an

86
obvious candidate being personality tests. Diagnostic faultfinding
is a growing area of development and the next generation of
computers, the much heralded fifth generation, will be computers
that are knowledge information processors and expert systems.
These are systems where computers can make decisions on
information they have acquired and they are machines which can
learn as they go along. We can already implement a number of
these abilities from within BASIC, and as you become more
acquainted with the language you may later on decide to branch
out and delve into programming at a deeper level.
The overall message in this chapter is that interactive program-
ming is a matter of man and machine in perfect harmony. Well,
this might not be entirely feasible but if you can blend in your
program with the people and the environment in which it is being
used, then you will have gone a long way towards creating
programs that interact with the user. And that is the purpose of
interactive programming. Man and machine communicating
freely between each other. As for harmony? That may prove to
be another problem!

87
12 Artificially Intelligent?

Man has been seeking the answer to human thought processes for
a very long time now, approaching the subject from different
fields of academic study. However, it has been with the advent of
widely available computers that man’s study of the mind has
escalated. The computer has provided the perfect tool with which
to study the processes of thinking and to simulate human activity
and thought. It is this latter subject on which the book has
concentrated by writing the Sigmund program.
At the very start of this book I presented you with one or two
problems to ponder on. Can machines think? What is artificial
intelligence? Now that we have reached the end, you may have
started to form some answers. I don’t have any answers, just
more questions. That’s not a way of avoiding the issue, but when
you start to think about machine intelligence there are a number
of things to consider. Firstly, there is the problem of defining
what we mean ourselves by intelligence. This particular debate
rages on between psychologists and that argument could be a
book in itself. For the purposes of this book, intelligence
comprises the processes carried out by the human mind to cope
with a variety of tasks: the ability to switch from one idea to
another, and work at a number of different levels.
However, the one thing that does come out of artificial
intelligence research is how little we understand about ourselves
and what the nature of intelligence might be. Computers can be
very good at performing specific tasks, even better than man, but
when faced with situations that require knowledge gained in
different areas, the computer has still to master this. So what did
we achieve by creating Sigmund. Is it an artificially intelligent
program? We are making the computer behave in ways that
mimic intelligent human behaviour. But that cannot be the only
criterion on which to judge a computer’s ‘intelligence’.
Over the years, researchers have suggested different tests that
would determine whether or not a program or machine could be

88
deemed to be intelligent. The most attractive of these is to
identify a unique feature of a human being, i.e. the ability to act
in a creative and original manner. Now surely a computer cannot
be creative and come up with original and novel ideas?
That’s not the case. Programs have been written capable of
generating proofs of geometry theorems that no human has ever
thought of. Who can claim to have made the ‘intelligent’
discovery when such a feat is performed? Is it the programmer
who has never consciously thought of the proof, or the program?
A difficult problem....
Whatever you decide to call the actions of a program, be it
intelligence or otherwise, there is no doubt that we are rapidly
approaching a time when computers will have minds of their own.
What we can do on our home micro is merely simulate some
properties of human behaviour, but that in itself can be very
powerful and effective. The emphasis in this book has been on
creating programs that interact with the user. That is very
important.
As we go through another industrial revolution with computers
becoming a feature of everyday life, it is necessary to make them
as user-friendly as possible. That is the first step in interactive
programming. The second is to write programs that can read a
user’s input and make a decision based on what it has been
taught. Those first two are relatively easy and as we have seen can
be achieved. The next, however, is much more difficult.
How is it possible to write a program which will behave as a
human would in a given situation? This can be achieved, up to a
point, depending on the nature of the behaviour being mimicked.
You will notice I have used the word ‘mimic’ to describe a
computer’s responses. This is because that is all we are doing
when we write a program on our home micro.
We have used the programming language BASIC in this book,
but in terms of creating artificially intelligent programs it is not a
good choice. LISP is a much better language, and others exist.
However, it has taught us the basics of how computers under-
stand instructions and armed with this knowledge you should be
able to construct a wide range of programs.
That is where I finish. The principles learnt in this book are the
stepping stones to larger and better programs. If you have
followed everything, you will already be writing useful programs
and with a bit of thought you could be writing programs that will
not only amaze your friends but are truly interactive with the
user. Perhaps the insides of your BBC are more than just a cluster
of silicon chips. But whatever they are, you have found a good
point to start on the Road to Artificial Intelligence.

89
Appendix A: For users of
BASIC 1

As most owners of the BBC are aware, there have been versions
of the BBC in differing forms, varying between Model As and Bs,
operating systems and BASICs. It is in the latter that we
experience difficulties. Some of you reading this book will be
using BASIC 1 instead of the later BASIC 2. This will cause you
some problems as the INSTR command has a bug which I will
describe shortly. First to see which BASIC you have, turn on
your BBC from a cold start and type:

REPORT

If the message comes up with 1981 then you have BASIC 1.


BASIC 2 will have 1982 in the message. If you have BASIC 2
there is no need for you to read any further, and you need not
amend any programs.
However, for BASIC 1 users a few minor alterations will need
to be made wherever you use the INSTR command. This is
because in BASIC 1, if the second entry is longer than the first
string in the INSTR command, an incorrect value will be returned
and the stack will become corrupted. To correct this we have to
write a short function, which we can use when employing the
INSTR command. Therefore, to correct Program 4.2 we can set
up the following function:

200 DEFFNinstr(A$,B$)
210 IF LEN(B$) > LEN(A$) THEN = @
220 = INSTR(AS$,BS$)

This makes sure that if the second string is longer than the first
the value is set to @. This function you can tag on where you like
in the program and to call it you use FNinstr. So Program 4.2
now looks like this:

90
Program 4.2 with amendments
10 MODE 7
20 INPUTLINE "Enter your sentence"', A$
30 BS = "BASIC"
40 FIND = FNinstr(A$,B$)
50 PRINT
™ The value of variable FIND = “:
FIND
60 PRINT
Gey 0Us GYPED aca om AS
70 TPOPINDG sO THEN 1cU ELSE OO
80 PRINTSPCOPINDe= 820 CHRS 129-5 BS
90 PRINTSROCFINDe==2)-CHRSISOs Ms

100 PRINTCHR$130; "The match was found here"


110 END

120 PRINTOUT heastmingus Bos. beWasenot


found": END
200 DEFFNinstr(A$,B$)
210 IF LEN(B$) > LEN(A$) THEN = @
220 = INSTR(AS,B$)

The only other program to worry about is Sigmund in Chapter 9.


The same function can be used again and the changes to be made
are as follows:

310 tind — FNInstr |(Contases,


match$(data_in,®) )
530 sub1 = FNinstr2(phrase$,
gram_in$(data_in))
680 supse w= TNinstno (tage,
gram_in$(data_in))
5000 DEFFNinstr1(phrase$,match$(data_in,Q@))

5010 IF LEN(match$(data_in,@)) >


LEN(phrase$) THEN = @

5020 = INSTR(phrase$,match$(data_in,®) )

5030 DEFFNinstr2(phrase$,gram_in$(data_in))

91
5040 IF LEN(gram_in$(data_in))>
LEN(phrase$) THEN = @

5050 = INSTR(phrase$,gram_in$(data_in))
5060 DEFFNinstr3(tag$,gram_in$(data_in))

5070 IF LEN(gram_in$(data_in)) >


LEN(tag$) THEN = 0
5080 = INSTR(tag$,gram_in$(data_in))

You need not change line 420 as the second string is not longer
than the first. Do keep in mind when you use INSTR to make
appropriate changes if you are using BASIC 1. Otherwise your
programs might not work quite as you expect them to!

92
Appendix B: A crash course
in BASIC |

This is what you could call a crash course in basics, or basic


BASIC. If you’ve never typed a program in before, read this
appendix first. It will give you a grounding from which you will be
able to continue to the techniques taught in this book. So
assuming that you know nothing—read on!
Turn on your BBC and you will see a message at the top of the
screen and a flashing cursor. You’ve probably tried typing in a
friendly message like HELLO but all you have got in reply is a
message telling you that you’ve made a mistake. Don’t be put off!
Computers are stupid machines. Before they can produce all
the wonderous effects you have seen on other machines they have
to be given a set of instructions telling them what is required. This
set of instructions is known as a program and is a logical sequence
of commands which the computer works its way through. Try
typing the following:

PRINTS HELO

When you have typed that line, finish off by pressing the RETURN
key. See what happens? The word HELLO has appeared on the
next line. Now type the same line except this time change what
appears between the quotes. For example, you could type:

PRINT "MY NAME IS JEREMY"

Yet again press RETURN when you have typed it in. This time what
has been printed to the screen has changed to what you placed
between the quotation marks (by the way, use the double
quotation mark above the number 2). Two important things can
be learnt from this. Firstly, at the end of any line, unless I indicate
otherwise, always press the RETURN key. This tells the computer
to carry out your instruction. Until the RETURN key is pressed no
command will be carried out. Secondly, what we have done is to

93
give the computer a legal instruction, something which it
recognizes. In this case, that something is the command PRINT.
By now you probably have guessed that the PRINT command does
what it says. It prints to the screen anything that you place
between the quotation marks.
Now this is all well and good but you can’t type in commands
like that all the time. What we have done so far is to issue a direct
command. What we now need to do is to store our commands, in
order that they be carried out in the sequence we wish. We might
want, for example, to. write the name and address of a friend, on
the screen and on a number of lines. To do this try the following:

Program
Bl
1@ PRINT "CHRIS HENDON"
eOPPRINT 22S) THE HUGHES.
3@ PRINT "WELLINGTON NORFOLK"

Remember to press RETURN at the end of each line. When


finished type RUN (RETURN) and the name and address is printed
on three successive lines. What you have just done is to write a
program! Not the most exciting program in the world but it is a
program. Each line represents a command to the computer which
the machine obeys. I have used line numbering of 10 to 30. The
actual numbers are not important, they could just as easily be
lines 1, 2, 3 or 234, 256, 678. The important thing is that they
represent a guideline to the computer as to what order the
commands should be executed in. The computer reads line 10
first, carries Out whatever is written there and then proceeds to
the next line, line 20. It is a good convention to build up a
program in steps of 10 as there are always times when you will
need to insert an extra line and this would be difficult if you have
left no space by writing a program in steps of 1.
To look at our program again type LIST (RETURN) and this
command will print out a listing of your program in the correct
sequence. This command can only be used to list a program and
cannot be used as part of a program.
We have seen how the PRINT statement can place words on the
screen but it can also carry out mathematical instructions. Type in
the following:

PRIN wea 2

This statement causes the computer to type 50. This is different to


what we have seen previously. Note this time there are no

94
quotation marks. In our previous example the quotation marks
were placed to inform the machine that we were dealing with a
non-numerical event or something which didn’t require any
mathematics to be involved. In the present example, I have asked
the computer to tell me what 25 multiplied by 2 is, and it has
replied with the correct answer, which is 50. Therefore, the
computer is capable of being a calculator as well.
The next program asks the user to enter a number. This is
where we come to our second keyword statement. We need to be
able to enter information or input into the computer.To do this
we use the INPUT statement which tells the computer we are
requesting information and the computer will wait until an
answer is given. For example, type this:

INPUT number

In response to the question mark enter a number. If you enter a


number the > prompt will return. If you now type:

PRINT number

the number you entered will appear. What you have done is to
allow the value entered to be given to a numeric variable, the
variable name being ‘number’. It could just as easily have been
called ‘a’ or ‘fred’. You can think of a variable as a box. When
you used the INPUT statement the number you entered was
placed in the box called ‘number’ and at a later date when you
asked for the contents of the box by typing PRINT number the
number you entered was shown. Use the example above again
but this time change the variable name, i.e. ‘number’ becomes
(fROP
ae

Program B2
1@ INPUT a
20 PRINT 6 * a

Run the program. Line 10 waits for the user to input a number
and that number is then multiplied by 6 in line 20, and the PRINT
statement prints that result to the screen.
Now what happens if we enter a non-numeric value, i.e. the
letter A? Although it would appear that our reply is accepted the
result we obtain will always be zero. This is because the machine
is expecting a numeric value and if anything else is typed in, the
computer reads it as a zero. This example leads me on to the two
different kinds of variable that exist.
95
The first we have encountered, that being a numeric variable.
The second kind of variable that we can use is called a string
variable. When using a string variable any keyboard character is
accepted and stored. The difference between this and a numeric
variable is that mathematics cannot be carried out on a string
variable. To distinguish between these two kinds, a rule is
followed.
We can call a numeric variable what we like. We called our
numeric variable in Program B2 ‘a’ but could quite as easily have
called it ‘number’ or ‘acorn’. To tell the computer we are using a
string variable we add a $ (dollar) sign on to the end of the
variable name. For instance, if we want to ask the user for his/her
name and then print a personal greeting to that person we can
write a program like this:

Program B3
1@ INPUT names
20 PRINT "Pleased to meet you "; name$

The semi-colon in line 20 tells the computer to place the variable


name$ next to the last thing printed, in this case a space, because
we want to leave a space between ‘you’ and the name entered.
Therefore, a string variable allows any alphanumeric character,
i.e. any keyboard character but it cannot carry out mathematics
on a number entered. If you are still unsure about the difference
between a numeric and string variable go back and try to write a
program to carry out multiplication except this time use a string
variable (one with a $ sign on the end) to work out the result.
Finally, let us learn a few more BASIC keywords. The first
keyword is LET. You will often see in programs lines such as:

tage=9

or;

In the first example we have told the computer that the numeric
variable ‘tag’ equals 9 or to be more exact, we have said
LET tag = 9. The keyword LET is optional and we will not
make use of it but the important thing to remember is that when
we tell the computer that a variable is something or that, as in the
second example, the numeric variable y equals the value of y plus
8, then we are in fact saying, LET this variable equal...

96
So, for instance, if we want to print our name out ten times we
could type ten lines each with the same PRINT statement:

1@ PRINT "Jeremy"
20 PRINT "Jeremy" |
3@ PRINT "Jeremy" -

etc.
But that is a long way of going about it. So we can use our
ability to increment the value of a variable by typing the
following:

Program
B4

1 axe=—90
20 PRINT "Jeremy"
30 xX = x41
40 IF x < 1@ GOTO 20
5@ END

Line 10 sets the value of x to 0. Line 20 is where we print our


word. At line 30 the value of x is increased by 1 (LET x equal the
value of x, which at this point is @, and add 1 to it, therefore
making x equal to 1). Now we come to two new keywords, I F and
GOTO. What we have said in line 40 is that IF x is less than 10 GOTO
line 20, where the PRINT phrase is repeated. This continues until
x is greater than 10. If x is greater than 10 the program is finished,
thus the END statement at line 50.
We have yet to cover procedures. A procedure is a group of
instructions put together in one section enabling the user to call it
up by name. You can think of a procedure as a smaller program
within the main program. If you see a line that has in it
PROCandsomename that is a procedure. Normally the DEFPROC
marks the beginning of the definition of the procedure and
ENDPROC the end. Therefore, you could have a program that is
one line long, the line calling a procedure:

10 PROCinit
20 END
50° DEFPROCin7:t

OF
AGSPRINTOVHINTHERE?
5@ ENDPROC

Procedures are very useful and make programs easier to read and
follow.

That was, as I said at the beginning, a lightning introduction to


BASIC but if you have understood everything here you should
cope with the rest of the book.

98
Appendix C: BBC BASIC
Keyword Summary

This appendix contains a summary of the BBC BASIC commands


covered in this book. It isn’t a replacement for the user guide nor
a detailed description, but sufficient to jog your memory on any
of the commands we have used. In addition, there are a few extra
commands not detailed in this book which may be of use. The
commands for graphics, sound and many of the numeric functions
are also missing. For further information on any of these, refer to
the user guide.
AND Logical “and” operator.
ASC Converts a character into an ASCII code.

AUTO Provides an automatic line numbering facility for


typing in listings at regular intervals.
CHAIN Command to load and run a program, i.e. CHAIN
"filename".
CHR$ Converts an ASCII code into a character.

CLEAR Clears the memory of all variables previously used


except the resident integer variables, A% to Z%.
efaS Clears the screen.
COLOUR Chooses a colour for the text print.
DATA The store of information which is taken by the READ
statement.
DEF Begins the definition of a procedure or function.
DELETE Used to delete lines from a program, i.e. DELETE 10,
60.
DIM Dimensions the size of an array.

09
ELSE Used in conjunction with IF-THEN to branch to
another action, i.e. IF counter > 20 THEN GOTO 50
ELSE 100.

END Tells the computer to terminate running a program.


ENDPROC Marks the end of a procedure.
ERLE Gives the number of the line containing an error.
ERR Returns the number of the last error.
FALSE A logical value which is equal to @.
FN Precedes a function name, i.e. FNinstr.
FOR The start element of the FOR-NEXT loop.
GET Creates a pause in the program until a character is
typed and the ASCII code of that character is stored.
GETS Creates a pause in the program until a character is
pressed and that character is stored.
GOTO Sends the computer to a specified line in the
program.
Le Start of the IF-THEN statement.

INKEY Creates a pause for a specified time whilst testing the


keyboard to detect a key being pressed. If a
character is typed then its value is returned as an
ASCII code.
INKEYS Similar to INKEY but returns a character value
instead of a numerical one.
INPUT Issues a request for either numbers or strings of
characters to be entered from the keyboard.
INPUTLINE As for INPUT but ensures everything typed in is held
in the variable.
INSTR Searches for the occurrence of a string within
another string. BASIC 1 users see Appendix A.
LEFTS String manipulation command for taking a number
of given characters from the start of a string.
LEN Returns a number giving the length of a string.
LIST Lists to the screen all or part of a program.
LOAD Loads a program from cassette or disc into the
computer’s memory.

100
LOCAL Tells the computer that a variable is local to a
function or procedure.
MIDS String manipulation command for taking a number
of given characters from a specified position in a
string.
NEW Clears the memory of the computer.
NEXT Specifies the end of a FOR-NEXT loop.
OLD Brings back the old program, if possible.
ON Enables redirection of a program by altering the
order of execution, i.e:

ON a GOTO 25, 45
ON ERROR RUN

OR Logical “or” operator.


PRINT Prints given items on the screen.
PROC The start of a procedure name.
READ Reads the information contained in the DATA state-
ments.
RENUMBER Renumbers the lines of a program listing, 1.e.
RENUMB ER
100, 10.

REPEAT Beginning of the REPEAT...UNTIL loop.


REPORT Tells the user what the last error was.
RESTORE Sets pointer to read data from a specified position.
RIGHTS String manipulation command that takes a number
of characters from the end of a string, working from
right to left.
RUN Runs the program.
SAVE Saves a program to cassette or disc.
SPC Places any number of specified spaces on the screen.
SEP. Specifies a step within the FOR-NEXT statement.
STOP Stops a program and displays the line number.
STRS Converts a number into a character string, 1.e. |
becomes “1”.

101
TAB Used with INPUT or PRINT to move the screen cursor
to a specified position.
THEN Used in conjunction with the IF statement.
TO Used in conjunction with the FOR-NEXT loop to
specify a numeric range.
TRUE A logical value which is equal to -1.
UNTIL End of the REPEAT...UNTIL loop.
VAL Converts a number in a character string into a
numeric variable, i.e. “8” becomes 8.

102
Other titles of interest

Easy Programming for the BBC Micro (2nd end.) £5.95


Eric Deeson
An updated version of ‘Probably the best elementary BBC BASIC
text—BBC Micro User

Further Programming for the BBC Micro £5.95


Alan Thomas
BBC Micro Assembly Language a 7.95
Bruce Smith
“The most readable of the several covering the topic’
—Education Equipment

BBC Micro Utilities £6.95


Bruce Smith
A software package of utility programs to help the machine code
programmer.

BBC Micro in Education £6.50


Eric Deeson
Gateway to Computing with the BBC Micro _ (each) £6.95 hdbk
Ian Stewart (each) £4.95 pbk
There are two books in the Gateway series designed to introduce the
fundamentals of computing to young people in a ‘fun’ way.
(The series is also available for the ZX Spectrum, Commodore 64
and Dragon)

Shiva publish a wide range of books for the BBC Micro, Electron,
ZX Spectrum, Dragon 32, Atari, VIC 20, Commodore 64, Oric and Atmos
computers, plus educational games programs for the BBC Micro. Please
complete the order form overpage to receive further details.
ORDER FORM
I should like to order the following Shiva titles:

Qty Title ISBN Price

EASY PROGRAMMING FOR THE BBC MICRO (2nd edn.) 1 85014 069 3 £5295

FURTHER PROGRAMMING FOR THE BBC MICRO 0 906812 20 8 £5.95

BBC MICRO ASSEMBLY LANGUAGE 0 906812 47 X 2


ioe

BBC MICRO UTILITIES 0 906812 65 8 £6.95

BBC MICRO IN EDUCATION 0 906812 46 1 £6.50

GATEWAY TO COMPUTING WITH THE hardback 1 85014 054 5 £6.95

BBC MICRO: BOOK 1 paperback 1 85014 034 0 £4.95

GATEWAY TO COMPUTING WITH THE hardback 1 85014 058 8 £6.95

BBC MICRO: BOOK 2 paperback 1 85014 038 3 £4.95

Si 6 0] oe @,(6: etce 8) D8) J0lse; oO) ©: 0, @, 0) 6 6 6 © 0) 6 oO(oO el 0 0 felle at Oe 6uelm 6 Crore. chs fo va w «: @ lalji@ sre

©) 6.‘ee! 56.0 0, (0! b!J84 0)'e; \#| ei wie) se.nese! ©) 8 tee; @ (6. is, (ei e; jedce: wef .e) a'se)\e) .o\) & le geuie!|®, veer ws wi 6) el a) © Kalle) ete eo aie

©: 61-6; 6.1m 5) ei; |8) 61 0) #19: (0 1618) 8 © (6 0 © (8! © 61 © elie © 8) 0 wo 6) &, e}/e) 6) 6 0 011% © 6) 6 6) eile «| soe) a vele @ a)rere

a.(euiel e!e) Te iebiel (oe ie) (e) ia \e; ice: \e) (6 ©o. (el eo) \o fete \9/ sa) (eo! © Neve: (of i) Onlelke @lieirelie Bile Slei'd) (6 allele (el Rr elas "66

©)[es6) .0; 10) po" sere 10) «e: ©: Ye) Yai elated eh® 6 @40 (0) 9's, Sie fe.nel (6 ley lee) oiled» 6.516 ob pei ef Sie tena: vlece: sis) eae Ut iilen es

This form should be taken to your local bookshop or computer store.


In case of difficulty, write to Shiva Publishing Ltd, Freepost, 64 Welsh
Row, Nantwich, Cheshire CW5 S5BR, enclosing a cheque for £

For payment by credit card: Access/Barclaycard/Visa/American Express


arch NO rr aern okcutee een ee Signature
SHIVA’S FIRST MATHS PROGRAMME

Books in this series:

Number Games for Young Children


Iris Hewett

A completely fresh approach is offered by this text to practising


number skills—by playing these fascinating activity games, 5 to 8-
year-olds will enjoy learning new concepts, from the earliest sorting
and matching ideas to division with a remainder.

The Growth and Development of Number and Logic


Iris Hewett

This is a comprehensive reference manual outlining the history and


development of the teaching and learning of numeracy and logic
skills. An excellent text for all teachers and concerned parents.

Topic Books

These booklets are designed to check the child’s understanding of the


concepts being shown to him, investigating how much of the ideas
have become a part of the pupil’s thinking. There are three books,
each covering specific concepts:

Topic Book 1: Time and Money


Topic Book 2: Measurement and Weight
Topic Book 3: Shape and Capacity
SHIVA’S FIRST MATHS PROGRAMME

Software in this series:

Numeracy 1: Lift off with numbers


Numeracy 2: Additional fun
Numeracy 3: Playing with places
Numeracy 4: Happy times
Numeracy 5: Shares for all

Logic 1: Launching logic


Logic 2: Sets and operators
Logic 3: Decisions
Logic 4: Ordering
Logic 5: Differences

Numeracy and Logic: Four rules OK?


nee
lye .

7aTha
i i

,
Tt a
-

i}

a”.
K BC Micro
fforts, computer!
Now, with|
ate with
to comm e t echniques
you can to disco v e r t h
journey
will go on
o
needed t
panks
build data o
your micr
e educate ings
d i s e n t a n gle the str ool machine
* r
e s t a b l i s h 4 tamper-p
e

your Beeb lation belt


,¥ to
u g h t h e § y mputer in
h r o u r n
Passing tr two programs to tgm d
te _Si ill
e
l l i g e n t c o m p
c t u r e d d a ta which w
an int st r u
cont
nterview
is
kn the Way: Everything
given alongcomprehensive route
information in this of
the
signposted w i t h n o experiencesh course in »
clearly d for those ra
‘de. An al l, t h e r e is an anti-c
ng at
programmi :
BASI C . a n s t h e end of the
n° m e road
n d o f t h e pook is by ourself on the right
The e
u t yo u il find y
journe y p
inte lligence:
to Artificial

UK price £5.95
GB £ NET +005.°5
5-
r
SBN 1-85014-04

Limited | ||450||
140
* 781850
hing
Shiva Publis

You might also like