@@ -12,6 +12,24 @@ All arithmetic operators are entered in prefix notation.
1212(+ 1 (* 2 2) (/ 10 5) 3 4 (- 5 6)) # Evaluates to 13
1313```
1414
15+ {% php_note() %}
16+ Phel uses prefix notation (operator comes first) instead of PHP's infix notation:
17+
18+ ``` php
19+ // PHP - infix notation
20+ 1 + (2 * 2) + (10 / 5) + 3 + 4 + (5 - 6);
21+
22+ // Phel - prefix notation
23+ (+ 1 (* 2 2) (/ 10 5) 3 4 (- 5 6))
24+ ```
25+
26+ This allows operators to accept any number of arguments and eliminates operator precedence concerns.
27+ {% end %}
28+
29+ {% clojure_note() %}
30+ Arithmetic works exactly like Clojure—prefix notation with variadic support for most operators.
31+ {% end %}
32+
1533Some operators support zero, one or multiple arguments.
1634
1735``` phel
@@ -34,6 +52,27 @@ Some operators support zero, one or multiple arguments.
3452(/ 24 4 2) #Evaluates to 3
3553```
3654
55+ {% php_note() %}
56+ Phel's variadic operators are more flexible than PHP's:
57+
58+ ``` php
59+ // PHP - requires at least two operands
60+ 1 + 2 + 3 + 4 + 5;
61+ // Can't do this: +(); <- syntax error
62+
63+ // Phel - supports 0, 1, or many operands
64+ (+) # 0 (identity)
65+ (+ 1) # 1 (identity)
66+ (+ 1 2 3 4 5) # 15 (sum of all)
67+ ```
68+
69+ **Useful patterns:**
70+ - `(+)` returns the additive identity (0)
71+ - `(*)` returns the multiplicative identity (1)
72+ - `(- x)` negates a number
73+ - `( / x)` computes the reciprocal
74+ {% end %}
75+
3776Further numeric operations are `%` to compute the remainder of two values and `**` to raise a number to the power. All numeric operations can be found in the API documentation.
3877
3978Some numeric operations can result in an undefined or unrepresentable value. These values are called _Not a Number_ (NaN). Phel represents these values by the constant `NAN`. You can check if a result is NaN by using the `nan?` function.
@@ -44,6 +83,24 @@ Some numeric operations can result in an undefined or unrepresentable value. The
4483(nan? NAN) # true
4584```
4685
86+ {% php_note() %}
87+ NaN handling is similar to PHP:
88+
89+ ```php
90+ // PHP
91+ is_nan(1); // false
92+ is_nan(log(-1)); // true
93+ is_nan(NAN); // true
94+
95+ // Phel
96+ (nan? 1) # false
97+ (nan? (php /log -1)) # true
98+ (nan? NAN) # true
99+ ```
100+
101+ The `%` operator for remainder and `**` for exponentiation work like PHP ' s `%` and `**` operators.
102+ {% end %}
103+
47104## Bitwise Operators
48105
49106Phel allows the evaluation and manipulation of specific bits within an integer.
@@ -80,3 +137,31 @@ Phel allows the evaluation and manipulation of specific bits within an integer.
80137(bit-test 0b1011 0) # Evaluates to true
81138(bit-test 0b1011 2) # Evaluates to false
82139```
140+
141+ {% php_note() %}
142+ Phel provides named functions for bitwise operations instead of PHP' s operators:
143+
144+ ```php
145+ // PHP bitwise operators
146+ 0b1100 & 0b1001; // AND
147+ 0b1100 | 0b1001; // OR
148+ 0b1100 ^ 0b1001; // XOR
149+ ~0b0111; // NOT
150+ 0b1101 << 1; // Left shift
151+ 0b1101 >> 1; // Right shift
152+
153+ // Phel named functions
154+ (bit-and 0b1100 0b1001)
155+ (bit-or 0b1100 0b1001)
156+ (bit-xor 0b1100 0b1001)
157+ (bit-not 0b0111)
158+ (bit-shift-left 0b1101 1)
159+ (bit-shift-right 0b1101 1)
160+ ```
161+
162+ Phel also provides additional bit manipulation functions not available in PHP: ` bit-set ` , ` bit-clear ` , ` bit-flip ` , and ` bit-test ` .
163+ {% end %}
164+
165+ {% clojure_note() %}
166+ Bitwise operators work exactly like Clojure's—same function names and behavior for bit manipulation.
167+ {% end %}
0 commit comments