Search code examples
jtacit-programming

A way to use conjunctions and adverbs tacitly?


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 :)


Solution

  • 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