home *** CD-ROM | disk | FTP | other *** search
-
- Turbo PROLOG:
- Backtracking/Unification Benchmark
-
-
-
- /* Test CPU-intensive unification and backtracking by generating and reversing
- lots of lists. */
-
-
- DOMAINS
-
- LIST = INTEGER* /* LIST will stand for a list of INTEGERs */
-
-
- PREDICATES
-
- append(LIST,LIST,LIST)
- reverse(LIST,LIST)
- makelist(INTEGER,LIST)
- test2(INTEGER,INTEGER)
- test(INTEGER)
-
-
- CLAUSES
-
- /* Append first argument to second, returning result via third argument.
- All arguments must be lists. */
-
- append([],L,L) :- !.
- append([H|L1],L2,[H|L3]) :- append(L1,L2,L3).
-
-
- /* Reverse first list argument and return result in second argument. */
-
- reverse([],[]) :- !.
- reverse([H|T],RevL) :- reverse(T,RevT), append(RevT,[H],RevL).
-
-
- /* Make a list of integers. First argument specifies number of elements,
- second argument returns the list. List elements are generated in descending
- numerical order. */
-
- makelist(0,[]) :- !.
- makelist(N,[N|L]) :- M = N - 1, makelist(M,L).
-
-
- /* The heart of the benchmark: using a tail recursive loop, generate and
- reverse lists starting at length N up to length Limit. */
-
- test2(Limit,Limit) :- !.
- test2(N,Limit) :- makelist(N,L), reverse(L,_), M = N + 1, test2(M,Limit).
-
-
- /* Shorthand to call the test2 predicate with the starting list length
- defaulting to 2. NOTE!!! Turbo Prolog doesn't allow two predicates
- of the same name and different arities, which is why test and test2
- have different names. */
-
- test(Limit) :- test2(2,Limit).
-
-
- GOAL
-
- /* Prompt for the desired limit and run the benchmark. */
-
- nl, write(ready), nl, readint(Limit), test(Limit), write(done), nl.
-
-
-