Pretty printing parse trees
I now want to introduce some built-in predicates for writing output to the screen.
write/1takes a Prolog term as argument and prints this term onto the screen. For example:?- write(cook(snape,potion)). cook(snape, potion) Yestab/1takes a number as argument and prints as many spaces onto the screen. For example:?- write(a),write(b). ab Yes ?- write(a),tab(5),write(b). a b Yesnlprints a newline onto the screen. For example:?- write(a),nl,write(b). a b Yes
Now, use these predicates to define a predicate
pptree/1 that takes a complex term representing a
tree, such as
s(np(det(the),n(bride)),vp(v(kills),np(det(a),n(man)))),
as its argument and prints a nicer and more readable output for
this tree to the screen. For example, something like this
s(
np(
det(the)
n(bride))
vp(
v(kills)
np(
det(a)
n(man))))
Hint: You will need the built in predicates functor
and arg for this. (See this exercise of day 1.) The
built in predicate =.. might also be
useful. =.. turns a complex term with n
arguments into a list of n+1 elements. The first element of
the list is the functor of the complex term and the other n
elements are the arguments of the complex term.
?- f(a,b,c(d)) =.. L. L = [f, a, b, c(d)] ; no