Arithmetic Operators#
All arithmetic operators are entered in prefix notation.
# (1 + (2*2) + (10/5) + 3 + 4 + (5 - 6))
# Evaluates to 13
PHP Coming from PHP? ›
Phel uses prefix notation (operator comes first) instead of PHP's infix notation:
This allows operators to accept any number of arguments and eliminates operator precedence concerns.
Clojure Coming from Clojure? ›
Arithmetic works exactly like Clojure—prefix notation with variadic support for most operators.
Some operators support zero, one or multiple arguments.
# Evaluates to 0
# Evaluates to 1
# Evalutaes to 3
# Evaluates to 45
# Evaluates to 0
# Evaluates to -1
# Evaluates to 1
# Evaluates to 0
# Evaluates to 1
# Evaluates to 2
#Evaluates to 24
# Evaluates to 1
# Evaluates to 0.5 (reciprocal of 2)
#Evaluates to 3
PHP Coming from PHP? ›
Phel's variadic operators are more flexible than PHP's:
Useful patterns:
(+)returns the additive identity (0)(*)returns the multiplicative identity (1)(- x)negates a number(/ x)computes the reciprocal
Further 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.
Some 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.
# false
# true
# true
PHP Coming from PHP? ›
NaN handling is similar to PHP:
The % operator for remainder and ** for exponentiation work like PHP's % and ** operators.
Bitwise Operators#
Phel allows the evaluation and manipulation of specific bits within an integer.
# Bitwise and
# Evaluates to 8 (0b1000)
# Bitwise or
# Evaluates to 13 (0b1101)
# Bitwise xor
# Evaluates to 5 (0b0101)
# Bitwise complement
# Evaluates to -8
# Shifts bit n steps to the left
# Evaluates to 26 (0b11010)
# Shifts bit n steps to the right
# Evaluates to 6 (0b0110)
# Set bit at index n
# Evalutes to (0b1111)
# Clear bit at index n
# Evaluates to 3 (0b0011)
# Flip bit at index n
# Evaluates to 15 (0b1111)
# Test bit at index n
# Evaluates to true
# Evaluates to false
PHP Coming from PHP? ›
Phel provides named functions for bitwise operations instead of PHP's operators:
Phel also provides additional bit manipulation functions not available in PHP: bit-set, bit-clear, bit-flip, and bit-test.
Clojure Coming from Clojure? ›
Bitwise operators work exactly like Clojure's—same function names and behavior for bit manipulation.