Untyped Declarative Languages: Prolog, Erlang, Oz

a side-by-side reference sheet

arithmetic and logic | strings | lists and tuples | other containers | functions | execution control | environment and i/o
libraries and modules | reflection and hooks | repl | contact

prolog (1972) erlang (1986) oz (1991)
version used SWI Prolog 5.10.1 5.7.4 1.4.0
get version $ swipl --version displayed by erl at startup
repl $ swipl $ erl
statement terminator . .
atom lower case letter followed by alphanumeric characters; can also include underscore: _ lower case letter followed by alphanumeric characters; can also include period: . at-sign: @ underscore: _ lower case letter followed by alphanumeric characters
quoted atom any printable characters inside single quotes; use backslash to escape a single quote. any printable characters inside single quotes; use backslash or two single quotes to escape a single quote. any printable characters inside single quotes
variable upper case letter followed by alphanumeric characters upper case letter following by alphanumeric characters and underscores.
variable definition previously unused variables on the left side of an equal sign will be assigned to values that make the left match the right declare X = 3
if if
  X > 0 -> 1;
  X == 0 -> X;
  X < 0 -> -1
end
if X > 0 then 1 else if X==0 then 0 else ~1 end end
case case X of
  1 -> true;
  0 -> false
end
single line comment % a comment % a comment % a comment
arithmetic and logic
prolog erlang oz
true and false true fail true false
logical operators , ; ?? ?? and or xor not
short circuit operators:
andalso orelse
in guards:
, ;
comparison operators = \= < > =< >= == /= < > =< >=
no numeric conversion:
=:= =/=
== \= < > =< >=
arithmetic expression is(X,2+2). X = 2 + 2. declare Y = 2 + 2
arithmetic operators + - * ** int: ?? ?? float: / + - * none int: div rem float: /
use math:pow for exponentiation
+ - * ?? int: div mod float: /
arithmetic functions sqrt exp log sin cos tan asin acos atan atan2 math:sqrt math:exp math:log math:sin math:cos math:tan math:asin math:acos math:atan math:atan2
arithmetic truncation truncate round floor ceiling trunc round ?? ??
arithmetic decomposition
convert from string, to string 7 + list_to_integer("12")
73.9 + list_to_float("0.039")
"value: " ++ integer_to_list(8)
"value: " ++ float_to_list(3.14)
unary minus -4 -4 ~4
float literal with exponent 2.0e2
-2.0E-2
2.0e2
-2.0E-2
2.0e2
~2.0E~2
random integer, float is(X, random(100)).
??
random:uniform().
random:uniform(100).
seed set_random(seed(17)). random:seed(17,17,17).
result of not seeding seeded using /dev/random or system time interpreter uses same seed at startup.
strings
prolog erlang oz
string literal "don't say \"no\"" "don't say \"no\"" "hello"
character literal none $A
chr and ord char_code(X, 65).
char_code('A', X).
convert atom to string, from string name(foo, X).
string_to_atom("foo", X).
atom_to_list(foo)
list_to_existing_atom("foo")
string length length("hello",X). length("hello")
concatenate append("one ","two ",Y), append(Y,"three",X). "one " ++ "two " ++ "three"
concatenates double quoted string literals only:
"one " "two " "three"
lists and tuples
prolog erlang oz
list literal [1,2,3] [1,2,3] [1 2 3]
cons X = [4|[3,2,1]]. [4|[3,2,1]] 4|[3 2 1]
head [X|_] = [1,2,3]. hd([1,2,3])
or use pattern matching:
[Head|_] = [1,2,3].
Head
[1 2 3].1
tail [_|X] = [1,2,3]. tl([1,2,3])
or use pattern matching:
[_|Tail] = [1,2,3].
Tail
[1 2 3].2
length length([1,2,3], X). length([1,2,3]) {Length [1 2 3]}
append append([1,2], [3,4], List). [1,2] ++ [3,4]
sort sort([1,3,2,4], X). lists:sort([1,3,2,4]).
reverse reverse([1,2,3,4], X). lists:reverse([1,2,3,4]).
zip lists:zip([1,2,3],["a","b","c"]).
map lists:map(fun(X) -> X*X end, [1,2,3]).
filter lists:filter(fun(X) -> X > 2 end, [1,2,3]).
left fold lists:foldl(fun(X,Y) -> X-Y end, 0, [1,2,3,4]).
right fold lists:foldr(fun(X,Y) -> X-Y end, 0, [1,2,3,4]).
tuple literal (1, "hello", 3.14) {1, "foo", 3.14} (1 "foo" 3.14)
tuple element access element(1, {1, "foo", 3.14})
setelement(2, {1, "foo", 3.14}, "bar")
tuple length tuple_size({1, "foo", 3.14})
other containers
record medal_count(country:"Spain" gold:7 silver:4 bronze:8)
prolog erlang oz
functions
function definition factorial(0,1).
factorial(N,F) :- is(N1, N - 1), factorial(N1,F1), is(F, N*F1).
factorial(0) -> 1;
factorial(N) -> N * factorial(N-1).
declare
fun {Fact N}
XXif N==0 then 1 else N*{Fact N-1} end
end
function definition with guards factorial(N) when N > 0 -> N * factorial(N-1);
factorial(0) -> 1.
anonymous function fun(X, Y) -> X+Y end
piecewise defined anonymous function fun([]) -> null;
  ([X|_]) -> X
end
execution control
prolog erlang oz
for for I in 1..10 do {Browse I} end
try/catch X = 0.
try (7 div X) of
  Val -> Val
catch
  error:badarith -> 0
end.
receive message -module(echo).
-export([loop/0]).
 
loop() ->
  receive
    {From, Msg} ->
      From ! { self(), Msg},
      loop();
    stop ->
      true
  end.
spawn process Pid = spawn(echo, loop, []).
send message Pid ! {self(), hello}.
list processes processes().
environment and i/o
prolog erlang oz
open file for reading open('foo.txt', read, Fd).
open file for writing open('foo.txt', write, Fd).
close file close(Fd).
read line X = io:get_line("type line: ").
read character get_char(X). X = io:get_chars("type char: ", 1).
read term read(X). {ok,X} = io:read("type term: ").
write character put_char("A").
put_char(65)
write term X = hello, write(X). io:write(X).
printf format('foo: ~s ~2f ~w~n', ["bar", 3.1415, 7]). io:format("foo: ~s ~.2f ~w~n", ["bar", 3.1415, 7]).
libraries and modules
prolog erlang oz
load file ways to load file data.pl:
[data].
['data.pl'].
consult(data)
define module in file factorial.erl
-module(factorial).
-export([factorial/1]).
definition of factorial
compile module c(factorial).
use function in module factorial:factorial(7).
reflection and hooks
prolog erlang oz
inspect module factorial:module_info().
repl
clear variable f(X).
clear all variables f().
display processes i().
________________________________________________________ ________________________________________________________ ________________________________________________________

General

Arithmetic and Logic

comparison operators

erlang:

Comparison operators can be performed on values with different types. In this case the precedence is determined by
the type according to this sequence:

number < atom < reference < fun < port < pid < tuple < list < binary

If a comparison is performed on an integer and a float, the integer will be converted to a float. =:= and =/= do not perform conversions and thus will always return false and true respectively when called on an integer and a float.

Strings

Lists and Tuples

Other Containers

Functions

function definition

function definition with guards

erlang:

The expressions in guards must be side-effect free. Thus they can only contain the following:

  • bound variables
  • literals
  • type tests: is_atom, is_boolean, is_tuple, …
  • comparison operators
  • arithmetic operators
  • boolean operators
  • a few built-in functions

Execution Control

Environment and I/O

read line

erlang:

io:get_line accepts an argument which is displayed as a prompt. The return value is a string which includes the newline.

read character

erlang:

io:get_chars accepts two arguments. The first is a string which is displayed as a prompt. The second is the number of characters to be read. The function keeps reading lines until the requested number of characters has been collected. The return value is a string which can contain newlines if a line of less than the requested number of characters was entered.

Libraries and Modules

Reflection and Hooks

REPL

Prolog

SWI Prolog Reference Manual
GNU Prolog Manual
Prolog: The ISO Standard Document

Erlang

Erlang Reference Manual User's Guide
Erlang Standard Library
A response to “Erlang - overhyped or underestimated”

Oz

The Oz Base Environment
System Modules
Tutorial of Oz

Oz runs in Emacs. There will be three buffers: Oz, which contains the source code, *Oz Compiler*, which contains messages from the compiler, and *Oz Emulator*, which contains standard output from running the code. A single line of Oz code can be executed with C-c . C-l

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License