Copyright (1997-2013) EDMGROUP Pty Ltd - EZY Prolog Reference

back to main page

Findall

 

findall(<VAR_DOMAIN> ArgumentName, <PredicateCall>, <VAR_DOMAIN_LIST> ValuesList)

Assemble a list of all solutions returned by a non-deterministic predicate

Arguments:

ArgumentName specifies which argument in the specified predicate PredicateCall is to be collected into a list ValuesList. The irregular syntax <VAR_DOMAIN> is used to indicate a domain to which ArgumentName belongs.

PredicateCall indicates the predicate from which values will be collected.

ValuesList is an output variable that holds the list of values collected through backtracking.

Remarks

The findall predicate builds the list ListVariable by backtracking into <PredicateCall> until it eventually fails. ListVariable consists of all instances of the variable ArgumentName, which must be an output argument of <PredicateCall>.

In order to findall to work, there must be a declaration of the list domain for ValuesList in the user domain declarations. That is, if ArgumentName belongs to a certain domain VAR_DOMAIN, then the program have to contain user domain declaration like the following:

DOMAINS

VAR_DOMAIN_LIST = VAR_DOMAIN*

This list domain VAR_DOMAIN_LIST should be used to handle ValuesList variable in the program.  

Fail

Never fails.

Errors

No Errors

Sample: see findall_test.pro

 

/* COMPLEX TEST FOR FINDALL.

NOTE - LISTS DECLARATIONS ARE NOT COMPATIBLE WITH PDC PROLOG SYNTAX

*/

 

prolog_main():-

write("Welcome to EZY Prolog\n"),

list_subsystem_test.

/*

THIS DECLARATION IS NECESSARY FOR CORRECT WORK OF FINDALL

nondeterm_get_list predicate calls non-deterministic predicate list_for_test

unfortunately findall(X,list_for_test(_,X),RESULT) will not work with EZY Prolog.

Instead, additional predicate nondeterm_get_list should be declared and

called inside findall

*/

nondeterm_get_list(LIST):-

list_for_test(_,LIST).

 

list_for_test("List of Integers",[1,2,3,4,5]).

list_for_test("List of strings",["one","two","three","four","five"]).

list_for_test("List of elements",[element(1,"One"),element(2,"Two"),element(3,"Three"),element(4,"Four")]).

list_for_test("Hybrid list",[1,2,"3",element(4,"Four")]).

list_for_test("list with list as MEMBER",[1,2,3,element(4,"Four"),[1,2,3,4,5]]).

 

list_subsystem_test():-

write("Test for findall:\n"),

findall(X,nondeterm_get_list(X),VARIOUS_LISTS),

/* NOTE - VARIOUS LIST IS 2D LIST */

member(VARIOUS_LISTS,MEMBER),

list_for_test(TYPE,MEMBER),

write("",TYPE,"- ",MEMBER,"\n"),

fail.

list_subsystem_test():-

write("List subsystem test completed\n").

 

Results:

EZY PROLOG. Copyright (C) 1997-2013 EDMGROUP (Australia)

http://www.ezy-software.com

Welcome to EZY Prolog

Test for findall:

List of Integers- [1,2,3,4,5]

List of strings- [one,two,three,four,five]

List of elements- [element(1,"One"),element(2,"Two"),element(3,"Three"),element(4,"Four")]

Hybrid list- [1,2,3,element(4,"Four")]

list with list as MEMBER- [1,2,3,element(4,"Four"),[1,2,3,4,5]]

List subsystem test completed

 

Copyright 1997-2013 EDMGROUP (Australia)