Basic Types

Type Values Description
Atom :ok, :error Erlang Atom type
Boolean(Bool) true \ false Boolean type
Char ‘c’, ‘x’ UTF-8 character
String “hello” List of UTF-8 character
Integer(Int) 1, 2, -10 Integer type
Float(Double) 3.14 Float type
List
Tuple (1, true)
Map #{“k” => “v”} Erlang Map
Record
Binary <<1,2,3>> Erlang Binary/Bitstring
Pid Erlang Pid
Port Erlang Port
Reference(Ref) Erlang Reference

Booleans

  1. true || false

Integers and Floats

Two types of numeric literals: Integers and Floats.

  1. -- Integer
  2. 1, 2, -10
  3. -- binary, octal, and hex literals
  4. 0x1, 0X1, 0x2a, 0X2A
  5. 0o1, 0O1, 0o52, 0O52
  6. 0b10, 0B10
  7. -- floats
  8. 1.0, 1e10
  9. 2.3
  10. 2.3e-3
  11. 0.0023

Atoms

In Hamler, atoms start with ‘:’, and correspond to Erlang atoms.

  1. :atom, :ok, :error

Chars

UTF-8 Unicode characters.

  1. 'a', 'b', 'の'

Strings

In Hamler, a string is a list of UTF-8 Unicode characters.

  1. "Hello, World!"
  2. "你好,世界"
  3. "ハロー、ワールド"
  4. -- Escape Codes
  5. "\r\n ..."
  6. -- ++ to concat strings
  7. "Hello " ++ " World"
  8. -- TODO
  9. printf "foo %s" "bar"

Tuples

A tuple is a sequence of values of different types. In Hamler, the maximum length of the tuple is 7.

  1. (1, "a", true)
  2. (1, "a")
  3. -- fst, snd
  4. fst (1, 'a') :: Integer -- 1
  5. snd (1, 'a') :: Char -- 'a'

Lists

A list is sequence of values of the same type:

  1. {-- List --}
  2. [] -- empty list
  3. [1,2,3] -- Integer list
  4. [1|[2,3]] -- Cons
  5. [1|[2|[3|[]]]] -- Cons
  6. [x|_] = [1,2,3] -- List pattern
  7. [_|xs] = [1,2,3] -- List pattern
  8. [x|xs] -- Cons

Maps

Erlang-style maps are available in Hamler:

  1. -- New map; all keys must have the same type, and all values must have the same type.
  2. m = #{:foo => "foo", :bar => "bar"}
  3. -- Pattern matching
  4. #{:foo := a, :bar := b} = m
  5. a :: String -- "foo"
  6. b :: String -- "bar"
  7. -- get, put
  8. import Data.Map as Map
  9. m1 = Map.put :key "val"
  10. Map.get :foo m :: String -- "foo"
  11. Map.get :key m1 :: String -- "val"
  12. -- keys, values
  13. keys = Map.keys m -- [String]
  14. values = Map.values m -- [String]

Records

  1. -- declare a Person record
  2. type Person = {name :: String, age :: Integer}
  3. -- create a Person record
  4. p = {name = "John", age = 12}
  5. -- update a Person record
  6. p1 = p{name = "Miles", age = 20}
  7. -- accessors
  8. name = p1.name :: String
  9. age = p1.age :: Integer

Binaries

Binaries are imported from Erlang, which are raw byte strings.

  1. -- Construct Binary
  2. <<127,0,0,1>>
  3. <<"ABC">>
  4. <<1:16,2:4,3:4>>
  5. -- Binary Pattern Match
  6. <<x:3,y:5,z:8>> = <<1,0>>
  7. <<bigI:16:Big-Unsigned-Integer>> = <<1,2>>
  8. <<litI:16:Little-Signed-Integer>> = <<1,2>>

Ports

TODO: Erlang port identifier identifies an Erlang port.

PIDs

TODO: Erlang process identifier, pid, identifies a process.

References

TODO: Erlang reference