diff --git a/src/termlist.c b/src/termlist.c index ae4f4e9..7aa04d9 100644 --- a/src/termlist.c +++ b/src/termlist.c @@ -790,3 +790,54 @@ termlist_iterate (Termlist tl, int (*func) ()) } return 1; } + +//! Create a tuple term from a termlist +Term termlist_to_tuple (Termlist tl) +{ + int width; + + width = termlistLength (tl); + if (width > 1) + { + // 2 parts + // Make two termlists for each side. + Term tresult; + Termlist tl1, tl2; + int split, i; + + /** + * This can be done much more efficiently by cutting + * the list temporarily, and reconnecting it afterwards. + */ + tl1 = NULL; + tl2 = NULL; + split = width / 2; + i = 0; + while (tl != NULL) + { + if (i < split) + tl1 = termlistAdd (tl1, tl->term); + else + tl2 = termlistAdd (tl2, tl->term); + tl = tl->next; + i++; + } + tresult = makeTermTuple (termlist_to_tuple (tl1), termlist_to_tuple (tl2)); + termlistDelete (tl1); + termlistDelete (tl2); + return tresult; + } + else + { + if (tl == NULL) + { + // W00t! Wtf? + error ("termlist_to_tuple called (internally?) with NULL"); + } + else + { + // Single node, simple + return termDuplicate(tl->term); + } + } +} diff --git a/src/termlist.h b/src/termlist.h index a20e5c5..dc917a4 100644 --- a/src/termlist.h +++ b/src/termlist.h @@ -55,5 +55,6 @@ Term termFunction (Termlist fromlist, Termlist tolist, Term tx); Termlist termlistForward (Termlist tl); int termlistOrder (Termlist tl1, Termlist tl2); int termlist_iterate (Termlist tl, int (*func) ()); +Term termlist_to_tuple (Termlist tl); #endif