/* ============================================================ | is_t_list() | |----------------------------------------------------------| | Params : 1)a term | | | | Returns:TRUE if term is a list, FALSE otherwise | |==========================================================| */ int is_t_list(struct term *list) { if (list==NULL) return FALSE; else return (list->functor==CONS_LIST||list->functor==EMPTY_LIST); } /* ============================================================ | t_empty_list() | |----------------------------------------------------------| | Params : 1) a term | | | | | | Desc : | | | | Returns:TRUE if term is an empty interpreter list,false | | otherwise | |==========================================================| */ int t_empty_list(struct term *t) { if (t!=NULL) return (t->functor==EMPTY_LIST); else return FALSE; } /* ============================================================ | t_object() | |----------------------------------------------------------| | Params : 1) a term representing a list | | Desc : | | | | Returns: the head element of the list | |==========================================================| */ struct term *t_object(struct term *l) { if (l==NULL) return NULL; if (l->functor==CONS_LIST) return l->argument_array[0]; else return NULL; } /* ============================================================ | t_next() | |----------------------------------------------------------| | Params : 1) a term representing a list | | | | | | Desc : | | | | Returns: the tail of the list | |==========================================================| */ struct term *t_next(struct term *l) { if (l==NULL) return NULL; if (l->functor==CONS_LIST) return l->argument_array[1]; else return NULL; } /* ============================================================ | t_length() | |----------------------------------------------------------| | Params : 1) a term representing a list | | Returns: the length of the list | |==========================================================| */ int t_length(struct term *list) { int result=0; while(is_t_list(list) && !t_empty_list(list)) { result++; list=t_next(list); } return result; } /* ============================================================ | t_null() | |----------------------------------------------------------| | Returns:an empty interpreter list | |==========================================================| */ struct term *t_null() { return maketerm(NONVAR,EMPTY_LIST,0,NULL,NULL); } /* ============================================================ | t_cons() | |----------------------------------------------------------| | Params : 1) a term | | 2) a term representing a list | | | | Returns: a term representing a list with the new element added to the head |==========================================================| */ struct term *t_cons(struct term *t, struct term *list) { struct term **r_array=maketermarray(2); r_array[0]=t; r_array[1]=list; if (is_t_list(list)) return maketerm(NONVAR,CONS_LIST,2,NULL,r_array); else return NULL; } /* ============================================================ | t_nth() | |----------------------------------------------------------| | Params : 1) a term representing a list | | 2) an integer | | | | Returns:the nth item on the list (starting from 0) | |==========================================================| */ struct term *t_nth(struct term *list, int n) { int i; for(i=0;ifunctor==functor); list=t_next(list); } return result; }