3

Here's a naive Fibonacci sequence:

   (,[:+/_2&{.)^:10]0 1      NB. 10 + 2 elements
0 1 1 2 3 5 8 13 21 34 55 89

And here's its explicit monadic version:

   3 :'(,[:+/_2&{.)^:y 0 1' 10
0 1 1 2 3 5 8 13 21 34 55 89

The questions is: in tacit definition, can I somehow supply rightmost argument to ^: conjunction, so as (off top of my head):

   ((,[:+/_2&{.)^:y 0 1)10
0 1 1 2 3 5 8 13 21 34 55 89

Will yield the expected result? Or, more correct definition (again, off top of my head):

   ((,[:+/_2&{.)^:(y-2)1 1)10
1 1 2 3 5 8 13 21 34 55

More generally: is it possible to tacitly define adverbs and conjunctions in J, or is it possible only with explicit definitions?

My gut (and material from this question) tells me that I should go to the dark side and learn more about gerunds and ` / `: conjunctions. Is that correct? If so, I would appreciate any newbie-friendly material on this matter :)

9214
  • 2,105
  • 11
  • 15

1 Answers1

2

I think that my natural approach would be to create a dyadic verb where the left argument is the number of iterations and the right argument is the initial string. This allows me to extend the string easily.

fib0=: (,[:+/_2&{.)@]^:[ 
   10 fib0 0 1
0 1 1 2 3 5 8 13 21 34 55 89
   11 fib0 0 1
0 1 1 2 3 5 8 13 21 34 55 89 144

I can create a verb monadically by filling in the (,[:+/_2&{.) as the left argument to ^: and 10 as the left argument. Not too flexible in extending the string though.

  fib1=: (,[:+/_2&{.)^: 10
   fib1 0 1
0 1 1 2 3 5 8 13 21 34 55 89

And I end up faking the result that you may be looking for by attaching 0 1 in the definition and creating a monadic verb looking for the number of iterations.

  fib2=: ((,[:+/_2&{.)@](^: [))& 0 1
   fib2 10
0 1 1 2 3 5 8 13 21 34 55 89
   fib2 11
0 1 1 2 3 5 8 13 21 34 55 89 144

But you wanted to know if there was a way to do this using adverbs tacitly. Taking what I have shown above you can create an adverb from the conjunction ^: by adding the verb (,[:+/_2&{.) to the left.

   afib=: (,[:+/_2&{.) ^: 
   (10 afib)                 NB. an adverb takes its left argument creating a conjunction.
(, ([: +/ _2&{.))^:10
   (10 afib) 0 1
0 1 1 2 3 5 8 13 21 34 55 89
   (11 afib) 0 1
0 1 1 2 3 5 8 13 21 34 55 89 144
bob
  • 4,282
  • 2
  • 22
  • 30
  • `((,[:+/_2&{.)@](^:(2-~[)))&1 1]10`... this is interesting. I get the main idea, need to read up on how modifiers work to fully understand all details. Thanks for your answer! – 9214 Mar 29 '18 at 00:50
  • Ah, now it makes sense. I got confused by your usage of `[`, but then noticed that rightmost argument is already bound with `&`. `(...)@]` combo is educational too. Adverb derives verb taking leftmost operand, conjunction with one operand on either side derives adverb, conjunction with two operands derives verb. Nice. – 9214 Mar 29 '18 at 01:39