| OCR Text |
Show 122 ability of using a! ready existing (large) functional and logic programs. A large programming task may thus be partitioned into segments that involve coding the functional and logic components separately, and subsequently integrated. We believe that the utility of such features are man1fest primarily in large pieces of software. However, the following small scenario may help Illustrate some of this flavor. Suppose that given a list of integers, we want to build up an ordered binary tree and then perform some other operations on it. Assume further that we prefer to define the function insert as follows: insert(A, empty) = tree(empty, A, empty) insert(A, tree(L, B, R)) = if A = B then tree(L, B, R) else if A< B then tree(insert(A, L), B, R) else tree(L, B, insert(A, R)) and then write a Funlog clause for building up the tree using the function in-sert as follows: build_up(empty, Tr, Tr). build_up(AAL, Tr, NewTr) :- build_up(L, insert(A, Tr), NewTr)., where NewTr serves simply to hold the resulting tree for further operations. Suppose next that we want to perform the following operation on the tree. The nodes of the tree are to be visited by inorder traversal and scaled as follows: 1 is to be subtracted from the first node visited, 2 subtracted from the second node visited, 3 from the third, and so on. This has been argued by Berztiss [6] to be a difficult problem from the point of view of functional programming. There is, therefore, good reason to program this in Prolog. One such Prolog solution is given by Wise in [11 9]. He claims that the reason for the difficulties with functional programming languages in solving this problem is due to their inability to save intermediate results for sister computations. Equipped with this solution for scale, we can then write the program solve which builds an ordered tree, and then scales it, as follows: solve(L, Tr) :- build_up(L, empty, Tr1), scale(Tr1, Tr)., where L is the given list, and scale performs the desired scaling. |