For C Programmers
Total Page:16
File Type:pdf, Size:1020Kb
J For C Programmers Henry Rich 2006/04/15 (J6.01) Copyright © 2004, 2006 Henry H. Rich. All rights reserved. Send comments to [email protected] Foreword You are an experienced C programmer who has heard about J, and you think you'd like to see what it's all about. Congratulations! You have made a decision that will change your programming life, if only you see it through. The purpose of this book is to help you do that. It won't be easy, and it certainly won't be what you're expecting. You've learned languages before, and you know the drill: find out how variables are declared, learn the syntax for conditionals and loops, learn how to call a function, get a couple of examples to edit, and you're a coder. Fuggeddaboutit! In J, there are no declarations, seldom will you see a loop, and conditionals often go incognito. As for coding from examples, well, most of our examples are only a couple of lines of code—you won't get much momentum from that! You're just going to have to grit your teeth and learn a completely new way to write programs. Why should you bother? To begin with, for the productivity. J programs are usually a fifth to a tenth as long as corresponding C programs, and along with that economy of expression comes coding speed. Next, for the programming environment: J is an interpreted language, so your programs will never crash, you can modify code while it's running, you don't have to deal with makefiles and linking, and you can test your code simply by entering it at the keyboard and seeing what it does. If you stick with it, J won't just help the way you code, it'll help the way you think. C is a computer language; it lets you control the things the computer does. J is a language of computation: it lets you describe what needs to be done without getting bogged down in details (but in those details, the efficiency of its algorithms is extraordinary). Because J expressions deal with large blocks of data, you will stop thinking of individual numbers and start thinking at a larger scale. Confronted with a problem, you will immediately break it down into pieces of the proper size and express the solution in J—and if you can express the problem, you have a J program, and your problem is solved. Unfortunately, it seems to be the case that the more experience you have as a C programmer, the less likely you are to switch to J. This may not be because prolonged exposure to C code limits your vision and contracts the scope of your thinking to the size of a 32-bit word—though studies to check that are still under way and it might be wise for you to stop before it's too late—but because the better you are at C, the more you have to lose by switching to J. You have developed a number of coding habits: for example, how to manage loops to avoid errors at extreme cases; how to manage pointers effectively; how to use type-checking to avoid errors. None of that will be applicable to J. J will take advantage of your skill in grasping the essence of a problem—indeed, it will develop that skill considerably by making it easier for you to express what you grasp—but you will go through a period during which it will seem like it takes forever to get things done. During that period, please remember that to justify your choice of J, you don't have to be as expert in J as you were in C; you only have to be more productive in J than you ii were in C. That might well happen within a month. After you have fully learned J, it will usually be your first choice for describing a program. Becoming a J programmer doesn't mean you'll have to give up C completely; every language has its place. In the cases where you want to write code in C (either to use a library you have in C or to write a DLL for a function that is inefficiently computed in J), you will find interfacing J to DLLs to be simple and effective. This book's goal is to explain rudimentary J using language familiar to a C programmer. After you finish reading it, you should do yourself the honor of carefully reading the J Dictionary, in which you can learn the full language, one of the great creations in computer science and mathematics. Acknowledgements I am obliged to the reviewers who commented on earlier versions: Michel Dumontier, Ken Iverson, Fraser Jackson, David Ness, Richard Payne, Ewart Shaw, and Keith Smillie. Brian Schott, Nicholas Spies, and Norman Thomson exchanged emails with me at length to smooth over rough spots. David Steele conducted a painstaking review of several early drafts and suggested many changes great and small. Björn Helgason translated the text into Icelandic, finding a number of errors along the way. Kip Murray's 'review' became more of a dismantling, cleaning, and reassembly operation in which large sections of prose were rewritten as he pointed out to me their essential meaninglessness; the reader should be as grateful to him as I am. Without the patient explanations of my early teachers in J, Raul Miller and Martin Neitzel, I would have given up on J. I hope that this book pays to others the debt I owe to them. My current happy career as a J programmer would not have been possible without the work of the staff at Jsoftware, Inc., who created J. For the patriarch, the late Ken Iverson, I am unworthy to express admiration: I have only awe. I hope his achievement eases the lives of programmers for generations to come. To the rest, both Iversons and non- Iversons, I give my thanks. The implementation of the J interpreter has required diverse skills: architectural vision, careful selection of algorithms, cold-eyed project management to select features for implementation, robust and efficient coding, performance optimization, and expertise in numerical analysis. Most improbably, all these talents have resided in one man, Roger Hui il miglior fabbro. J gives us all a way to have a little of Roger's code in our own. We should aspire no higher. Change History 2002/6/18: Add chapters on mathematics in J, and section on Symbols; minor changes to wording; bring text up to J Release 5.01 2002/8/16: Minor additions; added section on aliasing; added chapter on sockets 2002/9/26: Added sections on fndisplay, integrated rank support, and ordering of implied loops iii 2002/11/14 Added explanatory sections, section on the J Performance Monitor, and chapter on Error Messages 2003/07/18 (J5.03) added section on I., updated f. Added chapter on frequent errors. Added section on run-length decoding 2004/10/31 (J5.04) ^:a:, vector cut. Added chapter on Sequential Machines 2004/11/22 Added section on monad { 2005/04/14 Fixed discussion of throw. 2005/09/03 (J6.01) added chapter on Graphics, discussion of noun forks, section on Fast Searching, section on CRC. Switched chapters 10 & 11, and added pictorial discussion of @ and @: . Added example on partitions. Added section on 3!:4 5 conversions. Explained the difference between (number number) and (noun noun). 2006/01/25 Account for 6.01 changes. Describe dyad I. Discuss 8!:n 2006/03/10 (J6.01 final): New parameters x, y, etc. Discuss dyad p: Update dyad ;: . Add discussion of precision. Describe assignment in tacit definition. Move chapter on Performance, and add recommendations for high-performance code. Improve discussion of object programming. 2006/04/15 Fix section on ;.0 . Elucidate L: . iv Contents Foreword............................................................................................................................. ii Acknowledgements..................................................................................................iii Change History ........................................................................................................iii 1. Introduction................................................................................................................. 12 2. Culture Shock.............................................................................................................. 13 Programming In J 16 3. Preliminaries ............................................................................................................... 17 Notation Used in This Book ................................................................................... 17 Terminology............................................................................................................ 17 Sentences (statements) ......................................................................................... 18 Word Formation (tokenizing rules) ...................................................................... 18 Numbers.................................................................................................................. 19 Adjacent Numbers Form a Single Word ............................................................ 19 Adjacent Named Nouns Do NOT Form a Single Word..................................... 20 Characters ............................................................................................................... 20 Valence of Verbs (Binary and Unary Operators)............................................... 20 How Names (Identifiers) Get Assigned................................................................. 21 Order of Evaluation ................................................................................................ 21 How Names Are Substituted................................................................................... 22 What a verb (function) looks like........................................................................... 23 Running a J program..............................................................................................