Basic Types

Basic Types

Type Values Description
Atom :ok, :error Erlang Atom type
Boolean(Bool) true \ false
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

true || false

Integers and Floats

Two types of numeric literals: Integers and Floats.

-- Integer
1, 2, -10

-- binary, octal, and hex literals
0x1, 0X1, 0x2a, 0X2A
0o1, 0O1, 0o52, 0O52
0b10, 0B10

-- floats
1.0, 1e10
2.3
2.3e-3
0.0023

Atoms

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

:atom, :ok, :error

Chars

UTF-8 Unicode characters.

'a', 'b', 'の'

Strings

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

"Hello, World!"
"你好,世界"
"ハロー、ワールド"

-- Escape Codes
"\r\n ..."

-- ++ to concat strings
"Hello " ++ " World"

-- TODO
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, "a", true)
(1, "a")

-- fst, snd
fst (1, 'a') :: Integer -- 1
snd (1, 'a') :: Char    -- 'a'

Lists

A list is sequence of values of the same type:

{-- List --}
[] -- empty list
[1,2,3] -- Integer list

[1|[2,3]] -- Cons
[1|[2|[3|[]]]] -- Cons

[x|_] = [1,2,3] -- List pattern
[_|xs] = [1,2,3] -- List pattern
[x|xs] -- Cons

Maps

Erlang-style maps are available in Hamler:

-- New map; all keys must have the same type, and all values must have the same type.
m = #{:foo => "foo", :bar => "bar"}

-- Pattern matching
#{:foo := a, :bar := b} = m
a :: String -- "foo"
b :: String -- "bar"

-- get, put
import Data.Map as Map
m1 = Map.put :key "val"
Map.get :foo m :: String -- "foo"
Map.get :key m1 :: String -- "val"

-- keys, values
keys   = Map.keys   m -- [String]
values = Map.values m -- [String]

Records

-- declare a Person record
type Person = {name :: String, age :: Integer}

-- create a Person record
p = {name = "John", age = 12}

-- update a Person record
p1 = p{name = "Miles", age = 20}

-- accessors
name = p1.name :: String
age = p1.age   :: Integer

Binaries

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

-- Construct Binary
<<127,0,0,1>>
<<"ABC">>
<<1:16,2:4,3:4>>

-- Binary Pattern Match
<<x:3,y:5,z:8>> = <<1,0>>
<<bigI:16:Big-Unsigned-Integer>> = <<1,2>>
<<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