0

My problem is, that I don't know how i can dynamically concatenate 2 constants in Prolog. For example I have the following procedures:

word1(lys).
word2(ser).
word3(rta).
...
wordN(...).

How can I write a function or create a varibale which dynamically take values like [lys-ser] or [rta-ser-lys] depending on other variables?

Siminho
  • 109
  • 1
  • 2
  • 6
  • 1
    Unification. `words_term(A, B, A-B).` – lurker Nov 28 '19 at 13:59
  • Thank you very much :) Unfortunately now i have another problem with this. If A = lys and B = arg-blu and I call `words_term(lys, arg-blu, C)` then C = lys-(arg-glu), but I dont want the round brackets around...I want C = lys-arg-glu :/ – Siminho Nov 28 '19 at 17:57
  • `lys-arg-glu` is actually the grouping `(lys-arg)-glu` since Prilog interprets the term left to right and `-` is treated as a binary functor. If you need a specific grouping, then you'll need to do a bit more work to change it. If you imagine the term being represented as a binary tree, then you are reorganizing the tree. – lurker Nov 28 '19 at 18:48

1 Answers1

0

A more complex solution could be this:

word(lys).
word(ser).
word(rta).

subset([], []).
subset([E|Tail], [E|NTail]):-
  subset(Tail, NTail).
subset([_|Tail], NTail):-
  subset(Tail, NTail).

get_terms([A],ST,S):-
    string_concat(ST,A,S).
get_terms([A|T],S,SO):-
    string_concat(A,"-",AT),
    string_concat(S,AT,ST),
    get_terms(T,ST,SO).

test:-
    findall(W,word(W),LW),
    subset(LW,LS),
    get_terms(LS,"",LT),
    writeln(LT).

?- test.
lys-ser-rta
lys-ser
lys-rta
lys
ser-rta
ser
rta

The predicate subset/2 is taken from this answer

damianodamiano
  • 2,528
  • 2
  • 13
  • 21