Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Rich H.J for C programmers.2006.pdf
Скачиваний:
18
Добавлен:
23.08.2013
Размер:
1.79 Mб
Скачать

J

For C Programmers

Henry Rich

2006/01/25 (J6.01)

Copyright © 2004 Henry H. Rich. All rights reserved. Send comments to HenryHRich@nc.rr.com

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 nonIversons, 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).

2005/01/25 Account for 6.01 changes. Describe dyad I.

iv

 

Contents

 

Foreword.............................................................................................................................

ii

 

Acknowledgements..................................................................................................

iii

 

Change History ........................................................................................................

iii

1.

Introduction...................................................................................................................

1

2.

Culture Shock................................................................................................................

2

Programming In J

5

3.

Preliminaries .................................................................................................................

6

 

Notation Used in This Book .....................................................................................

6

 

Terminology..............................................................................................................

6

 

Sentences (statements) ...........................................................................................

7

 

Word Formation (tokenizing rules) ........................................................................

7

 

Numbers....................................................................................................................

8

 

Adjacent Numbers Form a Single Word ..............................................................

8

 

Adjacent Named Nouns Do NOT Form a Single Word.......................................

9

 

Characters .................................................................................................................

9

 

Valence of Verbs (Binary and Unary Operators) .................................................

9

 

How Names (Identifiers) Get Assigned.................................................................

10

 

Order of Evaluation ................................................................................................

10

 

How Names Are Substituted...................................................................................

11

 

What a verb (function) looks like...........................................................................

12

 

Running a J program...............................................................................................

13

 

Interrupting Execution........................................................................................

14

 

Errors ..................................................................................................................

14

 

The Execution Window; Script Windows ..............................................................

14

 

Names Defined at Startup .......................................................................................

15

 

Step-By-Step Learning: Labs..................................................................................

15

 

J Documentation .....................................................................................................

16

 

Getting Help............................................................................................................

16

4. A First Look At J Programs ........................................................................................

17

 

Average Daily Balance ...........................................................................................

17

 

Calculating Chebyshev Coefficients.......................................................................

20

5.

Declarations ................................................................................................................

22

 

Arrays......................................................................................................................

22

 

Cells ........................................................................................................................

23

 

Choosing Axis Order..........................................................................................

24

 

Negative Cell-Rank; Items .................................................................................

24

 

Lists ....................................................................................................................

24

 

Phrases To Memorize .............................................................................................

25

 

Constant Lists .........................................................................................................

25

 

Array-creating Verbs ..............................................................................................

25

 

Dyad $ ($hape) and monad $ ($hape Of) ..........................................................

25

v

Monad # (Tally) .................................................................................................

30

Monad i. (Integers) ..........................................................................................

30

6. Loopless Code I—Verbs Have Rank..........................................................................

32

Examples of Implicit Loops....................................................................................

32

The Concept of Verb Rank .....................................................................................

34

Verb Execution—How Rank Is Used (Monads) ....................................................

34

Controlling Verb Execution By Specifying a Rank................................................

36

Examples Of Verb Rank .........................................................................................

37

fndisplay—A Utility for Understanding Evaluation........................................

40

Negative Verb Rank................................................................................................

41

Verb Execution—How Rank Is Used (Dyads) .......................................................

42

Concatenating Lists: Dyad , (Append)..............................................................

44

When Dyad Frames Differ: Operand Agreement ...................................................

45

Order of Execution in Implied Loops .....................................................................

49

A Mistake To Avoid ...............................................................................................

49

7. Starting To Write In J .................................................................................................

51

8. More Verbs .................................................................................................................

54

Arithmetic Dyads....................................................................................................

54

Boolean Dyads........................................................................................................

55

Min and Max Dyads ...............................................................................................

55

Arithmetic Monads .................................................................................................

55

Boolean Monad.......................................................................................................

55

Operations on Arrays ..............................................................................................

56

Dyads..................................................................................................................

56

Monads ...............................................................................................................

62

9. Loopless Code II—Adverbs / and ~..........................................................................

66

Modifiers.................................................................................................................

66

The Adverb Monad u/ ...........................................................................................

66

The adverb ~ ...........................................................................................................

68

10. Continuing to Write in J..............................................................................................

70

11. Boxing (structures)....................................................................................................

76

Terminology............................................................................................................

78

Boxing As an Equivalent For Structures In C ......................................................

79

12. Compound Verbs ........................................................................................................

80

Verb Sequences—u@:v and u@v..........................................................................

80

The Difference Between u@:v and u@v ...........................................................

81

An Exercise in @ and @: ....................................................................................

82

Making a Monad Into a Dyad: The Verbs [ and ]...................................................

84

Making a Dyad Into a Monad: u&n and m&v.........................................................

85

13. Empty Operands..........................................................................................................

87

Execution On a Cell Of Fills...................................................................................

87

Empty cells .............................................................................................................

89

If Fill-Cells Are Not Enough ..................................................................................

89

14. Loopless Code III—Adverbs \ and \. .......................................................................

90

15. Verbs for Arithmetic...................................................................................................

93

Dyads ......................................................................................................................

93

vi

Monads (all rank 0).................................................................................................

94

16. Loopless Code IV .......................................................................................................

95

A Few J Tricks........................................................................................................

95

Power/If/DoWhile Conjunction u^:n and u^:v ................................................

96

Tie and Agenda (switch) ........................................................................................

99

The Tie Conjunction u`v u`n m`v m`n......................................................

99

The Agenda (switch) conjunction m@.v .........................................................

100

17. More Verbs For Boxes..............................................................................................

101

Dyad ; (Link) And Monad ; (Raze)....................................................................

101

Dyad , Revisited—the Case of Dissimilar Items ............................................

103

Verbs With More Than 2 Operands—Multiple Assignment ...........................

103

Dyad { Revisited: the Full Story ..........................................................................

104

Simplification 1: Remove Inner Boxing If Selectors Are Scalars....................

106

Simplification 2: Remove All Boxing To Select Full Items ............................

106

Split String Into J Words: Monad ;: ...................................................................

107

Fetch From Structure: Dyad {:: .........................................................................

107

Report Boxing Level: Monad L. .........................................................................

108

18. Verb-Definition Revisited.........................................................................................

110

What really happens during m :n and verb define .....................................

110

Compound Verbs Can Be Assigned .....................................................................

111

Dual-Valence verbs: u :v...................................................................................

112

The Suicide Verb [: ............................................................................................

112

Multi-Line Comments Using 0 :0 .....................................................................

113

Final Reminder .....................................................................................................

113

19. u^:_1, u&.v, u&.:v, and u :.v ........................................................................

114

The Obverse u^:_1.............................................................................................

114

Apply Under Transformation: u&.v and u&.:v ................................................

114

Defined obverses: u :.v ....................................................................................

116

u&:v and u&v ...................................................................................................

116

An observation about dyadic verbs.......................................................................

117

20. Performance: Measurement & Tips..........................................................................

118

Timing Individual Sentences ................................................................................

118

Compounds Recognized by the Interpreter ..........................................................

120

Use Large Verb-Ranks! and Integrated Rank Support .........................................

121

Shining a Light: The J Performance Monitor .......................................................

122

21. Input And Output ......................................................................................................

125

Foreigns ................................................................................................................

125

File Operations 1!:n; Error Handling.................................................................

125

Error Handling: u ::v, 13!:11, and 9!:8.................................................

126

Treating a File as a Noun: Mapped Files..............................................................

126

Format Data For Printing: Monad And Dyad ": .................................................

127

Monad ":.........................................................................................................

128

Format an Array: 8!:n........................................................................................

129

Format binary data: 3!:n ....................................................................................

130

printf, sprintf, and qprintf....................................................................

131

vii

Convert Character To Numeric: Dyad ".............................................................

131

22. Calling a DLL Under Windows................................................................................

133

Memory Management...........................................................................................

134

Filling a Structure: Conversions.......................................................................

135

Aliasing of Variables ............................................................................................

135

Aliasing of Mapped Nouns...............................................................................

136

Aliasing of DLL Operands ...............................................................................

136

23. Socket Programming.................................................................................................

138

sdselect ...........................................................................................................

138

Asynchronous Sockets and socket_handler ................................................

138

Names and IP Addresses.......................................................................................

139

Connecting............................................................................................................

140

Listening ...............................................................................................................

140

Other Socket Verbs...............................................................................................

141

Datagrams.........................................................................................................

141

Socket Options..................................................................................................

141

Housekeeping ...................................................................................................

142

24. Loopless Code V—Partitions ...................................................................................

143

Find Unique Items: Monad ~. and Monad ~: ....................................................

143

Apply On Subsets: Dyad u/. ..............................................................................

143

Apply On Partitions: Monad u;.1 and u;.2.....................................................

145

Apply On Specified Partitions: Dyad u;.1 and u;.2......................................

146

Find Sequence Of Items: Dyad E....................................................................

147

Multidimensional Partitions .............................................................................

148

Apply On Subarray: Dyad u;.0..........................................................................

148

Apply On All Subarrays: Dyad u;.3 and u;._3 ..............................................

149

Extracting Variable-Length Fields Using ^: and ;.1 ........................................

150

Example: Combining Adjacent Boxes..................................................................

151

25. When Programs Are Data .........................................................................................

153

Calling a Published Name.....................................................................................

153

Using the Argument To a Modifier ......................................................................

153

Invoking a Gerund: m`:6.....................................................................................

154

Passing the Definition Of a Verb: 128!:2 (Apply)............................................

155

Passing an Executable Sentence: Monad ". and 5!:5.......................................

156

26. Loopless Code VI .....................................................................................................

157

27. Loopless Code VII—Sequential Machines...............................................................

161

28. Modifying an array: m}.............................................................................................

164

Monad I.—Indexes of the 1s in a Boolean Vector.............................................

165

Modification In Place............................................................................................

166

29. Control Structures .....................................................................................................

167

for./do./end. and for_x./do./end..........................................................

167

while./do./end. and whilst./do./end. .................................................

167

if./do./else./end., if./do./elseif./do./end..................................

167

try./catch./catcht./end. and throw. .................................................

168

select./case./fcase./end. ......................................................................

168

viii

return...............................................................................................................

169

assert...............................................................................................................

169

30. Modular Code ...........................................................................................................

170

Locales And Locatives..........................................................................................

170

Assignment ...........................................................................................................

170

Name Lookup .......................................................................................................

171

Changing The Current Locale...............................................................................

172

The Shared Locale 'z'........................................................................................

175

Using Locales .......................................................................................................

175

31. Writing Your Own Modifiers ...................................................................................

177

Modifiers That Do Not Refer To x. Or y. .........................................................

177

Example: Creating an Operating-System-Dependent Verb .............................

178

Example: The LoopWithInitial Conjunction ........................................

179

Example: A Conjunction that Analyzes u and v..............................................

179

An Exception: Modifiers that Do Not Refer to u. or v. ................................

181

Modifiers That Refer To x. Or y. ......................................................................

181

32. Applied Mathematics in J .........................................................................................

184

Complex Numbers ................................................................................................

184

Matrix Operations.................................................................................................

184

Polynomials: p.....................................................................................................

185

Calculus: d., D., D:, and p.............................................................................. 186

Taylor Series: t., t:, and T...............................................................................

187

Hypergeometric Function with H. .......................................................................

187

Sparse Arrays: Monad and Dyad $. ....................................................................

188

Random Numbers: ?.............................................................................................

188

Computational Addons .........................................................................................

189

Useful Scripts Supplied With J.............................................................................

189

33. Elementary Mathematics in J....................................................................................

190

Verbs for Mathematics..........................................................................................

190

Extended Integers, Rational Numbers, and x:.....................................................

190

Factors and Primes: Monad p:, Monad and Dyad q:.........................................

191

Permutations: A. and C.......................................................................................

191

34. Graphics ....................................................................................................................

193

Plot Package..........................................................................................................

193

Plot Demo.........................................................................................................

193

Interfaces to Plot...............................................................................................

193

Commands and Options ...................................................................................

194

Data; Constructing a Plot with pd....................................................................

195

Quick Plots Using plot ..................................................................................

196

Generating Plots of Functions ..........................................................................

197

Assembling a Multipart Plot with pd...............................................................

198

Examples of Plots with Multiple Sets of Points ...............................................

199

Plots of Parametric Functions...........................................................................

199

Output Options .................................................................................................

199

2D Graphics: the gl2 Library................................................................................

199

ix

Example of Drawing.........................................................................................

200

Creating an isigraph Graphics Control.......................................................

200

Selecting an isigraph Control for Output ...................................................

201

Addressing the Screen ......................................................................................

201

Drawing Graphics.............................................................................................

201

Drawing Text....................................................................................................

201

The Drawing Surface........................................................................................

202

Screen Resizing: The paint Event.................................................................

202

Partial List of glxxx Drawing Commands.....................................................

202

isigraph Events ...........................................................................................

205

High Performance: Blocks of glxxx Commands Using glcmds.................

205

Displaying Tabular Data: the Grid Control ..........................................................

206

3D Graphics: OpenGL ..........................................................................................

206

35. Odds And Ends .........................................................................................................

207

Dyad # Revisited ..................................................................................................

207

Boxed words to string: Monad ;:^:_1 ..............................................................

207

Spread: #^:_1 .....................................................................................................

207

Choose From Lists Item-By-Item: monad m} ......................................................

207

Recursion: $: .......................................................................................................

208

Make a Table: Adverb dyad u/............................................................................

208

Cartesian Product: Monad { .................................................................................

209

Boolean Functions: Dyad m b............................................................................

210

Functions on Boolean operands........................................................................

210

Bitwise Boolean Operations on Integers ..........................................................

210

Operations Inside Boxes: u L: n, u S: n ......................................................

211

Comparison Tolerance !.f..................................................................................

213

Right Shift: Monad |.!.f ..................................................................................

213

Generalized Transpose: Dyad |: .........................................................................

214

Monad i: and Dyad i: .......................................................................................

214

Fast String Searching: s: (Symbols) ...................................................................

215

Fast Searching: m&i. ...........................................................................................

216

CRC Calculation...................................................................................................

216

Unicode Characters: u:........................................................................................

216

Window Driver And Form Editor.........................................................................

216

Tacit Programming

217

36. Tacit Programs ..........................................................................................................

218

37. First Look At Forks...................................................................................................

220

38. Parsing and Execution I ............................................................................................

222

39. Parsing and Execution II...........................................................................................

225

The Parsing Table .................................................................................................

225

Examples Of Parsing And Execution ...................................................................

226

Undefined Words..................................................................................................

230

40. Forks, Hooks, and Compound Adverbs....................................................................

231

Tacit and Compound Adverbs ..............................................................................

234

x

 

 

Referring To a Noun In a Tacit Verb....................................................................

235

41.

Readable Tacit Definitions .......................................................................................

236

 

Flatten a Verb: Adverb f.....................................................................................

236

 

Using f. to improve performance ...................................................................

237

42.

Explicit-To-Tacit Converter......................................................................................

239

 

Special Verb-Forms Used in Tacit Definitions.....................................................

240

43. Common Mistakes ....................................................................................................

241

 

Mechanics .............................................................................................................

241

 

Programming Errors .............................................................................................

242

44.

Valedictory................................................................................................................

245

45.

Glossary ....................................................................................................................

246

46.

Error Messages..........................................................................................................

250

47.

Index .........................................................................................................................

252

xi