Home > Archive > Prolog > April 2004 > Newbie question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I have the following fact:
fact(app, 200, 3,1985).
fact(app, 400, 5,1985).
fact(house, 300, 4 , 1977).
fact(house, 500, 9, 1995).
find :-
write('What type of home do you looking for ? : '), read(T), nl,
fat(T, M, V, Y),
write([T, M, V, Y]).
How do i make the function find/return all the fact that matches, and not
only the first ?
for an example if i answer 'house', the function will return: fact(house,
300, 4 , 1977)
| |
| Nick Wedd 2004-04-17, 8:32 am |
| In message <4080f2fc$0$251$edfadb0f@dread12.news.tele.dk>, Johs
<isl44007@tiscali.dk> writes
>I have the following fact:
>
>fact(app, 200, 3,1985).
>fact(app, 400, 5,1985).
>fact(house, 300, 4 , 1977).
>fact(house, 500, 9, 1995).
>
>find :-
> write('What type of home do you looking for ? : '), read(T), nl,
> fat(T, M, V, Y),
> write([T, M, V, Y]).
>
>How do i make the function find/return all the fact that matches, and not
>only the first ?
>for an example if i answer 'house', the function will return: fact(house,
>300, 4 , 1977)
Look in your manual for 'bagof', 'setof', or 'findall'.
Nick
--
Nick Wedd nick@maproom.co.uk
| |
| Martin Sondergaard 2004-04-17, 8:31 pm |
|
> <isl44007@tiscali.dk> writes
>
> Look in your manual for 'bagof', 'setof', or 'findall'.
>
> Nick
I don't think he needs 'bagof', 'setof', or 'findall'.
He needs to know how to use fail. (He said he's a newbie.)
Johs, you can use a "fail loop" to make it go on to the next fact.
You can use "repeat ... fail", like this :
find_repeat :-
repeat,
find,
fail.
Or like this :
find_repeat2 :-
repeat,
write('What type of home do you looking for ? : '), read(T), nl,
once( fact(T, M, V, Y) ),
write([T, M, V, Y]),
fail.
When it reaches "fail", it starts backtracking,
i.e. it starts reading the program from right to left.
It backtracks until it reaches the word "repeat",
then it starts forward tracking again.
So it moves between "repeat" and "fail" repeatedly.
Read about "repeat" and "fail" in your Prolog manual.
The word "fail" is very important in Prolog.
In the program above, "find_repeat2", I had to use "once"
to stop "fact/4" from finding another fact, and succeeding.
This would have caused it to stop backtracking and start forward tracking
before it had reached the word "repeat".
--
Martin Sondergaard.
| |
| Cesar Rabak 2004-04-17, 9:31 pm |
| Martin Sondergaard escreveu:
[snipped]
> I don't think he needs 'bagof', 'setof', or 'findall'.
> He needs to know how to use fail. (He said he's a newbie.)
>
> Johs, you can use a "fail loop" to make it go on to the next fact.
>
> You can use "repeat ... fail", like this :
>
> find_repeat :-
> repeat,
> find,
> fail.
>
> Or like this :
>
> find_repeat2 :-
> repeat,
> write('What type of home do you looking for ? : '), read(T), nl,
> once( fact(T, M, V, Y) ),
> write([T, M, V, Y]),
> fail.
>
> When it reaches "fail", it starts backtracking,
> i.e. it starts reading the program from right to left.
> It backtracks until it reaches the word "repeat",
> then it starts forward tracking again.
> So it moves between "repeat" and "fail" repeatedly.
Yes, but then the variable T gets a new binding and it loops forever. . .
--
Cesar Rabak
|
|
|
|
|