Vision Tutorial 1: Vision Basics
Reminder! To run these examples, you should first start a new session then load the sample database using:"/localvision/samples/general/sample.load" asFileContents evaluate ;Any other files referenced can be read from the /localvision/samples/general/ directory.Note: The sample.load file runs by default on a Unix environment. If you are using a Windows NT platform, this location may be prefixed by a drive and optional path (e.g. d:/visiondb/localvision/samples/general/sample.load). Check with your Vision Administrator for further details.
Numbers and Strings
Type the expression:
3Submit the request to Vision. You should see
3displayed as the result.
Now type:
1.23You will see:
1.23displayed. In fact, if you submit any number - positive, negative, integer, or decimal - Vision will respond by redisplaying the number you requested in the Output Window. Vision can accomplish this feat with strings as well. A string is any combination of characters enclosed in double quotes.
For example, type:
"This is a string"and submit this request. You will see:
This is a stringdisplayed. Vision can do more than merely redisplay whatever you submit. For example, type:
22 + 5The answer, 27, will be displayed. Try this with strings. For example, type:
"This is a string" + 5You should see:
>>> Selector '+' Not Found <<< NAdisplayed. What does this response mean and why was it only displayed after you tried to add 5 to a string? When you submit the request 22 + 5, you are asking Vision to perform the addition operation between two numbers, 22 and 5. Since this is a defined operation, Vision complies with your request by performing the calculation and displaying the result. When you ask Vision to add 5 to a string, Vision complains because the addition operation is not defined for strings. The Selector Not Found response identifies the specific operation that Vision could not perform. In this case, the operation is ;
Now try this experiment. Submit the request:
"This is a string" countYou should see:
16The count operation is defined to count the number of characters in a string. The answer indicates that the supplied string contains 16 characters.
Submit:
3 count
You should see
> Selector 'count' Not Found <<< NAIf you ask Vision to perform the operation count for a string, Vision complies by counting the number of characters in the string. When you ask Vision to perform the count operation on a number, Vision complains because this operation is not defined for numbers. In this case, the Selector Not Found response specifically identifies that count is the operation that could not be performed.
Now submit:
"This is a string" count + 3You should see:
19.00Although you cannot add 3 to a string, you can add 3 to the number of characters in the string. In this case, Vision counts that there are 16 characters in the supplied string, adds 3 to this number, and displays the result of 19.
This may be a good time to step back and explore what is really happening inside Vision. The basic unit of information in Vision is known as an Object. For example, the number 3, the string "This is a string", GM's common stock, and a list of companies are all objects in Vision. As you have seen in the above examples, different operations are defined for different types of objects. Arithmetic operations such as + are meaningful with numbers but not with strings. The count operation is meaningful with strings but not with numbers.
Similar kinds of objects are organized into Classes. For example 3, 17, and 1.75 are all part of the class Number and "a", "This is a string", and "3" are all part of the class String. Objects in a class are referred to as Instances of that class. For example, 3 is an instance of the class Number. Objects in a class all respond to a set of operations defined for that class. In the Vision language, operations are known as Messages. A request for an object to carry out one of its operations is known as Sending A Message. All the instances in a class respond to the same set of messages.
In the example:
"This is a string" count + 3the message count is sent to the string "This is a string" and the number 16 is returned. The message + 3 is then automatically sent to the number 16, producing the result 19. In the example:
3 countthe message count is sent to the number 3. Since the class Number has no definition for the message count, the Selector 'count' Not Found response is displayed.
All objects in Vision repond to a set of messages. All objects respond to the message displayMessages by displaying the list of messages defined for object. For example, type:
3 displayMessagesYour should see the messages that are valid to send to the number 3. The list of messages is expanded over time. The displayMessages message will always provide an up-to-date list.
Printing
All objects in the system have a default way in which they print. For example, integers are right justified in a 9 character field and print with no decimal places and decimal numbers are right justified in a 9 character field and print with 2 decimal places. Strings are left justified when they are printed.
When you submit a request, Vision automatically sends the message print to the final result. In other words, when you submit the request:
3Vision actually evaluates the expression:
3 printand therefore prints the value 3 when it is done. Try submitting the request:
3 printYou will see the result:
3 3The 3 is printed twice. The first 3 is the one you explicitly requested; the second is Vision automatically sending the print message to the result. Both versions are printed in 9 character positions; 8 blanks followed by the number.
You can alter the format of the result. For instance, suppose you wanted to print the number 1.1 to 3 decimals places. Submit the expression:
1.1 print: 8.3You will see the result:
1.100 1.10The first value is the result of the print: message. The second value is again the result of Vision's automatic print feature. To suppress this second value, place a semi-colon at the end of your input line as shown below:
1.1 print: 8.3 ;You will see the result:
1.100The print format of 8.3 indicates that you wanted your result printed in 8 character positions with 3 decimal places to the right of the decimal. The total character width of 8 includes the space required for the decimal point and digits.
Many other formatting tools are available for printing. A few simple messages are described below.
To print the number 5 million with commas, type the following:
5000000 printWithCommas: 12 ;You should see:
5,000,000To start a new line after the value is printed use the printNL or printNL: message. For example, type:
3 printNLYou should see:
3 3The second 3 is the result of Vision's automatic print message. It appears on a separate line because the printNL message automatically prints a carriage return after the first 3. In both cases, the number is printed with 9 characters.
To left justify a number, send it the print: message with a negative value for the parameter. For example, the expression:
3 printNL: -9displays the output:
3######## (where # indicates a blank space) ########3The first 3 is left justified in 9 character positions. The second 3 is the result of Vision's automatic print message which right justifies the value in 9 character positions. It appears on a separate line because the printNL: message automatically prints a carriage return after the first 3.
By default, all characters in a string are printed. No truncation or padding occurs. The print: message can be used to control the output width of a string. The value after the : indicates the number of characters that should be output. Strings containing fewer characters than the supplied width are padded with the appropriate number of blanks on the right (i.e., they are left-justified) by default. A String containing more characters than the supplied width will have its rightmost characters truncated.
For example, the expression:
"xyz" print: 2 ;displays
xyand the expression:
"xyz" print: 10 ;displays the output:
xyz####### (where # indicates a blank space)To right justify a string, send it the print: message with a negative value for the parameter. For example, the expression:
"x" print: -20 ;Produces the output:
#19 spaces xThe message center: can be sent to a string to center it over a specified number of columns. For example to center a title over the 80 columns type:
"My Title" center: 80You should see:
My TitleThe message centerNL: works in the same way as center:, but follows the output by a carriage return.
Parameters
The print: message differs from the standard print message in one important way; it has a parameter. A Parameter is an extra piece of information needed to carry out the message. In the case of the print: message, the parameter provides information regarding the column width and number of decimal places to use when printing the value. In the example:
1.1 print: 8.3 ;the parameter 8.3 indicates that the result should occupy 8 character positions and that the result should be rounded to 3 decimal places.
Try omitting this parameter as shown below:
3 print:You should see the response:
>>> 'syntax error' near source line 1, character 11 <<< 3 print: ** ^Since Vision was expecting a value following the print: message and none was provided, you are informed that you have made a syntax error. Vision tries to identify where the error occurred and provides an indicator arrow (i.e., the ^ symbol) in the area where the problem was first identified. In this case, the arrow points to the area directly after the print: message.
You have actually already seen another example that uses a parameter. The message + requires that you provide an additional piece of information, namely a number that you wish to add to the original number. Try leaving off this parameter as shown below:
3 +You should see:
>>> 'syntax error' near source line 1, character 4 <<< 3 + ** ^As you can see, the error message is the same as in the prior example. Although the operations you were trying to perform were different (i.e., printing versus adding), in both cases you left out a piece of information that Vision needed to execute your request.
Programs
Whether you realize it or not, you have already written your first Vision program. A program is merely a set of one or more Vision requests. All the programs you have seen so far have asked Vision to perform a single request. There are situations where you would like to perform a set of operations, one after another. Vision provides a simple mechanism for accomplishing this.
For example, submit the following:
"The Answer Is: " print ; 2 + 2You should see:
The Answer Is: 4.00The semi-colon is used to separate expressions within a Vision program. Each expression is executed in sequence. Vision automatically sends the print message to the result of the last expression. If you terminate the last expression with a semi-colon, the automatic printing is suppressed.
A block is a special form of program that represents a deferred sequence of steps. The general form for a block is illustrated below:
[ statement 1 ; statement 2 ; . . . ]Blocks always return the value of the last statement executed. Blocks are often used as parameters to other messages. For example, the ifTrue:ifFalse: message requires two parameters that are blocks that contain the program to run if a specific condition is (is not) met. For example:
myList count > 100 ifTrue: [ "This is a big list." print; ] ifFalse: [ "This is a small list." print; ];If the number of elements in the list myList is greater than 100 the program supplied by the first block is executed. In this case the string "This is a big list" will be displayed. Otherwise, the program supplied by the second block is executed. In this case the string "This is a small list" will be displayed.
Variables
It is often useful to save the results of specific tasks for later use. For example, type in the following program:
!x <- 2 + 2The answer 4 should appear. Now type:
x + 1You should see the answer 5.
The results of any Vision expression can be saved into a Variable. You can create as many variables as you need during the course of your session. Any information that you save into a variable is available for the remainder of your session or until you redefine the variable.
To create a variable, type ! followed by a valid variable name. The <- message is used to assign a value into this variable. For example, enter the following program:
!x <- 2 ; !y <- 3 ; !z <- x + y ; "The Value Of x is: " print; x printNL; "The Value Of y is: " print ; y printNL ; "The Value of x + y (z) is: " print; z printNL ;Your should see:
The Value Of x is: 2 The Value Of y is: 3Variables can be used to store values other than numbers. For example, create a variable called answer containing the string: "The Answer Is:" as illustrated below:
The Value of x + y (z) is: 5.00
!answer <- "The Answer Is: "Now try the following program:
answer print; 2 + 2You should see:
The Answer Is: 4.00