Recall the following simple example:
TO TESTPOS :NUMBER IF :NUMBER > 0 [PRINT [THE NUMBER IS POSITIVE.]] ENDThis procedure used the PRINT command to report a conclusion about the argument :NUMBER. It is a good first example of the use of the IF command, but otherwise it is greatly lacking in elegance. This exercise presents several improvments to this procedure.
You can make the first improvement to TESTPOS on your own: write a procedure TESTSIGN that uses IFELSE to print one message if the argument :NUMBER is negative and a different message if the argument is non-negative.
It's good to know that whether the argument given to TESTSIGN is negative or not, but the output of TESTSIGN would be more useful if we also knew what the argument was. This information may not be available to the reader if TESTSIGN is called from within a program, as in the following example:
IF :DEBUG = :TRUE [TESTSIGN XCOR]
Of course there is a way to include the value of the argument in
the output of the procedure. The SENTENCE command
(abbreviated SE) will join any number of lists into a single
list. Here's an example of a TESTSIGN procedure that gives
some slightly more useful output:
TO TESTSIGN :NUMBER IFELSE :NUMBER > 0 ~ [PRINT (SENTENCE [THE NUMBER] :NUMBER [IS POSITIVE.])] ~ [PRINT (SENTENCE [THE NUMBER] :NUMBER [IS NOT POSITIVE.])] ENDType this in and run it. Be sure to include the tildes at the ends of the third and fourth lines; these are used to tell the computer to continue reading the current command on the next line. Notice that the two lists of commands given to the IFELSE command have been indented to make the code easier to read.
You've made the output of the TESTSIGN procedure much more informative. However, if you ever share this procedure with anyone else, you will not be able to control the input. Recall the old saying "Garbage in, garbage out". Try typing TESTSIGN "TESTSIGN and see what happens!
Our next task is to check that the argument given to TESTSIGN is actually a number. UCB LOGO includes a NUMBER? predicate to makes this test:
TO TESTSIGN :NUMBER IF NUMBER? :NUMBER ~ [IFELSE :NUMBER > 0 ~ [PRINT (SENTENCE [THE NUMBER] :NUMBER [IS POSITIVE.])] ~ [PRINT (SENTENCE [THE NUMBER] :NUMBER [IS NOT POSITIVE.])]] ENDThis version of TESTSIGN checks to see if the input is valid before comparing it to 0. If the input is not a number, it prints nothing at all. Try to edit the procedure so that it will output some warning if the argument :NUMBER is not a number. Use indenting and the ~ character to make your code easier to type in and easier to read. Add comments (prefaced by a ;) if you get confused as to which part of the code does what.
Testing for valid input and carefully formatting output are two very important parts of writing computer programs. UCB LOGO includes the more sophisticated error handling commands CATCH and THROW, which you can read about in the user manual. Modern computer programs output to complex graphical user interfaces, which are beyond the scope of this version of LOGO.