/usr/local/lib/swipl/library/lazy_lists.pl
All Application Manual Name SummaryHelp

  • swipl
    • library
      • error.pl
      • debug.pl
      • apply.pl
      • lists.pl
      • broadcast.pl
      • shlib.pl -- Utility library for loading foreign objects (DLLs, shared objects)
      • option.pl -- Option list processing
      • thread_pool.pl
      • gensym.pl
      • settings.pl
      • arithmetic.pl -- Extensible arithmetic
      • main.pl
      • readutil.pl
      • operators.pl
      • pairs.pl
      • prolog_source.pl
      • record.pl
      • quasi_quotations.pl
      • pure_input.pl
      • solution_sequences.pl
      • ordsets.pl -- Ordered set manipulation
      • random.pl
      • base64.pl
      • aggregate.pl
      • yall.pl
      • sandbox.pl -- Sandboxed Prolog code
      • apply_macros.pl -- Goal expansion rules to avoid meta-calling
      • assoc.pl
      • prolog_format.pl -- Analyse format specifications
      • predicate_options.pl
      • csv.pl -- Process CSV (Comma-Separated Values) data
      • pprint.pl
      • atom.pl
      • modules.pl
      • occurs.pl
      • prolog_xref.pl -- Prolog cross-referencer data collection
      • prolog_colour.pl -- Prolog syntax colouring support.
      • lazy_lists.pl -- Lazy list handling
        • lazy_list/2
        • lazy_list/3
        • lazy_list_materialize/1
        • lazy_list_length/2
        • lazy_list_iterator/4
        • lazy_get_codes/4
        • lazy_read_terms/4
        • lazy_read_lines/4
        • lazy_message_queue/4
        • lazy_engine_next/4
        • lazy_findall/3
        • lazy_findall/4
      • ugraphs.pl -- Graph manipulation library
      • url.pl
      • www_browser.pl -- Open a URL in the users browser
      • prolog_pack.pl -- A package manager for Prolog
      • git.pl
      • utf8.pl
      • dialect.pl
      • system.pl
      • terms.pl
      • date.pl
      • persistency.pl
      • iostream.pl -- Utilities to deal with streams
      • prolog_stack.pl
      • edinburgh.pl
      • prolog_clause.pl
      • prolog_breakpoints.pl -- Manage Prolog break-points
      • wfs.pl
      • prolog_code.pl
      • sort.pl
      • dicts.pl
      • dif.pl
      • varnumbers.pl
      • pio.pl
      • base32.pl -- Base32 encoding and decoding
      • charsio.pl
      • codesio.pl
      • coinduction.pl
      • heaps.pl
      • rbtrees.pl
      • statistics.pl -- Get information about resource usage
      • when.pl
      • backcomp.pl
      • ansi_term.pl -- Print decorated text to ANSI consoles
      • prolog_versions.pl -- Demand specific (Prolog) versions
      • ctypes.pl -- Character code classification
      • check.pl
      • prolog_codewalk.pl -- Prolog code walker
      • portray_text.pl
      • listing.pl -- List programs and pretty print clauses
      • streams.pl
      • shell.pl
      • quintus.pl
      • edit.pl -- Editor interface
      • thread.pl -- High level thread primitives
      • prolog_coverage.pl
      • prolog_debug.pl
      • prolog_trace.pl
      • threadutil.pl -- Interactive thread utilities
      • make.pl
      • prolog_wrap.pl
      • tty.pl
      • intercept.pl -- Intercept and signal interface
      • optparse.pl
      • writef.pl
      • nb_set.pl
      • exceptions.pl -- Exception classification
      • zip.pl
      • explain.pl
      • oset.pl
      • increval.pl
      • tables.pl
      • macros.pl
      • strings.pl
      • prolog_jiti.pl
      • prolog_config.pl
      • prolog_metainference.pl
 lazy_list(:Next, +State0, -List)
Create a lazy list where the next element is defined by
call(Next, State0, State1, Head)

The example below uses this predicate to define a lazy list holding the Fibonacci numbers. Our state keeps the two previous Fibonacci numbers.

fibonacci_numbers(L) :-
    lazy_list(fib, state(-,-), L).

fib(state(-,-), state(0,-), 0) :- !.
fib(state(0,-), state(1,0), 1) :- !.
fib(state(P,Q), state(F,P), F) :-
    F is P+Q.

The above can be used to retrieve the Nth Fibonacci number. As fib/2 provides no access to the complete list of Fibonacci numbers, this can be used to generate large Fibonacci numbers.

fib(N, F) :-
    fibonacci_numbers(L),
    nth1(N, L, F).