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

Welcome to Dream.In.Code
Become an Expert!

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




Fun with J

 
Reply to this topicStart new topic

> Fun with J, J with Fun

Jaakuuta
Group Icon



post 9 Sep, 2009 - 03:33 AM
Post #1


Hi everybody!
I wanted to introduce you today to an awesome language that's used to manipulate arrays, but is very powerful and can be used for a number of things.

A little bit of history first.
Back in the late '50s a man by the name of Kenneth Iverson invented a mathematical notation for work on a research project. He then released a book about his mathematical notation called "A programming language".
The notation in this book eventually got turned into an actual programming language, and, inspired by the book's title, it was called APL. APL is a very special language that is based on this notation he invented, because of this it requires a special keyboard, special fonts, and a unique input method editor to be able to use it properly. It often involved the use of overstrikes to create new symbols and could be difficult to use. Some years later, Dr. Iverson created a language based on APL called J which uses the standard ASCII character sets and relies on digraphs in substitution for the special mathematical symbols that APL uses. Therefore, anybody can use it without a problem biggrin.gif

Anyway, let's get on to the lesson.

First off, your basic addition and subtraction operations are the same as one might expect

CODE

2 + 3
5

2 - 3
_1


as you may have noticed, on the first one 2 + 3 yields 5 as expected, while 2 - 3 yields _1
this is because _ when used as a monad (accepts one argument) represents the negative sign

+ and - can also be used as monads

CODE

+1
1

+2
2

+2j3
2j_3

-1
_1

-2j3
_2j_3


Okay then, let's go over these one at a time...
+ as a monad represents a conjugate pair
When used with a real number, it evaluates as normal
but when used with a complex number (such as 2j3) it creates the conjugate pair of that number.
Thus +2j3 => 2j_3

If this were to be written using standard mathematical notation it would be
2 + 3i => 2 - 3i

- when used as a monad changes the sign of the number
so -2 becomes _2 and -2j3 becomes _2j_3
in other words -(2 + 3i) => (-2 - 3i)

Okay, now that we've worked with the basic + and - operators (as well as touching a little bit on the _ and j operators) let's talk about multiplication and division.

Multiplication still uses the * symbol, however, contrary to most programming languages, J uses % for division as this more closely resembles the division symbol used in APL, whereas / is used for something different entirely. (which I will cover later)

CODE

3 * 5
15

3 % 2
1.5


These are fairly self-explanatory.

Now I want to cover some really neat symbols.

CODE

2 < 3
1

2 <. 3
2

2 <: 3
1

2 > 3
0

2 >. 3
3

2 >: 3
0


Okay, let's start from the top
2 < 3 ? This is a boolean statement, it's true, so it yields a 1
2 <. 3 This is asking for the lesser value between 2 and 3, so it yields 2
2 <: 3 ? This is also a boolean statement, it's asking if 2 is less than or equal to 3, which also yields 1 for true

2 > 3 ? false => 0
2 >. 3 => 3 is the larger of the two
2 >: 3 ? false => 0

These ones can also be used as monads with rather interesting results.
I'm not going to cover the use of < and > as monads right now though, as those are a bit more complicated.

CODE

<.2
2

<.3
3

<.2.5
2

>.2
2

>.3
3

>.2.5
3

>:1
2

>:2
3

<:2
1

<:1
0


The <. and >. used as monads are used for the floor and ceiling functions respectively so <.2.5 goes to 2 and >.2.5 goes to 3

<: and >: are used like the -- and ++ operators in other languages, in other words, they decrement or increment by 1

Next, I want to cover the simple left and right operand operators
CODE

[ 2
2

] 2
2

2 [ 3
2

2 ] 3
3


[ and ] when used by themselves are self-referential, but when used as dyads evaluate to either the left or right operand. This may not seem very useful at first, but comes in very handy in tacit programming. biggrin.gif

One of the cool things about J is that it can process arrays very easily. Not only can it manipulate them very easily with functions, it can also generate them very easily.

In general, functions can be used with any number of arguments.

Now I am going to teach you about a neat operator that generates arrays.
In APL this would be represented by the Greek letter iota
CODE

i.5
0 1 2 3 4

i.10
0 1 2 3 4 5 6 7 8 9

>:i.5
1 2 3 4 5


Isn't that neat, you can use this operator create a sequential list of numbers, and use operations on it to manipulate it.

Now let's have some fun, now is the time to introduce some new features that work with arrays.

CODE

i.5
0 1 2 3 4

+/i.5
10

*/>:i.5
120


The first one was just showing you the array again. The second and third ones are a bit unique. The / operator is referred to as the 'fold' operator. This is used with dyadic functions to insert whatever operator precedes it into an array.
Let's see what this would look like in normal mathematical notation.
+/i.5 => 0 + 1 + 2 + 3 + 4 => 10

The second one works the same way, except I've applied the increment operator to the array before processing the fold. (Evaluation in J is from right to left)
*/>:i.5 => 1 * 2 * 3 * 4 * 5 => 120
Wow... Check that out, it's 5! Look how easy it is to make a factorial with just raw operations. Of course, there is a factorial operator built in too.

CODE

!5
120


There is also an operator called the 'tally' operator. This operator counts how many elements there are in the array.

CODE

#i.5
5

#i.6
6

#1 3 5 7 7 9 7 8 5
9


This can be useful in certain situations. There is something called a fork. If you have two monadic functions surrounding a dyadic function and all three are being applied to the same number or array, they work together and apply on it in a special way. Once again, let's look at this in normal mathematical notation.
(f g h) x => (f x) g (h x)
If g takes two arguments

Knowing this, we can start doing our own tacit functions. biggrin.gif
Let's start with a really simple one. This function computes the mean average of an array of numbers.

CODE

+/%#


That's it! You already know what all three of those mean, but let's apply them to an array and see it in action.

First, I'm going to show you how to assign values to variables.

CODE

2 = 2
1

2 = 3
0

x =. 2

x
2

y =: 3

y
3


The first two demonstrate that = by itself is the equality operator and creates a boolean statement.
The next two demonstrate the two assignment statements.
=. is a local assignment, and =: is a global assignment

So now, let's make a new function and just call it avg for 'average'
CODE

avg=:+/%#


Now, we can apply this to any array and it will average the values
CODE

avg i.5
2

Therefore, the mean average of 0 1 2 3 4 is 2

I think I'm going to leave the tutorial for today.
I'd like to create a second section to this later that goes more deeply into some more interesting topics such as 3d graph plotting and tacit function definitions, but most of those are out of the depth of this simple beginning tutorial. Anyway, I hope you have fun and enjoy J as much as I do!

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 03:41PM

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