for loop in prolog - PROLOG

This is a discussion on for loop in prolog - PROLOG ; 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......

+ Reply to Thread
Results 1 to 5 of 5

for loop in prolog

  1. Default for loop in prolog

    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...

  2. Default Re: for loop in prolog

    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


  3. Default Re: for loop in prolog

    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

  4. Default Re: for loop in prolog

    % my_for(start_value,end_value,increment,action)
    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
    newsan.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...




  5. Default Re: for loop in prolog

    On 2006-01-16, Diogo Bastos <damrho@netcabo.pt> wrote:
    > % my_for(start_value,end_value,increment,action)
    > 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
    > newsan.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...

    >
    >


+ Reply to Thread

Similar Threads

  1. Method to stop the For loop in the middle of exection of For loop
    By Application Development in forum labview
    Replies: 0
    Last Post: 12-17-2007, 03:40 AM
  2. Interpreter loop - best approach in Prolog?
    By Application Development in forum PROLOG
    Replies: 4
    Last Post: 10-09-2007, 09:55 PM
  3. Replies: 0
    Last Post: 09-20-2007, 06:40 PM
  4. swi-prolog or visual prolog or CLIPS
    By Application Development in forum PROLOG
    Replies: 3
    Last Post: 02-22-2006, 07:57 PM
  5. turbo prolog or visual prolog
    By Application Development in forum PROLOG
    Replies: 20
    Last Post: 08-28-2005, 03:17 AM