Gotchas: Difference between revisions

→‎{{header|J}}: more gotchas
(Added Quackery.)
(→‎{{header|J}}: more gotchas)
Line 210:
Here, we are adding to lists and then (after the first sentence) summing the result. But as you can see in the last sentence, summing the individual numbers by themselves doesn't accomplish anything useful.
 
J also has gotchas with its token formation and syntax rules.
 
# The character '.' is also "token forming" in non-numeric words and tokens. Here, ':' also follows this rule. (In both cases, this was to allow the language to use strict ascii while representing characters which were originally conceived of as having an [[wikipedia:accent|accent]] or [[wikipedia:diacrit|diacrit]]. Though, once the word forming rule was established it was applied in ways where that original analogy was no longer relevant.)
# a sequence of numbers separated by spaces is a single token, in many languages, spaces are allowed in quoted strings, but J's numbers are not quoted.
# While most people are familiar with the character 'e' being used in numbers, J's token forming rules allow any letter to be used in numbers (even when the result is not a legal number -- also 'x' has a different significance than in C).
# J uses a different character to represent negative numbers than it uses for subtraction.
# J does not have precedence rules distinguishing between <code>+</code> and <code>*</code>. All such verbs have the same precedence and are processed strictly right to left (same as assignment statements and arabic numerals).
# Also, (like many languages) J uses index origin 0 and there are some people who think in terms of index origin 1.
 
Here's a example of the token forming gotcha:
<syntaxhighlight lang=J> 10 100 1000 +/ .*1 2 3 NB. vector multiplication
3210
10 100 1000 +/.*1 2 3 NB. complex conjugate of signum of right argument grouped by the left argument
1
1
1</syntaxhighlight>
 
Here's an example of using spaces in numbers, and how it can mess with people:
<syntaxhighlight lang=J>
A=: 2 3 5 7 11 NB. legal
B=: 999 NB. legal
B 0} A NB. functional array update (does not change A)
999 3 5 7 11
0} A NB. legal
2
999 0} A NB. not legal
|rank error
(999)0} A NB. what was intended
999 3 5 7 11</syntaxhighlight>
 
Here's a few other examples of how J's [[j:Vocabulary/Constants|constant notation]] can mess with people:
<syntaxhighlight lang=J>
0x100 NB. 0*e^100 where e is the natural logarithm base 2.71828...
0
0xff
|ill-formed number
16b100 16bff
256 255
8ad90 NB. 8 on the complex plane at an angle of 90 degrees from the real axis
0j8
-100 NB. two tokens
_100
_100+10 NB. three tokens
_90
-100+10 NB. four tokens
_110</syntaxhighlight>
 
The precedence and right-to-left approach give J a syntactic simplicity similar to LISP or Forth (even simpler than some implementations of those languages) while remaining an [[wikipedia:Infix|infix]] language. However, this can be confusing for people who have trained themselves on languages with more complex syntax:
 
<syntaxhighlight lang=J>
1+2*3 NB. processed right-to-left
7
2*3+1 NB. processed right-to-left
8
 
=={{header|jq}}==
6,954

edits