After working out a solution (given below) to the problem of matrix spread from the other thread and Schott's reply to an adverb ins0 posted there, and considering all the interest that pure-functional programming has received recently, I've come up with this, a bit longer, post about programming higher-order functions in J, or, in J terminology, about programming without verbs.
The starting point and paradigmatic example that I use is the adverb ins0, that inserts a row and a column of zeros into a matrix, and whose inner workings will be explained in a second: ins0=:&|.((|:@(0&,)^:2)&.) NB. x is row,column y is matrix. 2 3 ins0 1+i.5 5 1 2 3 0 4 5 6 7 8 0 9 10 0 0 0 0 0 0 11 12 13 0 14 15 16 17 18 0 19 20 21 22 23 0 24 25 When reading definition of ins0, we can read from left to right, notice two adverbs, those containing & and &. , recalling a few rules in the process 0) conjunction with only one argument produces adverb. 1) adverbs take their argument from the left hand side; 2) adverb's argument, x, can be a verb or a noun. 3) adverbs furthermore can be composed into adverbs without specifying their left argument, as in adverb =: \/~ NB. adv adv adv ~~> adv We can play around a bit now to rearrange ins0 so that function (0&,) takes a more standard position, i.e place left of an adverb: ins0a =: &|.(0&,(|:@)(^:2)&.) Now hopefully becomes easier to see even without debugger what's going on, and to do so we can always rewrite the expression into speech parts instead of debugging it: RL=:&|. NB. adv. rotated TR=:|:@ NB. adv. transposed TW=:^:2 NB. adv. twice applied ins0b=: RL (0&, TR TW &.) "Gramatology" of this expression is adv (verb adv adv conj) ~~> adv (verb conj) ~~> adv adv ~~> adv More precisely, notice that the order of evaluation here is from left to right adv (verb adv adv conj) === (adv (((verb adv) adv) conj)) ~~> adv((verb adv)conj) ~~> adv(verb conj) ~~> adv adv ~~> adv . . . . . . . . . . . . . . . . . . . . . The challenge now is to enhance ins0 so that it takes as argument not only fill position, but also fill element, using the technique of higher-order functions described so far. To do that, we can first define: F=: &,(|:@)(^:2) NB. adv adv adv 0 F |:@(0&,)^:2 1 F |:@(1&,)^:2 ins0c =: &|.(0 F &.) NB. this is ins0 written using F ins1 =: &|.(1 F &.) NB. should be obvious. 2 3 ins1 1+i.5 5 1 2 3 1 4 5 6 7 8 1 9 10 1 1 1 1 1 1 11 12 13 1 14 15 16 17 18 1 19 20 21 22 23 1 24 25 We should note here that we have now defined ins0 using only adverbs, conjunctions and constants, and now we have reached the limit of J's expression(i)s(m), since the J's powerful constructs of trains, hooks and forks are designed to work with verbs and nouns, and we are left with none at all. In other words, I use now scripts since I don't see how to write tacit version of this: insert=: 2 : 'x &|.(y F &.)' 2 3 insert _ (1+i.5 5) 1 2 3 _ 4 5 6 7 8 _ 9 10 _ _ _ _ _ _ 11 12 13 _ 14 15 16 17 18 _ 19 20 21 22 23 _ 24 25 Furthermore, insert behaves very purely, functionally speaking: Iinf=: insert _ NB. a new function that inserts _ 2 3 Iinf 1+i.5 5 1 2 3 _ 4 5 6 7 8 _ 9 10 _ _ _ _ _ _ 11 12 13 _ 14 15 16 17 18 _ 19 20 21 22 23 _ 24 25 We see that the function insert is Curried, which means that we get proper (high-order) functions of one argument when we give only second (or the first) argument to insert, and without any explicit bonding. Bonding is implicitly at work within adverbs of course. What would make things really pure-functional is a tacit version of the above defined insert conjunction. Unfortunately, as far as I can see there is not much expressions left around for writing tacit conjunctions: to be more precise, none at all. I think we should consider making some room in the interpreter to get around this misfortune, for instance by giving to, currently erroneous, train: adverb conjunction adverb some meaning, oriented towards writing tacit conjunctions, adverbs or verbs. -- View this message in context: http://www.nabble.com/Higher-order-functions-in-J%2C-or%2C-programming-without-verbs.-tp21441404s24193p21441404.html Sent from the J Programming mailing list archive at Nabble.com. ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
