Last active
August 29, 2015 13:57
-
-
Save yeonsh/9606813 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> 1 # integer | |
iex> 0x1F # integer | |
iex> 1.0 # float | |
iex> :atom # atom / symbol | |
iex> {1,2,3} # tuple | |
iex> [1,2,3] # list | |
iex> <<1,2,3>> # bitstring | |
iex> size { 1, 2, 3 } | |
3 | |
iex> length [ 1, 2, 3 ] | |
3 | |
iex> size [1, 2, 3] | |
** (ArgumentError) argument error | |
:erlang.size([1, 2, 3]) | |
iex> length {1, 2, 3} | |
iex(15)> length {1,2,3} | |
** (ArgumentError) argument error | |
:erlang.length({1, 2, 3}) | |
iex> "하하하" | |
"하하하" | |
iex> name = "world" | |
iex> "hello #{name}" | |
"hello world" | |
iex> is_binary("hello") | |
true | |
iex> is_binary('hello') | |
false | |
iex> is_list('hello') | |
true | |
iex> byte_size("hello") | |
5 | |
iex> is_bitstring("hello") | |
true | |
iex(41)> is_bitstring 'hello' | |
false | |
iex> bit_size("hello") | |
40 | |
iex> byte_size("하") | |
3 | |
iex> bit_size("하") | |
24 | |
iex> byte_size([1,2,3]) | |
** (ArgumentError) argument error | |
:erlang.byte_size([1, 2, 3]) | |
iex> is_binary([1,2,3]) | |
false | |
iex> is_binary(<<1,2,3>>) | |
true | |
iex> is_binary <<1,2,3>> | |
true | |
iex> is_bitstring(<<1,2,3>>) | |
true | |
iex> bytes=<<1,2,3,>> | |
<<1, 2, 3>> | |
iex> "hello #{bytes}" | |
<<104, 101, 108, 108, 111, 32, 1, 2, 3>> | |
iex> true | |
true | |
iex> is_boolean false | |
true | |
iex> is_atom(true) | |
true | |
# function | |
iiex(42)> x = fn(a,b) -> a + b end | |
#Function<12.80484245/2 in :erl_eval.expr/5> | |
iex(43)> x.(1,2) | |
3 | |
iex(44)> x.("a","b") | |
** (ArithmeticError) bad argument in arithmetic expression | |
:erlang.+("a", "b") | |
iex(44)> x.('a','b') | |
** (ArithmeticError) bad argument in arithmetic expression | |
:erlang.+('a', 'b') | |
iex(44)> x.([1],[2]) | |
** (ArithmeticError) bad argument in arithmetic expression | |
:erlang.+([1], [2]) | |
iex(44)> x.({1},{2}) | |
** (ArithmeticError) bad argument in arithmetic expression | |
:erlang.+({1}, {2}) | |
iex> [1,2,3] ++ [4,5,6] | |
[1,2,3,4,5,6] | |
iex> [1,2,3] -- [2] | |
[1,3] | |
iex> "foo" <> "bar" | |
"foobar" | |
iex> true and true # expect a boolean (true or false) as their first argument: | |
true | |
iex> false or is_atom(:example) | |
true | |
iex> 1 and true | |
** (ArgumentError) argument error | |
iex> false and error("This error will never be raised") | |
false | |
iex> true or error("This error will never be raised") | |
true | |
iex(44)> error("error!") | |
** (RuntimeError) undefined function: error/1 | |
# &&, || : all values except false and nil will evaluate to true: | |
# or | |
iex> 1 || true | |
1 | |
iex> false || 11 | |
11 | |
# and | |
iex> nil && 13 | |
nil | |
iex> true && 17 | |
17 | |
# ! | |
iex> !true | |
false | |
iex> !1 | |
false | |
iex> !nil | |
true | |
iex(45)> true and 17 and 18 | |
** (ArgumentError) argument error: 17 | |
iex(47)> true && 17 && 18 | |
18 | |
# As a rule of thumb, use and, or and not when you are expecting booleans. | |
# If any of the arguments are non-boolean, use &&, || and !. | |
iex> 1 == 1 | |
true | |
iex> 1 != 2 | |
true | |
iex> 1 < 2 | |
true | |
iex> 1 == 1.0 | |
true | |
iex> 1 === 1.0 | |
false | |
iex(48)> 1 != 1.0 | |
false | |
iex(49)> 1 !== 1.0 | |
true | |
iex> 1 < :atom | |
true | |
# The reason we can compare different data types is for pragmatism. | |
# Sorting algorithms don't need to worry about different data types | |
# in order to sort. The overall sorting order is defined below: | |
# number < atom < reference < functions < port < pid < tuple < list < bitstring |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment