All paradigms in one program

Recursion, control flow, lists, events (when/emit), logic (fact/query), OO (new/send/get), and functional map/filter via apply.

PatLang source
# Feature demo in the Stage 1 self-hosted language.
# Exercises: functions + recursion, control flow, lists, member access,
# events (when/emit), logic (fact/query), OO (new/send/get), and
# functional style (map/filter over named functions via apply).

make a function called fib takes n returns r
  if n < 2 then
    return n
  else
    return fib(n - 1) + fib(n - 2)
  end
end

make a function called sum_list takes xs returns total
  let total = 0
  let i = 0
  while i < xs.length do
    let total = total + xs[i]
    let i = i + 1
  end
  return total
end

when greeting do
  print("event received: " + event_data)
end

let nums = [1, 2, 3, 4]
print("fib(10) = " + fib(10))
print("sum = " + sum_list(nums))
if sum_list(nums) == 10 then
  print("sum ok")
else
  print("sum wrong")
end
emit("greeting", "hello events")
fact("parent", "alice", "bob")
fact("parent", "alice", "carol")
print("alice children: " + query("parent", "alice", 0))
new("Person", "kim")
send("kim", "set", "age", 42)
print("kim age: " + get("kim", "age"))

# functional style: higher-order map/filter over named functions
make a function called double takes x returns r
  return x * 2
end

make a function called is_even takes x returns r
  return x % 2 == 0
end

make a function called map_list takes xs, fname returns out
  let out = []
  let i = 0
  while i < xs.length do
    let out = list_push(out, apply(fname, xs[i]))
    let i = i + 1
  end
  return out
end

make a function called filter_list takes xs, fname returns out
  let out = []
  let i = 0
  while i < xs.length do
    if apply(fname, xs[i]) then
      let out = list_push(out, xs[i])
    end
    let i = i + 1
  end
  return out
end

print("doubled: " + map_list(nums, "double"))
print("evens: " + filter_list(nums, "is_even"))

(not run yet)

Native run on the build machine:

fib(10) = 55
sum = 10
sum ok
event received: hello events
alice children: 2
kim age: 42
doubled: [2, 4, 6, 8]
evens: [2, 4]