Sway uses a port system for input and output. When Sway starts up, the current input port defaults to stdin (the keyboard) and the current output port defaults to stdout (the screen). To change these ports, one first creates new port and then sets the port. For example, to read from a file (say "data") instead of the keyboard, first create a file port: var p = open("data",:read); //p points to a port var oldInput = setPort(p); Once the port is set, all input will come from the new port. To change the output port, the procedure is similar, except the symbol :write is used instead. var p = open("data",:write); //p points to a port var oldOutput = setPort(p); Opening a file in :write mode overwrites the file; to append content to an existing file, use the :append symbol instead. Sway only allows a limited number of ports to be open at any given time. If you no longer need a port, close it with the built-in function close, which takes a port as its sole argument: close(p); Sway supplies built-in functions for reading characters, integers, reals, strings, and whitespace delimited tokens: s = readChar(); i = readInt(); r = readReal(); s = readString(); t = readToken(); Both the readChar and the readToken functions return strings. Sway uses the same rules as the C programming language for what characters constitute an integer and a real . None of these functions take an argument; they use the current input port. To read a symbol, use the symbol function in conjunction with the readToken function: s = symbol(readToken()); To read a line of text, use the built-in readLine function: l = readLine(); The readLine function reads up to, and including, the next newline character, but the newline is not part of the returned string. The pause function always reads from stdin, regardless of the current input port. It reads (and discards) a line of text (up to and including the newline). Its purpose is to pause execution of a program for debugging purposes. Most output functions write to the current output port. The simplest output function is display. It takes a single argument, which can be any Sway object: display("Hello, world!\n"); The character sequence '\' followed by 'n' indicate that a newline is to be displayed. More useful than display are the functions print and println in that they take any number of arguments: print("(f(x) is ",f(x),"\n"); println("(f(x) is ",f(x)); The println function is just like print, except it outputs a newline after the displaying the last argument. Thus, the two calls above produce the same output. When a string is printed, the quote marks are not displayed. Likewise, when a symbol is printed, the colon is not displayed. The inspect function is discussed in more detail in ../ch10, but, in short, prints out the unevaluated version of its argument followed by its evaluated argument: sway> inspect(f(x)); f(x) is 3 INTEGER: 3 The inspect function always prints to stdout, regardless of the current output port. There are three built-in pretty printing functions: pp, ppFlat, and ppObject. They print reasonable representations of all Sway objects to the current output port. The difference between pp and ppFlat is that ppFlat coalesces all contiguous white space (including tabs and newlines) into a single space, thus printing the representation on a single line. The ppObject function is used for printing Sway functions in object form. Given the function: function f(x) { var y = x + 1; y * y; } the pp function outputs: sway> pp(f); function f(x) { var y = x + 1; y * y; } FUNCTION:  while ppObject outputs: sway> ppObject(f); : context:  prior: :null filter: :null parameters: (x) code: { var y = x + 1; y * y } name: f FUNCTION:  The fmt function can be used to format numbers and strings if the default formatting is not acceptable. It uses the C programming language formatting scheme, taking a formatting specification as a string, and the item to be formatted. The function returns a string. For example, sway>"<" + fmt("%6d",3) + ">"; STRING: "< 3>" sway>"<" + fmt("%-6d",3) + ">"; STRING: "<3 >" A format specification begins with '%' and usually followed by a number representing the width (in number of characters) of the resulting string. If the width is positive, the item is right justified in the resulting string. If the width is negative, the item is left justified. After any width specification is a character specifying the type of the item to be formatted: 'd' for integer, 'f' for string, and 's' for string. The format specification is quite a bit more sophisticated than shown here. You can read more on a Linux system by typing the command 'man 3 printf' at the system prompt. The eof? function can be used to test whether the last read was successful or not. The function is NOT used to test if the next read is successful. Here is a typical use of eof? in tokenizing a file: t = readToken(); while (eof? () == :false) { store(t); t = readToken(); } Sometimes, it is necessary to read one character too many from the input. This happens in cases like advancing past whitespace in the input. Here is a typical whitespace-clearing loop: ch = readChar(); while (space? (ch)) { ch = readChar(); } //last character read wasn't whitespace //so push it back to be read again later pushBack(ch); The pushBack function takes a string as its sole argument, but only pushes back the first character of the string; subsequent characters in the string are ignored. ← Recursion · Arrays and Lists → ← Recursion · The Sway Reference Manual · Arrays and Lists →
{'wiki_source': 'wikibooks', 'other_info': "{'content_model': 'wikitext', 'language': 'en', 'title': 'The Sway Reference Manual/Input and Output', 'type': 'text'}"}