Standard Library Overview
← MeTTa Tutorial · Lesson 4 of 5
Standard Library Overview. A tour of the everyday building blocks MeTTa ships with — arithmetic, output, nondeterminism, Spaces, control flow, and operations that take expressions apart.
Basic grounded functions
Arithmetic, comparison, and Boolean operators are grounded (implemented natively):
!(+ 2 3) ; [5]
!(< 3 10) ; [True]
!(== 4 4) ; [True]
!(and True False) ; [False]
Console output and debugging
println! prints a value and returns the unit atom; trace! prints a message alongside a value while passing the value through — handy for inspecting a computation without changing its result:
!(println! "Hello, MeTTa!") ; prints: Hello, MeTTa!
!(trace! computing (* 6 7)) ; prints: computing, returns [42]
Handling nondeterministic results
superpose turns a tuple into many results; collapse gathers many results back into one tuple. They are inverses:
!(superpose (1 2 3)) ; [1, 2, 3]
!(collapse (superpose (1 2 3))) ; [(1 2 3)]
Working with spaces
Beyond &self you can make new Spaces and add, query, or remove atoms in them:
!(bind! &kb (new-space))
!(add-atom &kb (color sky blue))
!(match &kb (color sky $c) $c) ; [blue]
!(remove-atom &kb (color sky blue))
Control flow
if branches on a Boolean; let* binds intermediate results in sequence:
!(if (> 5 3) bigger smaller) ; [bigger]
!(let* (($x 4) ($y (* $x $x)))
(result $y)) ; [(result 16)]
Operations over atoms
Expressions are data, so you can take them apart and rebuild them — car-atom (first element), cdr-atom (the rest), and cons-atom (prepend):
!(car-atom (a b c)) ; [a]
!(cdr-atom (a b c)) ; [(b c)]
!(cons-atom a (b c)) ; [(a b c)]
With these you can write functions that traverse and transform arbitrary expressions — the heart of MeTTa's “code is data” design.
Next
Lesson 5 — Using MeTTa from Python: drive MeTTa from Python and extend it with your own grounded operations.