Prelude: stack-oriented programming
Xlerb, like Forth (from which most of its syntax and semantics derives) is a stack-oriented programming language. But hold up - what does it even mean for something to be « x-oriented »?
We’re probably used to the term “Object-oriented” – like say Smalltalk or Ruby; everything in the language is an “object”, a thing you can send messages to and make it change or do something:
class Person
attr_accessor :name
def initialize(name, title)
@name = name
@title = title
end
def greet
puts @name
end
end
p = Person.new("Alice", "OO Wizard")
p.greet()
# Change their name!
p.name = "Bob"
10.times { p.greet() }
In function-oriented languages, every action is just a function from one thing to the next, and we typically can’t change anything, just make a new, slightly different version. This lends itself well to say, data processing pipelines whose steps we can easily reason about:
defmodule ETL do
def extract do
[
%{name: "alice", job: "function spell caster"},
%{name: "bob", job: "data tamer"}
]
end
def transform(rows) do
Enum.map(rows, & %{&1 | name: String.capitalize(row.name) }
end)
end
def load(rows) do
Enum.each(rows, &DB.insert(&1))
end
def run, do: extract() |> transform() |> load()
end
In logic oriented languages, we don’t write classes or even functions at all, but set up webs of logical relations between things. For some
works_at(brian, bell_labs).
works_at(dennis, bell_labs).
works_at(joe, ericsson).
works_at(mike, ericsson).
works_at(robert, ericsson).
% Two different people are colleagues if
colleague(X, Y) :-
% they work together
works_at(X, Workplace),
works_at(Y, Workplace),
% they're different people!
X @< Y.
% find all the colleague pairs
?- colleague(X, Y).
X = brian,
Y = dennis ;
X = joe,
Y = mike ;
X = joe,
Y = robert ;
%...
Now in stack oriented languages, we could say that ‘everything is a stack’. Or really, ‘there is a stack’, and that’s the only thing you can manipulate.