Sunday, August 19, 2012

Some primitives...

I will briefly go through some of the primitive verbs used in J. These are some of the inbuilt functions (both dyadic and monadic) that seem to be pretty ubiquitous in J code.

Arithmetic Dyadic Verbs
Addition: 2 + 5 6 7  
Subtraction: 4 - 6 
Multiplication: 2 * 4 6 8 
Division: 5 % 6
Exponent: 8 ^ 9
Logarithm: 8 ^. 64 ( = 2)Root: 3 %: 27 ( =3)
Greater: 5 >. 20 ( =20)
Lesser: 9 <. 21 ( =9)GCD:  3 +. 7 ( =1)LCM: 4 *. 10 ( =20)

Arithmetic Monadic Verbs 
Floor: <. 6.1   ( = 6) 
Ceiling: >. 6.1 ( =7) 
Sign: * _4 ( =_1) 
Factorial: ! 5 
Random Integer: ? 7 ( = random int from 0,...,6) 
Random Real: ? 0 ( = random real from 0 to 1)
Comparisons, producing 1 or 0, for true and false, respectively
Equality: 2 = 1 ( = 0) 
Greater than: 1 > 2  
Greater than or equal to: 1 >: 2  Note - don't confuse with dyadic >. 
Less than: 3 < 4 
Less than or equal to: 3 <: 4 Note - don't confuse with dyadic <. 
Inequality: 3 ~: 4
Boolean Operations
OR: +.NOR: +: AND: *.NAND: *:XOR: ~:XNOR: =  
Monadic: 
NOT: -. 


Some boolean examples:

   1 +. 0 (OR)
1
   1 *. 0 (AND)
0
 
   1 0 1 0 0 1 *: 1 (NAND)
0 1 0 1 1 0
Some Binary:

We can also do bit shifting. Here we will shift the number 1 by 3 bits (left rotate):

3 (32 b.)18
 Of course, the output is 8. We can also display the output as a binary value:


#: 4 (32 b.) 31 1 0 0 0 0


Here we used the verb "#:" to change from a decimal number into its binary form.
We can change it back:

#.1 1 0 0 0 0
48


Perhaps in another post I will look into using hexadecimal representations.

Take and Drop List Verbs:

 Take - here the left operand is the index and we take the elements from the list with index less than this.


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



Drop - same as take but we drop these elements from the original list.


   3 }. i.10
3 4 5 6 7 8 9
 
   6 }. 'A long time ago in a galaxy far, far away...'
 time ago in a galaxy far, far away...
 
   _10 }. 'A long time ago in a galaxy far, far away...'
A long time ago in a galaxy far, f

Well those are some of the basic operations. You can see more here: http://www.jsoftware.com/jwiki/HenryRich?action=AttachFile&do=get&target=J602_RefCard_color_letter_current.pdf







No comments:

Post a Comment