Apr 11, 2002 - You are an experienced C programmer who has heard about J, and you think you'd like to see what it's all
J For C Programmers Henry Rich 2002/04/11 Copyright © 2002 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 1 0 emp_client) * empprofit 0 will have one item per employee, each item having the amount billed to each client; we sum these items to produce a list with the profit from each client, and use that to order the client numbers. Solution: custbyprofit =: monad define clientlist \: +/ (clientlist="1 0 emp_client) * empprofit 0 ) For our final problem in the payroll department, consider how to calculate withholding tax on each employee's earnings. The tax rate within tax brackets will be assumed to be the same for all employees. C code for this would look like:
61
int renderuntocaesar(shekels) float shekels[]; // result: withholding for each employee { // tax-bracket table: start of each bracket, ending with high value float bktmin[4] = {0,10000,20000,1e9}; // tax-bracket table: rate in each bracket float bktrate[3] = {0.05,0.10,0.20}; int earns[nemp]; int i, j; empearnings(earns); // get earnings for each employee for(i = 0;i. (bracket_top UsedToSelect _3 0 3 |valence error | 0 >UsedToSelect _3 0 3 This happens because explicitly-defined adverbs produce verbs that may have monadic and dyadic valences, and we have defined only the monadic. We could go ahead and code a dyadic case, but that seems a pity because what we want in either case is verb # ]; maybe we can find a tacit form to produce that. Our first thought should be to ask the interpreter for help using 11 :, but since our adverb refers to u. and y., that is doomed to fail. So, we run our finger down the fragment table, looking for lines that produce an adverb, and then seeing if the definition seems useful. Sure enough, A0 V1 V2 is just what we want, so we try UsedToSelect =: ]: # ] and it works: 0&> UsedToSelect _3 0 3 _3 0 > UsedToSelect _3 0 3 _3 Since UsedToSelect is tacit, we can even see the result of its application to an operand: > UsedToSelect > # ] Yes, that's the derived verb we wanted. Now that we see the answer we realize that we could have used 11 : after all if we had thought of the proper way to write it: UsedToSelect =: 11 : 'x. # ]' ]: # ] That's easier to understand, so that's the definition we'll use.
Graduate Project For Gurus As a final project, we will define the conjunction LoopWithInitial which we promised as an alternative to writing a hard-to-follow sequence of operations. We expect u LoopWithInitial n to produce the verb u&.>/\. &.(,&(/\. &.(,&(y.))&.|.&.(/\.&.(,&(: dyad · 45 >: monad · 45 A a: · 70 agreement of operands · 36 Amend adverb } · 127 anonymous entity · 4 Antilog verb ^ · 80 Apply At Level conjunction L: · 144 Apply On Subsets adverb /. · 112 Apply Under Transformation conjunction &. · 99 array · 15 assignment · 6 multiple · 89 private · 132 public · 133 atom · 16 atomic representation · 84 axis · 17 order · 17 B Behead verb }. · 52 Boolean Functions verb m b. · 144 Box verb < · 69 boxing · 69
character constants · 5, 18 comments in code · 98 comparison tolerance !. · 146 compound verb · 65 Constant verb m"n · 53 control structures · 130 for. do. end. · 130 if. do. else. elseif. end. · 130 return. · 131 try. catch. end. · 131 while. whilst. do. end. · 130 copula · 6 Copy verb # · 46 Curtail verb }: · 52 D Deal verb ? · 143 derived entity · 4 derived verb · 65 Do verb ". · 121 DoWhile conjunction u^:v^:_ · 83 Drop verb }. · 48 E e. dyad · 47 E. dyad · 116 Element Of verb e. · 47 empty cells · 75 empty operands · 73 Enfile verb , · 51 entity · 3 error handling · 106, 131 F
C C belittled · ii derided · 41 J equivalent for class · 132 structure equivalent in J · 72 calling a DLL · 110 cd verb · 110 cell · 16
f. · 168 Factorial verb ! · 80 Fetch verb {:: · 93 fill · 70 framing · 32 verb · 48 with empty operands · 73 Find Sequence verb E. · 116 Fit conjunction !. · 48 3
Flatten adverb · 168 Foreign conjunction !: · 105 form editor · 149 format for printing · 106 frame · 16, 26, 33 From verb { · 46 G gerund · 85 Grade verbs /: \\: · 53 H Head verb {. · 52 I i. dyad · 47 i. monad · 22 i: · 148 Identity adverb ]: · 175 Identity conjunctions [. ]. · 175 identity element · 56 Identity verbs [ ] · 66 If conjunction ^: · 83 Index Of verb i. · 47 Infix adverb · 77 input and output · 105 interrupting execution · 9 inverse of verb · 99 Invoke Gerund conjunction `: · 120 item · 17 Itemize verb ,: · 53 J J Forum · 9 L L. monad · 94 L: · 144 Laminate verb ,: · 49 Level verb L. · 94
4
Link verb ; · 87 list · 17 load verb · 8 locale changing current · 134 current · 132 defined · 132 locative · 132 name lookup · 133 search path · 133 used for modular programming · 137 z · 136 Log verb ^. · 80 M m :n · 95 m b. · 144 m"n · 53 m&v monad · 67
[email protected] · 85 m`:6 · 120 m} dyad · 127 m} monad · 142 Match verb -: · 51 matrix verbs · 149 memory management · 111 mema · 111 memf · 111 memr · 111 memw · 111 modification in place · 128, 142 modifier · 4 user-defined · 138 Modifier order of execution · 55 Modify adverb } · 127 modular code · 132 N name · 4 Nub Sieve verb ~: · 112 Nub verb ~. · 112 Number Of Combinations verb ! · 79 Numbers verb ". · 109
O o. (trigonometric function) · 45 obverse of verb · 99 defined by u :.v · 101 discussed under ^: · 99 P parts of speech · 3 passing a verb as an argument · 119 performance measurement · 102 plot package · 148 Power conjunction ^: · 82 primitive · 4 printf · 108 punctuation · 4 R random numbers · 143 rank · 15, 20, 22, 26, 96 negative · 32 Rank conjunction " · 29 Ravel verb , · 51 Raze verb ; · 88 recursion · 143 Remove verb -. · 47 Reverse verb |. · 52 Right Shift verb |.!.f · 147 Roll verb ? · 143 Root verb %: · 79 Rotate Left verb |. · 49 S S: · 144 scalar · 16 sentence · 4 shape · 15 Shift Left verb |.!.f · 50 Sort Using verb /: \\: · 50 Sort verb /: \\: · 51 Spread verb #^:_1 · 142 Square Root verb %: · 80 Stitch verb ,. · 49
Suicide verb [: · 97 T Table adverb / · 143 tacit programming anonymous verb · 155 explicit-to-tacit converter · 171, 177 fork · 153 forks and hooks · 164 fragment table · 174 improving readability · 168 introduction · 151 parsing and execution · 155 parsing table · 158 summary of execution · 158 tacit modifiers · 173 trains · 153 Tail verb {: · 52 Take verb {. · 48 Tie conjunction ` · 85 Transpose verb |: · 52, 147 type · 16 U u ::v · 106 u :v · 97 u"n · 29 u&.v · 99 u&:v · 101 u&n monad · 67 u&v · 101 u/ dyad · 143 u/ monad · 55 u/. dyad · 112 u/\\ monad · 76 u@:v dyad · 65 u@v dyad · 66 u\\ dyad · 77 u\\ monad · 76 u\\. dyad · 77 u\\. monad · 77 for performance · 104 u^:_1 (obverse) · 99 u^:n · 82 5
u^:v · 83 u`v · 85 undefined names · 163
verb definition · 95 W
V valence · 6, 97
6
window driver · 149 word · 3, 4