Y
Y
yourisus2017-12-08 18:33:26
Prolog
yourisus, 2017-12-08 18:33:26

Ternary functors in Prolog, how to declare them?

Binary trees are defined using the ternary functor tree(Left,Root,Right), where Root is the element at the top, and Left and Right are the left and right subtrees, respectively. The empty tree is represented by the nil atom. The following term is an example of a more complex tree tree(nil, 5n, tree(nil, 6, tree(tree(nil, 8, nil), 10, nil))).
Write a predicate p(+T,+X,+Y,–R) that, from a binary tree T, makes a binary tree R that matches T, except that the nodes of the tree that also contain X in the list are changed to the corresponding ones (by order) vertices from list Y.

Example,

<b>?– p(tree(nil, 5, tree(nil, 6, tree(tree(nil, 8, nil), 10, nil))),[5,8,7],[50,80,70],R).
R= tree(nil, 50, tree(nil, 6, tree(tree(nil, 80, nil), 10, nil)))
Yes</b>

1) I would just like to know how to set the functor tree(nil, 5, tree(nil, 6, tree(tree(nil, 8, nil), 10, nil))).
I don't understand

how to define a functor.
- how to set a functor recursively,
- how to parse this functor for my task

Examples like owner("Mary", "Garfield") and so on, give me nothing.
swipl interpreter 7.6.3.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question