Introduction to Programming with Arrays Using ELI
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Programming with Arrays using ELI by Wai-Mee Ching July, Nov., Dec. 2014 Copyright © 2014 Contents 1. Array, List and Primitive Operations in ELI ......................................................................................................... 3 1.1 Computers and Programming Languages ............................................................................................................ 3 1.2 ELI System and its Data Types ............................................................................................................................ 6 1.3 Shape of Data, Reshape and Data Conversion ................................................................................................... 10 1.4 Mathematical Computations .............................................................................................................................. 13 1.5 Comparisons, Selection, Membership and Index of .......................................................................................... 17 1.6 Array Indexing, Indexed Assignment and taking Sections ................................................................................ 21 1.7 Array Transformations ....................................................................................................................................... 26 1.8 Operators and Derived Functions ...................................................................................................................... 31 1.9 Lists and Operations on Lists............................................................................................................................. 34 2. Defined Functions, Control Structures and Files ................................................................................................. 38 2.1 Defined Functions, Short-Form and Order of Evaluation .................................................................................. 38 2.2 One-liner Functions .......................................................................................................................................... 41 2.2.1 number conversion and lexicographic ordering .......................................................................................... 41 2.2.2 a queuing network model ............................................................................................................................ 44 2.3 Control Structures .............................................................................................................................................. 46 2.4 Recursion .......................................................................................................................................................... 47 2.4.1 sorting ......................................................................................................................................................... 47 2.4.2 tower of Honoi ............................................................................................................................................ 50 2.4.3 determinant ................................................................................................................................................. 51 2.5 Script Files ......................................................................................................................................................... 52 3. Array Implementation of Data Structures ............................................................................................................... 54 3.1 Emulation of PASCAL Data Structures in ELI ................................................................................................. 54 3.1.1 a small database for a company .................................................................................................................. 54 3.1.2 implementation of linked lists in ELI ......................................................................................................... 58 3.1.3 implementation of queries to a database ..................................................................................................... 61 3.2 Binary Trees ..................................................................................................................................................... 63 3.2.1 tree representation ....................................................................................................................................... 63 3.2.2 tree operations ............................................................................................................................................. 65 3.2.3 tree transversals .......................................................................................................................................... 68 3.3 Quad Trees ........................................................................................................................................................ 70 3.4 Graph Algorithms ............................................................................................................................................. 72 3.4.1 graph representations .................................................................................................................................. 72 3.4.2 depth first search ......................................................................................................................................... 73 3.4.3 single-source source shortest path .............................................................................................................. 74 4. Computational Algorithms ..................................................................................................................................... 77 4.1 Iterative Method ................................................................................................................................................ 77 4.2 Simple Encryption and Monte Carlo Method .................................................................................................... 79 4.3 Sparse Matrix Computation ............................................................................................................................... 82 References .................................................................................................................................................................... 85 The process of preparing programs for a digital computer is especially attractive, not only because it can be economically and scientifically rewarding, but it can be an aesthetic experience much like composing poetry or music. -D. Knuth, the Art of Programming By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and in effect increases the mental power of the race. -A.N. Whitehead, quoted in Ken Iverson’s Turing Lecture Preface The mathematician and computer scientist Jacob T. Schwartz once remarked that programming (and programming languages) consists of two sides: internal and external. The internal side is of a mathematical nature concerning the algorithmic transformation of data. The external side is of a mundane nature concerning system interface and human factors. In the programming language APL, designed by Ken Iverson, language idiosyncrasies arising from the external side have been kept to a bare minimum so a programmer can concentrate his effort on the algorithmic side of a programming job, and its programming environment is particularly simple. APL treats arrays as its primary data structure, and provides a comprehensive set of array operations as language primitives each denoted by one character in a special font (APL font). As Alan Perlis once pointed out APL encourages a dataflow style of programming , i.e. organizing tasks in chains of functions and operators on arrays, or what we call array- oriented programming . This combination of succinct notation and powerful primitives allows an APL programmer to have a clearer global view of his programming constructs than in any other language, with the possible exception of SETL, since many low level details have been suppressed. While the APL font used to denote primitives achieves succinctness with exquisite beauty it presents difficulties for input/output, exchange of code segments in mails, and APL systems often do not communicate easily with ASCII-based text files. ELI is an array programming language based on APL where primitive operations are denoted by one or two ASCII characters thus maintaining the one-character one symbol principle of APL to preserves APL’s dataflow style in array-oriented programming . ELI has the same programming environment centered on a workspace as that in APL, it also makes input/output of ASCII-based text files containing code as well as data much easier. ELI has all the language features of the classical APL [9]; it does not have the general array feature of APL2 [13] but it provides lists and basic operations on lists to deal with irregular or non-homogeneous data . In addition, ELI supports complex numbers , symbol type and temporal data . Any discussion of program design inevitably brings up the question of efficiency . There are actually two different kinds of efficiency which are of concern here: the human programming efficiency and the machine execution efficiency . In other words, we like to know how quickly a working program can be implemented, and how