Martin Kreuzer wrote:
>
> To calculate the area of a flat triangle, using Heron's formula,
> A(a,b,c)= sqrt( s2*(s2-a)*(s2-b)*(s2-c) )
>
> Q: Is there a way to reference the intermediate result of ([: -: +/)
> the half perimeter s2 within the tacit expression, as has been done in
> the explicit..?
Nothing easier than that. You just need to think in functional style.
Define s2 as the function you already suggested:
s2 =. [: -: +/
and use this function in the tacit form just like you would use
"its intermediate result":
A =. %: @ (s2 * */@(s2 - ]))
If you'd rather use pseudo-forks, this would be
A =. [: %: s2 * [: */ s2 - ]
What you use is a matter of taste / personal preference.
What *does* matter is that using such auxiliary functions is *good*.
Eugene McDonnell was a great teacher demonstrating elegant, readable
code composed out of useful little parts.
Martin Neitzel
PS:
There's little reason not to use auxiliary functions. One objection
could be:
"In A =. %: @ (s2 * */@(s2 - ])),
the half perimeter is needlessly
computed twice."
If this bugs you, too, you can hunt for a tacit expression which
factors the s2 out into a single instance.
B =. %: @ (s2 ([ * */@:-) ])
The (dyad)fork-within-in-a-(monadic)fork here makes me wondering
if it could be rewritten into some equivalent, nicer form. This
is how I tackle these things:
The (f g ]) there is an anti-pattern to me crying out for a hook
instead. We can get there by commuting the fork's tines into
the equivalent (] g~ f), i.e.
B =. %: @ (] ([ * */@:-)~ s2)
which we can now simplify into a hook because "(] f g) y <--> (f g) y":
B =. %: @ (([ * */@:-)~ s2)
The ~ Reflection can be pulled into the tines of the fork, and then
directly applied to the [ yielding ]. Since the inner * Times is
commutative, swapping the tines is possible and makes it a teensy little
bit easier to see that it will pick up the s2 result:
B =. %: @ (([ * */@:-)~ s2)
B =. %: @ (([~ * */@:-~) s2)
B =. %: @ ((] * */@:-~) s2)
B =. %: @ ((*/@:-~ * ]) s2)
B =. %:@((*/@:-~ * ]) s2) NB. canonical spacing
(Again, we have the "(f g ]) anti-pattern"; but this time it is used
dyadically and cannot be turned into a hook. At least I think so :-)
Is the final B more readable than the initial B?
Is any of the Bs more readable than any of the As?
You decide.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm