On Mon, Feb 24, 2014 at 10:27 PM, Jon Hough <jgho...@outlook.com> wrote:
> {: 3 p: 567That works fine.OK, so I want to wrap that into a tacit > verb.Here was my first attempt: > bigdiv =. {: 3 p:|syntax error| bigdiv=. {:3 p: I am not sure why > this fails. Apparently we need a & between the two verbs "{:" and "3 p:". > Could anyone explain why? I thought for monadic verbs putting & in between > them is superfluous (incidentally same as putting @). > (I am taking a break - I do this kind of thing to relax...) Here's how J parses {: 3 p: 567 QUEUE STACK RULE § {: 3 p: 567 Move § {: 3 p: 567 Move § {: 3 p: 567 Move § {: 3 p: 567 Move § {: *3 p: 567* 2 Dyad § {: 3 3 3 3 7 Move § {: 3 3 3 3 7 0 Monad § 7 (done) This is based on the rules at http://www.jsoftware.com/help/dictionary/dicte.htm Now here is your first attempt: QUEUE STACK RULE § bigdiv =: {: 3 p: Move § bigdiv =: {: 3 p: Move § bigdiv =: {: 3 p: Move § bigdiv =: {: 3 p: Move § bigdiv =: *{: 3 * p: 6 Bident § bigdiv =: *3 p:* 6 Bident And the bident handler rejects 3 p: as a syntax error. So my next attempt is to stick a & between the verbs: > bigdiv =. {: & 3 p: bigdiv 45|domain error: bigdiv| bigdiv 45 > > I would like to know what the cause of this domain error is. > If you are in doubt about how J is parsing a sentence, you can try its trace facility require'trace' trace 'bigdiv=: {: & 3 p:' This will give you similar information to what I have diagramed here, though without the "move" lines, and also a bit more verbosely for the execution stages (since in the general case, results can be large). Try it on the above sentences until you can see the before/after process. In this case, bigdiv gets the expression ({:&3 p:) -- this is a hook using the dyadic case of the verb defined by & (see http://www.jsoftware.com/help/dictionary/d630n.htm for an overview of that definition). But, briefly, the execution of your example sentence works like this (and trace only shows parsing, it does not show the progress of evaluation in tacitly defined code. I think I remember Henry working on a facility that shows progress of evaluation): bigdiv 45 ({:&3 p:) 45 NB. from definition of bigdev 45 {:&3 (199) NB. from definition of hook 199 {:^:45 (3) NB. from definition of & (bond) |domain error NB. from definition of {: (dyadic case) In other words, all of those sentences should give you a domain error. See also: hook http://www.jsoftware.com/help/dictionary/dictf.htm bond http://www.jsoftware.com/help/dictionary/d630n.htm {: http://www.jsoftware.com/help/dictionary/d522.htm > Next I put a & between the 3 and p:bigdiv =. {: & 3 & p: bigdiv > 45|domain error: bigdiv| bigdiv 45 This accomplishes basically the same thing as your previous definition. {:&3 is going to use the dyadic case of {: and that is going to be a domain error because that definition is empty. > That was a pure guess. > In exasperation I decided to build my verb by making smaller verbs first. > divs =. 3 & p: divs 542 3 3 3 This works, but I am unsure why I need > the &. > Techically, you do not need the & -- for example: divs=: 3 p: ] What you do need, though, is a grammatically correct sentence. And, fortunately or unfortunately, J's grammar is extremely simple. A complete description of it fits on one page ( http://www.jsoftware.com/help/dictionary/dicte.htm). But you have to think in terms of definitions to really get it. When you are defining a name, that name is going to be one of four types of values: noun verb adverb conjunction A noun is just data. A verb has two definitions, one for the monadic case and one for the dyadic case. For example: - 3 NB. monadic definition of minus _3 5 - 3 NB. dyadic definition of minus 2 Unfortunately, this is so simple that it can be hard to remember. It's almost like we need a certain amount of pain before the memories really stick. So try things out, grumble at the errors and sooner or later it will be second nature to you. > This seems to work perfectly. Is this the best way to go about doing this > in J? i.e. If I want to get the largest prime divisor of each element of a > list then my bigdiv does the job perfectly. Is there a better/faster/more > J-esque way? > I'd have used {:@q: But there's nothing wrong with {:@(3&p:"0) or the equivalent {:&(3&p:"0) -- q: is basically the same thing as 3&p:"0 Also, monadic definition of a verb defined by @ is the same as the monadic definition of the verb defined by & That said, you also used "1 and this was not really necessary because divs was "0. > Finally, why when I try to rebuild my tacit verb does it fail? > bigdiv =. {:"1 & 3 & p:"0 bigdiv 3 4 23 190|domain error: bigdiv| > bigdiv 3 4 23 190 > You can use the trace facility to see the detailed steps of how this gets evaluated. Alternatively, you can use bigdiv f. to compare the resulting expressions. (You'll need to either redefine it to see the differences or use two different names.) Note that you might want to change the display form for verbs. That's either a menu option (exactly where you configure this depends on which version of J you are using) or you can use expressions like: 5!:1 <'bigdiv' 5!:2 <'bigdiv' 5!:3 <'bigdiv' 5!:4 <'bigdiv' 5!:6 <'bigdiv' http://www.jsoftware.com/help/dictionary/dx005.htm Or you can change your session configuration using 9!:3 http://www.jsoftware.com/help/dictionary/dx009.htm I know this is a lot to absorb in one sitting. So do not feel bad if you skimmed over this. But it would be great if you came back later a few times until you were comfortable with the concepts. (Or you can try reading the dictionary - everything I have presented here is covered in the dictionary. Though, granted, not in a form which is tailored to your questions.) Thanks, -- Raul ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm