> I am new to J programming, and I don't quite understand script files.
Script files are just your source code. J scripts (*.ijs) are analogous to C
programs (*.c). However, whereas C is compiled, J is interpreted, so there's
no need for a separate compilation step (or linking, or header files, or memory
allocation....).
> How do you write the equivalent of a main function from C programming?
J programs don't have a standard entry point. They have no equivalent to a
"main function". When you feed a script to J, it executes it line by line, so
the first instruction you give it will the first instruction it executes.
To run a J program from the command line, you simply type
jconsole my_program.ijs
what happens after that depends on the instructions in the script.
> I don't want to define a verb. I simply want to create a main program
Programs consist of functions, and all functions in J are verbs. You cannot
write a (useful) program without defining a verb.
> where I can query the user and except user input.
The instructions to query the user and printing a response will be a verb. All
functions in J are verbs.
Having said that: be aware that J is block, not stream oriented.
That is, J's strength lies in processing all the data at once. That implies
that it must have all the data in advance and consequently that it is poorly
suited for interactive tasks.
Since you're new to J, if I were you, I would choose a programming problem that
would play to J's strengths, to acquire a flavor of the language (and reasons
why someone would choose it over others).
And, since familiar with C, I'd recommend that you read through Henry Rich's
excellent "J for C programmers" at
http://www.jsoftware.com/help/jforc/contents.htm .
All the preceding isn't to say that interactive programs in J are not possible.
They are. In fact, they come in two flavors: command line and GUI. GUI
solutions are by far the easiest and most prevalent method for user interaction
in J, and that's the road I would recommend for you. Take a look at the menu
item Edit>Form Editor .
Command line solutions, while less common, are still possible (but are somewhat
involved). To get started, download this script:
http://www.jsoftware.com/svn/DanBron/trunk/general/r_pl.ijs
and save it in the "temp" directory under your J installation. Then, create a
new script in that same directory, and name it "interactive.ijs". This should
be the contents of interactive.ijs :
require '~temp\r_pl.ijs strings'
query =: verb define
y =. deb tolower y
if. 'quit' -: deb tolower y do.
0 ; 'bye!'
else.
_ ; 'Why do you say "', y , '"?'
end.
)
echo 'Welcome to psychotherapy!'
query r_plx ''
Now, open a command prompt:
> cd x:\path\to\j\root\
> jconsole ~temp\interactive.ijs
Welcome to psychotherapy!
Hi.
Why do you say "Hi."?
To greet you.
Why do you say "To greet you."?
Because you asked me why I said `Hi'.
Why do you say "Because you asked me why I said `Hi'."?
You're a jerk.
Why do you say "You're a jerk."?
quit
bye!
-Dan
PS: Another reason that interactive programs aren't the norm in J is because J
subscribes to the philosophy of functional programming. Functional programs
disdain side effects, which is exactly what asynchronous events like user
interaction are.
Which doesn't mean you can't write an asynchronous program in J; to the
contrary, I earned my living for 3 years with such a system (though the events
came over sockets, not from user input).
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm