Home > Archive > Prolog > January 2006 > for loop in prolog
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]
| Author |
for loop in prolog
|
|
| nobody 2006-01-10, 9:58 pm |
| hi to all,
How could i implement a for loop in prolog. Let's say for
example that i have an integer variable X and i want to repeat X
times a specific action. Any idea welcome...
| |
| Guybrush Threepwood 2006-01-10, 9:58 pm |
| On Tue, 10 Jan 2006 21:29:20 +0200, nobody wrote:
> hi to all,
>
> How could i implement a for loop in prolog. Let's say for
> example that i have an integer variable X and i want to repeat X times a
> specific action. Any idea welcome...
I'm a beginner myself, but I think it would be something like this:
do_n_times(0):- !.
do_n_times(N):-
%do actions,
NewN is N-1,
do_n_times(NewN).
--
"Don't worry about people stealing your ideas. If your ideas are any
good, you'll have to ram them down people's throats."
-- Howard Aiken
| |
| Roland Illig 2006-01-11, 7:01 pm |
| nobody wrote:
> hi to all,
>
> How could i implement a for loop in prolog. Let's say for
> example that i have an integer variable X and i want to repeat X
> times a specific action. Any idea welcome...
?- between(1, 5, X), write(X), nl, fail.
1
2
3
4
5
No
% The fail/0 prevents my prolog processor from asking for each X
% whether to continue or not.
Roland
| |
| Diogo Bastos 2006-01-16, 7:04 pm |
| % my_for(start_value,end_value,increment,a
ction)
my_for(V,V,_,_) :- !.
my_for(Start,End,Inc,Action) :-
End > Start,
NewValue is Start+Inc,
call(Action),
my_for(NewValue,End,Inc,Action).
"nobody" <nobody@do-not-spam.me> escreveu na mensagem
news:pan.2006.01.10.19.29.18.959870@do-not-spam.me...
> hi to all,
>
> How could i implement a for loop in prolog. Let's say for
> example that i have an integer variable X and i want to repeat X
> times a specific action. Any idea welcome...
| |
| Jan Wielemaker 2006-01-16, 7:04 pm |
| On 2006-01-16, Diogo Bastos <damrho@netcabo.pt> wrote:
> % my_for(start_value,end_value,increment,a
ction)
> my_for(V,V,_,_) :- !.
> my_for(Start,End,Inc,Action) :-
> End > Start,
> NewValue is Start+Inc,
> call(Action),
> my_for(NewValue,End,Inc,Action).
Nice, but if you want to go into the trouble of proving a start
and a step, you probable want to pass the current value to the action
predicate. The following however won't work:
?- my_for(0, 10, 1, writeln(X)).
1
2
....
--- Jan
> "nobody" <nobody@do-not-spam.me> escreveu na mensagem
> news:pan.2006.01.10.19.29.18.959870@do-not-spam.me...
>
>
|
|
|
|
|