2 Diving In
2.1 Numbers and Operators
2.1.1 Integers and Floats
Let's start with something simple :-) To follow along, fire up your LFE REPL. Numbers are simple in LFE, just like Erlang:
Of course, it might be more interesting to look at something like different bases:
LFE supports representing binary (#b
), octal (#o
), decimal
(#d
), hexidecimal (#x
), as well as aribtrary bases from 1 through
36 (#XrY
).
With some help from calling an Erlang function, we can work the bases in reverse, too:
Note that the first argument is the number you want to convert and the second is the base you want to use (see here for more details).
2.1.2 Arithmatic Operators
But numbers by themselves aren't going to do us much good if we can't operate on them. The usual apply:
2.1.3 Logical Operators
The usual suspects are used as follows:
Note the rather awkward different between "less than" and "greater than": it's easy to forget that the angle brackets go at different ends for each.
Then there are the operators which also check against type for exact (non-)equality:
2.1.4 Boolean Operators
How about some logic?
There are also two boolean operators that you can use if you want to make a decision based on the truth value of the first term without having to compute the second term (useful if you have no need to do the second computation when the first term is false):
In the case of andalso
if the first argument is false
the second
one will not be evaluated; false
will be returned. In the case of
orelse
if the first argument is true
then true
will be
returned without evaluating the second argument.
Contrast this to regular or
and and
:
2.1.5 Bitwise Operators
As one would expect, Erlang has the usual bitwise operators as well. Binary representation is used below for clarity of demonstration. Let's define a utility function that will save a little typing:
With that defined so that we can use it, let's take a look at some of these operators: