Talk:Forward difference

From Rosetta Code

forward difference of one number

What's the forward difference of one element? --Mwn3d 07:17, 11 January 2008 (MST)

With regard to pure mathematics I suppose it is undefined. It is natural, however, to extend it by definition so that the forward difference of a list of one element is the empty list. That's how the J code resolves it, too. --TBH 07:31, 11 January 2008 (MST)
I guess the FD of an empty list should just be another empty list then? These corner cases always get you. --Mwn3d 07:39, 11 January 2008 (MST)
Yes, that's the way to handle receiving an empty list. (A similar "corner case" has been discussed here.)
One of the things I appreciate about J is how many of these exception-situations have been resolved within the language itself. In this instance both the single-element and empty-list possibilities require no specific code. --TBH 09:45, 11 January 2008 (MST)
The formula given Bn = An+1 - An only applies to lists of two or more elements. It could be argued that it is more correct to signal an error condition when given a list of less than two elements to work on.--Paddy3118 04:42, 27 August 2008 (UTC)

It is very nice to see the Python entry. Studying it has improved my grasp of that language. --TBH 09:45, 11 January 2008 (MST)

J: verb vs adverb

With regard to the replacement of ((}. - }:) ^:) by (2&(-/\)): I agree that a solution in the form of a verb is somewhat better than a solution that is an adverb, although the benefits strike me as subtle and minor. I think the original solution is worth retaining as an example because of its form. It is not only a different algorithm, it is another strong example of how tacit form allows one to "code the concept." These two solutions complement one another, so both should be included. Contrary to the comment upon replacement, the new program is not shorter than the first solution. This is demonstrated by the following J interaction:

   #;:'(}. - }:) ^:'
6
   #;:'2&(-/\)'
7

--TBH 21:13, 20 August 2008 (UTC)

Yep, both solutions could be retained. I'll put the orig back in a moment. To address your specific comments:
I actually think that 2 -/\ y is a clearer expression of the concept of forward differences. That is, I think of a forward difference as "inserting a - between each pair of numbers", which is exactly what 2 -/\ y tells J to do; it took me a moment to realize subtracting the curtail from the behead is equivalent.
Of course, this is entirely subjective: it depends on the way you think about the problem. So you may think of "forward differences" exactly as "subtracting the behead from the curtail". In fact, I think some of the other languages calculate their results exactly that way, so the original solution may serve better as a "direct translation".
Regarding verbal vs adverbal: I don't think the differences are minor (though they may be subtle). The primary argument for a verb solution runs like this: we have two noun inputs and want to produce a noun output, which is the very definition of a dyadic verb. All the arguments are supplied at "run time" not "definition time", and no new verbs are produced, so the adverb does not benefit us. Yet it costs us plenty: adverbs are hard to use like verbs. For example, how would you write the equivalent of +/@(2&(-/\)) (the sum of the forward differences) without rewriting your the adverb as a verb?
A secondary (but still important) difference is generality: Verbal solutions can be easily extended with rank, but it's difficult to slice-and-dice arguments to adverbs. For example, how would you recreate this result using the adverb solution?
   (3 2 3) 2&(-/\)"_1 ?. 3 5 $ 10
 16 _20  0
 16 _14 11
_17  22  0
Or even this (however contrived)?
   (-:@#`],:<@#`+:) 2&(-/\)"1 ] 16 7 82 4    NB.  Apply different _verbs_ (i.e. a multidim array of _verbs_ is an argument)
 84 _153   0 0
  0    0   0 0
  0    0   0 0
  0    0   0 0

 32   14 164 8
 18 _150 156 0
168 _306   0 0
474    0   0 0
You're right that the adverb is a concise expression of the concept, which emphasizes J's strengths. I find the verb solution another compelling example of that: 2&(-/\) is how you would code the simple forward difference (i.e. as a monad). The fact that it works as a dyad to produce the Nth forward difference (without a single change) highlights J's value as a notation. Put another way, I find it pleasant that monad is the dyad with an implicit left argument of 1, which is the "normal use case".
Finally, With regard to length, my metric was # rather than #@;:, as in :
   #'(}.-}:)^:'
9
   #'2&(-/\)'
7
I chose # because the comment on the solution specifically highlighted succinctness, and total length is the metric of succictness for the solution's target audience (i.e. developers unfamiliar with J, who we're trying to impress).

DanBron 23:01, 26 August 2008 (UTC)