Re: fund with findall
"Richard A. O'Keefe" <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
On 1/04/2014, at 10:03 AM, Ross Boylan wrote:
> Given
> foo(1, 2).
> foo(3, 2).
> foo(1, 10).
>
> funky(Src, Dest, Matches) :-
> findall(x(Src, Dest), foo(Src, Dest), Matches).
>
> Then funky(1, B, M) produces
>
> M = [x(1, 2), x(1, 10)].
>
> I find this slightly disturbing because it means that Dest got bound to
> 2 separate values, while remaining unbound at the end.
Well, yes. By putting Dest in the template, you *asked*
for it to be existentially quantified.
Roughly,
findall(Template, Generator, List) :-
push a marker on a stack,
( call(Generator),
push a copy of Template on the stack
fail ; true
),
pop results off the stack until the marker is reached,
putting them in List.