Changeset 4090 for trunk/library

Show
Ignore:
Timestamp:
02/15/08 07:27:42 (11 months ago)
Author:
pmoura
Message:

Added a foreach/3 meta-predicate to the library object "loop".

Location:
trunk/library
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/library/loop.lgt

    r4089 r4090  
    2222        whiledo(Condition, Action). 
    2323 
     24    :- meta_predicate(foreach(*, *, ::)). 
     25    foreach(Count, List, Goal) :- 
     26        foreach_inv(List, Count, Goal). 
     27 
     28    foreach_inv([], _, _). 
     29    foreach_inv([Element| List], Count, Goal) :- 
     30        \+ \+ (Count = Element, call(Goal)), 
     31        foreach_inv(List, Count, Goal). 
     32 
    2433    :- meta_predicate(forto(*, *, ::)). 
    25     forto(FirstExp, LastExp, Call) :- 
     34    forto(FirstExp, LastExp, Goal) :- 
    2635        First is FirstExp, 
    2736        Last is LastExp, 
    2837        (   First =< Last -> 
    29             \+ \+ call(Call), 
     38            \+ \+ call(Goal), 
    3039            Next is First + 1, 
    31             forto(Next, Last, Call) 
     40            forto(Next, Last, Goal) 
    3241        ;   true 
    3342        ). 
    3443 
    3544    :- meta_predicate(forto(*, *, *, ::)). 
    36     forto(Count, FirstExp, LastExp, Call) :- 
     45    forto(Count, FirstExp, LastExp, Goal) :- 
    3746        First is FirstExp, 
    3847        Last is LastExp, 
    3948        (   First =< Last -> 
    40             \+ \+ (Count = First, call(Call)), 
     49            \+ \+ (Count = First, call(Goal)), 
    4150            Next is First + 1, 
    42             forto(Count, Next, Last, Call) 
     51            forto(Count, Next, Last, Goal) 
    4352        ;   true 
    4453        ). 
    4554 
    4655    :- meta_predicate(fordownto(*, *, ::)). 
    47     fordownto(FirstExp, LastExp, Call) :- 
     56    fordownto(FirstExp, LastExp, Goal) :- 
    4857        First is FirstExp, 
    4958        Last is LastExp, 
    5059        (   First >= Last -> 
    51             \+ \+ call(Call), 
     60            \+ \+ call(Goal), 
    5261            Next is First - 1, 
    53             fordownto(Next, Last, Call) 
     62            fordownto(Next, Last, Goal) 
    5463        ;   true 
    5564        ). 
    5665 
    5766    :- meta_predicate(fordownto(*, *, *, ::)). 
    58     fordownto(Count, FirstExp, LastExp, Call) :- 
     67    fordownto(Count, FirstExp, LastExp, Goal) :- 
    5968        First is FirstExp, 
    6069        Last is LastExp, 
    6170        (   First >= Last -> 
    62             \+ \+ (Count = First, call(Call)), 
     71            \+ \+ (Count = First, call(Goal)), 
    6372            Next is First - 1, 
    64             fordownto(Count, Next, Last, Call) 
     73            fordownto(Count, Next, Last, Goal) 
    6574        ;   true 
    6675        ). 
  • trunk/library/loopp.lgt

    r4089 r4090  
    1515        argnames is ['Action', 'Condition']]). 
    1616 
     17    :- public(foreach/3). 
     18    :- meta_predicate(foreach(*, *, ::)). 
     19    :- mode(foreach(-term, +list(term), @callable), zero_or_one). 
     20    :- info(foreach/3, [ 
     21        comment is 'For each element Count in List call Goal.', 
     22        argnames is ['Count', 'List', 'Goal']]). 
     23 
    1724    :- public(forto/3). 
    1825    :- meta_predicate(forto(*, *, ::)). 
    1926    :- mode(forto(+integer, +integer, @callable), zero_or_one). 
    2027    :- info(forto/3, [ 
    21         comment is 'Counting from First to Last do Call. For convenience and clarity, First and Last can be arithmetic expressions.', 
    22         argnames is ['First', 'Last', 'Call']]). 
     28        comment is 'Counting from First to Last call Goal. For convenience and clarity, First and Last can be arithmetic expressions.', 
     29        argnames is ['First', 'Last', 'Goal']]). 
    2330 
    2431    :- public(forto/4). 
     
    2633    :- mode(forto(-integer, +integer, +integer, @callable), zero_or_one). 
    2734    :- info(forto/4, [ 
    28         comment is 'Do Call counting from First to Last and instantiating Count to each successive value. For convenience and clarity, First and Last can be arithmetic expressions.', 
    29         argnames is ['Count', 'First', 'Last', 'Call']]). 
     35        comment is 'Call Goal counting from First to Last and instantiating Count to each successive value. For convenience and clarity, First and Last can be arithmetic expressions.', 
     36        argnames is ['Count', 'First', 'Last', 'Goal']]). 
    3037 
    3138    :- public(fordownto/3). 
     
    3340    :- mode(fordownto(+integer, +integer, @callable), zero_or_one). 
    3441    :- info(fordownto/3, [ 
    35         comment is 'Counting from First to Last do Call. For convenience and clarity, First and Last can be arithmetic expressions.', 
    36         argnames is ['First', 'Last', 'Call']]). 
     42        comment is 'Counting from First to Last call Goal. For convenience and clarity, First and Last can be arithmetic expressions.', 
     43        argnames is ['First', 'Last', 'Goal']]). 
    3744 
    3845    :- public(fordownto/4). 
     
    4047    :- mode(fordownto(-integer, +integer, +integer, @callable), zero_or_one). 
    4148    :- info(fordownto/4, [ 
    42         comment is 'Do Call counting from First to Last and instantiating Count to each successive value. For convenience and clarity, First and Last can be arithmetic expressions.', 
    43         argnames is ['Count', 'First', 'Last', 'Call']]). 
     49        comment is 'Call Goal counting from First to Last and instantiating Count to each successive value. For convenience and clarity, First and Last can be arithmetic expressions.', 
     50        argnames is ['Count', 'First', 'Last', 'Goal']]). 
    4451 
    4552    :- public(whiledo/2).