How to repeat the evaluation of an expression in J?
Asked Answered
G

1

6

Given the following expression in J, how do I execute it n times?

6?99

The only solution I could come up with was to turn it into a verb, which works nicely, but I suspect there's a better way:

(3 : '6?99')"0 i.100

Now, before anyone gets confused, this question is not about the specific expression 6?99. It is a general question about the J programming language. Your answer should be general enough to apply to any J expression, not just 6?99.

Gery answered 2/2, 2018 at 12:42 Comment(2)
I don't think there is a better way in general. Maybe if you can construct an adverb or a conjunction, you could hold up the evaluation. E.g. you could set v =: 6&? and run v (n $ 99). See evaluating expressionsCarbonization
There's also the idea of using the power conjunction to trace the results over 4 consecutive executions on itself. (6?99"_)^:(>:i.4) ''Grog
E
3

Still turning it into a verb, but this time tacitly

   (6 ? 99"_)"0 i.4
92 61 82  7 67 12
56 76 77 67  9 24
16 31  9 76 70 98
65 24  2 28  1 39

Depending on the verb this could be a little cleaner than explicit.

The usual way of doing this would be to make copies of the left argument, but I sense that is not what you would be looking for?

   n=.4
   6 ? n $ 99
43 55 79 71 35 33
41 56 67  3 78 24
38 34  7 61 14 13
95 63 43 47 73 29
Exhibitionist answered 2/2, 2018 at 20:46 Comment(5)
It's not what I'm looking for, though it's certainly more idiomatic in J. I wanted to see how to do it if the expression is effectively an unmodifiable "black box".Gery
@GregoryHigley The way you show in your question is the idiomatic approach for the black box case. The approach bob takes in this answer of algebraically deconstructing and manipulating the particular expression takes advantage of more of J’a strengths and will develop a stronger intuition for the language (in addition to being more intellectually satisfying), and is therefore more common. Outside of special cases like ? which is not idempotent, the need for repetition ignoring input is uncommon, so it’s more typical to take each expression case by case, rather than black boxFootpoundal
@DanBron Agreed. I'm familiar enough with J that "algebraic deconstruction" is the approach I would take myself. When I wrote the question, I was already well aware of alternate approaches, having been a J-er for many years now. However, I am always interested in exploring J, and was curious what the "black box" approach would be.Gery
Could you please explain the role of "_? It seems that it applies to 99, i.e. ` (6 ? (99"_))"0 i.4` gives the same result. How can 99 have a rank different from 0? (#$(99"_) returns # $ 99"_, what is that supposed to mean?) And how does ? (Deal) operate on this thing? I would have though (6 ? (99))"0 i.4 would work but it returns 4 identical lists.Sunrise
Giving a constant such as 99 infinite rank 99"_ means that whatever argument that it is given it returns the value 99 . Essentially you are making it a verb that returns 99 for all arguments. If you give it a rank of 0 then each atom of the argument will return 99 You have to use rank to make the constant noun 99 into a verb because in the fork` (# ? 99"_)` the form is (V V V) which allowed, while (# ? 99) or (V V N) is not. You could use (99 ?~ #) because (N V V) is allowed and ?~ reverses the arguments. Does that help?Exhibitionist

© 2022 - 2024 — McMap. All rights reserved.