As a beginner exercise I tried to calculate the following sum in J, sum(1/(1+0.03)^n for n = 1 to 30
using +/%(1 + 0.03)^ >:i.30
. How can I write this into a simple tacit form? all I tried are significantly uglier than the explicit form above like >:@[ (+/&:%)@:^ >:&i.@]
Asked
Active
Viewed 197 times
3

ahala
- 4,683
- 5
- 27
- 36
-
2Tacit form works much more smoothly for some calculations than others. As values need to be applied irregularly within a formula, the overhead of passing them through to those points becomes burdensome. This fact is discussed (in a non-J context) in a blog post, http://evincarofautumn.blogspot.com/2012/02/why-concatenative-programming-matters.html , under the section heading "The Dark Side". In the formula at hand I think the two arguments are not inherently clumsy, but are instead handled nicely by a hook as Tikkanz demonstrates. – kaleidic Feb 22 '13 at 22:44
2 Answers
3
You could start with
+/@:%@((1 + 0.03) ^ >:@i.) 30
You can make the 0.03 a left argument using a fork, but using a hook can be cleaner
(1 + 0.03) +/@:%@([ ^ >:@i.@]) 30 NB. use fork
(1 + 0.03) +/@:%@(^ >:@i.) 30 NB. use hook
The same operation (increment) is being performed on both the left and right arguments to ^
. That is a hint that &
(Compose) may be useful.
0.03 +/@:%@(^&>: i.) 30 NB. apply increment to both left & right arg

Tikkanz
- 2,398
- 15
- 21
-
I like how you identified increment into something common to both sides, and also your use of hook. I prefer Cap in this case, so the phrasing I favor at the moment is `formula=: [: +/ [: % ( ^&>: i.)` – kaleidic Feb 22 '13 at 22:31
1
When I want a tacit function I often let 13 :
bang it out for me. In this case, some variations:
13 : '+/ %((1+0.03)^1+i.y)'
[: +/ [: % 1.03 ^ 1 + i.
13 : '+/ %((1+0.03)^>:i.y)'
[: +/ [: % 1.03 ^ [: >: i.
And with 1+0.03
or whatever as a leftargument:
13 : '+/ %(x^1+i.y)'
[: +/ [: % [ ^ 1 + [: i. ]
13 : '+/ %(x^>:i.y)'
[: +/ [: % [ ^ [: >: [: i. ]
There are way too many caps ([:
) to call it less ugly, though, but that's a start.

MPelletier
- 16,256
- 15
- 86
- 137