APL\1130 Primer Student Text
Total Page:16
File Type:pdf, Size:1020Kb
IBM APL\1130 Primer Student Text Student Text APL\1130 Primer ACKNOWLEDGEMENTS This Primer was written by Paul Berry of the IBM Research Division, T. J. Watson Research Center, York·town Heights, New York; it was adapted for use with the IBM 1130 from another APL Primer by the same author. The basic text benefitted greatly from the suggestions, criticisms, and comments of the readers of the various early drafts, and especially those of A. D. Falkoff, K. E. Iverson, J. C. McPherson, L. M. Breed, and R. H. Lathwell. The adaptation for the APL\1130 System was assisted by S. M. Raucher of the IBM Data Processing Division, Wheaton, Maryland, and Miss C. A. Conroy, who ran the sample problems at the 1130 console. NOTICE This text is distributed by IBM as a service to users of the 1130 computing system and to those who are interested in the APL language. Publication of this text does not imply support of the APL\1130 program; that program is a contributed program, distributed but not maintained by IBM. It is not part of the IBM product line, and has not been subject to product testing. Recipients of the APL\1130 program are expected to make their own final evaluation of its usefulness. This text contains description of certain features which were not provided in the original APL\1130 program distributed in February, 1968. These features include the use of labels (Chapter 13), common libraries (Chapter 15), and passwords for the locking of workspaces and sign-on numbers. Copies of this and other IBM publications can be obtained through IBM branch offices. Address comments concerning the contents of this publication to: IBM, Technical Publications Department, 112 East Post Road, White Plains, N. Y. 10601 (0 International Business Machines Corporation 1968 j i CON TEN TS =============== PART I 1: INTRODUCTION 1 vvhy an APL Primer? 1 lihat Is a Computer Program? 2 What Is a Programming Language? 3 Power, Relevance, and Simplicity 3 How to Read This Primer 4 2: COMMUNICATING WI'I'H THE COMPUTER 5 The APL Typeface 5 What Equipment Do You Need? 6 Getting Started 6 Getting Started From the Console 6 Getting Started From a Terminal 7 Whose Turn to Type? 8 Distinguishing Who Typed What 9 Fixing Typing Errors as You Go 9 Overstrikes Not Allowed 9 Visual Fidelity 10 Interrupting the Computer 10 3: BASIC OPERATIONS IN ARITHMETIC 11 The Workspace 11 The Two Modes of Operation: Execution vs. Definition 11 Addition, Subtraction, Multiplication, and Division 12 The Idea of a Variable 13 Storing or Printing the Result of a Calculation 13 Using the Stored Value of a Variable 14 possible Names for Variables 15 Storing a Result Under a Name that Has Already Been Used 15 Referring to a Variable Which Has No Value 16 Examples of Arithmetic with Variables 16 4: HORE ARITHMETIC OPERATIONS 19 Negation 19 Reciprocals 19 Monadic and Dyadic Operators 19 Raising to a Power (Exponentiation) 20 Taking a Root 20 Haximum: Taking the -Larger... of Two Numbers 21 Minimum: Taking the Smaller of Two Numbers 22 The Floor and the Ceiling of a Number 23 Rounding to the Nearest Integer 24 Summary of Arithmetic Operators Mentioned Thus Far 24 iii 5: SEVERAL OPERATIONS IN THE SAME INSTRUCTION 25 Order in Which Operations Get Executed in APL 25 Use of Parentheses 27 Rewriting the Earlier Examples With Several Operations in the Same Instruction 29 Do You HAVE to Write Many Operations in A Single APL Instruction? 30 6: ENTERING THE DEFINITION OF A PROGRAM SO THAT IT CAN BE USED REPEATEDLY 31 Starting the Definition 31 Focal Length of a Lens: A Simple Calculation to Illustrate Program Definition 32 Sample Use of the Program Just Defined 34 Another Sample Program: Efficiency of a Diesel Engine 35 Writing the DIESEL Program in a Single Line 36 An Instruction in One Program Can Call for the Execution of Another Program 37 7: DISPLAYING OR CHANGING THE PROGRAM AFTER YOU'VE DEFINED IT 39 Adding Another Line 39 Replacing a Line 40 Displaying What Is Already on a Line 40 Displaying the Whole Stored Definition 42 Inserting a Line Between Lines That Are Already Defined 42 Deleting a Line of the Definition 44 Deleting a Program Entirely from Your Workspace 45 Deleting a Variable from Your Workspace 45 8: REPRESENTING NUMBERS 47 Deci.mal Form 47 Exponential Form 48 Negative Numbers 49 Negative Numbers in Exponential Form 49 Very Small Numbers S,O Precision of Numbers 50 Number Display 50 ~~ich Form Does the Computer Use? 51 9: TESTING THE TRUTH OF A RELATIONSHIP 53 Example of Test for Equal 54 Example: The Sign Function 55 How Close is Equal? 55 iv 10: MORE OPERATIONS IN ARITHMETIC 57 Absolute Value 57 Residue and Remainder 57 Powers of the Natural Constant e 58 Logarithms 59 Natural Logarithms 60 Antilogs 60 Logical Operations 60 Logical Or 61 Logical And 61 Exclusive OR 62 Not: Logical Negation 63 11: CONTROLLING THE SEQUENCE IN WHICH THE LINES OF A PROGRAM ARE EXECUTED 65 "Ordinary" Order of Execution 65 Branches 65 Branching Out of a Program 66 Computed Branches 66 The Factorial: An Example of a Program With a Branch 67 Program Loops 68 The Roots of a Quadratic: Another Example of a Program With a Conditional Branch Out 68 Branch or Continue 69 The Factorial Again: An Improved Version Using Two Branch Instructions 71 12: ARRM~GING THE WAY THE PROGRAM TYPES ITS OUTPUT 75 Printing Text 75 Results and Heading Appearing on the Same Line 78 13: LINE LABELS FOR EASIER BRANCHING 79 14: WHAT TO DO WHEN THE PROGRAM STOPS 81 Halt When an Instruction in Your Program Can't Be Executed 81 A Program Error Doesn't Mean That Execution is AllOver 82 Resuming Execution 83 Where Was Work Suspended? 84 Area of a Segment of a Circle: Illustrating Procedure for Correcting a Mistake in a Program 84 Tracing the Execution of a Program 86 Trace Can Be Controlled by the Program Itself 87 v 15: SYSTEM COMMANDS 89 Distinguishing System Commands from Other Intructions 89 Signing On 89 Signing Off 90 Establishing a Sign-On Password 90 Saving a Workspace 90 Getting Back a Saved Workspace 91 Getting a List of the Workspaces You Have Saved 92 Dropping a Workspace from Your Library 92 Loading a Workspace from a Common Library 93 Loading a.Workspace From the Private Library of Another User 93 Revising a Workspace You've Saved 93 Loading a Workspace and then Saving it Under a Different Name 94 Clearing the Active Workspace 94 Diagram Summarizing Information Flow Between You, Your Active Workspace, and Saved Workspaces 94 PART II 16: VECTORS: PARALLEL PROCESSING OF THE ELEMENTS OF ARRAYS 97 Entering a Vector of Numbers 97 Parallel Processing of Vectors 98 Using Parallel Processing In Some of the Problems Introduced Earlier 99 Vectors Must Have Matching Lengths 99 Extending a Single Number To Match the Length of a Vector 100 Parallel Processing Requires All The Elements To Be Treated in the Same Way 102 Adjusting a Formula to Facilitate Work With Vectors 103 A Vector in a Branch Instruction 104 17: "REDUCING" A VECTOR: APPLYING THE SA:rJ!...E OPERATION TO ALL THE ELEMENTS 105 Summation 105 Product 106 Maximum Reduction: Looking for the Largest 106 Minimum Reduction: Looking for the Smallest 107 OR Reduction: Looking for "Any" 107 AND Reduction: Looking for "All" 108 Example Using the Sum of Products: Price Times Quantity Ordered 109 The Area Under a Curve 109 vi 18: GENERATING ARRAYS AND FINDING THEIR DIMENSIONS III Generating an Array by Restructuring III Vectors of Literal Characters 112 An Array Can Have Zero Length 113 Generating Consecutive Integers 113 Finding Out How Long a Vector Is 114 ~vhat is the Length of a Single Number? 116 Another Example Using Parallel Processing of Vectors: The Correlation Coefficient 117 19: SELECTING PARTICULAR ELEMENTS FROM AN ARRAY BY USING INDEX Nill1BERS 119 Respecifying Certain Elements Within an Array 119 The Index Numbers May Result From an Expression 120 Indexing an Expression 120 Indexing by an Empty Vector of Indices 121 Indexing a Matrix 121 20: FINDING THE INDEX NUMBERS THAT LOCATE PARTICULAR ELEMENTS WITHIN A VECTOR 123 Finding Several Indices at Once 123 Indexing Works Just as Well for Arrays of Literal Characters 124 Looking for the Index Number of a Value that Isn't There 125 The Index for a Value That Occurs at Several Locations in the Vector 126 An Example Using Iota to Find Index Numbers: Evaluating Hexadecimal Representations 126 21: CATENATION: BUILDING A VECTOR BY CHAINING ITEMS TOGETHER 129 Building a Vector of Results by Catenating the Latest Result to the Earlier Ones 131 Example Using Catenation: Accumulating Primes 131 Making Any Variable Into a Vector 133 Maximum Length of Vectors 133 Inserting New Elements Between Existing Elements of a Vector 133 Building Pascal's Triangle: An Example Using Catenation 135 22: LOOPS 137 Exit From a Loop 137 Leading Decisions 138 Standard Procedure for Writing a Loop with a Counter 139 An Iterative Program to Print an Interest Table 140 vii Alignment of Output in Columns 141 Interest Table with Output as a Matrix 141 Interest Table with Fixed Format on Each Line 142 A Footnote: The PRINT Program 144 Repaying the Bank 145 An Iterative Program for Finding Prime Factors 146 23: COMPRESSION: SELECTING SOME ELEMENTS FROM A VECTOR AND OMITTING OTHERS 149 Tests of the Truth of a Relationship Provide the Zeroes and Ones Needed to Control Compresssion 150 Example: Compression and the Sieve of Eratosthenes 151 Another Program Using Catenation and Compression: Sorting the Elements of a Vector 154 A Useful Variant of the Sorting Program 155 Why the Branch-or-Continue Instruction Includes a Compression 157 24: THE PROGRAM ASKS FOR INPUT,