Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TeamSPoon/62b26d489e653fee87ee74aa983d043b to your computer and use it in GitHub Desktop.
Save TeamSPoon/62b26d489e653fee87ee74aa983d043b to your computer and use it in GitHub Desktop.
Prolog-like append/3 for Hyperon in MeTTa
use hyperon::metta::*;
use hyperon::metta::interpreter::*;
fn main() {
let space = metta_space("
(= (append () $L $L) True)
(= (append (Cons $H $T) $L (Cons $H $R)) (append $T $L $R))
");
println!("{:?}", interpret(&space, &metta_atom("(append (Cons $A (Cons 2 $L1)) $L2 (Cons 1 (Cons $B (Cons 2 (Cons 3 (Cons 4 ()))))))")));
}
@TeamSPoon
Copy link
Author

TeamSPoon commented Aug 3, 2023

!(if (append (Cons $A (Cons 2 $L1)) $L2 (Cons 1 (Cons $B (Cons 3 (Cons 4 (Cons 5 ())))))) (print $A $B $L1 $L2) nop)

[(print 1 2 (Cons 3 (Cons 4 (Cons 5 ()))) ()),
(print 1 2 (Cons 3 (Cons 4 ())) (Cons 5 ())),
(print 1 2 (Cons 3 ()) (Cons 4 (Cons 5 ()))),
(print 1 2 () (Cons 3 (Cons 4 (Cons 5 ()))))]

?- append(
     [A,2|L1], L2,
      [1,B,3,4,5]).

A = 1, B = 2, L1 = [], L2 = [3,4,5] ; 
A = 1, B = 2, L1 = [3], L2 = [4,5];
A = 1, B = 2, L1 = [3,4], L2 = [5];
A = 1, B = 2, L1 = [3,4,5], L2 = [];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment