NYCJUG/2008-03-11
Self-reference for recursion, continuing problems naming symbols and ideas, a start on a "VB Rosetta" project, some ideas for puzzles and challenges, some ideas for a group project.
Meeting Summary
The format for this meeting was different from usual as we are exploring ideas for a group project. So, the "Advanced Topics" and "Learning and Teaching J" sections were replaced by two types of project ideas: short puzzles suitable for challenging people to match elegant J solutions with those in other languages, and larger, more useful projects that aim to contribute to the J code-base.
As we had a couple of novices turn up, more time than usual was spent covering some of the basics of J - why it has striking differences from other languages, its strengths and weaknesses, why each of us likes it, and so on.
Meeting Agenda for NYCJUG of 20080311
1. Beginner's regatta: self-reference "$" - how is it used? Why is it useful? See "$ Self-Reference.htm". What is "multiple, indirect assignment" called? How could one look up this? See "whatIsIndirAssignCalled.txt". 2. Show-and-tell: start of VB Rosetta: see "VB Rosetta - String Functions.doc". 3a. Some project/puzzle/challenge ideas: Finding contiguous blocks and gaps between them: see "gradeEGprobContigBlocks.txt". Inserting 1 and _1 for each zero: see "replaceZeroW1_1.txt". 3b. Some project ideas: Printing web pages well: see "BarCampNYC3FromFaceBook.doc". Porter Stemmer: see "PorterStemmer_AlgoDefn.txt", "PorterStemmer_js.txt", and "PorterStemmer_perl.txt" A new way of generating fractals: see "FractalNewWayMAA.doc". Directory tree use: see "Directory Tree Parsing.doc" +.--------------------+. However beautiful the strategy, you should occasionally look at the results. - Winston Churchill
Proceedings
Beginner's regatta
Explaining what "$:" (self-reference) is and how it's used turned out to be a rather unfortunate starting point for some of those completely new to the language as this is an advanced feature and is atypical of the way things are usually done in J. John mentioned that Roger has commented that he wouldn't include this if he were designing the language now.
However the documentation for "$:" has much to recommend it in spite of some immediate drawbacks. The supplied example uses "$:" for recursion to define the factorial function.
In some ways, this is generically a bad choice for a J example as the language already has a powerful built-in factorial function. However, both the problem and its naive solution are familiar to a lot of people. In fact, we briefly noted the usual recursive algorithm one would use for this and, upon examining the example, found that the example given for "$:" maps nicely to this well-understood version.
Expressed explicitly in a form much like any other language, the recursive version might look something like this:
factorialRecurse=: 3 : 0"0 if. 0>:y do. 1 else. y*factorialRecurse y-1 end. )
Or, given conventional languages' love of unnecessary whitespace:
factorialRecurse=: 3 : 0"0 if. 0>:y do. 1 else. y*factorialRecurse y-1 end. )
Visual Basic Rosetta: String Functions
Visual Basic String Functions Function Description InStr Returns the position of the first occurrence of one string within another. The search begins at the first character of the string InStrRev Returns the position of the first occurrence of one string within another. The search begins at the last character of the string LCase Converts a specified string to lowercase Left Returns a specified number of characters from the left side of a string Len Returns the number of characters in a string LTrim Removes spaces on the left side of a string RTrim Removes spaces on the right side of a string Trim Removes spaces on both the left and the right side of a string Mid Returns a specified number of characters from a string Replace Replaces a specified part of a string with another string a specified number of times Right Returns a specified number of characters from the right side of a string Space Returns a string that consists of a specified number of spaces StrComp Compares two strings and returns a value that represents the result of the comparison String Returns a string that contains a repeating character of a specified length StrReverse Reverses a string UCase Converts a specified string to uppercase
Starting with basic string functions: Left Returns the N leftmost characters of a string Right Returns the N rightmost characters of a string Mid Returns characters from the middle of a string J equivalents ------------- left0=: 4 : 'x{.y' NB. Example use: 4 left0 'hyperactive' hype NB. Another way to write this with only left-hand arguments: left1=: 3 : '(>0{y){.>1{y' NB. Example use: left1 5;'radioactive' radio NB. An advantage of the first way - easier to apply multiple arguments, e.g. 4 5 left0 &.>'hyperactive';'radioactive' +----+-----+ |hype|radio| +----+-----+ NB. as opposed to all-right-hand arguments: left1 &.> (4;'hyperactive');<5;'radioactive' +----+-----+ |hype|radio| +----+-----+ NB. Analogously, for "Right": right0=: 4 : 'y{.~-x' NB. or right0=: 4 : '(-x){.y' NB. and right1=: 3 : '(->0{y){.>1{y' 6 right0 'hyperactive' active right1 6;'hyperactive' active However, whereas "Left" and "Right" are arbitrary names, the J verb "take" ({.) relates these two concepts to each other simply and logically: _6 left0 'hyperactive' active _4 right0 'hyperactive' hype Also, where "Left" and "Right" only work on a limited class of things, the J equivalent is much more general: NB. Works on numeric vectors: 4 left0 1 2 3 4 5 6 7 8 1 2 3 4 NB. Also works on tables: ]mat=. i. 3 3 0 1 2 3 4 5 6 7 8 2 left0 mat 0 1 2 3 4 5 3 right0 1 2 3 4 5 6 7 8 6 7 8 2 right0 mat 3 4 5 6 7 8 ]cmat=. >'hyperactive';'radioactive';'unlikely';'zoo' hyperactive radioactive unlikely zoo 5 left0"1 cmat hyper radio unlik zoo NB. Now, in VB, "Mid" is the more general case which encompasses the "Right" and "Left" ideas: examplesMid=: 0 : 0 Mid(string,start[,length]) … ) mid0=: 4 : '(1{x){.(<:0{x){.y [ ''st len'=. 2{x,>:(#y)-x' J equivalents by example ------------------------ VB J Result -- - ------ Left('Carlsbad',4) 4{.'Carlsbad' Carl Right('Carlsbad',3) _3{.'Carlsbad' bad Mid('Carlsbad',2,3) 'Carlsbad'{~1+i.3 arl 3{.1}.'Carlsbad' arl Mid('Carlsbad',10,3) '' 3{.9}.'Carlsbad' ' '