/* readfile.plg: display a named file to the screen */ /* DH 28/1/89 */ /*---------------------------------------- import using [readfile]. This demonstrates the use of RECURSION, the natural way in PROLOG of doing anything in a repeated way, and also the use of the CUT. To do this in the system in the normal way, use the built-in predicate copyfile(From,To) specifying the terminal file user as To. ------------------------------------------*/ rf1c(26,_). /* fails at end of file (26 is control-Z) */ /* otherwise... */ rf1c(X,Y) :- put(X), /* output the last character read */ get0(Y), /* get the next one */ !, /* CUT - see below */ rf1c(Y,Z). /* recurse, using the just-read character */ readfile(X) :- seeing(Isav), /* save current input stream */ see(X), /* set input to X */ get0(Y), /* get first character */ rf1c(Y,Z), /* test the recursive goal */ seen, /* close the input stream */ see(Isav), /* restore saved input */ !, /* CUT and fail....... */ fail. /* so as not to do it again! */ /*------------------------------------------------ a note on the CUT ================= The cut in PROLOG (represented by the predicate ! ) marks a cut-off point in the system's back-tracking to resatisfy previously satisfied goals. If subsequent goals fail, the system will backtrack to the cut, then NOT attempt to satisfy any more goals at the current level (this makes the CUT-FAIL combination in the main readfile goal a common sight in PROLOG programs, and means that the rule rf1c will not attempt to read past the EOF character. --------------------------------------------------*/ /* EOF readfile.plg */not attempt to read pas