0

Is there in prolog something like 'two or more' (or another option) to make the following shorter ?

rule(foo,  [bar,bar]). 
rule(foo,  [bar,bar,bar]).
rule(foo,  [bar,bar,bar,bar]).
rule(foo,  [bar,bar,bar,bar,bar]).

you can read it as: if there is a serie of n bars then assign the tag foo

Brutalized
  • 85
  • 2
  • 9

2 Answers2

2

between/3 is probably what you're after:

rule(foo, L) :-
    between(2, 5, N),
    length(L, N),
    maplist(=(bar), L).
lurker
  • 56,987
  • 9
  • 69
  • 103
  • Yes! Thankyou, this is what i was looking for. – Brutalized Feb 20 '16 at 13:12
  • 2
    @Brutalized you are not supposed to change your question drastically. It invalidates the answers and comments given. Please change your question back and open a new question. But, if you give it some thought, you should be able to apply the technique I showed to solve that problem, too. – lurker Feb 20 '16 at 18:48
0

This is a possibility

rule(foo, Out) :-
    random(2,6,N),
    length(Out,N),
    maplist(=(bar),Out).
joel76
  • 5,565
  • 1
  • 18
  • 22