• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • CPACK
    • Home
    • List packs
    • Submit pack
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

12.4.4.5 Reading a list
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
      • Foreign Language Interface
        • The Foreign Include File
          • Analysing Terms via the Foreign Interface
            • Reading a list
              • PL_get_list()
              • PL_get_head()
              • PL_get_tail()
              • PL_get_nil()
              • PL_skip_list()
              • PL_get_list_slice()
    • Packages
Availability:C-language interface function
bool PL_get_list_slice(term_t +list, size_t start, size_t length, pl_list_slice_f func, void *closure, size_t *count, unsigned int flags)
Call func for the elements start ... start+length-1 of list, where the first element is 0. If length is (size_t)-1, call func for all elements from start to the end of the list. The type of func is below. The argument index is the index of the element in the slice, i.e., it starts at 0 for the element start.
typedef int (*pl_list_slice_f)(term_t elem, size_t index, void *closure);

The term reference elem is reused for all elements and is only valid during the call to func. func may call any API function, including functions that create terms or call Prolog. If func returns FALSE, PL_get_list_slice() stops and returns FALSE, leaving an exception raised by func in place. If count is not NULL, it is filled with the number of elements processed.

The function returns FALSE if the list is too short, if length is (size_t)-1 and the list is partial, does not end in [] or is cyclic, or if the list is partial, not a list or cyclic before the end of the slice. If flags includes CVT_EXCEPTION, a partial list raises an instantiation error and a cyclic list or a term that is not a list raises a type error. A list that is too short does not raise an exception. Cycles are detected without extra cost for short lists. Below is the list example using this function:

static int
write_atom(term_t a, size_t index, void *closure)
{ int rc;
  char *s;

  PL_STRINGS_MARK();
  if ( (rc=PL_get_chars(a, &s, CVT_ATOM|REP_MB|CVT_EXCEPTION)) )
    rc = Sfprintf(closure, "%s\n", s) >= 0;
  PL_STRINGS_RELEASE();

  return rc;
}

foreign_t
pl_write_atoms(term_t l)
{ return PL_get_list_slice(l, 0, (size_t)-1,
                           write_atom, Scurrent_output,
                           NULL, CVT_EXCEPTION);
}

ClioPatria (version V3.1.1-59-g7b9a7a3)