基本操作符

    Elixir也提供了对列表的++--操作:

    字符串连接符是<>

    1. iex> "foo" <> "bar"
    2. "foobar"

    Elixir也提供了三个布尔操作符:orandnot。这些操作符要求以布尔类型作为其弟一个参数:

    1. iex> true and true
    2. true
    3. iex> false or is_atom(:example)
    4. true

    若第一个参数不是布尔类型则会抛出异常:

    1. iex> false and raise("This error will never be raised")
    2. false
    3. iex> true or raise("This error will never be raised")
    4. true

    除此之外,Elixir也提供||&&和操作符,它们接受任何形式的参数。在这些操作符中,除了falsenil之外的值都会被认定为真:

    1. # or
    2. iex> 1 || true
    3. 1
    4. iex> false || 11
    5. 11
    6. iex> nil && 13
    7. nil
    8. iex> true && 17
    9. 17
    10. # !
    11. iex> !true
    12. false
    13. iex> !1
    14. false
    15. iex> !nil
    16. true

    推荐的做法是,当你期望布尔型时使用andornot。如果参数不是布尔型,那么使用||&&!

    Elixir也提供了==!====,,<=>=<>作为比较运算符:

    1. iex> 1 == 1.0
    2. true
    3. iex> 1 === 1.0
    4. false

    在Elixir中,我们可以比较不同的数据类型:

    1. iex> 1 < :atom
    2. true

    这是处于实用角度考虑。排序算法不用再担心不同的数据类型。排序定义如下:

    你不必记住这个顺序,但需要知道它的存在。

    操作符表

    1. @ Unary
    2. . Left to right
    3. + - ! ^ not ~~~ Unary
    4. * / Left to right
    5. + - Left to right
    6. ++ -- .. <> Right to left
    7. in Left to right
    8. |> <<< >>> ~>> <<~ ~> <~ <~> <|> Left to right
    9. < > <= >= Left to right
    10. == != =~ === !== Left to right
    11. && &&& and Left to right
    12. || ||| or Left to right
    13. = Right to left
    14. => Right to left
    15. | Right to left
    16. :: Right to left
    17. when Right to left

    这些操作符中的大部分会在我们的教程中学习到。在下一章,我们将讨论一些基本函数,数据类型转换和一点点控制流。