John Jaques
John Jaques

Reputation: 560

Prolog Brackets

I am working with trees in Prolog; A tree node is represented using a term that takes the form Left-NodeValue-Right or nil. In order to enforce precedence, round brackets are used. Note that round brackets are crucial, because the trees (nil-5+nil)-3+nil and nil-5+(nil-3+nil) are obviously different. My question is: How do I deal with the brackets?

One solution I tried was to use concat_atom(+List,-Atom). But then the expression [(,nil,)] would obviously give an error and ['(',nil,')'] would yield the atom '(nil)', not (nil).

Any suggestions? Thanks!

Upvotes: 0

Views: 407

Answers (1)

mat
mat

Reputation: 40768

In the first place, why did you choose such an unusual representation? It seems much more appropriate to represent an inner node with a ternary term like node(Left, Value, Right). You instead represent it as -(-(Left, Value), Right), which seems a bit unsuitable. As to your question: To enforce precedence, simply use parentheses yourself when you write down the term. The Prolog toplevel will print the term in the "right" way automatically in answers, I see no need to use concat_atom/2, or if there is one, please explain why you cannot enter the term directly.

Upvotes: 2

Related Questions