Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
J (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== J permits [[tacit programming|point-free style]] and [[function composition (computer science)|function composition]]. Thus, its programs can be very terse and are considered difficult to read by some programmers. The [["Hello, World!" program]] in J is: <syntaxhighlight lang="j"> 'Hello, World!' </syntaxhighlight> This implementation of hello world reflects the traditional use of J β programs are entered into a J interpreter session, and the results of expressions are displayed. It's also possible to arrange for J scripts to be executed as standalone programs. Here's how this might look on a [[Unix]] system: <syntaxhighlight lang="j"> #!/bin/jc echo 'Hello, world!' exit '' </syntaxhighlight> (Note that current j implementations install either <code>jconsole</code> or (because jconsole is used by java), <code>ijconsole</code> and likely install this to [[/usr/bin]] or some other directory (perhaps the Application directory on OSX). So, there's a system dependency here which the user would have to solve.) Historically, APL used <code>/</code> to indicate the [[fold (higher-order function)|fold]], so <code>+/1 2 3</code> was equivalent to <code>1+2+3</code>. Meanwhile, division was represented with the mathematical [[division symbol]] ({{code|Γ·}}). Because ASCII does not include a division symbol ''per se'', J uses % to represent division, as a visual approximation or reminder. (This illustrates something of the mnemonic character of J's tokens, and some of the quandaries imposed by the use of ASCII.) Defining a J function named <code>avg</code> to calculate the average of a list of numbers yields: {{code|2=j|1=avg=: +/ % #}} * <code> +/ </code>sums the items of the array. * <code> # </code>counts the number of items in the array. * <code> % </code>divides the sum by the number of items. This is a test execution of the function: {{code|2=j|1=avg 1 2 3 4}} <span style="color:sienna;">2.5</span> Above, ''avg'' is defined using a train of three verbs (<code>+/</code>, <code>%</code>, and <code>#</code>) termed a ''fork''. Specifically, <code>(V0 V1 V2) Ny</code> is the same as <code>(V0(Ny)) V1 (V2(Ny))</code> which shows some of the power of J. (Here V0, V1, and V2 denote verbs and Ny denotes a noun.) Some examples of using <code>avg</code>: {{code|2=j|1=v=: ?. 20 $100}} <span style="color:gray;">NB. a random vector</span> {{code|v}} <span style="color:sienna">46 55 79 52 54 39 60 57 60 94 46 78 13 18 51 92 78 60 90 62</span> {{code|2=j|1=avg v}} <span style="color:sienna;">59.2</span> {{code|2=j|1=4 avg\ v}} <span style="color:gray;">NB. moving average on periods of size 4</span> <span style="color:sienna;">58 60 56 51.25 52.5 54 67.75 64.25 69.5 57.75 38.75 40 43.5 59.75 70.25 80 72.5</span> {{code|2=j|1=m=: ?. 4 5 $50}} <span style="color:gray;">NB. a random matrix</span> {{code|m}} <span style="color:sienna;">46 5 29 2 4 39 10 7 10 44 46 28 13 18 1 42 28 10 40 12</span> {{code|2=j|1=avg"1 m}} <span style="color:gray;">NB. apply avg to each rank 1 subarray (each row) of m</span> <span style="color:sienna;">17.2 22 21.2 26.4</span> [[Rank (J programming language)|Rank]] is a crucial concept in J. Its significance in J is similar to the significance of <code>select</code> in [[SQL]] and of <code>while</code> in [[C (programming language)|C]]. Implementing [[quicksort]], from the J Dictionary yields: <syntaxhighlight lang="j"> sel=: adverb def 'u # [' quicksort=: verb define if. 1 >: #y do. y else. (quicksort y <sel e),(y =sel e),quicksort y >sel e=.y{~?#y end. ) </syntaxhighlight> The following is an implementation of quicksort demonstrating [[tacit programming]]. The latter involves composing functions together and not referring explicitly to any variables. J's support for ''forks'' and ''hooks'' dictates rules on how arguments applied to this function will be applied to its component functions. <syntaxhighlight lang="j"> quicksort=: (($:@(<#[), (=#[), $:@(>#[)) ({~ ?@#)) ^: (1<#) </syntaxhighlight> Sorting in J is usually accomplished using the built-in (primitive) verbs <code>/:</code> (sort up) and <code>\:</code> (sort down). User-defined sorts such as quicksort, above, typically are for illustration only. The following example demonstrates the usage of the self-reference verb <code>$:</code> to recursively calculate fibonacci numbers: <syntaxhighlight lang="j"> 1:`($:@-&2+$:@<:)@.(>&2) </syntaxhighlight> This recursion can also be accomplished by referring to the verb by name, although this is of course possible only if the verb is named: <syntaxhighlight lang="j"> fibonacci=:1:`(fibonacci@-&2+fibonacci@<:)@.(>&2) </syntaxhighlight> The following expression exhibits [[pi]] with n digits and demonstrates the extended precision abilities of J: {{code|2=j|1=n=: 50}} <span style="color:gray;">NB. set n as the number of digits required</span> {{code|2=j|1=<.@o. 10x^n}} <span style="color:gray;">NB. extended precision 10 to the nth * pi</span> <span style="color:sienna;">314159265358979323846264338327950288419716939937510</span>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)