PLOGHELP STRINGS Jonathan Laventhol July 1983 revised by Kathryn Seifert August 1986 revised by Simon Nichols May 1987 Making Prolog strings; library package to print Prolog strings Keywords: strings, datastructures, printing CONTENTS - (Use g to access required sections) -- INTRODUCTION -- CONSTRUCTING PROLOG STRINGS -- PRINTING STRINGS -- WHAT'S IN THE LIBRARY PACKAGE -- ASCII CODES -- CONTROL CHARACTERS -- POP-11 STRINGS -- RELATED DOCUMENTATION -- INTRODUCTION ------------------------------------------------------- Strings are just a special notation for lists of integers which represent characters. POPLOG uses the ASCII representation for characters: each character is represented by an integer between 0 and 127 (See HELP * ASCII). A table of all the characters and their associated ASCII codes is given at the end of this file. Although strings are easy to make in Prolog, there are no built-in facilities for printing them out. However, POPLOG Prolog provides a library package to do this, LIBRARY STRINGS, which is discussed below. -- CONSTRUCTING PROLOG STRINGS ---------------------------------------- Making a string: ?- X = "hello". X = [104, 101, 108, 108, 111] ? If you want to get the double quote character into a string you have to precede it with a backslash: ?- X = "\"". X = [34] ? It is possible to put control characters in strings by the use of escape sequences introduced by a backslash (see below). -- PRINTING STRINGS --------------------------------------------------- LIBRARY STRINGS provides facilities to print strings in Prolog. To make use of these facilities, type: ?- library(strings). Once the library file is loaded, the predicates 'writes/1', 'string/1', 'charcode/1' and 'putchar/1' will be available to you. These predicates are explained in more detail in the next section. Examples using these predicates are given below: :- writes("hello there "). hello there ?- string("hello"). yes ?- charcode(126). yes ?- charcode(568). no :- putchar("f"). f You could make the built-in predicate 'print' (see PLOGHELP * PRINT, PLOGHELP * I_O) always print strings like this, by adding the following clause to the built-in definition of 'portray' (see PLOGHELP * PORTRAY, PLOGHELP * I_O) portray(S) :- writes(S). -- WHAT'S IN THE LIBRARY PACKAGE -------------------------------------- You will have these predicates defined when LIBRARY STRINGS is loaded: ?- string(S). This goal will succeed only if S is instantiated to a list of character codes (numbers from 0 to 127) -- that is, if S is a string. ?- writes(X). This behaves like the built-in predicate 'write' (see PLOGHELP * WRITE, PLOGHELP * I_O), but if X is a string, then the characters will be printed. Any other object will be written normally. ?- charcode(X). If X is a legitimate character code (an integer between 0 and 127), then the goal succeeds. ?- putchar(X). If X is a character code inside a list, then that character will be printed using the built-in predicate 'put' (see PLOGHELP * PUT, PLOGHELP * I_O). Apart from that, it is exactly the same as 'put'. -- ASCII CODES -------------------------------------------------------- Note: CTRL-A means the character you get when you press A when holding down the control key. On most terminals, some of these have their own keys: CTRL-H backspace CTRL-I tab CTRL-J linefeed CTRL-M carriage return CTRL-[ escape 0 CTRL-@ 16 CTRL-P 32 space 48 0 64 @ 80 P 96 ` 112 p 1 CTRL-A 17 CTRL-Q 33 ! 49 1 65 A 81 Q 97 a 113 q 2 CTRL-B 18 CTRL-R 34 " 50 2 66 B 82 R 98 b 114 r 3 CTRL-C 19 CTRL-S 35 # 51 3 67 C 83 S 99 c 115 s 4 CTRL-D 20 CTRL-T 36 $ 52 4 68 D 84 T 100 d 116 t 5 CTRL-E 21 CTRL-U 37 % 53 5 69 E 85 U 101 e 117 u 6 CTRL-F 22 CTRL-V 38 & 54 6 70 F 86 V 102 f 118 v 7 CTRL-G 23 CTRL-W 39 ' 55 7 71 G 87 W 103 g 119 w 8 CTRL-H 24 CTRL-X 40 ( 56 8 72 H 88 X 104 h 120 x 9 CTRL-I 25 CTRL-Y 41 ) 57 9 73 I 89 Y 105 i 121 y 10 CTRL-J 26 CTRL-Z 42 * 58 : 74 J 90 Z 106 j 122 z 11 CTRL-K 27 CTRL-[ 43 + 59 ; 75 K 91 [ 107 k 123 { 12 CTRL-L 28 CTRL-\ 44 , 60 < 76 L 92 \ 108 l 124 | 13 CTRL-M 29 CTRL-] 45 - 61 = 77 M 93 ] 109 m 125 } 14 CTRL-N 30 CTRL-^ 46 . 62 > 78 N 94 ^ 110 n 126 ~ 15 CTRL-O 31 CTRL-_ 47 / 63 ? 79 O 95 _ 111 o 127 -- CONTROL CHARACTERS ------------------------------------------------- In strings, the backslash character "\" is special: it introduces an escape sequence which is the way control (or non-printing) characters are put in strings (and also in atoms - see PLOGHELP * SYNTAX). Certain characters, when preceded in strings by a backslash, change their meanings, thus: \t a tab \s a space \b a backspace \n a newline \r a carriage return \" a double quote mark \\ a backslash A backslash followed by a caret (^) and an uppercase letter (or certain synbolic characters - see the ASCII table above) is used to specify a control character: \^A a CTRL-A \^B a CTRL-B Note that \^? gives you the character in the chart above. -- POP-11 STRINGS ----------------------------------------------------- For those who know POP-11 and are writing mixed language programs, it is sometimes useful to put POP-11 strings inside Prolog clauses. These people should see PLOGHELP * POPSTRING. -- RELATED DOCUMENTATION ---------------------------------------------- HELP * ASCII ASCII representation of characters in POPLOG HELP * STRINGS Strings in POP-11 PLOGHELP * LIBRARIES Overview of Prolog library programs PLOGHELP * I_O Overview of input/output operations in Prolog PLOGHELP * NAME Predicate which converts between atomic terms and strings PLOGHELP * POPSTRING Library package to put POP-11 strings in Prolog programs PLOGHELP * PORTRAY Predicate which changes the action of the 'print' predicate. PLOGHELP * PRINT Predicate which prints argument to current output. PLOGHELP * PUT Predicate which takes the ASCII code for a character and writes that character to the current output. PLOGHELP * SYNTAX A description of the syntax of POPLOG Prolog. PLOGHELP * WRITE Predicate which writes a term to current output (not in prefix format) --- C.all/plog/help/strings --- Copyright University of Sussex 1991. All rights reserved. ----------