tree - How I can extract a subterm from a term in Prolog? -


how can extract subterm term in prolog?

an example, trying extract predicate called r wherever know how. help?

term_extraxtions(s(n(1),t(3,4,r(2,4))),modifiedterm,extractedterm) follows.

modifiedterm = s(n(1),t(3,4)).

extractedterm = r(2,4).

easy manipulation of terms derive uniform, recursive definition. so, let's first 'extract' argument compound:

exterm(t, m, e) :-     t =.. [f|as],     select(e, as, bs),     m =.. [f|bs]. 

this predicate of work:

?- exterm(a(1,b(u)),m,e). m = a(b(u)), e = 1 ; m = a(1), e = b(u) ; m = a(1, b), e = u ; false. 

to generalize , complete assignment, must allow recurse on each argument, modify extracting element, , recompose argument list.

... append(x, [y|ys], as), ... % recursive call append(x, [z|ys], bs), ... 

see if can complete it:

?- exterm(s(n(1),t(3,4,r(2,4))),modifiedterm,extractedterm), extractedterm=r(_,_). modifiedterm = s(n(1), t(3, 4)), extractedterm = r(2, 4) ; false. 

here i've constrained extracted term match show in question, otherwise there long list of solutions display...


Comments