School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,109 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,018 people online right now. Registration is fast and FREE... Join Now!




J tutorial II

 
Reply to this topicStart new topic

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

Jaakuuta
Group Icon



post 14 Sep, 2009 - 03:52 AM
Post #1


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 biggrin.gif

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

CODE

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...

CODE

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...
CODE

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.
CODE

fact=:(1:`(]*$:@<:)@.*)

There, we've finished it.
now you can do something like
CODE

fact 1 3 5 7 9
1 6 120 5040 362880


Here's a couple more fork examples for you.

CODE

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
CODE

1 (+%-) 2
_3


that is 1 + 2 % 1 - 2
or you can use a definition like the one given above by itself
CODE

(+/%#) 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 biggrin.gif
Hope you've enjoyed it biggrin.gif

Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 12:52PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month