Page 1 of 1

## J tutorial II combinators, gerunds, agendas, and tacit programming in J

### #1 Jaakuuta

Reputation: 1
• Posts: 163
• Joined: 02-July 09

Posted 14 September 2009 - 04:52 AM

If you liked the last tutorial, you'll love this one. It gets into the basics of some of the special control structures that J offers. This tutorial will hopefully give you a new way of looking at programming logic

First off, let's start with some simple tacit functions that will allow you to stream some arithmetic to create algebraic functions using function composition and a technique called currying.
in algebra:
f(x)=(x-1)/(x+1) would be a simple rational function. Let's see how you'd make it in J

```f=.<:%>:

f 1
0

f 2
0.333333

```

This is what's referred to as a fork. I showed you this before with the mean function. mean=:+/%#
What it's basically saying is for any three functions f g and h where g is dyadic and f and h are both monadic, a fork occurs wherein it's evaluated as f y g h y or "f of y composed of g composed of h of y" where y is the one operand of a monadic function. The identity of which is ]
So what +/%# actually means is +/ of ] % # of ]
So when you say mean 1 2 3 this evaluates to +/ 1 2 3 % # 1 2 3
+/ 1 2 3 => 1 + 2 + 3 and # 1 2 3 => 3 as there are three elements.

So when you say f=.<:%>: you're saying f is a fork such that <: of ] % >: of ]
Then with something like f 2 you have <: 2 % >: 2 => 1 % 3 => 0.333333

To do tacit control structures in J you have to use what are called gerunds and agendas. The gerund is a set of actions that work somewhat like a function array. The agenda is like mixed form of conditional statements that generates an enumeration for the array. If there are only two options, only one conditional is necessary, but the list of conditionals can become arbitrarily large.

So let's look at a recursive factorial definition.
First, in case you don't know, a factorial is a function that takes a product of a number and every number below it down to one.
The recursive definition states that while the number is greater than 1 the definition becomes the number multiplied by the factorial of the decrement of the number.
in other words:
if number >= 1 f = fact of decrement of number else f = 1
Let's break that into parts
First the conditional. If number is greater than or equal to 1 is easy. * represents signum which, for real numbers, is 1 if it's positive, 0 if it's zero, and _1 if it's negative.
Agenda is represented as @. we now have our agenda...

```fact=: ... @.*

```

Next, we have to break apart the rest of the statement.
f = number times fact of decrement of number
f = 1

well the false condition 0 comes first, so that would be 1
In tacit definitions, usually you would use a : after numbers, so this would be 1:
This is what we have so far...
```fact=: 1: ... @.*

```

Now to create a gerund, you add the gerund operator `
but before we add it, let's work out the second statement.
In a tacit definition, there is no explicit variable representing the number being used in the function. In J, there are temporary placeholders for these variables. If you have a v b for a dyad (such as 2 + 3) then you can interpret this simply as [ + ] ... For something like v b for a monad (such as >: 0) then you would have >: ]
So if we have number, which in this case would be ] because fact is a monad, number times fact would be ] * fact...
Now J has a special operator that refers back to the recursive function without explicitly needing the name of the function, so that it needn't be actually be defined as a function. This is the \$: or "self-referential" operator.
So to say "number times fact" we would have ] * \$:
Now if you want to apply a function to something, you use atop @ The monadic forms of @ and & bond are equivalent.

So now let's see what we have.
```fact=:(1:`(]*\$:@<:)@.*)

```

There, we've finished it.
now you can do something like
```fact 1 3 5 7 9
1 6 120 5040 362880

```

Here's a couple more fork examples for you.

```am=:+/%# NB. Arithmetic Mean

am 1 3 5 7 9
5

gm=:#%:*/ NB. Geometric Mean

gm 1 3 5 7 9
3.93628

```

Forks can even be used by themselves and with two operands
```1 (+%-) 2
_3

```

that is 1 + 2 % 1 - 2
or you can use a definition like the one given above by itself
```(+/%#) 1 3 5 7
5

```

Well that's enough fun for today, I'll go over some more later once I get some more experience with it
Hope you've enjoyed it

Is This A Good Question/Topic? 0