TXR(1)

TXR - Programming Language (Version 294) TXR is a general-purpose, multi-paradigm programming language. It comprises two languages integrated into a single tool: a text scanning and extraction language referred to as the TXR Pattern Language (sometimes just "TXR"), and a general-purpose dialect of Lisp called TXR Lisp. TXR can be used for everything from "one liner" data transformation tasks at the command line, to data scanning and extracting scripts, to full application development in a wide range of areas. A script written in the TXR Pattern Language, also referred to in this document as a query, specifies a pattern which matches one or more sources of inputs, such as text files. Patterns can consist of large chunks of multiline free-form text, which is matched literally against material in the input sources. Free variables occurring in the pattern (denoted by the symbol) are bound to the pieces of text occurring in the corresponding positions. Patterns can be arbitrarily complex, and can be broken down into named pattern functions, which may be mutually recursive. In addition to embedded variables which implicitly match text, the TXR pattern language supports a number of directives, for matching text using regular expressions, for continuing a match in another file, for searching through a file for the place where an entire subquery matches, for collecting lists, and for combining subqueries using logical conjunction, disjunction and negation, and numerous others. Patterns can contain actions which transform data and generate output. These actions can be embedded anywhere within the pattern-matching logic. A common structure for small TXR scripts is to perform a complete matching session at the top of the script, and then deal with processing and reporting at the bottom. The TXR Lisp language can be used from within TXR scripts as an embedded language, or completely standalone. It supports functional, imperative and object-oriented programming, and provides numerous data types such as symbols, strings, vectors, hash tables with weak reference support, lazy lists, and arbitrary-precision ("bignum") integers. It has an expressive foreign function interface (FFI) for calling into libraries and other software components that support C-language-style calls. TXR Lisp source files as well as individual functions can be optionally compiled for execution on a virtual machine that is built into TXR. Compiled files execute and load faster, and resist reverse-engineering. Standalone application delivery is possible. TXR is free software offered under the two-clause BSD license which places almost no restrictions on redistribution, and allows every conceivable use, of the whole software or any constituent part, royalty-free, free of charge, and free of any restrictions. If TXR is given no arguments, it will enter into an interactive mode. See the INTERACTIVE LISTENER section for a description of this mode. When TXR enters interactive mode this way, it prints a one-line banner announcing the program name and version, and one line of help text instructing the user how to exit. If TXR is invoked under the name it behaves as if the option had been specified before any other option. Similarly, if TXR is invoked under the name it behaves as if the option had been given. Unless the or options are present, the first non-option argument is treated as a which is executed. This is described after the following descriptions of all of the options. Any additional arguments have no fixed meaning; they are available to the TXR query or TXR Lisp application for specifying input files to be processed, or other meanings under the control of the application. Options which don't take an argument may be combined together. The and options are mutually exclusive. Of these two, the one which occurs in the rightmost position in the argument list dominates. The and options are also mutually exclusive; if both are specified, it is a fatal error. Bind the variable to the value prior to processing the query. The name is in scope over the entire query, so that all occurrences of the variable are substituted and match the equivalent text. If the value contains commas, these are interpreted as separators, which give rise to a list value. For instance binds to the list of the strings and (See the directive.) List variables provide a multiple match. That is to say, if a list variable occurs in a query, a successful match occurs if any of its values matches the text. If more than one value matches the text, the first one is taken. Binds the variable to an empty string value prior to processing the query. Quiet operation during matching. Certain error messages are not reported on the standard error device (but if the situations occur, they still fail the query). This option does not suppress error generation during the parsing of the query, only during its execution. If this option is present, then TXR will enter into an interactive interpretation mode after processing all options, and the input query if one is present. See the INTERACTIVE LISTENER section for a description of this mode. Invoke the interactive TXR debugger. See the DEBUGGER section. Implies Turns on the establishment of backtrace frames for function calls so that a backtrace can be produced when an unhandled exception occurs, and in other situations. Backtraces are helpful in identifying the causes of errors, but require extra stack space and slow down execution. This option affects behavior related to TXR's stream. It also has a another, unrelated effect, on the behavior of the interactive listener; see below. Normally, if this stream is connected to a terminal device, it is automatically marked as having the real-time property when TXR starts up (see the functions and The option suppresses this behavior; the stream remains ordinary. The TXR pattern language reads standard input via a lazy list, created by applying the function to the stream. If that stream is marked real-time, then the lazy list which is returned by that function has behaviors that are better suited for scanning interactive input. A more detailed explanation is given under the description of this function. If the option is effect and TXR enters into the interactive listener, the listener operates in plain mode instead of the visual mode. The listener reads buffered lines from the operating system without any character-based editing features or history navigation. In plain mode, no prompts appear and no terminal control escape sequences are generated. The only output is the results of evaluation, related diagnostic messages, and any output generated by the evaluated expressions themselves. Verbose operation. Detailed logging is enabled. This option binds a Lisp global lexical variable (as if by the function) to an object described by Lisp syntax. It requires an argument of the form where must be, syntactically, a token denoting a bindable symbol, and is arbitrary TXR Lisp syntax. The syntax is converted to the symbol it denotes, which is bound as a global lexical variable, if it is not already a variable. The syntax is parsed to the Lisp object it denotes. This object is not subject to evaluation; the object itself is stored into the variable binding denoted by Note that if already exists as a global variable, then it is simply overwritten. If is marked special, then it stays special. If the query is successful, print the variable bindings as a sequence of assignments in shell syntax that can be eval-ed by a POSIX shell. II the query fails, print the word "false". Evaluation of this word by the shell has the effect of producing an unsuccessful termination status from the shell's eval command. This option implies Print the variable bindings in Lisp syntax instead of shell syntax. This option implies The decimal integer argument specifies the maximum number of array dimensions to use for list-valued variable bindings. The default is 1. Additional dimensions are expressed using numeric suffixes in the generated variable names. For instance, consider the three-dimensional list arising out of a triply nested collect: ((("a" "b") ("c" "d")) (("e" "f") ("g" "h"))). Suppose this is bound to a variable With this will be reported as:
  V_0_0[0]="a"
  V_0_1[0]="b"
  V_1_0[0]="c"
  V_1_1[0]="d"
  V_0_0[1]="e"
  V_0_1[1]="f"
  V_1_0[1]="g"
  V_1_1[1]="h" With it comes out as:
  V_0[0][0]="a"
  V_1[0][0]="b"
  V_0[0][1]="c"
  V_1[0][1]="d"
  V_0[1][0]="e"
  V_1[1][0]="f"
  V_0[1][1]="g"
  V_1[1][1]="h" The leftmost bracketed index is the most major index. That is to say, the dimension order is: Specifies the query in the form of a command-line argument. If this option is used, the argument is omitted. The first non-option argument, if there is one, now specifies the first input source rather than a query. Unlike queries read from a file, (nonempty) queries specified as arguments using -c do not have to properly end in a newline. Internally, TXR adds the missing newline before parsing the query. Thus is a valid query which matches a line. Example: Shell script which uses TXR to read two lines and from standard input, binding them to variables and Standard input is specified as and the data comes from shell "here document" redirection:

    code:
        #!/bin/sh
         txr -B -c "@a
         @b" - <<!
         1
         2
         !

    output:
        a=1
         b=2

    The comment syntax can be used for better formatting:
      txr -B -c "@;
      @a
      @b"
Provides a way to specify the file from which the query is to be read, as an alternative to using the main argument. This is useful in ("hash bang") scripts. (See Hash-Bang Support below.) Use of this option does not affect the order of processing. All of the options are processed first, before the is read, as if it were specified by the main argument. If the argument to is (dash) then the script will be read from standard input instead of a file. If this option is used, the first non-option argument, if there is one, no longer specifies the It is an argument to the script, such as the name of an input source. Evaluates zero or more TXR Lisp expressions for their side effects, without implicitly printing their values. Can be specified more than once. The argument may be empty, in which case the argument has no effect, since it calls for an empty sequence of forms to be evaluated. The argument becomes optional if at least one or option is processed. If the evaluation of every evaluated this way terminates normally, and there is no argument, then TXR terminates with a successful status, instead of entering the interactive listener. The option can be used to request the listener. The argument must specify exactly one valid TXR Lisp form. If this is successfully parsed and evaluated, the value of the expression is printed as if using the function. Like but prints using the function. Like but prints using the function. Requests TXR to behave in a manner that is compatible with the specified version of TXR. This makes a difference in situations when a release of TXR breaks backward compatibility. If some version N+1 deliberately introduces a change which is backward incompatible, then can be used to request the old behavior. The requested value of N can be too low, in which case TXR will complain and exit with an unsuccessful termination status. This indicates that TXR refuses to be compatible with such an old version. Users requiring the behavior of that version will have to install an older version of TXR which supports that behavior, or even that exact version. If the option is specified more than once, the behavior is not specified. Compatibility can also be requested via the environment variable instead of the option. For more information, see the COMPATIBILITY section. The argument to this option must be a decimal integer. It represents a megabyte value, the "GC delta": one megabyte is 1048576 bytes. The "GC delta" controls an aspect of the garbage collector behavior. See the function for a description. This option turns on debugging, like but also requests stepping into the autoload processing of TXR Lisp library code. Normally, debugging through the evaluations triggered by autoloading is suppressed. Implies This option turns on debugging, like but also requests stepping into the parse-time macro-expansion of TXR Lisp code embedded in TXR queries. Normally, this is suppressed. Implies Prints usage summary on standard output, and terminates successfully. Prints the software license. This depends on the software being installed such that the LICENSE file is in the data directory. Use of TXR implies agreement with the liability disclaimer in the license. Prints a message on standard output which includes the program version, and then immediately causes TXR to terminate with a successful status. If TXR was built with an embedded build ID string, this option prints that string. Otherwise nothing is printed. In either case, TXR then immediately terminates with a successful status. The option provides a way to encode multiple arguments as a single argument, which is useful on some systems which have limitations in their implementation of the hash-bang mechanism. For details about its special syntax, see Hash-Bang Support below. It is also useful in standalone application deployment. See the section STANDALONE APPLICATION SUPPORT, in which example uses of are shown. The option (extended is like but must be followed by an argument. The argument is removed from the argument list and substituted in place of occurrences of among the arguments expanded from the syntax. These options influence the treatment of query files which do not have a recognized suffix indicating their type. The option causes a file with an unrecognized suffix, or no suffix, to be treated as Lisp source; causes it to be treated as a compiled TXR Lisp file. Moreover, and influence the suffix search. By default, when a query file name does not have a recognizable suffix, and the file does not exist, TXR adds the suffix to the name and tries opening that name, and in a similar way tries and finally In this situation, if either of these two options is specified, TXR tries only the and suffixes, in that order, avoiding the suffix. The search order is always first, then regardless of whether or is specified. Note that and influence how the argument of the option is treated, but only if they precede that option. If the file has a recognized suffix: or then these options have no effect. The suffix determines the interpretation of the content. Moreover, no suffix search takes place: only the given path name is tried. On platforms which support the POSIX family of functions, this option causes TXR to re-execute itself. The re-executed image receives the remaining arguments which follow the argument. Note: this option is useful for supporting setuid operation in hash-hang scripts. On some platforms, the interpreter designated by a hash-bang script runs without altered privilege, even if that interpreter is installed setuid. If the interpreter is executed directly, then setuid applies to it, but not if it is executed via hash bang. If the option is used in the interpreter command line of such a script, the interpreter will re-execute itself, thereby gaining the setuid privilege. The re-executed image will then obtain the script name from the arguments which are passed to it and determine whether that script will run setuid. See the section SETUID/SETGID OPERATION. If entering the interactive listener, suppress the reading of the in the home directory. See the Interactive Profile File subsection in the INTERACTIVE LISTENER section of the manual. This option enables a behavior which stresses the garbage collector with frequent garbage collection requests. The purpose is to make it more likely to reproduce certain kinds of bugs. Use of this option severely degrades the performance of TXR. If TXR is enabled with Valgrind support, then this option is available. It enables code which uses the Valgrind API to integrate with the Valgrind debugger, for more accurate tracking of garbage collected objects. For example, objects which have been reclaimed by the garbage collector are marked as inaccessible, and marked as uninitialized when they are allocated again. This option specifies that all memory allocated by TXR should be freed upon normal termination. This behavior is useful for debugging memory leaks. An accurate leak detection tool, such as the one built into Valgrind, should report zero leaked or still reachable memory if has been used and TXR has terminated normally. that indicates either a leak in TXR, a leak or global object retention in a platform library, or else a a leak introduced due to misuse of FFI. If this option is used, then regular expressions are all treated using the derivative-based back-end. The NFA-based regex implementation is disabled. Normally, only regular expressions which require the intersection and complement operators are handled using the derivative back-end. This option makes it possible to test that back-end on test cases that it wouldn't normally receive. This option changes to the specified package, by finding the package of the specified and assigning that to the special variable. If the package is not found, a diagnostic is issued, and TXR terminates unsuccessfully. The package thus specified is visible to the subsequent occurrences of the family of options as well as of the option. It does not affect the value of which is in effect when a is executed or when the interactive listener is entered. This option invokes the on If is specified, it is passed to as the target argument; otherwise, that argument is defaulted. The option can be used multiple times to process multiple files. Unsuccessful compilation throws an exception, causing TXR to terminate abnormally. Similarly to the option, if this option is used at least once, and all of the invocations are successful, and there is no argument, then TXR terminates with a successful status instead of entering the interactive listener. The option can be used request the listener. Signifies the end of the option list. This argument is not interpreted as an option, but treated as a filename argument. After the first such argument, no more options are recognized. Even if another argument looks like an option, it is treated as a name. This special argument means "read from standard input" instead of a file. The or any of the data files, may be specified using this option. If two or more files are specified as the behavior is system-dependent. It may be possible to indicate EOF from the interactive terminal, and then specify more input which is interpreted as the second file, and so forth.

After the options, the remaining arguments are treated as follows. If neither the nor the options were specified, then the first argument is treated as the If no arguments are present, then TXR enters interactive mode, provided that none of the or options had been processed, in which case it instead terminates. The TXR Pattern Language has features for implicitly treating the subsequent command-line arguments as input files. It follows the convention that an argument consisting of a single (dash) character specifies that standard input is to be used, instead of opening a file. If the query does not use the directive to select an alternative data source, and a pattern-matching construct is processed which demands data, then the first argument will be opened as a data source. Arguments not opened as data sources can be assigned alternative meanings and uses, or can be ignored entirely, under control of the query. Specifying standard input as a source with an explicit argument is unnecessary. If no arguments are present, then TXR scans standard input by default. This was not true in versions of TXR prior to 171; see the COMPATIBILITY section.

TXR begins by reading the script, which is given as the contents of the argument of the option, or else as the contents of an input source specified by the option or by the argument. If or the argument specify (dash) then the script is read from standard input. In the case of the TXR pattern language, the entire query is scanned, internalized, and then begins executing, if it is free of syntax errors. (TXR Lisp is processed differently, form by form.) On the other hand, the pattern language reads data files in a lazy manner. A file isn't opened until the query demands material from that file, and then the contents are read on demand, not all at once. The suffix of the is significant. If the name has no suffix, or if it has a suffix, then it is assumed to be in the TXR pattern language. If it has the suffix, then it is assumed to be TXR Lisp. The and options change the treatment of unsuffixed script file names, causing them to be interpreted as TXR Lisp source or compiled TXR Lisp, respectively. If a file name is specified which does not have a recognized suffix, and names a file which doesn't exist, then TXR adds the suffix and tries again. If that doesn't exist, another attempt is made with the suffix, which will be treated as as a TXR Lisp compiled file. If that doesn't exist, then is tried, expected to be a file compressed in format. Finally, if that doesn't exist, the suffix is tried, which will be treated as containing TXR Lisp source. If either the or option has been specified, then TXR skips trying the suffix, and tries only followed by and A TXR Lisp file is processed as if by the macro: forms from the file are read and evaluated. If the forms do not terminate the TXR process or throw an exception, and there are no syntax errors, then TXR terminates successfully after evaluating the last form. If syntax errors are encountered in a form, then TXR terminates unsuccessfully. TXR Lisp is documented in the section TXR LISP. If a query file is specified, but no file arguments, it is up to the query to open a file, pipe or standard input via the directive prior to attempting to make a match. If a query attempts to match text, but has run out of files to process, the match fails. TXR sends errors and verbose logs to the standard error device. The following paragraphs apply when TXR is run without enabling verbose mode with or the printing of variable bindings with or If the command-line arguments are incorrect, TXR issues an error diagnostic and terminates with a failed status. If the specifies a query, and the query has a malformed syntax, TXR likewise issues error diagnostics and terminates with a failed status. If the query fails due to a mismatch, TXR terminates with a failed status. No diagnostics are issued. If the query is well-formed, and matches, then TXR issues no diagnostics, and terminates with a successful status. In verbose mode (option TXR issues diagnostics on the standard error device even in situations which are not erroneous. In bindings-printing mode (options or TXR prints the word if the query fails, and exits with a failed termination status. If the query succeeds, the variable bindings, if any, are output on standard output. If the is TXR Lisp, then it is processed form by form. Each top-level Lisp form is evaluated after it is read. If any form is syntactically malformed, TXR issues diagnostics and terminates unsuccessfully. This is somewhat different from how the pattern language is treated: a script in the pattern language is parsed in its entirety before being executed. A query may contain comments which are delimited by the sequence and extend to the end of the line. Whitespace can occur between the and A comment which begins on a line swallows that entire line, as well as the newline which terminates it. In essence, the entire comment line disappears. If the comment follows some material in a line, then it does not consume the newline. Thus, the following two queries are equivalent:

1.  @a@; comment: match whole line against variable @a
     @; this comment disappears entirely
     @b

2.  @a
     @b

The comment after the does not consume the newline, but the comment which follows does. Without this intuitive behavior, line comment would give rise to empty lines that must match empty lines in the data, leading to spurious mismatches. Instead of the character, the character can be used. This is an obsolescent feature. TXR has several features which support use of the hash-bang convention for creating apparently standalone executable programs. Special processing is applied to TXR query or TXR Lisp script files that are specified on the command line via the option or as the first non-option argument. If the first line of such a file begins with the characters that entire line is consumed and processed specially. This removal allows for TXR queries to be turned into standalone executable programs in the POSIX environment using the hash-bang mechanism. Unlike most interpreters, TXR applies special processing to the line, which is described below, in the section Argument Generation with the Null Hack. Shell session example: create a simple executable program called and run it. This assumes TXR is installed in
  $ cat > hello.txr
  #!/usr/bin/txr
  @(bind a "Hey")
  @(output)
  Hello, world!
  @(end)
  $ chmod a+x hello.txr
  $ ./hello.txr
  Hello, world! When this plain hash-bang line is used, TXR receives the name of the script as an argument. Therefore, it is not possible to pass additional options to TXR. For instance, if the above script is invoked like this
  $ ./hello.txr -B the option isn't processed by TXR, but treated as an additional argument, just as if had been executed directly. This behavior is useful if the script author wants not to expose the TXR options to the user of the script. However, the hash-bang line can use the option:
  #!/usr/bin/txr -f Now, the name of the script is passed as an argument to the option, and TXR will look for more options after that, so that the resulting program appears to accept TXR options. Now we can run
  $ ./hello.txr -B
  Hello, world!
  a="Hey" The option is honored. On some operating systems, it is not possible to pass more than one argument through the hash-bang mechanism. That is to say, this will not work.
  #!/usr/bin/txr -B -f To support systems like this, TXR supports the special argument as well as an extended version, With it is possible to encode multiple arguments into one argument. The option must be followed by a separator character, chosen by the programmer. The characters after that are split into multiple arguments on the separator character. The option is then removed from the argument list and replaced with these arguments, which are processed in its place. Example:
  #!/usr/bin/txr --args:-B:-f The above has the same behavior as
  #!/usr/bin/txr -B -f on a system which supports multiple arguments in the hash-bang line. The separator character is the colon, and so the remainder of that argument, is split into the two arguments The option is similar to but must be followed by one more argument. After performs the argument splitting in the same manner as any of the arguments which it produces which are the two-character sequence are replaced with that following argument. Whether or not the replacement occurs, that following argument is then removed. Example:
  #!/usr/bin/txr --eargs:-B:{}:--foo:42 This has an effect which cannot be replicated in any known implementation of the hash-bang mechanism. Suppose that this hash-bang line is placed in a script called When this script is invoked with arguments, as in:
  script.txr a b c then TXR is invoked similarly to:
  /usr/bin/txr --eargs:-B:{}:--foo:42 script.txr a b c Then, when processing takes place, firstly the argument sequence
  -B {} --foo 42 is produced by splitting into four fields using the (colon) character as the separator. Then, within these four fields, all occurrences of are replaced with the following argument resulting in:
  -B script.txr --foo 42 Furthermore, that argument is removed from the remaining argument list. The four arguments are then substituted in place of the original syntax. The resulting TXR invocation is, therefore:
  /usr/bin/txr -B script.txr --foo 42 a b c Thus, allows some arguments to be encoded into the interpreter script, such that script name is inserted anywhere among them, possibly multiple times. Arguments for the interpreter can be encoded, as well as arguments to be processed by the script. The and mechanisms do not solve the following problem: the POSIX utility is often exploited for its searching capability, and used to express hash-bang scripts in the following way:
  #!/usr/bin/env txr Here, the utility searches for the program in the directories indicated by the variable, which liberates the script from having to encode the exact location where the program is installed. However, if the operating system allows only one argument in the hash-bang mechanism, then no arguments can be passed to the program. To mitigate this problem, TXR supports a special feature in its hash-bang support. If the hash-bang line contains a null byte, then the text from after the null byte until the end of the line is split into fields using the space character as a separator, and these fields are inserted into the command line. This manipulation happens during command-line processing, i.e. prior to the execution of the file. If this processing is applied to a file that is specified using the option, then the arguments which arise from the special processing are inserted after that option and its argument. If this processing is applied to the file which is the first non-option argument, then the options are inserted before that argument. However, care is taken not to process that argument a second time. In either situation, processing of the command-line options continues, and the arguments which are processed next are the ones which were just inserted. This is true even if the options had been inserted as a result of processing the first non-option argument, which would ordinarily signal the termination of option processing. In the following examples, it is assumed that the script is named, and invoked, as and is given arguments and that resolves to The code indicates a literal ASCII NUL character (the zero byte). Basic example:
  #!/usr/bin/env txr<NUL>-a 3 Here, searches for finding it in Thus, including the executable name, TXR receives this full argument list:
  /usr/bin/txr /home/jenny/foo.txr --bar abc The first non-option argument is the name of the script. TXR opens the script, and notices that it begins with a hash-bang line. It consumes the hash-bang line and finds the null byte inside it, retrieving the character string after it, which is This is split into the two arguments and which are then inserted into the command line ahead of the the script name. The effective command line then becomes:
  /usr/bin/txr -a 3 /home/jenny/foo.txr --bar abc Command-line option processing continues, beginning with the option. After the option is processed, is encountered again. This time it is not opened a second time; it signals the end of option processing, exactly as it would immediately do if it hadn't triggered the insertion of any arguments. Advanced example: use to invoke passing options to the interpreter and to the script:
  #!/usr/bin/env txr<NUL>--eargs:-C:175:{}:--debug This example shows how can be used in conjunction with the null hack. When begins executing, it receives the arguments
  /usr/bin/txr /home/jenny/foo.txr The script file is opened, and the arguments delimited by the null character in the hash-bang line are inserted, resulting in the effective command line:
  /usr/bin/txr --eargs:-C:175:{}:--debug /home/jenny/foo.txr Next, is processed in the ordinary way, transforming the command line into:
  /usr/bin/txr -C 175 /home/jenny/foo.txr --debug The name of the script file is encountered, and signals the end of option processing. Thus receives the option, instructing it to emulate some behaviors from version 175, and the script receives as its argument: it executes with the list containing one element, the character string The hash-bang null-hack feature was introduced in TXR 177. Previous versions ignore the hash-bang line, performing no special processing. Where a risk exists that programs which depend on the feature might be executed by an older version of TXR, care must be taken to detect and handle that situation, either by means of the variable, or else by some logic which infers that the processing of the hash-bang line hasn't been performed. It is possible to use the Hash-Bang Null Hack, such that the resulting executable program recognizes TXR options. This is made possible by a special behavior in the processing of the option. For instance, suppose that the effect of the following familiar hash-bang line is required:
  #!/path/to/txr -f However, suppose there is also a requirement to use the utility to find TXR. Furthermore, the operating system allows only one hash-bang argument. Using the Null Hack, this is rewritten as:
  #!/usr/bin/env txr<NUL>-f then if the script is invoked with arguments the command line will ultimately be transformed into:
  /path/to/txr -f /path/to/scriptfile -i a b c which allows TXR to process the option, leaving and as arguments for the script. However, note that there is a subtle issue with the option that has been inserted via the Null Hack: namely, this insertion happens after TXR has opened the script file and read the hash-bang line from it. This means that when the inserted option is being processed, the script file is already open. A special behavior occurs. The option processing notices that the argument to is identical to the pathname of name of the script file that TXR has already opened for processing. The option and its argument are then skipped. TXR supports setuid hash-bang scripting, even on platforms that do not support setuid and setgid attributes on hash-bang scripts. On such platforms, TXR has to be installed setuid/setgid. See the section SETUID/SETGID OPERATION. On some platforms, it may also be necessary to to use the option. Outside of directives, whitespace is significant in TXR queries, and represents a pattern match for whitespace in the input. An extent of text consisting of an undivided mixture of tabs and spaces is a whitespace token. Whitespace tokens match a precisely identical piece of whitespace in the input, with one exception: a whitespace token consisting of precisely one space has a special meaning. It is equivalent to the regular expression match an extent of one or more spaces (but not tabs!). Multiple consecutive spaces do not have this meaning. Thus, the query line (one space between and matches with any number of spaces between the two letters. For matching a single space, the syntax can be used (backslash-escaped space). It is more often necessary to match multiple spaces than to match exactly one space, so this rule simplifies many queries and inconveniences only a few. In output clauses, string and character literals and quasiliterals, a space token denotes a space. Query material which is not escaped by the special character is literal text, which matches input character for character. Text which occurs at the beginning of a line matches the beginning of a line. Text which starts in the middle of a line, other than following a variable, must match exactly at the current position, where the previous match left off. Moreover, if the text is the last element in the line, its match is anchored to the end of the line. An empty query line matches an empty line in the input. Note that an empty input stream does not contain any lines, and therefore is not matched by an empty line. An empty line in the input is represented by a newline character which is either the first character of the file, or follows a previous newline-terminated line. Input streams which end without terminating their last line with a newline are tolerated, and are treated as if they had the terminator. Text which follows a variable has special semantics, described in the section Variables below. A query may not leave a line of input partially matched. If any portion of a line of input is matched, it must be entirely matched, otherwise a matching failure results. However, a query may leave unmatched lines. Matching only four lines of a ten-line file is not a matching failure. The directive can be used to explicitly match the end of a file. In the following example, the query matches the text, even though the text has an extra line.

code:
    Four score and seven
     years ago our

data:
    Four score and seven
     years ago our
     forefathers

In the following example, the query fails to match the text, because the text has extra material on one line that is not matched:

code:
    I can carry nearly eighty gigs
     in my head

data:
    I can carry nearly eighty gigs of data
     in my head

Needless to say, if the text has insufficient material relative to the query, that is a failure also. To match arbitrary material from the current position to the end of a line, the "match any sequence of characters, including empty" regular expression can be used. Example:

code:
    I can carry nearly eighty gigs@/.*/

data:
    I can carry nearly eighty gigs of data

In this example, the query matches, since the regular expression matches the string "of data". (See the Regular Expressions section below.) Another way to do this is:

code:
    I can carry nearly eighty gigs@(skip) Control characters may be embedded directly in a query (with the exception of newline characters). An alternative to embedding is to use escape syntax. The following escapes are supported: A backslash immediately followed by a newline introduces a physical line break without breaking up the logical line. Material following this sequence continues to be interpreted as a continuation of the previous line, so that indentation can be introduced to show the continuation without appearing in the data. A backslash followed by a space encodes a space. This is useful in line continuations when it is necessary for some or all of the leading spaces to be preserved. For instance the two line sequence
      abcd@\
        @\ efg is equivalent to the line
      abcd efg The two spaces before the in the second line are consumed. The spaces after are preserved. Alert character (ASCII 7, BEL). Backspace (ASCII 8, BS). Horizontal tab (ASCII 9, HT). Line feed (ASCII 10, LF). Serves as abstract newline on POSIX systems. Vertical tab (ASCII 11, VT). Form feed (ASCII 12, FF). This character clears the screen on many kinds of terminals, or ejects a page of text from a line printer. Carriage return (ASCII 13, CR). Escape (ASCII 27, ESC) A immediately followed by a sequence of hex digits is interpreted as a hexadecimal numeric character code. For instance is the ASCII character A. If a semicolon character immediately follows the hex digits, it is consumed, and characters which follow are not considered part of the hex escape even if they are hex digits. A immediately followed by a sequence of octal digits (0 through 7) is interpreted as an octal character code. For instance is character 8, same as If a semicolon character immediately follows the octal digits, it is consumed, and subsequent characters are not treated as part of the octal escape, even if they are octal digits.

Note that if a newline is embedded into a query line with this does not split the line into two; it's embedded into the line and thus cannot match anything. However, may be useful in the directive and in TXR represents text internally using wide characters, which are used to represent Unicode code points. Script source code, as well as all data sources, are assumed to be in the UTF-8 encoding. In TXR and TXR Lisp source, extended characters can be used directly in comments, literal text, string literals, quasiliterals and regular expressions. Extended characters can also be expressed indirectly using hexadecimal or octal escapes. On some platforms, wide characters may be restricted to 16 bits, so that TXR can only work with characters in the BMP (Basic Multilingual Plane) subset of Unicode. TXR does not use the localization features of the system library; its handling of extended characters is not affected by environment variables like and The program reads and writes only the UTF-8 encoding. TXR deals with UTF-8 separately in its parser and in its I/O streams implementation. TXR's text streams perform UTF-8 conversion internally, such that TXR applications use Unicode code points. In text streams, invalid UTF-8 bytes are treated as follows. When an invalid byte is encountered in the middle of a multibyte character, or if the input ends in the middle of a multibyte character, or if an invalid character is decoded, such as an overlong from, or code in the range U+DC00 through U+DCFF, the UTF-8 decoder returns to the starting byte of the ill-formed multibyte character, and extracts just one byte, mapping that byte to the Unicode character range U+DC00 through U+DCFF, producing that code point as the decoded result. The decoder is then reset to its initial state and begins decoding at the following byte, where the same algorithm is repeated. Furthermore, because TXR internally uses a null-terminated character representation of strings which easily interoperates with C language interfaces, when a null character is read from a stream, TXR converts it to the code U+DC00. On output, this code converts back to a null byte, as explained in the previous paragraph. By means of this representational trick, TXR can handle textual data containing null bytes. In contrast to the above, the TXR parser scans raw UTF-8 bytes from a binary stream, rather than using a text stream. The parser performing its own recognition of UTF-8 sequences in certain language constructs, using a UTF-8 decoder only when processing certain kinds of tokens. Comments are read without regard for encoding, so invalid encoding bytes in comments are not detected. A comment is simply a sequence of bytes terminated by a newline. Invalid UTF-8 encountered while scanning identifiers and character names in character literal (hash-backslash) syntax is diagnosed as a syntax error. UTF-8 in string literals is treated in the same way as UTF-8 in text streams. Invalid UTF-8 bytes are mapped into code points in the U+DC000 through U+DCFF range, and incorporated as such into the resulting string object which the literal denotes. The same remarks apply to regular-expression literals. In place of a piece of text (see section Text above), a regular-expression directive may be used, which has the following syntax:
  @/RE/ where the RE part enclosed in slashes represents regular-expression syntax (described in the section Regular Expressions below). Long regular expressions can be broken into multiple lines using a backslash-newline sequence. Whitespace before the sequence or after the sequence is not significant, so the following two are equivalent:
  @/reg \
    ular/
  @/regular/ There may not be whitespace between the backslash and newline. Whereas literal text simply represents itself, regular expression denotes a (potentially infinite) set of texts. The regular-expression directive matches the longest piece of text (possibly empty) which belongs to the set denoted by the regular expression. The match is anchored to the current position; thus if the directive is the first element of a line, the match is anchored to the start of a line. If the regular-expression directive is the last element of a line, it is anchored to the end of the line also: the regular expression must match the text from the current position to the end of the line. Even if the regular expression matches the empty string, the match will fail if the input is empty, or has run out of data. For instance suppose the third line of the query is the regular expression but the input is a file which has only two lines. This will fail: the data has no line for the regular expression to match. A line containing no characters is not the same thing as the absence of a line, even though both abstractions imply an absence of characters. Like text which follows a variable, a regular-expression directive which follows a variable has special semantics, described in the section Variables below. Much of the query syntax consists of arbitrary text, which matches file data character for character. Embedded within the query may be variables and directives which are introduced by a character. Two consecutive characters encode a literal A variable-matching or substitution directive is written in one of several ways: The forms with an indicate a long match, see Longest Match below. The forms with the embedded regexp or function or have special semantics; see Positive Match below. The identifier cannot be used as a name; it is a reserved symbol which denotes the value true. An attempt to use the variable will result in an exception. The symbol can be used where a variable name is required syntactically, but it has special semantics, described in a section below. A is a "simple identifier" form which is not delimited by braces. A consists of any combination of one or more letters, numbers, and underscores. It may not look like a number, so that for instance is not a valid but is valid. Case is sensitive, so that is different from which is different from The braces around an identifier can be used when material which follows would otherwise be interpreted as being part of the identifier. When a name is enclosed in braces it is a The following additional characters may be used as part of a which are not allowed in a
  ! $ % & * + - < = > ? \ ~ Moreover, most Unicode characters beyond U+007F may appear in a with certain exceptions. A character may not be used if it is any of the Unicode space characters, a member of the high or low surrogate region, a member of any Unicode private-use area, or is either of the two characters U+FFFE and U+FFFF. These situations produce a syntax error. Invalid UTF-8 in an identifier is also a syntax error. The rule still holds that a name cannot look like a number so is not a valid but these are valid: The syntax introduces the name whereas means the variable named followed by the text There may be whitespace between the and the name, or opening brace. Whitespace is also allowed in the interior of the braces. It is not significant. If a variable has no prior binding, then it specifies a match. The match is determined from some current position in the data: the character which immediately follows all that has been matched previously. If a variable occurs at the start of a line, it matches some text at the start of the line. If it occurs at the end of a line, it matches everything from the current position to the end of the line. If a variable is one of the plain forms then this is a "negative match". The extent of the matched text (the text bound to the variable) is determined by looking at what follows the variable, and ranges from the current position to some position where the following material finds a match. This is why this is called a "negative match": the spanned text which ends up bound to the variable is that in which the match for the trailing material did not occur. A variable may be followed by a piece of text, a regular-expression directive, a function call, a directive, another variable, or nothing (i.e. occurs at the end of a line). These cases are described in detail below. If the variable is followed by nothing, the negative match extends from the current position in the data, to the end of the line. Example:

code:
    a b c @FOO

data:
    a b c defghijk

result:
    FOO="defghijk" For the purposes of determining the negative match, text is defined as a sequence of literal text and regular expressions, not divided by a directive. So for instance in this example:
      @a:@/foo/bcd e@(maybe)f@(end)

the variable is considered to be followed by If a variable is followed by text, then the extent of the negative match is determined by searching for the first occurrence of that text within the line, starting at the current position. The variable matches everything between the current position and the matching position (not including the matching position). Any whitespace which follows the variable (and is not enclosed inside braces that surround the variable name) is part of the text. For example:

code:
    a b @FOO e f

data:
    a b c d e f

result:
    FOO="c d"

In the above example, the pattern text matches the data So when the variable is processed, the data being matched is the remaining The text which follows is This is found within the data at position 3 (counting from 0). So positions 0–2 ("c d") constitute the matching text which is bound to FOO. If the variable is followed by a function call, or a directive, the extent is determined by scanning the text for the first position where a match occurs for the entire remainder of the line. (For a description of functions, see Functions.) For example:
  @foo@(bind a "abc")xyz Here, will match the text from the current position to where occurs, even though there is a directive. Furthermore, if more material is added after the it is part of the search. Note the difference between the following two:
  @foo@/abc/@(func)
  @foo@(func)@/abc/ In the first example, matches the text from the current position until the match for the regular expression is not considered when processing In the second example, matches the text from the current position until the position which matches the function call, followed by a match for the regular expression. The entire sequence is considered. If an unbound variable specifies a fixed-width match or a regular expression, then the issue of consecutive variables does not arise. Such a variable consumes text regardless of any context which follows it. However, what if an unbound variable with no modifier is followed by another variable? The behavior depends on the nature of the other variable. If the other variable is also unbound, and also has no modifier, this is a semantic error which will cause the query to fail. A diagnostic message will be issued, unless operating in quiet mode via The reason is that there is no way to bind two consecutive variables to an extent of text; this is an ambiguous situation, since there is no matching criterion for dividing the text between two variables. (In theory, a repetition of the same variable, like could find a solution by dividing the match extent in half, which would work only in the case when it contains an even number of characters. This behavior seems to have dubious value.) An unbound variable may be followed by one which is bound. The bound variable is effectively replaced by the text which it denotes, and the logic proceeds accordingly. It is possible for a variable to be bound to a regular expression. If is an unbound variable and is bound to a regular expression then means A variable can be bound to a regular expression using, for example, The syntax for longest match is available. Example:

code:
    @FOO:@BAR@FOO

data:
    xyz:defxyz

result:
    FOO=xyz, BAR=def

Here, is matched with based on the delimiting around the colon. The colon in the pattern then matches the colon in the data, so that is considered for matching against is followed by which is already bound to Thus is located in the data following and so BAR is bound to If an unbound variable is followed by a variable which is bound to a list, or nested list, then each character string in the list is tried in turn to produce a match. The first match is taken. An unbound variable may be followed by another unbound variable which specifies a regular expression or function call match. This is a special case called a "double variable match". What happens is that the text is searched using the regular expression or function. If the search fails, then neither variable is bound: it is a matching failure. If the search succeeds, then the first variable is bound to the text which is skipped by the search. The second variable is bound to the text matched by the regular expression or function. Example:

code:
    @foo@{bar /abc/}

data:
    xyz@#abc

result:
    foo="xyz@#", BAR="abc"

Two variables can be de facto consecutive in a manner shown in the following example:
  @var1@(all)@var2@(end) This is treated just like the variable followed by directive. No semantic error is identified, even if both variables are unbound. Here, matches everything at the current position, and so ends up bound to the empty string. Example 1: matches at position 0 and binds the empty string:

code:
    @a@(all)@b@(end)

data:
    abc

result:
    a=""
     b="abc"

Example 2: specifies longest match (see Longest Match below), and so it takes everything:

code:
    @*a@(all)@b@(end)

data:
    abc

result:
    a="abc"
     b=""

The closest-match behavior for the negative match can be overridden to longest match behavior. A special syntax is provided for this: an asterisk between the and the variable, e.g.:

code:
    a @*{FOO}cd

data:
    a b cdcdcdcd

result:
    FOO="b cdcdcd"

code:
    a @{FOO}cd

data:
    a b cdcdcd

result:
    FOO="b "

In the former example, the match extends to the rightmost occurrence of and so receives In the latter example, the syntax isn't used, and so a leftmost match takes place. The extent covers only the stopping at the first occurrence. There are syntactic variants of variable syntax which have an embedded expression enclosed with the variable in braces: These specify a variable binding that is driven by a positive match derived from a regular expression, function or character count, rather than from trailing material (which is regarded as a "negative" match, since the variable is bound to material which is skipped in order to match the trailing material). The positive match syntax is processed without considering any following syntax, and therefore may be followed by an unbound variable. In the form, the match extends over all characters from the current position which match the regular expression (See the Regular Expressions section below.) If the variable already has a value, the text extracted by the regular expression must exactly match the variable. In the form, the match extends over lines or characters which are matched by the call to the function, if the call succeeds. Thus is just like except that the region of text skipped over by is also bound to the variable Except in one special case, the matching takes place horizontally within the current line, and the spanned range of text is treated as a string. The exception is that if the appears as the only element of a line, and has a binding as a vertical function, then the function is invoked in the same manner as it would be by the syntax. Then the variable indicated by is bound to the list of lines matched by the function call. Pattern functions are described in the Functions section below. The function is invoked even if the variable already has a value. The text matched by the function must match the variable. In the form, the match processes a field of text which consists of the specified number of characters, which must be a nonnegative number. If the data line doesn't have that many characters starting at the current position, the match fails. A match for zero characters produces an empty string. The text which is actually bound to the variable is all text within the specified field, but excluding leading and trailing whitespace. If the field contains only spaces, then an empty string is extracted. This fixed-field extraction takes place whether or not the variable already has a binding. If it already has a binding, then it must match the extracted, trimmed text. The syntax allows the or modifier to come from a variable. The variable must be bound and contain a nonnegative integer or regular expression. For example, behaves like if is bound to the integer 3. It is an error if is unbound. Just like in the Common Lisp language, the names and are special. symbol stands for the empty list object, an object which marks the end of a list, and Boolean false. It is synonymous with the syntax which may be used interchangeably with in most constructs. In TXR Lisp, and cannot be used as variables. When evaluated, they evaluate to themselves. In the TXR pattern language, can be used in the variable binding syntax, but does not create a binding; it has a special meaning. It allows the variable-matching syntax to be used to skip material, in ways similar to the directive. The symbol is also used as a name, both in the TXR pattern language and in TXR Lisp. A block named is considered to be anonymous. Names beginning with the (colon) character are keyword symbols. These also stand for themselves and may not be used as variables. Keywords are useful for labeling information and situations. Regular expressions are a language for specifying sets of character strings. Through the use of pattern-matching elements, a regular expression is able to denote an infinite set of texts. TXR contains an original implementation of regular expressions, which supports the following syntax: The period is a "wildcard" that matches any character. Character class: matches a single character, from the set specified by special syntax written between the square brackets. This supports basic regexp character class syntax. POSIX notation like is not supported. The regex tokens and are permitted in character classes, but not their complementing counterparts. These tokens simply contribute their characters to the class. The class means match an uppercase or lowercase letter; the class means match a digit or a lowercase letter; the class means match a non-digit, and so forth. There are no locale-specific behaviors in TXR regular expressions; denotes an ASCII/Unicode range of characters. The class means match a digit or the period character. A or can be used within a character class, but must be escaped with a backslash. A in the first position denotes a complemented class, unless it is escaped by backslash. In any other position, it denotes itself. Two backslashes code for one backslash. So for instance means match a or character, means match any character other than and means match either a or a backslash. Regex operators such as and appearing in a character class represent ordinary characters. The characters and occurring outside of a character class are ordinary. Unescaped characters can appear within a character class. The empty character class matches no character at all, and its complement matches any character, and is treated as a synonym for the (period) wildcard operator. These regex tokens each match a single character. The regex token matches a wide variety of ASCII whitespace characters and Unicode spaces. The token matches alphabetic word characters; it is equivalent to the character class The token matches a digit, and is equivalent to These regex tokens are the complemented counterparts of and The token matches all those characters which does not match, matches all characters that does not match and matches nondigits. An empty expression is a regular expression. It represents the set of strings consisting of the empty string; i.e. it matches just the empty string. The empty regex can appear alone as a full regular expression (for instance the TXR syntax with nothing between the slashes) and can also be passed as a subexpression to operators, though this may require the use of parentheses to make the empty regex explicit. For example, the expression means: match either or nothing. The forms and are syntax errors; though not useful, the correct way to match the empty expression zero or more times is the syntax The nomatch regular expression represents the empty set: it matches no strings at all, not even the empty string. There is no dedicated syntax to directly express nomatch in the regex language. However, the empty character class is equivalent to nomatch, and may be considered to be a notation for it. Other representations of nomatch are possible: for instance, the regex which is the complement of the regex that denotes the set of all possible strings, and thus denotes the empty set. A nomatch has uses; for instance, it can be used to temporarily "comment out" regular expressions. The regex is equivalent to since the branch cannot match anything. Using to "block" a subexpression allows you to leave it in place, then enable it later by removing the "block". If is a regular expression, then so is The contents of parentheses denote one regular expression unit, so that for instance in the operator applies to the entire parenthesized group. The syntax is valid and equivalent to the empty regular expression. Optionally match the preceding regular expression Match the expression zero or more times. This operator is sometimes called the "Kleene star", or "Kleene closure". The Kleene closure favors the longest match. Roughly speaking, if there are two or more ways in which can match, then that match occurs in which matches the longest possible text. Match the preceding expression one or more times. Like this favors the longest possible match: is equivalent to Match zero or more times, then match If this match can occur in more than one way, then it occurs such that is matched the fewest number of times, which is opposite from the behavior of Repetitions of terminate at the earliest point in the text where a nonempty match for occurs. Because it favors shorter matches, is termed a non-greedy operator. If is the empty expression, or equivalent to it, then reduces to So for instance is equivalent to since the missing right operand is interpreted as the empty regex. Note that whereas the expression is equivalent to the expression is not equivalent to Also note that is equivalent to This is because the precedence of is higher than that of catenation on its left side; this rule prevents the given syntax from expressing the catenation. The expression may be understood as: where the inner parentheses clarify how the syntax surrounding the operator is being parsed, and the outer parentheses are superfluous. The correct way to assert catenation of as the left operand of is To specify as the left operand, and limit the right operand to just the correct syntax is By contrast, the expression is not equivalent to because the precedence of is lower than that of catenation on its right side. The operator is effectively "bi-precedential". Match the opposite of the following expression that is, match exactly those texts that does not match. This operator is called complement, or logical not. Two consecutive regular expressions denote catenation: the left expression must match, and then the right. Match either the expression or This operator is known by a number of names: union, logical or, disjunction, branch, or alternative. Match both the expression and simultaneously; i.e. the matching text must be one of the texts which are in the intersection of the set of texts matched by and the set matched by This operator is called intersection, logical and, or conjunction.

Any character which is not a regular-expression operator, a backslash escape, or the slash delimiter, denotes a one-position match of that character itself. Any of the special characters, including the delimiting and the backslash, can be escaped with a backslash to suppress its meaning and denote the character itself. Furthermore, all of the same escapes that are described in the section Special Characters in Text above are supported — the difference is that in regular expressions, the character is not required, so for example a tab is coded as rather than Octal and hex character escapes can be optionally terminated by a semicolon, which is useful if the following characters are octal or hex digits not intended to be part of the escape. Only the above escapes are supported. Unlike in some other regular-expression implementations, if a backlash appears before a character which isn't a regex special character or one of the supported escape sequences, it is an error. This wasn't true of historic versions of TXR. See the COMPATIBILITY section.

Precedence table, highest to lowest:
    Operators | Class | Associativity
    (R) [] | primary |
    R? R+ R* R%... | postfix | left-to-right
    R1R2 | catenation | left-to-right
    ~R ...%R | unary | right-to-left
    R1&R2 | intersection | left-to-right
    R1|R2 | union | left-to-right

The operator is like a postfix operator with respect to its left operand, but like a unary operator with respect to its right operand. Thus is demonstrating right-to-left associativity, where all of may be regarded as a unary operator being applied to Similarly, means where the trailing behaves like a postfix operator. In TXR, regular expression matches do not span multiple lines. The regex language has no feature for multiline matching. However, the directive allows the remaining portion of the input to be treated as one string in which line terminators appear as explicit characters. Regular expressions may freely match through this sequence. It's possible for a regular expression to match an empty string. For instance, if the next input character is facing the regular expression there is a zero-character match: the regular expression's state machine can reach an acceptance state without consuming any characters. Examples:

code:
    @A@/a?/@/.*/

data:
    zzzzz

result:
    A=""

code:
    @{A /a?/}@B

data:
    zzzzz

result:
    A="", B="zzzz"

code:
    @*A@/a?/

data:
    zzzzz

result:
    A="zzzzz"

In the first example, variable is followed by a regular expression which can match an empty string. The expression faces the letter at position 0 in the data line. A zero-character match occurs there, therefore the variable takes on the empty string. The regular expression then consumes the line. Similarly, in the second example, the regular expression faces a and thus yields an empty string which is bound to Variable consumes the entire line. The third example requests the longest match for the variable binding. Thus, a search takes place for the rightmost position where the regular expression matches. The regular expression matches anywhere, including the empty string after the last character, which is the rightmost place. Thus variable fetches the entire line. For additional information about the advanced regular-expression operators, see NOTES ON EXOTIC REGULAR EXPRESSIONS below. If the escape character is followed by an open parenthesis or square bracket, this is taken to be the start of a TXR Lisp compound expression. The TXR language has the unusual property that its syntactic elements, so-called directives, are Lisp compound expressions. These expressions not only enclose syntax, but expressions which begin with certain symbols de facto behave as tokens in a phrase structure grammar. For instance, the expression begins a block which must be terminated by the expression otherwise there is a syntax error. The expression can contain arguments which modify the behavior of the construct, for instance In some ways, this situation might be compared to HTML, in which an element such as must be terminated by and can have attributes such as Compound expressions contain subexpressions which are other compound expressions or literal objects of various kinds. Among these are: symbols, numbers, string literals, character literals, quasiliterals and regular expressions. These are described in the following sections. Additional kinds of literal objects exist, which are discussed in the TXR LISP section of the manual. Some examples of compound expressions are:
  (banana)
  (a b c (d e f))
  ( a (b (c d) (e ) ))
  ("apple" #\b #\space 3)
  (a #/[a-z]*/ b)
  (_ `@file.txt`) Symbols occurring in a compound expression follow a slightly more permissive lexical syntax than the in the syntax introduced earlier. The (slash) character may be part of an identifier, or even constitute an entire identifier. In fact a symbol inside a directive is a This is described in the Symbol Tokens section under TXR LISP. A symbol must not be a number; tokens that look like numbers are treated as numbers and not symbols. Character literals are introduced by the (hash-backslash) syntax, which is either followed by a character name, the letter followed by hex digits, the letter followed by octal digits, or a single character. Valid character names are:
  nul linefeed return
  alarm newline esc
  backspace vtab space
  tab page pnul For instance denotes the escape character. This convention for character literals is similar to that of the Scheme language. Note that and are the same character. The character is specific to TXR and denotes the code in Unicode; the name stands for "pseudo-null", which is related to its special function. For more information about this, see the section "Character Handling and International Characters". String literals are delimited by double quotes. A double quote within a string literal is encoded using \" and a backslash is encoded as Backslash escapes like and are recognized, as are hexadecimal escapes like or and octal escapes like Ambiguity between an escape and subsequent text can be resolved by adding a semicolon delimiter after the escape: is a string consisting of the character followed by The semicolon delimiter disappears. To write a literal semicolon immediately after a hex or octal escape, write two semicolons, the first of which will be interpreted as a delimiter. Thus, represents Note that the source code syntax of TXR string literals is specified in UTF-8, which is decoded into an internal string representation consisting of code points. The numeric escape sequences are an abstract syntax for specifying code points, not for specifying bytes to be inserted into the UTF-8 representation, even if they lie in the 8-bit range. Bytes cannot be directly specified, other than literally. However, when a TXR string object is encoded to UTF-8, every code point lying in the range U+DC00 through U+DCFF is converted to a single byte by taking the low-order eight bits of its value. By manipulating code points in this special range, TXR programs can reproduce arbitrary byte sequences in text streams. Also note that the escape sequence for specifying code points found in some languages is unnecessary and absent, since the existing hexadecimal and octal escapes satisfy this requirement. More detailed information is given in the earlier section Character Handling and International Characters. If the line ends in the middle of a literal, it is an error, unless the last character is a backslash. This backslash is a special escape which does not denote a character; rather, it indicates that the string literal continues on the next line. The backslash is deleted, along with whitespace which immediately precedes it, as well as leading whitespace in the following line. The escape sequence (backslash space) can be used to encode a significant space. Example:
  "foo \
   bar"
  "foo \
  \ bar"
  "foo\ \
   bar" The first string literal is the string The second two are A word list literal (WLL) provides a convenient way to write a list of strings when such a list can be given as whitespace-delimited words. There are two flavors of the WLL: the regular WLL which begins with #" (hash, double quote) and the splicing list literal which begins with #*" (hash, star, double quote). Both types are terminated by a double quote, which may be escaped as \" in order to include it as a character. All the escaping conventions used in string literals can be used in word literals. Unlike in string literals, whitespace (tabs and spaces) is not significant in word literals: it separates words. A whitespace character may be escaped with a backslash in order to include it as a literal character. Just like in string literals, an unescaped newline character is not allowed. A newline preceded by a backslash is permitted. Such an escaped backslash, together with any leading and trailing unescaped whitespace, is removed and replaced with a single space. Example:
  #"abc def ghi" --> notates ("abc" "def" "ghi")
  #"abc def \
      ghi" --> notates ("abc" "def" "ghi")
  #"abc\ def ghi" --> notates ("abc def" "ghi")
  #"abc\ def\ \
   \ ghi" --> notates ("abc def " " ghi") A splicing word literal differs from a word literal in that it does not produce a list of string literals, but rather it produces a sequence of string literals that is merged into the surrounding syntax. Thus, the following two notations are equivalent:
  (1 2 3 #*"abc def" 4 5 #"abc def")
  (1 2 3 "abc" "def" 4 5 ("abc" "def")) The regular WLL produced a single list object, but the splicing WLL expanded into multiple string literal objects. Quasiliterals are similar to string literals, except that they may contain variable references denoted by the usual syntax. The quasiliteral represents a string formed by substituting the values of those variables into the literal template. If is bound to and to the quasiliteral represents the string A backquote escaped by a backslash represents itself. Unlike in directive syntax, two consecutive characters do not code for a literal but cause a syntax error. The reason for this is that compounding of the syntax is meaningful. Instead, there is a escape for encoding a literal character. Quasiliterals support the full output variable syntax. Expressions within variable substitutions follow the evaluation rules of TXR Lisp. This hasn't always been the case: see the COMPATIBILITY section. Quasiliterals can be split into multiple lines in the same way as ordinary string literals. The quasiword list literals (QLLs) are to quasiliterals what WLLs are to ordinary literals. (See the above section Word List Literals.) A QLL combines the convenience of the WLL with the power of quasistrings. Just as in the case of WLLs, there are two flavors of the QLL: the regular QLL which begins with (hash, backquote) and the splicing QLL which begins with (hash, star, backquote). Both types are terminated by a backquote, which may be escaped as in order to include it as a character. All the escaping conventions used in quasiliterals can be used in QLLs. Unlike in quasiliterals, whitespace (tabs and spaces) is not significant in QLLs: it separates words. A whitespace character may be escaped with a backslash in order to include it as a literal character. A newline is not permitted unless escaped. An escaped newline works exactly the same way as it does in WLLs. Note that the delimiting into words is done before the variable substitution. If the variable contains spaces, then nevertheless expands into a list of one item: the string derived from Examples:
  #`abc @a ghi` --> notates (`abc` `@a` `ghi`)
  #`abc @d@e@f \
  ghi` --> notates (`abc` `@d@e@f` `ghi`)
  #`@a\ @b @c` --> notates (`@a @b` `@c`) A splicing QLL differs from an ordinary QLL in that it does not produce a list of quasiliterals, but rather it produces a sequence of quasiliterals that is merged into the surrounding syntax. TXR supports integers and floating-point numbers. An integer literal is made up of digits through optionally preceded by a or sign. The character (comma) may appear between digits, as a visual separator of no semantic significance. The digit sequence must start and end with a digit. Runs of consecutive commas are permitted. Commas outside of the digit sequence are interpreted as the Lisp unquote syntax. Compatibility node: support for separator commas appeared in TXR 283. Older TXR versions will interpret commas in the middle of numeric constants as instances of the unquote syntax. Examples:
  123
  -34
  +0
  -0
  +234483527304983792384729384723234
  -1,000,000,001
  1,2,3,,4 ;; equivalent to 1234 Examples that are not integer tokens:
  ,123 ;; equivalent to (sys:unquote 123)
  123,a ;; equivalent to 123, followed by (sys:unquote a)
  -,1 ;; symbol - followed by (sys:unquote 1) An integer constant can also be specified in hexadecimal using the prefix followed by an optional sign, followed by hexadecimal digits: through and the uppercase or lowercase letters through
  #xFF ;; 255
  #x-ABC ;; -2748 These digits may contain separator commas, just as in the case of the decimal integer:
  #xFFFF,FFFF,FFFF Similarly, octal numbers are supported with the prefix followed by octal digits:
  #o777 ;; 511
  #o123,456 ;; 42797 and binary numbers can be written with a prefix:
  #b1110 ;; 14
  #b1111,1111 ;; 255 A comma between the radix prefix and digits is a syntax error:
  #x,DEF5,549C ;; Syntax error
  #b,1001,1101 ;; Likewise Note that the prefix is also used for buffer literals. A floating-point literal is marked by the inclusion of a decimal point, the scientific E notation, or both. It is an optional sign, followed by a mantissa consisting of digits, a decimal point, more digits, and then an optional E notation consisting of the letter or an optional or sign, and then digits indicating the exponent value. In the mantissa, the digits are not optional. At least one digit must either precede the decimal point or follow it. That is to say, a decimal point by itself is not a floating-point constant. The digits of the mantissa may include separator commas, in the same manner as decimal integer literals, in both the integer and fractional part. The digits of the exponent may not include separator commas. Examples:
  .123
  123.
  1E-3
  20E40
  .9E1
  9.E19
  -.5
  +3E+3
  1.E5
  1,123,456.935,342E+013 Examples which are not floating-point constant tokens:
  . ;; dot token, not a number
  123E ;; the symbol 123E
  1.0E- ;; syntax error: invalid floating point constant
  1.0E ;; syntax error: invalid floating point constant
  1.E ;; syntax error: invalid floating point literal
  .e ;; syntax error: dot token followed by symbol
  ,1.0 ;; equivalent to (sys:unquote 1.0) In TXR there is a special "dotdot" token consisting of two consecutive periods. An integer constant followed immediately by dotdot is recognized as such; it is not treated as a floating constant followed by a dot. That is to say, does not mean (floating point value followed by dot token). It means (integer followed by token). Dialect Note: unlike in Common Lisp, is not an integer, but the floating-point number Integers within a certain small range centered on zero have type. Values in the range fit into a Lisp value directly, not requiring heap allocation. A value which is implemented as a reference to a heap-allocated object is called boxed, whereas a self-contained value not referencing any storage elsewhere is called unboxed. Thus values in the are unboxed; those outside of the range have type instead, and are boxed. The variables and indicate the range. Floating-point values are all unboxed if TXR is built with "NaN boxing" enabled, otherwise they are all boxed. The Lisp expression returns under NaN boxing, indicating that the two instances of 0.0 are the same object. In the absence of NaN boxing, the two calls produce distinct, boxed representations of 0.0, which compare unequal under (The expression may not be relied upon if it is compiled, since compilation may deduplicate identical boxed literals, leading to a false positive.) Comments of the form were introduced earlier. Inside compound expressions, another convention for comments exists: Lisp comments, which are introduced by the (semicolon) character and span to the end of the line. Example:
  @(foo ; this is a comment
    bar ; this is another comment
    ) This is equivalent to When a TXR Lisp compound expression occurs in TXR preceded by a it is a directive. Directives which are based on certain symbols are, additionally, involved in a phrase-structure syntax which uses Lisp expressions as if they were tokens. For instance, the directive
  @(collect) not only denotes a compound expression with the symbol in its head position, but it also introduces a syntactic phrase which requires a matching directive. In other words, is not only an expression, but serves as a kind of token in a higher-level, phrase-structure grammar. Effectively, is a reserved symbol in the TXR language. A TXR program cannot use this symbol as the name of a pattern function due to its role in the syntax. The symbol has no reserved role in TXR Lisp. Usually if this type of directive occurs alone in a line, not preceded or followed by other material, it is involved in a "vertical" (or line-oriented) syntax. If such a directive is embedded in a line (has preceding or trailing material) then it is in a horizontal syntactic and semantic context (character-oriented). There is an exception: the definition of a horizontal function looks like this:
  @(define name (arg))body material@(end) Yet, this is considered one vertical item, which means that it does not match a line of data. (This is necessary because all horizontal syntax matches something within a line of data, which is undesirable for definitions.) Many directives exhibit both horizontal and vertical syntax, with different but closely related semantics. Some are vertical only, some are horizontal only. A summary of the available directives follows: Explicitly match the end of file. Fails if unmatched data remains in the input stream. Can capture or match the termination status of a pipe. Explicitly match the end of line. Fails if the current position is not the end of a line. Also fails if no data remains (there is no current line). Continue matching in another file or data source. Groups together a sequence of directives into a logical name block, which can be explicitly terminated from within by using the and directives. Blocks are described in the section Blocks below. Treat the remaining query as a subquery unit, and search the lines (or characters) of the input file until that subquery matches somewhere. A is also an anonymous block. Treat the remaining query or subquery as a match for a trailing context. That is to say, if the remainder matches, the data position is not advanced. Treat the remainder of the input as one big string, and apply the following query line to that string. The newline characters (or custom separators) appear explicitly in that string. The directive, inspired by the patch utility, specifies a partial match for some lines. These directives match a variable or expression against the current line number or character position. Match a variable against the name of the current data source. Match a variable against the remaining data (a lazy list of strings). Multiple clauses are each applied to the same input. Succeeds if at least one of the clauses matches the input. The bindings established by earlier successful clauses are visible to the later clauses. Multiple clauses are applied to the same input. Succeeds if and only if each one of the clauses matches. The clauses are applied in sequence, and evaluation stops on the first failure. The bindings established by earlier successful clauses are visible to the later clauses. Multiple clauses are applied to the same input. Succeeds if and only if none of them match. The clauses are applied in sequence, and evaluation stops on the first success. No bindings are ever produced by this construct. Multiple clauses are applied to the same input. No failure occurs if none of them match. The bindings established by earlier successful clauses are visible to the later clauses. Multiple clauses are applied to the same input. Evaluation stops on the first successful clause. The directive is similar to the directive in that it evaluates one or more TXR Lisp expressions. If the result of the rightmost expression is then triggers a match failure. See the TXR LISP section far below. The directive with optional and clauses allows one of multiple bodies of pattern-matching directives to be conditionally selected by testing the values of Lisp expressions. It is also available inside for conditionally selecting output clauses. Multiple clauses are applied to the same input. The one whose effect persists is the one which maximizes or minimizes the length of a particular variable. The directive matches the empty string. It is useful in certain situations, such as expressing an empty match in a directive that doesn't accept an empty clause. The syntax has another meaning in clauses, in conjunction with Introduces a function. Functions are described in the Functions section below. Performs function indirection. Evaluates which must produce a symbol that names a pattern function. Then that pattern function is invoked. Searches text for matches for multiple clauses which may occur in arbitrary order. For convenience, lines of the first clause are treated as separate clauses. Search the data for multiple matches of a clause. Collect the bindings in the clause into lists, which are output as array variables. The directive is line-oriented. It works with a multiline pattern and scans line by line. A similar directive called works within one line. A collect is an anonymous block. Separator of clauses for and Equivalent to The choice is stylistic. Separator of clauses for and Equivalent to The choice is stylistic. Required terminator for and Terminate the processing of a block, as if it were a failed match. Blocks are described in the section Blocks below. Terminate the processing of a block, as if it were a successful match. What bindings emerge may depend on the kind of block: has special semantics. Blocks are described in the section Blocks below. Indicates the start of a try block, which is related to exception handling, described in the Exceptions section below. Special clauses within See Exceptions below. Define custom exception types; throw an exception. See Exceptions below. The directive requires the following material to match, otherwise it throws an exception. It is useful for catching mistakes or omissions in parts of a query that are surefire matches. Normalizes a set of specified variables to one-dimensional lists. Those variables which have a scalar value are reduced to lists of that value. Those which are lists of lists (to an arbitrary level of nesting) are converted to flat lists of their leaf values. Binds a new variable which is the result of merging two or more other variables. Merging has somewhat complicated semantics. Decimates a list (any number of dimensions) to a string, by catenating its constituent strings, with an optional separator string between all of the values. Binds one or more variables against a value using a structural pattern match. A limited form of unification takes place which can cause a match to fail. Destructively assigns one or more existing variables using a structural pattern, using syntax similar to bind. Assignment to unbound variables triggers an error. Evaluates an expression in the current binding environment, and then creates new bindings for the variables in the structural pattern. Useful for temporarily overriding variable values in a scope. Removes variable bindings. Synonym of A directive which encloses an output clause in the query. An output section does not match text, but produces text which can be directed to various destinations, the default being standard output. Most directives cannot be used inside an output clause. The clause is a variant of which produces text that implicitly pushed back into the input stream to be matched. A directive understood within an section, for repeating multiline text, with successive substitutions pulled from lists. The directive produces iteration over lists horizontally within one line. These directives have a different meaning in matching clauses, providing a shorthand notation for and respectively. The directive is used for defining named filters, which are useful for filtering variable substitutions in output blocks. Filters are useful when data must be translated between different representations that have different special characters or other syntax, requiring escaping or similar treatment. Note that it is also possible to use a function as a filter. See Function Filters below. Named filters are stored in the hash table held in the Lisp special variable The directive passes one or more variables through a given filter or chain or filters, updating them with the filtered values. The and directives allow TXR programs to be modularized. They bring in code from a file, in two different ways. The directive is used to evaluate TXR Lisp expressions, discarding their result values. See the TXR LISP section far below. The (macro directive evaluates TXR Lisp expressions immediately, during the parsing of the TXR syntax in which it occurs. The directive is used to switch to a different symbol package. It mirrors the TXR Lisp macro of the same name.

Some directives contain subexpressions which are evaluated. Two distinct styles of evaluations occur in TXR: bind expressions and Lisp expressions. Which semantics applies to an expression depends on the syntactic context in which it occurs: which position in which directive. The evaluation of TXR Lisp expressions is described in the TXR LISP section of the manual. Bind expressions are so named because they occur in the directive. TXR pattern function invocations also treat argument expressions as bind expressions. The and directives also use bind expression evaluation. Bind expression evaluation also occurs in the argument position of the keyword in the directive. Unlike Lisp expressions, bind expressions do not support operators. If a bind expression is a nested list structure, it is a template denoting that structure. Any symbol in any position of that structure is interpreted as a variable. When the bind expression is evaluated, those corresponding positions in the template are replaced by the values of the variables. Anywhere where a variable can appear in a bind expression's nested list structure, a Lisp expression can appear preceded by the character. That Lisp expression is evaluated and its value is substituted into the bind expression's template. Moreover, a Lisp expression preceded by can be used as an entire bind expression. The value of that Lisp expression is then taken as the bind expression value. Any object in a bind expression which is not a nested list structure containing Lisp expressions or variables denotes itself literally. In the following examples, the variables and are assumed to have the string values and respectively. The notation indicates the value of each expression.
  a -> "foo"
  (a b) -> ("foo" "bar")
  ((a) ((b) b)) -> (("foo") (("bar") "bar"))
  (list a b) -> error: unbound variable list
  @(list a b) -> ("foo" "bar") ;; Lisp expression
  (a @[b 1..:]) -> ("foo" "ar") ;; Lisp eval of [b 1..:]
  (a @(+ 2 2)) -> ("foo" 4) ;; Lisp eval of (+ 2 2)
  #(a b) -> (a b) ;; Vector literal, not list.
  [a b] -> error: unbound variable dwim The last example above is a notation equivalent to and so follows similarly to the example involving The directive indicates that the remaining directives in the current block are to be applied against a new input source. It can only occur by itself as the only element in a query line, and takes various arguments, according to these possibilities: The lone without arguments specifies that subsequent directives will match inside the next file in the argument list which was passed to TXR on the command line. If is given, it must be a TXR Lisp expression which denotes an input source. Its value may be a string or an input stream. For instance, if variable contains the text then means switch to the file called and means to switch to the file The directive switches to the input stream connected to the output of the command. If the input source cannot be opened for whatever reason, TXR throws an exception (see Exceptions below). An unhandled exception will terminate the program. Often, such a drastic measure is inconvenient; if is invoked with the keyword, then if the input source cannot be opened, the situation is treated as a simple match failure. The keyword also ensures that when the stream is later closed, which occurs when the lazy list reads all of the available data, the implicit call to the function specifies as the argument value to that function's parameter. This mechanism does not suppress all exceptions related to the processing of that stream; unusual conditions encountered during the reading of data from the stream may throw exceptions. When the subsequent directives which follow are processed, the directive terminates, and any stream which had been opened for is closed. If the keyword is present, then this is prevented; the stream remains open. Note: keeping the stream open may be necessary if the directive is used to capture the input list into a variable whose value is used after the directive terminates, because the input list is lazy, and may depend on the stream continuing to be open. The variant means that the remaining command-line arguments are to be treated as a data source. For this purpose, each argument is considered to be a line of text. The argument list does include that argument which specifies the file that is currently being processed or was most recently processed. As the arguments are matched, they are consumed. This means that if a directive without arguments is executed in the scope of it opens the file named by the first unconsumed argument. To process arguments, and then continue with the original file and argument list, wrap the argument processing in a When the block terminates, the input source and argument list are restored to what they were before the block. The variant means that the list of process environment variables is treated as a source of data. It looks like a text file stream consisting of lines of the form If this feature is not available on a given platform, an exception is thrown. The syntax treats TXR Lisp expression as a source of text. The value of is flattened to a simple list in a way similar to the directive. The resulting list is treated as if it were the lines of a text file: each element of the list must be a string, which represents a line. If the strings happen contain embedded newline characters, they are a visible constituent of the line, and do not act as line separators. The syntax is similar to except that is not a TXR Lisp expression, but a TXR bind expression. The syntax requires to be a previously bound variable. The value of the variable is retrieved and treated like a list, in the same manner as under Note that is not always the same as because strictly requires to be a TXR variable, whereas the in is an expression which can potentially refer to Lisp variable. The syntax treats expression as a source of text. The value of the expression must be a string. Newlines in the string are interpreted as line terminators. A string which is not terminated by a newline is tolerated, so that:
  @(next :string "abc")
  @a binds to Likewise, this is also the case with input files and other streams whose last line is not terminated by a newline. However, watch out for empty strings, which are analogous to a correctly formed empty file which contains no lines:
  @(next :string "")
  @a This will not bind to it is a matching failure. The behavior of is different. The query
  @(next :list "")
  @a binds to The reason is that under the string is flattened to the list ("") which is not an empty input stream, but a stream consisting of one empty line. The variant indicates that the following subquery is applied to empty data, and the list of data sources from the command line is considered empty. This directive is useful in front of TXR code which doesn't process data sources from the command line, but takes command-line arguments. The incantation absolutely prevents TXR from trying to open the first command-line argument as a data source. Note that the directive only redirects the source of input over the scope of subquery in which the that directive appears. For example, the following query looks for the line starting with at the top of the file within a directive. After the which terminates the the is matched in the previous input stream which was in effect before the directive:
  @(some)
  @(next "foo.txt")
  xyz@suffix
  @(end)
  abc However, if the subquery successfully matched within the file there is now a binding for the variable, which is visible to the remainder of the entire query. The variable bindings survive beyond the clause, but the data stream does not. The directive considers the remainder of the query as a search pattern. The remainder is no longer required to strictly match at the current line in the current input stream. Rather, the current stream is searched, starting with the current line, for the first line where the entire remainder of the query will successfully match. If no such line is found, the directive fails. If a matching position is found, the remainder of the query is processed from that point. The remainder of the query can itself contain directives. Each such directive performs a recursive subsearch. Skip comes in vertical and horizontal flavors. For instance, skip and match the last line:
  @(skip)
  @last
  @(eof) Skip and match the last character of the line:
  @(skip)@{last 1}@(eol) The directive has two optional arguments, which are evaluated as TXR Lisp expressions. If the first argument evaluates to an integer, its value limits the range of lines scanned for a match. Judicious use of this feature can improve the performance of queries. Example: scan until matches, which must happen within the next 15 lines:
  @(skip 15)
  size: @SIZE Without the range limitation, will keep searching until it consumes the entire input source. In a horizontal the range-limiting numeric argument is expressed in characters, so that
  abc@(skip 5)def means: there must be a match for at the start of the line, and then within the next five characters, there must be a match for Sometimes a skip is nested within a or following another skip. For instance, consider:
  @(collect)
  begin @BEG_SYMBOL
  @(skip)
  end @BEG_SYMBOL
  @(end) The above iterates over the entire input. But, potentially, so does the embedded Suppose that is matched, but the data has no matching The skip will search in vain all the way to the end of the data, and then the collect will try another iteration back at the beginning, just one line down from the original starting point. If it is a reasonable expectation that an occurs 15 lines of a this can be specified instead:
  @(collect)
  begin @BEG_SYMBOL
  @(skip 15)
  end @BEG_SYMBOL
  @(end) If the symbol is used in place of a number, it means to scan an unlimited range of lines; thus, is equivalent to If the symbol is used, it changes the semantics of the skip to longest match semantics. For instance, match the last three space-separated tokens of the line:
  @(skip :greedy) @a @b @c Without the variable may match multiple tokens, and end up with spaces in it, because nothing follows and so it matches from any position which follows a space to the end of the line. Also note the space in front of Without this space, will get an empty string. A line-oriented example of greedy skip: match the last line without using
  @(skip :greedy)
  @last_line There may be a second numeric argument. This specifies a minimum number of lines to skip before looking for a match. For instance, skip 15 lines and then search indefinitely for
  @(skip nil 15)
  begin @BEG_SYMBOL The two arguments may be used together. For instance, the following matches if and only if the 15th line of input starts with
  @(skip 1 15)
  begin @BEG_SYMBOL Essentially, means "hard skip by lines". is the same as which is a noop, because it means: "the remainder of the query must match starting on the next line", or, more briefly, "skip exactly zero lines", which is the behavior if the directive is omitted altogether. Here is one trick for grabbing the fourth line from the bottom of the input:
  @(skip)
  @fourth_from_bottom
  @(skip 1 3)
  @(eof) Or using greedy skip:
  @(skip :greedy)
  @fourth_from_bottom
  @(skip 1 3) Non-greedy skip with the directive has a slight advantage because the greedy skip will keep scanning even though it has found the correct match, then backtrack to the last good match once it runs out of data. The regular skip with explicit will stop when the matches. The directive can consume considerable CPU time when multiple skips are nested. Consider:
  @(skip)
  A
  @(skip)
  B
  @(skip)
  C This is actually nesting: the second and third skips occur within the body of the first one, and thus this creates nested iteration. TXR is searching for the combination of skips which match the pattern of lines and with backtracking behavior. The outermost skip marches through the data until it finds followed by a pattern match for the second skip. The second skip iterates to find followed by the third skip, and the third skip iterates to find If and are only one line each, then this is reasonably fast. But suppose there are many lines matching and giving rise to a large number of combinations of skips which match and and yet do not find a match for triggering backtracking. The nested stepping which tries the combinations of and can give rise to a considerable running time. One way to deal with the problem is to unravel the nesting with the help of blocks. For example:
  @(block)
  @ (skip)
  A
  @(end)
  @(block)
  @ (skip)
  B
  @(end)
  @(skip)
  C Now the scope of each skip is just the remainder of the block in which it occurs. The first skip finds and then the block ends. Control passes to the next block, and backtracking will not take place to a block which completed (unless all these blocks are enclosed in some larger construct which backtracks, causing the blocks to be re-executed. This rewrite is not equivalent, and cannot be used for instance in backreferencing situations such as:
  @;
  @; Find three lines anywhere in the input which are identical.
  @;
  @(skip)
  @line
  @(skip)
  @line
  @(skip)
  @line This example depends on the nested search-within-search semantics. The directive introduces a trailing portion of a query or subquery which matches input material normally, but in the event of a successful match, does not advance the current position. This can be used, for instance, to cause to match partially overlapping regions. Trailer can be used in vertical context: or horizontal: A vertical prevents the vertical input position from advancing as it is matched by whereas a horizontal prevents the horizontal position from advancing. In other words, performs matching without consuming the input, providing a lookahead mechanism. Example:
  @(collect)
  @line
  @(trailer)
  @(skip)
  @line
  @(end) This script collects each line which has a duplicate somewhere later in the input. Without the directive, this does not work properly for inputs like:
  111
  222
  111
  222 Without the first duplicate pair constitutes a match which spans over the After that pair is found, the matching continues after the second With the directive in place, the collect body, on each iteration, only consumes the lines matched prior to The directive provides a useful alternative to TXR's line-oriented matching discipline. The directive treats all remaining input from the current input source as one big line. The query line which immediately follows freeform is applied to that line. The syntax variations are:
  @(freeform)
  ... query line ..
  ... query line ..
  ... query line ..
  ... query line .. where and denote TXR Lisp expressions which evaluate to an integer or string value, respectively. If and are both present, they may be given in either order. If the argument is given, its value limits the range of lines which are combined together. For instance means to only consider the next five lines to be one big line. Without this argument, is "bottomless". It can match the entire file, which creates the risk of allocating a large amount of memory. If the argument is given, it specifies a custom line terminator. The default terminator is The terminator does not have to be one character long. Freeform does not convert the entire remainder of the input into one big line all at once, but does so in a dynamic, lazy fashion, which takes place as the data is accessed. So at any time, only some prefix of the data exists as a flat line in which newlines are replaced by the terminator string, and the remainder of the data still remains as a list of lines. After the subquery is applied to the virtual line, the unmatched remainder of that line is broken up into multiple lines again, by looking for and removing all occurrences of the terminator string within the flattened portion. Care must be taken if the terminator is other than the default All occurrences of the terminator string are treated as line terminators in the flattened portion of the data, so extra line breaks may be introduced. Likewise, in the yet unflattened portion, no breaking takes place, even if the text contains occurrences of the terminator string. The extent of data which is flattened, and the amount of it which remains, depends entirely on the query line underneath In the following example, lines of data are flattened using $ as the line terminator.

code:
    @(freeform "$")
     @a$@b:
     @c
     @d

data:
    1
     2:3
     4

output (-B):
    a="1"
     b="2"
     c="3"
     d="4"

The data is turned into the virtual line The subquery matches the portion, binding to and to The remaining portion is then split into separate lines again according to the line terminator
  3
  4 Thus the remainder of the query
  @c
  @d faces these lines, binding to and to Note that since the data does not contain dollar signs, there is no ambiguity; the meaning may be understood in terms of the entire data being flattened and split again. In the following example, is used to solve a tokenizing problem. The Unix password file has fields separated by colons. Some fields may be empty. Using freeform, we can join the password file using as a terminator. By restricting freeform to one line, we can obtain each line of the password file with a terminating allowing for a simple tokenization, because now the fields are colon-terminated rather than colon-separated. Example:
  @(next "/etc/passwd")
  @(collect)
  @(freeform 1 ":")
  @(coll)@{token /[^:]*/}:@(end)
  @(end) The directive allows for an imperfect match spanning a set number of lines. It takes two arguments, both of which are TXR Lisp expressions that should evaluate to integers:
  ... This expresses that over the next query lines, the matching strictness is relaxed a little bit. Only out of those lines have to match. Afterward, the rest of the query follows normal, strict processing. In the degenerate situation where there are fewer than query lines following the directive, then of them must succeed anyway. (If there are fewer than then this is impossible.) The and directives perform binding between the current input line number or character position within a line, against an expression or variable:
  @(line 42)
  @(line x)
  abc@(chr 3)def@(chr y) The directive means "match the current input line number against the integer 42". If the current line is 42, then the directive matches, otherwise it fails. is a vertical directive which doesn't consume a line of input. Thus, the following matches at the beginning of an input stream, and ends up bound to the first line of input:
  @(line 1)
  @(line 1)
  @(line 1)
  @x The directive binds variable to the current input line number, if is an unbound variable. If is already bound, then the value of must match the current line number, otherwise the directive fails. The directive is similar to except that it's a horizontal directive, and matches the character position rather than the line position. Character positions are measured from zero, rather than one. does not consume a character. Hence the two occurrences of in the following example both match, and takes the entire line of input:
  @(chr 0)@(chr 0)@x The argument of or may be an Lisp expression. This is useful for matching computed lines or character positions:
  @(line @(+ a (* b c))) The directive performs a binding between the name of the current data source and a variable or bind expression:
  @(name na)
  @(name "data.txt") If is an unbound variable, it is bound and takes on the name of the data source, such as a file name. If is bound, then it has to match the name of the data source, otherwise the directive fails. The directive @(name "data.txt") fails unless the current data source has that name. The directive performs a binding between the unmatched data at the current position, and and a variable or bind expression. The unmatched data takes the form of a list of strings:
  @(data d) The binding is performed on object equality. If is already bound, a matching failure occurs unless contains the current unmatched data. Matching the current data has various uses. For instance, two branches of pattern matching can, at some point, bind the current data into different variables. When those paths join, the variables can be bound together to create the assertion that the current data had been the same at those points:
  @(all)
  @ (skip)
  foo
  @ (skip)
  bar
  @ (data x)
  @(or)
  @ (skip)
  xyzzy
  @ (skip)
  bar
  @ (data y)
  @(end)
  @(require (eq x y)) Here, two branches of the match some material which ends in the line However, it is possible that this is a different line. The directives are used to create an assertion that the data regions matched by the two branches are identical. That is to say, the unmatched data captured after the first and the unmatched data captured after the second must be the same object in order for to succeed, which implies that the same was matched in both branches of the Another use of is simply to gain access to the trailing remainder of the unmatched input in order to print it, or do some special processing on it. The Lisp function is useful for printing the unmatched data as newline-terminated lines:
  @(data remainder)
  @(do (tprint remainder)) The directive, if not given any argument, matches successfully when no more input is available from the current input source. In the following example, the variable captures the text and then since that is the last line of input, the directive matches:

code:
    @line
     @(eof)

data:
    One-line file

If the data consisted of two or more lines, would fail. The directive may be given a single argument, which is a pattern that matches the termination status of the input source. This is useful when the input source is a process pipe. For the purposes of sources which are not process pipes have the symbol as their termination status. In the following example, which assumes the availability of a POSIX shell command interpreter in the host system, the variable captures the string and the variable captures the integer value which is the termination status of the command:
  @(next (open-command "echo a; exit 5"))
  @a
  @(eof status) These directives, called the parallel directives, combine multiple subqueries, which are applied at the same input position, rather than to consecutive input. They come in vertical (line mode) and horizontal (character mode) flavors. In horizontal mode, the current position is understood to be a character position in the line being processed. The clauses advance this character position by moving it to the right. In vertical mode, the current position is understood to be a line of text within the stream. A clause advances the position by some whole number of lines. The syntax of these parallel directives follows this example:
  @(some)
  subquery1
  .
  .
  .
  @(and)
  subquery2
  .
  .
  .
  @(and)
  subquery3
  .
  .
  .
  @(end) And in horizontal mode:
  @(some)subquery1...@(and)subquery2...@(and)subquery3...@(end) Long horizontal lines can be broken up with line continuations, allowing the above example to be written like this, which is considered a single logical line:
  @(some)@\
     subquery1...@\
  @(and)@\
     subquery2...@\
  @(and)@\
     subquery3...@\
  @(end) The or must be followed by at least one subquery clause, and be terminated by If there are two or more subqueries, these additional clauses are indicated by or which are interchangeable. The separator and terminator directives also must appear as the only element in a query line. The directive requires keyword arguments. See below. The syntax supports arbitrary nesting. For example:
  QUERY: SYNTAX TREE:
  @(all) all -+
  @ (skip) +- skip -+
  @ (some) | +- some -+
  it | | +- TEXT
  @ (and) | | +- and
  @ (none) | | +- none -+
  was | | | +- TEXT
  @ (end) | | | +- end
  @ (end) | | +- end
  a dark | +- TEXT
  @(end) *- end nesting can be indicated using whitespace between and the directive expression. Thus, the above is an query containing a clause which applies to a that is followed by the text line The clause combines the text line and a clause which contains just one clause consisting of the line The semantics of the parallel directives is: Each of the clauses is matched at the current position. If any of the clauses fails to match, the directive fails (and thus does not produce any variable bindings). Clauses following the failed directive are not evaluated. Bindings extracted by a successful clause are visible to the clauses which follow, and if the directive succeeds, all of the combined bindings emerge. Each of the clauses is matched at the current position. If any of the clauses succeed, the directive succeeds, retaining the bindings accumulated by the successfully matching clauses. Evaluation does not stop on the first successful clause. Bindings extracted by a successful clause are visible to the clauses which follow. The parameter is for situations when the directive has multiple clauses that need to bind some common variables to different values: for instance, output parameters in functions. Resolve takes a list of variable name symbols as an argument. This is called the resolve set. If the clauses of bind variables in the resolve set, those bindings are not visible to later clauses. However, those bindings do emerge out of the directive as a whole. This creates a conflict: what if two or more clauses introduce different bindings for a variable in the resolve set? This is why it is called the resolve set: conflicts for variables in the resolve set are automatically resolved in favor of later directives. Example:
  @(some :resolve (x))
  @ (bind a "a")
  @ (bind x "x1")
  @(or)
  @ (bind b "b")
  @ (bind x "x2")
  @(end) Here, the two clauses both introduce a binding for Without the parameter, this would mean that the second clause fails, because comes in with the value which does not bind with But because is placed into the resolve set, the second clause does not see the binding. Both clauses establish their bindings independently creating a conflict over The conflict is resolved in favor of the second clause, and so the bindings which emerge from the directive are:
  a="a"
  b="b"
  x="x2" Each of the clauses is matched at the current position. The directive succeeds only if all of the clauses fail. If any clause succeeds, the directive fails, and subsequent clauses are not evaluated. Thus, this directive never produces variable bindings, only matching success or failure. Each of the clauses is matched at the current position. The directive always succeeds, even if all of the clauses fail. Whatever bindings are found in any of the clauses are retained. Bindings extracted by any successful clause are visible to the clauses which follow. Each of the clauses is matched at the current position. The clauses are matched, in order, at the current position. If any clause matches, the matching stops and the bindings collected from that clause are retained. Any remaining clauses after that one are not processed. If no clause matches, the directive fails, and produces no bindings. Each of the clauses is matched at the current position in order. In this construct, bindings established by an earlier clause are not visible to later clauses. Although any or all of the clauses can potentially match, the clause which succeeds is the one which maximizes or minimizes the length of the text bound to the specified variable. The other clauses have no effect. For all of the parallel directives other than and the query advances the input position by the greatest number of lines that match in any of the successfully matching subclauses that are evaluated. The directive does not advance the input position. For instance if there are two subclauses, and one of them matches three lines, but the other one matches five lines, then the overall clause is considered to have made a five line match at its position. If more directives follow, they begin matching five lines down from that position. The syntax of is: The directive evaluates a TXR Lisp expression. (See TXR LISP far below.) If the expression yields a true value, then it succeeds, and matching continues with the directives which follow. Otherwise the directive fails. In the context of the directive, the expression should not be introduced by the symbol; it is expected to be a Lisp expression. Example:
  @; require that 4 is greater than 3
  @; This succeeds; therefore, @a is processed
  @(require (> (+ 2 2) 3))
  @a The directive allows for conditional selection of pattern-matching clauses, based on the Boolean results of Lisp expressions. A variant of the directive is also available for use inside an clauses, where it similarly allows for the conditional selection of output clauses. The syntax of the directive can be exemplified as follows:
  .
  .
  .
  .
  .
  .
  .
  .
  .
  @(else)
  .
  .
  .
  @(end) The and clauses are all optional. If is present, it must be last, before after any clauses. Any of the clauses may be empty.
  @(if (> (length str) 42))
  foo: @a @b
  @(else)
  {@c}
  @(end) In this example, if the length of the variable is greater than then matching continues with otherwise it proceeds with

More precisely, how the directive works is as follows. The Lisp expressions are evaluated in order, starting with the expression, then the expressions if any are present. If any Lisp expression yields a true result (any value other than then evaluation of Lisp expressions stops. The corresponding clause of that Lisp expression is selected and pattern matching continues with that clause. The result of that clause (its success or failure, and any newly bound variables) is then taken as the result of the directive. If none of the Lisp expressions yield true, and an clause is present, then that clause is processed and its result determines the result of the directive. If none of the Lisp expressions yield true, and there is no clause, then the directive is deemed to have trivially succeeded, allowing matching to continue with whatever directive follows it. The directive supports the embedding of Lisp expressions, whose values are interpolated into the output. In particular, Lisp expressions are useful. For instance reproduces if yields a true value, otherwise Yet the directive is also supported in How the apparent conflict between the two is resolved is that the two take different numbers of arguments. An which has no arguments at all is a syntax error. One that has one argument is the head of the directive syntax which must be terminated by and which takes the optional and clauses. An which has two or more arguments is parsed as a self-contained Lisp expression. Sometimes text is structured as items that can appear in an arbitrary order. When multiple matches need to be extracted, there is a combinatorial explosion of possible orders, making it impractical to write pattern matches for all the possible orders. The directive is for these situations. It specifies multiple clauses which all have to match somewhere in the data, but in any order. For further convenience, the lines of the first clause of the directive are implicitly treated as separate clauses. The syntax follows this pattern:
  @(gather)
  one-line-query1
  one-line-query2
  .
  .
  .
  one-line-queryN
  @(and)
  multi
  line
  query1
  .
  .
  .
  @(and)
  multi
  line
  query2
  .
  .
  .
  @(end) The multiline clauses are optional. The directive takes keyword parameters, see below. Similarly to has an optional clause:
  @(gather)
  ...
  @(until)
  ...
  @(end) How works is that the text is searched for matches for the single-line and multiline queries. The clauses are applied in the order in which they appear. Whenever one of the clauses matches, any bindings it produces are retained and it is removed from further consideration. Multiple clauses can match at the same text position. The position advances by the longest match from among the clauses which matched. If no clauses match, the position advances by one line. The search stops when all clauses are eliminated, and then the cumulative bindings are produced. If the data runs out, but unmatched clauses remain, the directive fails. Example: extract several environment variables, which do not appear in a particular order:
  @(next :env)
  @(gather)
  USER=@USER
  HOME=@HOME
  SHELL=@SHELL
  @(end) If the or clause is present and a match occurs, then the matches from the other clauses are discarded and the terminates. The difference between is that any bindings bindings established in are retained, and the input position is advanced past the matching material. The clause has visibility to bindings established in the previous clauses in that same iteration, even though those bindings end up thrown away. For consistency, the keyword is supported in the clause of The semantics of using in this situation is tricky. In particular, if it is in effect, and the terminates successfully by collecting all required matches, it will trigger a failure. On the other hand, if the or clause activates before all required matches are gathered, a failure also occurs, whether or not the clause is Meaningful use of requires that the gather be open-ended; it must allow some (or all) variables not to be required. The presence of the option means that for to succeed, all required variables must be gathered first, but then termination must be achieved via the clause before all clauses are satisfied. The directive accepts the keyword parameter The argument to is a list of required and optional variables. A required variable is specified as a symbol. An optional variable is specified as a two element list which pairs a symbol with a Lisp expression. That Lisp expression is evaluated and specifies the default value for the variable. Example:
  @(gather :vars (a b c (d "foo")))
  ...
  @(end) Here, and are required variables, and is optional, with the default value given by the Lisp expression The presence of changes the behavior in three ways. Firstly, even if all the clauses in the match successfully and are eliminated, the directive will fail if the required variables do not have bindings. It doesn't matter whether the bindings are existing, or whether they are established by Secondly, if some of the clauses of did not match, but all of the required variables have bindings, then the directive succeeds. Without the presence of it would fail in this situation. Thirdly, if succeeds (all required variables have bindings), then all of the optional variables which do not have bindings are given bindings to their default values. The expressions which give the default values are evaluated whenever the directive is evaluated, whether or not their values are used. The syntax of the directive is:
  @(collect)
  ... lines of subquery
  @(end) or with an or clause:
  @(collect)
  ... lines of subquery: main clause
  @(until)
  ... lines of subquery: until clause
  @(end)
  @(collect)
  ... lines of subquery: main clause
  @(last)
  ... lines of subquery: last clause
  @(end) The symbol may be specified instead of which changes the meaning:
  @(repeat)
  ... lines of subquery
  @(end) The syntax is equivalent to and doesn't take the clause. It accepts other parameters. The subquery is matched repeatedly, starting at the current line. If it fails to match, it is tried starting at the subsequent line. If it matches successfully, it is tried at the line following the entire extent of matched data, if there is one. Thus, the collected regions do not overlap. (Overlapping behavior can be obtained: see the directive.) Unless certain keywords are specified, or unless the collection is explicitly failed with it always succeeds, even if it collects nothing, and even if the clause never finds a match. If no clause is specified, and the is not limited using parameters, the collection is unbounded: it consumes the entire data file. If an clause is specified, the collection stops when that clause matches at the current position. If an clause terminates no bindings are collected at that position, even if the main clause matches at that position also. Moreover, the position is not advanced. The remainder of the query begins matching at that position. If a clause terminates the behavior is different. Any bindings captured by the main clause are thrown away, just like with the clause. However, the bindings in the clause itself survive, and the position is advanced to skip over that material. Example:

code:
    @(collect)
     @a
     @(until)
     42
     @b
     @(end)
     @c

data:
    1
     2
     3
     42
     5
     6

result:
    a[0]="1"
     a[1]="2"
     a[2]="3"
     c="42"

The line is not collected, even though it matches Furthermore, the does not advance the position, so variable takes If the is changed to the output will be different:

result:
    a[0]="1"
     a[1]="2"
     a[2]="3"
     b="5"
     c="6"

The is not collected into a list, just like before. But now the binding captured by emerges. Furthermore, the position advances so variable now takes The binding variables within the clause of a are treated specially. The multiple matches for each variable are collected into lists, which then appear as array variables in the final output. Example:

code:
    @(collect)
     @a:@b:@c
     @(end)

data:
    John:Doe:101
     Mary:Jane:202
     Bob:Coder:313

result:
    a[0]="John"
     a[1]="Mary"
     a[2]="Bob"
     b[0]="Doe"
     b[1]="Jane"
     b[2]="Coder"
     c[0]="101"
     c[1]="202"
     c[2]="313"

The query matches the data in three places, so each variable becomes a list of three elements, reported as an array. Variables with list bindings may be referenced in a query. They denote a multiple match. The command-line option can establish a one-dimensional list binding. The clauses of may be nested. Variable matches collated into lists in an inner are again collated into nested lists in the outer Thus an unbound variable wrapped in N nestings of will be an N-dimensional list. A one-dimensional list is a list of strings; a two-dimensional list is a list of lists of strings, etc. It is important to note that the variables which are bound within the main clause of a that is, the variables which are subject to collection, appear, within the as normal one-value bindings. The collation into lists happens outside of the So for instance in the query:
 @(collect)
 @x=@x
 @(end) The left establishes a binding for some material preceding an equal sign. The right refers to that binding. The value of is different in each iteration, and these values are collected. What finally comes out of the clause is a single variable called which holds a list containing each value that was ever instantiated under that name within the clause. Also note that the clause has visibility over the bindings established in the main clause. This is true even in the terminating case when the clause matches, and the bindings of the main clause are discarded. By default, searches the rest of the input indefinitely, or until the clause matches. It skips arbitrary amounts of nonmatching material before the first match, and between matches. Within the syntax, it is possible to specify keyword parameters for additional control of the behavior. A keyword parameter consist of a keyword symbol followed by an argument, enclosed within the syntax. The following are the supported keywords. The keyword takes a numeric argument which is a Lisp expression. It causes to terminate if it fails to find a match after skipping lines from the starting position, or more than lines since any successful match. For example,
  @(collect :maxgap 5) specifies that the gap between the current position and the first match for the body of the or between consecutive matches can be no longer than five lines. A value of means that the collected regions must be adjacent and must match right from the starting position. For instance:
  @(collect :maxgap 0)
  M @a
  @(end) means: from here, collect consecutive lines of the form This will not search for the first such line, nor will it skip lines which do not match this form. The keyword complements though not exactly. Its argument a Lisp expression, specifies a minimum number of lines which must separate consecutive matches. However, it has no effect on the distance from the starting position to the first match. The keyword effectively specifies and at the same time, and can only be used if these other two are not used. Thus:
  @(collect :gap 1)
  @a
  @(end) means: collect every other line starting with the current line. This shorthand means the same thing as if were specified. This means that exactly matches must occur. If fewer occur, then fails. The stops once it achieves matches. The argument of the keyword is a Lisp expression which specifies that at least matches must occur, or else fails. The Lisp argument expression of the keyword specifies that at most matches are collected. The argument of the keyword parameter is a Lisp expression which specifies the upper bound on how many lines should be scanned by measuring from the starting position. The extent of the body is not counted. Example:
  @(collect :lines 2)
  foo: @a
  bar: @b
  baz: @c
  @(end) The above will look for a match only twice: at the current position, and one line down. The keyword specifies a restriction on what variables will emanate from the Its argument is a list of variable names. An empty list may be specified using empty parentheses or, equivalently, the symbol The element of the syntax is a Lisp expression. The behavior of the keyword is specified in the following section, "Specifying variables in The keyword indicates a list of variables. After the terminates, each in the list which does not have a binding is bound to the empty list symbol Unlike the mechanism doesn't assert that only the listed variables may emanate from the It also doesn't assert that each iteration of the must bind each of those variables. The keyword's argument is a variable name symbol, or a compound expression consisting of a variable name symbol and the TXR Lisp expression If this keyword argument is specified, then a binding for is established prior to each repetition of the body, to an integer value representing the repetition count. By default, repetition counts begin at zero. If is specified, it must evaluate to a number. This number is then added to each repetition count, and takes on the resulting displaced value. If there is an existing binding for prior to the processing of the then the variable is shadowed. The binding is collected in the same way as other bindings that are established in the body. The repetition count only increments after a successful match. The is visible to the clause. If that clause is being processed after a successful match of the body, then holds an integer value. If the body fails to match, then the clause sees a binding for with a value of

Normally, any variable for which a new binding occurs in a block is collected. A clause may be "sloppy": it can neglect to collect some variables on some iterations, or bind some variables which are intended to behave like local temporaries, but end up collated into lists. Another issue is that the clause might not match anything at all, and then none of the variables are bound. The keyword allows the query writer to add discipline the body. The argument to is a list of variable specs. A variable spec is either a symbol, denoting a required variable, or a pair, where is a Lisp expression whose value specifies a default value for the variable, which is optional. When a list is specified, it means that only the given variables can emerge from the successful Any newly introduced bindings for other variables do not propagate. More precisely, whenever the body matches successfully, the following three rules apply:

1.  If specifies required variables, the body must bind all of them, or else must not bind any variable at all, whether listed in or not, otherwise an exception of type is thrown.

2.  If specifies required variables, and also specifies default variables, and the body binds no variable at all, then the default variables are not bound to their default values.

3.  If specifies optional variables, and all required variables are bound by the body, then all those optional variables that are not bound by the body are bound to their default values. Under this rule, if specifies no required variables, that is deemed to be logically equivalent to all required variables being bound.

In the event that does not match anything, the variables specified in whether required or optional, are all bound to empty lists. These bindings are established after the processing of the clause, if present. Example:
  @(collect :vars (a b (c "foo")))
  @a @c
  @(end) Here, if the body matches, an error will be thrown because one of the mandatory variables is and the body neglects to produce a binding for Example:
  @(collect :vars (a (c "foo")))
  @a @b
  @(end) Here, if matches, only will be collected, but not because is not in the variable list. Furthermore, because there is no binding for in the body, a binding is created with the value exactly as if matched such a piece of text. In the following example, the assumption is that is not found anywhere in the input but the line is found and has a successor which is bound to Because the body did not match, the and should be bound to empty lists. But is bound by the last clause to some text, so this takes precedence. Only is bound to an empty list.
  @(collect :vars (a b))
  THIS NEVER MATCHES
  @(last)
  THIS DOES MATCH
  @a
  @(end) The following means: do not allow any variables to propagate out of any iteration of the and therefore collect nothing:
  @(collect :vars nil)
  ...
  @(end) Instead of writing it is possible to write takes all keywords, except for There is a directive used in clauses; that is a different directive. The clause supports the option keyword exemplified by the following:
  @(collect)
  ...
  @(last :mandatory)
  ...
  @(end) This means that the must be terminated by a match for the clause, or else by an explicit Specifically, the cannot terminate due to simply running out of data, or exceeding a limit on the number of matches that may be collected. In those situations, if an or clause is present with the is deemed to have failed. The directive is the horizontal version of Whereas works with multiline clauses on line-oriented material, works within a single line. With it is possible to recognize repeating regularities within a line and collect lists. Regular-expression-based Positive Match variables work well with Example: collect a comma-separated list, terminated by a space.

code:
    @(coll)@{A /[^, ]+/}@(until) @(end)@B

data:
    foo,bar,xyzzy blorch

result:
    A[0]="foo"
     A[1]="bar"
     A[2]="xyzzy"
     B=blorch

Here, the variable is bound to tokens which match the regular expression nonempty sequence of characters other than commas or spaces. Like searches for matches. If no match occurs at the current character position, it tries at the next character position. Whenever a match occurs, it continues at the character position which follows the last character of the match, if such a position exists. If not bounded by an until clause, it will exhaust the entire line. If the until clause matches, then the collection stops at that position, and any bindings from that iteration are discarded. Like collect, coll also supports an clause, which propagates variable bindings and advances the position. The keyword is supported. clauses nest, and variables bound within a coll are available to clauses within the rest of the clause, including the clause, and appear as single values. The final list aggregation is only visible after the clause. The behavior of leads to difficulties when a delimited variable are used to match material which is delimiter separated rather than terminated. For instance, entries in a comma-separated files usually do not appear as but rather So for instance, the following result is not satisfactory:

code:
    @(coll)@a @(end)

data:
    1 2 3 4 5

result:
    a[0]="1"
     a[1]="2"
     a[2]="3"
     a[3]="4"

The is missing because it isn't followed by a space, which the text-delimited variable match looks for. After matching "4 ", coll continues to look for matches, and doesn't find any. It is tempting to try to fix it like this:

code:
    @(coll)@a@/ ?/@(end)

data:
    1 2 3 4 5

result:
    a[0]=""
     a[1]=""
     a[2]=""
     a[3]=""
     a[4]=""
     a[5]=""
     a[6]=""
     a[7]=""
     a[8]=""

The problem now is that the regular expression (match either a space or nothing), matches at any position. So when it is used as a variable delimiter, it matches at the current position, which binds the empty string to the variable, the extent of the match being zero. In this situation, the directive proceeds character by character. The solution is to use positive matching: specify the regular expression which matches the item, rather than a trying to match whatever follows. The directive will recognize all items which match the regular expression:

code:
    @(coll)@{a /[^ ]+/}@(end)

data:
    1 2 3 4 5

result:
    a[0]="1"
     a[1]="2"
     a[2]="3"
     a[3]="4"
     a[4]="5"

The clause can specify a pattern which, when recognized, terminates the collection. So for instance, suppose that the list of items may or may not be terminated by a semicolon. We must exclude the semicolon from being a valid character inside an item, and add an until clause which recognizes a semicolon:

code:
    @(coll)@{a /[^ ;]+/}@(until);@(end);

data:
    1 2 3 4 5;

result:
    a[0]="1"
     a[1]="2"
     a[2]="3"
     a[3]="4"
     a[4]="5"

Whether followed by the semicolon or not, the items are collected properly. Note that the is followed by a semicolon. That's because when the clause meets a match, the matching material is not consumed. This repetition can be avoided by using instead of since consumes the terminating material. Instead of the above regular-expression-based approach, this extraction problem can also be solved with

code:
    @(coll)@(cases)@a @(or)@a@(end)@(end)

data:
    1 2 3 4 5

result:
    a[0]="1"
     a[1]="2"
     a[2]="3"
     a[3]="4"
     a[4]="5"

The directive takes most of the same parameters as See the section Keyword parameters in above. So for instance means that the collects must be consecutive, and means that at most two matches will be collected. The keyword does not exist, but there is an analogous keyword. The directive takes the keyword. The shorthand may be used instead of takes all keywords, except The directive can be used to convert variables to one-dimensional lists. Variables which have a scalar value are converted to lists containing that value. Variables which are multidimensional lists are flattened to one-dimensional lists. Example (without

code:
    @b
     @(collect)
     @(collect)
     @a
     @(end)
     @(end)

data:
    0
     1
     2
     3
     4
     5

result:
    b="0"
     a_0[0]="1"
     a_1[0]="2"
     a_2[0]="3"
     a_3[0]="4"
     a_4[0]="5"

Example (with

code:
    @b
     @(collect)
     @(collect)
     @a
     @(end)
     @(end)
     @(flatten a b)

data:
    0
     1
     2
     3
     4
     5

result:
    b="0"
     a[0]="1"
     a[1]="2"
     a[2]="3"
     a[3]="4"
     a[4]="5"

The syntax of follows the pattern: is a variable, which receives a new binding. are bind expressions. The directive provides a way of combining collected data from multiple nested lists in a way which normalizes different nesting levels among the sources. This directive is useful for combining the results from collects at different levels of nesting into a single nested list such that parallel elements are at equal depth. A new binding is created for the variable, which holds the result of the operation. The directive performs its special function if invoked with at least three arguments: a destination and two sources. The one-argument case binds a new variable and initializes it with the empty list and is thus equivalent to Likewise, the two-argument case is equivalent to establishing a binding for which is initialized with the value of To understand what merge does when two sources are given, as in we first have to define a property called depth. The depth of an atom such as a string is defined as The depth of an empty list is The depth of a nonempty list is one plus the depth of its deepest element. So for instance has depth 1, ("foo") has depth 2, and ("foo" ("bar")) has depth three. We can now define a binary (two argument) merge(A, B) function as follows. First, merge(A, B) normalizes the values A and B to produce a pair of values which have equal depth, as defined above. If either value is an atom it is first converted to a one-element list containing that atom. After this step, both values are lists; and the only way an argument has depth zero is if it is an empty list. Next, if either value has a smaller depth than the other, it is wrapped in a list as many times as needed to give it equal depth. For instance if A is and B is then A is converted to Finally, the list values are appended together to produce the merged result. In the case of the preceding two example values, the result is: The result is stored into a the newly bound destination variable If more than two source arguments are given, these are merged by a left-associative reduction, which is to say that a three argument is defined as The leftmost two values are merged, and then this result is merged with the third value, and so on. The directive converts a list variable into a single piece of text. The syntax is: The argument is a Lisp expression whose value specifies a separating piece of text. If it is omitted, then a single space is used as the separator. Example:

code:
    @(coll)@{a /[^ ]+/}@(end)
     @(cat a ":")

data:
    1 2 3 4 5

result:
    a="1:2:3:4:5"

The syntax of the directive is: The directive is a kind of pattern match, which matches one or more variables given in against a value produced by the on the right. Variable names occurring in the expression may refer to bound or unbound variables. All variable references occurring in must have a value. Binding occurs as follows. The tree structure of and the value of are considered to be parallel structures. Any variables in which are unbound receive a new binding, which is initialized with the structurally corresponding piece of the object produced by Any variables in which are already bound must match the corresponding part of the value of or else the directive fails. Variables which are already bound are not altered, retaining their current values even if the matching is inexact. The simplest is of one variable against itself, for instance binding against
  @(bind A A) This will throw an exception if is not bound. If is bound, it succeeds, since matches itself. The next simplest binds one variable to another:
  @(bind A B) Here, if is unbound, it takes on the same value as If is bound, it has to match or the fails. Matching means that either

-   and are the same text

-   is text, is a list, and occurs within

-   vice versa: is text, is a list, and occurs within

-   and are lists and are either identical, or one is found as a substructure within the other.

The right-hand side does not have to be a variable. It may be some other object, like a string, quasiliteral, regexp, or list of strings, etc. For instance,
  @(bind A "ab\tc") will bind the string to the variable if is unbound. If is bound, this will fail unless already contains an identical string. However, the right-hand side of a cannot be an unbound variable, nor a complex expression that contains unbound variables. The left-hand side of can be a nested list pattern containing variables. The last item of a list at any nesting level can be preceded by a (dot), which means that the variable matches the rest of the list from that position. Suppose that the list A contains ("now" "now" "brown" "cow"). Then the directive assuming that and are unbound variables, will bind to code N to and to the remainder of the list ("brown" "cow"). Example: suppose that the list is nested to two dimensions and contains (("how" "now") ("brown" "cow")). Then binds to to to and to The dot notation may be used at any nesting level. it must be followed by an item. The forms and are invalid, but is valid and equivalent to The number of items in a left pattern match must match the number of items in the corresponding right side object. So the pattern only matches an empty list. The notations and mean exactly the same thing. The symbols and keyword symbols may be used on either side. They represent themselves. For example fails, but succeeds since the two sides denote the same keyword symbol object. In this example, suppose contains and contains bar. Then binds to to and to This is because the produces the object ("foo" ("bar" "hey")) which is then structurally matched against the pattern and the variables receive the corresponding pieces. The directive accepts these keywords: The argument to is a filter specification. When the left side pattern contains a binding which is therefore matched against its counterpart from the right side expression, the left side is filtered through the filter specified by for the purposes of the comparison. For example:
  @(bind "a" "A" :lfilt :upcase) produces a match, since the left side is the same as the right after filtering through the :upcase filter. The argument to is a filter specification. The specified filter is applied to the right-hand-side material prior to matching it against the left side. The filter is not applied if the left side is a variable with no binding. It is only applied to determine a match. Binding takes place the unmodified right-hand-side object. For example, the following produces a match:
  @(bind "A" "a" :rfilt :upcase) This keyword is a shorthand to specify both filters to the same value. For instance is equivalent to For a description of filters, see Output Filtering below. Compound filters like are supported with all these keywords. The filters apply across arbitrary patterns and nested data. Example:
  @(bind (a b c) ("A" "B" "C"))
  @(bind (a b c) (("z" "a") "b" "c") :rfilt :upcase) Here, the first bind establishes the values for and and the second bind succeeds, because the value of a matches the second element of the list ("z" "a") if it is upcased, and likewise matches and matches if these are upcased. TXR Lisp forms, introduced by may be used in the argument of or as the entire form. This is consistent with the rules for bind expressions. TXR Lisp forms can be used in the expression also. Example:
  @(bind a @(+ 2 2))
  @(bind @(+ 2 2) @(* 2 2)) Here, is bound to the integer The second then succeeds because the forms and produce equal values. The syntax of the directive is: The directive syntactically resembles but is not a pattern match. It overwrites the previous values of variables with new values from the right-hand side. Each variable that is assigned must have an existing binding: will not induce binding. Examples follow. Store the value of back into an operation with no effect:
  @(set A A) Exchange the values of and
  @(set (A B) (B A)) Store a string into
  @(set A "text") Store a list into
  @(set A ("line1" "line2")) Destructuring assignment. ends up with ends up with ("B1" "B2") and binds to ("C1" "C2").
  @(bind D ("A" ("B1" "B2") "C1" "C2"))
  @(bind (A B C) (() () ()))
  @(set (A B . C) D) Note that does not support a TXR Lisp expression on the left side, so the following are invalid syntax:
  @(set @(+ 1 1) @(* 2 2))
  @(set @b @(list "a")) The second one is erroneous even though there is a variable on the left. Because it is preceded by the escape, it is a Lisp variable, and not a pattern variable. The directive also doesn't support Lisp expressions in the which must consist only of variables. The syntax of the directive is: The directive resembles It combines the semantics of and into a single directive. The is evaluated in the current environment, and its value remembered. Then a new environment is produced in which all the variables specified in are absent. Then, the pattern is newly bound in that environment against the previously produced value, as if using The old environment with the previous variables is not modified; it continues to exist. This is in contrast with the directive, which mutates existing bindings. makes it easy to create temporary bindings based on existing bindings.
  @(define pattern-function (arg))
  @;; inside a pattern function:
  @(rebind recursion-level @(+ recursion-level 1))
  @;; ...
  @(end) When the function terminates, the previous value of recursion-level is restored. The effect is less verbose and more efficient than the following equivalent
  @(define pattern-function (arg))
  @;; inside a pattern function:
  @(local temp)
  @(set temp recursion-level)
  @(local recursion-level)
  @(set recursion-level @(+ temp 1))
  @;; ...
  @(end) Like supports nested patterns, such as
  @(rebind (a (b c)) (1 (2 3)) but it does not support any keyword arguments. The filtering features of do not make sense in because the variables are always reintroduced into an environment in which they don't exist, whereas filtering applies in situations when bound variables are matched against values. The directive also doesn't support Lisp expressions in the which must consist only of variables. The has two spellings: and The arguments are one or more symbols, for example:
  @(forget a)
  @(local a b c) this can be written
  @(local a)
  @(local a b c) Directives which follow the or directive no longer see any bindings for the symbols mentioned in that directive, and can establish new bindings. It is not an error if the bindings do not exist. It is strongly recommended to use the spelling in functions, because the forgetting action simulates local variables: for the given symbols, the machine forgets any earlier variables from outside of the function, and consequently, any new bindings for those variables belong to the function. (Furthermore, functions suppress the propagation of variables that are not in their parameter list, so these locals will be automatically forgotten when the function terminates.) The syntax of is: The directive evaluates zero or more TXR Lisp expressions. (See TXR LISP far below.) The value of the expression is ignored, and matching continues with the directives which follow the directive, if any. In the context of the directive, the expression should not be introduced by the symbol; it is expected to be a Lisp expression. Example:
  @; match text into variables a and b, then insert into hash table h
  @(bind h @(hash))
  @a:@b
  @(do (set [h a] b)) The syntax of is: Like the directive, (macro-time evaluates zero or more TXR Lisp expressions. Unlike performs this evaluation immediately upon being parsed. Then it disappears from the syntax. The effect of is exactly like except that doesn't disappear from the syntax. Another difference is that can be used as a horizontal or vertical directive, whereas is only vertical. The directive shares the same syntax and semantics as the TXR Lisp macro of the same name: The directive is evaluated immediately upon being parsed, leaving no trace in the syntax tree of the surrounding TXR query. It causes the special variable to take on the package denoted by The directive that is either a string or symbol. An error exception is thrown if this isn't the case. Otherwise it searches for the package. If the package is not found, an error exception is thrown. Blocks are sections of a query which are either denoted by a name, or are anonymous. They may nest: blocks can occur within blocks and other constructs. Blocks are useful for terminating parts of a pattern-matching search prematurely, and escaping to a higher level. This makes blocks not only useful for simplifying the semantics of certain pattern matches, but also an optimization tool. Judicious use of blocks and escapes can reduce or eliminate the amount of backtracking that TXR performs. The directive introduces a named block, except when is the symbol The directive introduces an unnamed block, equivalent to The and directives introduce implicit anonymous blocks, as do function bodies. Blocks must be terminated by and can be vertical:
  ... or horizontal: The names of blocks are in a distinct namespace from the variable binding space. So is unrelated to the variable A block extends from the directive which introduces it, until the matching and may be empty. For instance:
  @(some)
  abc
  @(block foo)
  xyz
  @(end)
  @(end) Here, the block foo occurs in a clause, and so it extends to the which terminates the block. After that the name foo is not associated with a block (is not "in scope"). The second terminates the block. The implicit anonymous block introduced by has the same scope as the it extends over all of the material which follows the skip, to the end of the containing subquery. Blocks may nest, and nested blocks may have the same names as blocks in which they are nested. For instance:
  @(block)
  @(block)
  ...
  @(end)
  @(end) is a nesting of two anonymous blocks, and
  @(block foo)
  @(block foo)
  @(end)
  @(end) is a nesting of two named blocks which happen to have the same name. When a nested block has the same name as an outer block, it creates a block scope in which the outer block is "shadowed"; that is to say, directives which refer to that block name within the nested block refer to the inner block, and not to the outer one. A block normally does nothing. The query material in the block is evaluated normally. However, a block serves as a termination point for and directives which are in scope of that block and refer to it. The precise meaning of these directives is: Immediately terminate the enclosing query block called as if that block failed to match anything. If more than one block by that name encloses the directive, the innermost block is terminated. No bindings emerge from a failed block. Immediately terminate the innermost enclosing anonymous block, as if that block failed to match. The directive has a vertical and horizontal form. If the implicit block introduced by is terminated in this manner, this has the effect of causing itself to fail. In other words, the behavior is as if search did not find a match for the trailing material, except that it takes place prematurely (before the end of the available data source is reached). If the implicit block associated with a is terminated this way, then the entire fails. This is a special behavior, because a normally does not fail, even if it matches nothing and collects nothing! To prematurely terminate a by means of its anonymous block, without failing it, use Immediately terminate the enclosing query block called as if that block successfully matched. If more than one block by that name encloses the directive, the innermost block is terminated. Immediately terminate the innermost enclosing anonymous block, as if that block successfully matched. communicates the current bindings and input position to the terminated block. These bindings and current position may be altered by special interactions between certain directives and described in the following section. Communicating the current bindings and input position means that the block which is terminated by exhibits the bindings which were collected just prior to the execution of that and the input position which was in effect at that time. has a vertical and horizontal form. In the horizontal form, it communicates a horizontal input position. A horizontal input position thus communicated will only take effect if the block being terminated had been suspended on the same line of input. If the implicit block introduced by is terminated by this has the effect of causing the skip itself to succeed, as if all of the trailing material had successfully matched. If the implicit block associated with a is terminated by then the collection stops. All bindings collected in the current iteration of the collect are discarded. Bindings collected in previous iterations are retained, and collated into lists in accordance with the semantics of collect. Example: alternative way to achieve termination:
  @(collect)
  @ (maybe)
  ---
  @ (accept)
  @ (end)
  @LINE
  @(end) This query will collect entire lines into a list called However, if the line is matched (by the embedded the collection is terminated. Only the lines up to, and not including the line, are collected. The effect is identical to:
  @(collect)
  @LINE
  @(until)
  ---
  @(end) The difference (not relevant in these examples) is that the until clause has visibility into the bindings set up by the main clause. However, the following example has a different meaning:
  @(collect)
  @LINE
  @ (maybe)
  ---
  @ (accept)
  @ (end)
  @(end) Now, lines are collected until the end of the data source, or until a line is found which is followed by a line. If such a line is found, the collection stops, and that line is not included in the collection! The terminates the process of the collect body, and so the action of collecting the last binding into the list is not performed.

Example: communication of bindings and input position:

code:
    @(some)
     @(block foo)
     @first
     @(accept foo)
     @ignored
     @(end)
     @second

data:
    1
     2
     3

result:
    first="1"
     second="2"

At the point where the occurs, the foo block has matched the first line, bound the text to the variable The block is then terminated. Not only does the binding emerge from this terminated block, but what also emerges is that the block advanced the data past the first line to the second line. Next, the directive ends, and propagates the bindings and position. Thus the which follows then matches the second line and takes the text Example: abandonment of clause by In the following query, the foo block occurs inside a maybe clause. Inside the foo block there is a clause. Its first subclause matches variable and then terminates block foo. Since block foo is outside of the directive, this has the effect of terminating the clause:

code:
    @(maybe)
     @(block foo)
     @ (some)
     @first
     @ (accept foo)
     @ (or)
     @one
     @two
     @three
     @four
     @ (end)
     @(end)
     @second

data:
    1
     2
     3
     4
     5

result:
    first="1"
     second="2"

The second clause of the directive, namely:
  @one
  @two
  @three
  @four is never processed. The reason is that subclauses are processed in top to bottom order, but the processing was aborted within the first clause the The construct never gets the opportunity to match four lines. If the line is removed from the above query, the output is different:

code:
    @(maybe)
     @(block foo)
     @ (some)
     @first
     @# <-- @(accept foo) removed from here!!!
     @ (or)
     @one
     @two
     @three
     @four
     @ (end)
     @(end)
     @second

data:
    1
     2
     3
     4
     5

result:
    first="1"
     one="1"
     two="2"
     three="3"
     four="4"
     second="5"

Now, all clauses of the directive have the opportunity to match. The second clause grabs four lines, which is the longest match. And so, the next line of input available for matching is which goes to the variable. If one of the clauses which follow a requests a successful termination to an outer block via then intercepts the escape and adjusts the data extent to the position that it was given. Example:

code:
    @(block)
     @(trailer)
     @line1
     @line2
     @(accept)
     @(end)
     @line3

data:
    1
     2
     3

result:
    line1="1"
     line2="2"
     line3="1"

The variable is bound to because although yields a data position which has advanced to the third line, this is intercepted by and adjusted back to the first line. Neglecting to do this adjustment would violate the semantics of When the clauses under a directive are terminated by an such that control passes to a block which surrounds that the is intercepted by The input position being communicated by the is replaced with the original input position in the original stream which is in effect prior to the directive. The transfer is then resumed. In other words, cannot be used to "leak" the new stream out of a scope. However, has no effect on the bindings being communicated. Example: @(next "file-x")
 @(block b)
 @(next "file-y")
 @line
 @(accept b)
 @(end) Here, the variable matches the first line of the file after which an transfer is initiated, targeting block This transfer communicates the binding, as well as the position within pointing at the second line. However, the traverses the directive, causing it to be abandoned. The special unwinding action within that directive detects this transfer and rewrites the input position to be the original one within the stream associated with Note that this special handling exists in order for the behavior to be consistent with what would happen if the were removed, and the block terminated normally: because the inner is nested within that block, TXR would backtrack to the previous input position within If a pattern function is terminated due to the function return mechanism intercepts the The bindings being communicated by that are then subject to the special resolution with respect to the function parameters, exactly as if the bindings were being returned normally out of the function. The resolved bindings then replace those being communicated by the and the transfer is resumed. Example: @(define fun (a))
 @ (bind a "a")
 @ (bind b "b")
 @ (accept blk)
 @(end)
 @(block blk)
 @(fun x)
 this line is skipped by accept
 @(end) Here, the initiates a control transfer which communicates the and variable bindings which are visible in that scope. This transfer is intercepted by the function, and the treatment of the bindings follows to the same rules as a normal return (which, in the given function, would readily take place if the directive were removed). The variable is suppressed, because isn't a parameter of the function. Because is a parameter, and the argument to that parameter is the unbound variable the effect is that is bound to the value of When the accept transfer reaches block and terminates it, all that emerges is the binding carrying If the invocation is removed from then the function returns normally, producing the binding. In that case, the line isn't skipped since the block isn't being terminated; that line must match something. If the exception handling directive protected body is terminated by an transfer, and if that has a block, then there is a special interaction between the block and the transfer. The processing of the block detects that it has been triggered by an transfer. Consequently, it retrieves the current input position and bindings from that transfer, and uses that position and those bindings for the processing of the clauses. If the clauses succeed, then the new input position and new bindings are installed into the control transfer and that transfer resumes. If the clauses fail, then the transfer is converted to a with exactly the same block as its destination. The and directives comes in horizontal and vertical forms. This creates the possibility that an in horizontal context targets a vertical or vice versa, raising the question of how the input position is treated. The semantics of this is defined. If a horizontal-context targets a vertical block, the current position at the target block will be the following line. That is to say, when the horizontal occurs, there is a current input line which may have unconsumed material past the current position. If the communicates its input position to a vertical context, that unconsumed material is skipped, as if it had been matched and the vertical position is advanced to the next line. If a horizontal block catches a vertical accept, it rejects that position and stays at the current backtracking position for that block. Only the bindings from the are retained. It is possible for a horizontal to terminate in a horizontal block which is processing a different line of input (or even a different input stream). This situation is treated the same way as vertical accept terminating in a horizontal block: the position communicated by is ignored, and only the bindings are taken. TXR functions allow a query to be structured to avoid repetition. On a theoretical note, because TXR functions support recursion, functions enable TXR to match some kinds of patterns which exhibit self-embedding, or nesting, and thus cannot be matched by a regular language. Functions in TXR are not exactly like functions in mathematics or functional languages, and are not like procedures in imperative programming languages. They are not exactly like macros either. What it means for a TXR function to take arguments and produce a result is different from the conventional notion of a function. A TXR function may have one or more parameters. When such a function is invoked, an argument must be specified for each parameter. However, a special behavior is at play here. Namely, some or all of the argument expressions may be unbound variables. In that case, the corresponding parameters behave like unbound variables also. Thus TXR function calls can transmit the "unbound" state from argument to parameter. It should be mentioned that functions have access to all bindings that are visible in the caller; functions may refer to variables which are not mentioned in their parameter list. With regard to returning, TXR functions are also unconventional. If the function fails, then the function call is considered to have failed. The function call behaves like a kind of match; if the function fails, then the call is like a failed match. When a function call succeeds, then the bindings emanating from that function are processed specially. Firstly, any bindings for variables which do not correspond to one of the function's parameters are thrown away. Functions may internally bind arbitrary variables in order to get their job done, but only those variables which are named in the function argument list may propagate out of the function call. Thus, a function with no arguments can only indicate matching success or failure, but not produce any bindings. Secondly, variables do not propagate out of the function directly, but undergo a renaming. For each parameter which went into the function as an unbound variable (because its corresponding argument was an unbound variable), if that parameter now has a value, that value is bound onto the corresponding argument. Example:
  @(define collect-words (list))
  @(coll)@{list /[^ \t]+/}@(end)
  @(end) The above function contains a query which collects words from a line (sequences of characters other than space or tab), into the list variable called This variable is named in the parameter list of the function, therefore, its value, if it has one, is permitted to escape from the function call. Suppose the input data is:
  Fine summer day and the function is called like this:
  @(collect-words wordlist) The result (with is:
  wordlist[0]=Fine
  wordlist[1]=summer
  wordlist[1]=day How it works is that in the function call is an unbound variable. The parameter corresponding to that unbound variable is the parameter Therefore, that parameter is unbound over the body of the function. The function body collects the words of into the variable and then yields the that binding. Then the function call completes by noticing that the function parameter now has a binding, and that the corresponding argument has no binding. The binding is thus transferred to the variable. After that, the bindings produced by the function are thrown away. The only enduring effects are:

-   the function matched and consumed some input; and

-   the function succeeded; and

-   the variable now has a binding.

Another way to understand the parameter behavior is that function parameters behave like proxies which represent their arguments. If an argument is an established value, such as a character string or bound variable, the parameter is a proxy for that value and behaves just like that value. If an argument is an unbound variable, the function parameter acts as a proxy representing that unbound variable. The effect of binding the proxy is that the variable becomes bound, an effect which is settled when the function goes out of scope. Within the function, both the original variable and the proxy are visible simultaneously, and are independent. What if a function binds both of them? Suppose a function has a parameter called which is called with an argument which is an unbound variable, and then, in the function, both and bound. This is permitted, and they can even be bound to different values. However, when the function terminates, the local binding of A simply disappears (because the symbol is not among the parameters of the function). Only the value bound to emerges, and is bound to which still appears unbound at that point. The binding disappears also, and the net effect is that is now bound. The "proxy" binding of through the parameter "wins" the conflict with the direct binding. Function definition syntax comes in two flavors: vertical and horizontal. Horizontal definitions actually come in two forms, the distinction between which is hardly noticeable, and the need for which is made clear below. A function definition begins with a directive. For vertical functions, this is the only element in a line. The symbol must be followed by a symbol, which is the name of the function being defined. After the symbol, there is a parenthesized optional argument list. If there is no such list, or if the list is specified as or the symbol then the function has no parameters. Examples of valid syntax are:
  @(define foo)
  @(define bar ())
  @(define match (a b c)) If the directive is followed by more material on the same line, then it defines a horizontal function:
  @(define match-x)x@(end) If the define is the sole element in a line, then it is a vertical function, and the function definition continues below:
  @(define match-x)
  x
  @(end) The difference between the two is that a horizontal function matches characters within a line, whereas a vertical function matches lines within a stream. The former matches the character advancing to the next character position. The latter matches a line consisting of the character advancing to the next line. Material between and is the function body. The define directive may be followed directly by the directive, in which case the function has an empty body. Functions may be nested within function bodies. Such local functions have dynamic scope. They are visible in the function body in which they are defined, and in any functions invoked from that body. The body of a function is an anonymous block. (See Blocks above.) If a horizontal function is defined as the only element of a line, it may not be followed by additional material. The following construct is erroneous:
  @(define horiz (x))@foo:@bar@(end)lalala This kind of definition is actually considered to be in the vertical context, and like other directives that have special effects and that do not match anything, it does not consume a line of input. If the above syntax were allowed, it would mean that the line would not only define a function but also match This would, in turn, would mean that the is actually in horizontal mode, and so it matches a span of zero characters within a line (which means that is would require a line of input to match: a surprising behavior for a nonmatching directive!) A horizontal function can be defined in an actual horizontal context. This occurs if its is in a line where it is preceded by other material. For instance:
  X@(define fun)...@(end)Y This is a query line which must match the text It also defines the function The main use of this form is for nested horizontal functions:
  @(define fun)@(define local_fun)...@(end)@(end) A function of the same name may be defined as both vertical and horizontal. Both functions are available at the same time. Which one is used by a call is resolved by context. See the section Vertical Versus Horizontal Calls below. A function is invoked by compound directive whose first symbol is the name of that function. Additional elements in the directive are the arguments. Arguments may be symbols, or other objects like string and character literals, quasiliterals ore regular expressions. Example:

code:
    @(define pair (a b))
     @a @b
     @(end)
     @(pair first second)
     @(pair "ice" cream)

data:
    one two
     ice milk

result:
    first="one"
     second="two"
     cream="milk"

The first call to the function takes the line The parameter takes and parameter takes These are rebound to the arguments and The second call to the function binds the a parameter to the word and the is unbound, because the corresponding argument is unbound. Thus inside the function, is forced to match Then a space is matched and collects the text When the function returns, the unbound variable gets this value. If a symbol occurs multiple times in the argument list, it constrains both parameters to bind to the same value. That is to say, all parameters which, in the body of the function, bind a value, and which are all derived from the same argument symbol must bind to the same value. This is settled when the function terminates, not while it is matching. Example:

code:
    @(define pair (a b))
     @a @b
     @(end)
     @(pair same same)

data:
    one two

result:
    [query fails]

Here the query fails because and are effectively proxies for the same unbound variable and are bound to different values, creating a conflict which constitutes a match failure. A function call which is the only element of the query line in which it occurs is ambiguous. It can go either to a vertical function or to the horizontal one. If both are defined, then it goes to the vertical one. Example:

code:
    @(define which (x))@(bind x "horizontal")@(end)
     @(define which (x))
     @(bind x "vertical")
     @(end)
     @(which fun)

result:
    fun="vertical"

Not only does this call go to the vertical function, but it is in a vertical context. If only a horizontal function is defined, then that is the one which is called, even if the call is the only element in the line. This takes place in a horizontal character-matching context, which requires a line of input which can be traversed: Example:

code:
    @(define which (x))@(bind x "horizontal")@(end)
     @(which fun)

data:
    ABC

result:
    [query fails]

The query fails because since is in horizontal mode, it matches characters in a line. Since the function body consists only of which doesn't match any characters, the function call requires an empty line to match. The line is not empty, and so there is a matching failure. The following example corrects this: Example:

code:
    @(define which (x))@(bind x "horizontal")@(end)
     @(which fun)

data:
    [empty line]

result:
    fun="horizontal"

A call made in a clearly horizontal context will prefer the horizontal function, and only fall back on the vertical one if the horizontal one doesn't exist. (In this fallback case, the vertical function is called with empty data; it is useful for calling vertical functions which process arguments and produce values.) In the next example, the call is followed by trailing material, placing it in a horizontal context. Leading material will do the same thing: Example:

code:
    @(define which (x))@(bind x "horizontal")@(end)
     @(define which (x))
     @(bind x "vertical")
     @(end)
     @(which fun)B

data:
    B

result:
    fun="horizontal"

As described earlier, variables bound in a function body which are not parameters of the function are discarded when the function returns. However, that, by itself, doesn't make these variables local, because pattern functions have visibility to all variables in their calling environment. If a variable exists already when a function is called, then an attempt to bind it inside a function may result in a failure. The directive must be used in a pattern function to list which variables are local. Example:
  @(define path (path))@\
    @(local x y)@\
    @(cases)@\
      (@(path x))@(path y)@(bind path `(@x)@y`)@\
    @(or)@\
      @{x /[.,;'!?][^ \t\f\v]/}@(path y)@(bind path `@x@y`)@\
    @(or)@\
      @{x /[^ .,;'!?()\t\f\v]/}@(path y)@(bind path `@x@y`)@\
    @(or)@\
      @(bind path "")@\
    @(end)@\
  @(end) This is a horizontal function which matches a path, which lands into four recursive cases. A path can be parenthesized path followed by a path; it can be a certain character followed by a path, or it can be empty This function ensures that the variables it uses internally, and do not have anything to do with any inherited bindings for and Note that the function is recursive, which cannot work without and being local, even if no such bindings exist prior to the top-level invocation of the function. The invocation causes to be bound, which is visible inside the invocation but that invocation needs to have its own binding of for local use. Function definitions may appear in a function. Such definitions are visible in all functions which are invoked from the body (and not necessarily enclosed in the body). In other words, the scope is dynamic, not lexical. Inner definitions shadow outer definitions. This means that a caller can redirect the function calls that take place in a callee, by defining local functions which capture the references. Example:

code:
    @(define which)
     @ (fun)
     @(end)
     @(define fun)
     @ (output)
     top-level fun!
     @ (end)
     @(end)
     @(define callee)
     @ (define fun)
     @ (output)
     local fun!
     @ (end)
     @ (end)
     @ (which)
     @(end)
     @(callee)
     @(which)

output:
    local fun!
     top-level fun!

Here, the function is defined which calls A top-level definition of is introduced which outputs The function provides its own local definition of which outputs before calling When is invoked, it calls whose call is routed to callee's local definition. When is called directly from the top level, its call goes to the top-level definition. Function indirection may be performed using the directive. If is an Lisp expression which evaluates to a symbol, and that symbol names a function which takes no arguments, then
  @(call fun-expr) may be used to invoke the function. Additional expressions may be supplied which specify arguments. Example 1: @(define foo (arg))
  @(bind arg "abc")
  @(end)
  @(call 'foo b) In this example, the effect is that is invoked, and ends up bound to The directive here uses the expression to calculate the name of the function to be invoked. (See the operator). This particular expression can just be replaced by the direct invocation syntax The power of lies in being able to specify the function as a value which comes from elsewhere in the program, as in the following example. @(define foo (arg))
  @(bind arg "abc")
  @(end)
  @(bind f @'foo)
  @(call f b) Here the directive obtains the name of the function from the variable. Note that function names are resolved to functions in the environment that is apparent at the point in execution where the takes place. The directive is precisely equivalent to if, at the point of the call, is a variable which holds the symbol and symbol is defined as a function. Otherwise it is erroneous. The syntax of the and directives is: Where is a Lisp expression that evaluates to a string giving the path of the file to load. Firstly, the path given by is converted to an effective path, as follows. If the value of the variable has a current value which is not and the path given in is pure relative according to the function, then the effective path is interpreted taken relative to the directory portion of the path which is stored in If is or the load path is not pure relative, then the path is taken as-is as the effective path. Next, an attempt is made to open the file for processing, in almost exactly the same manner as by the TXR Lisp function The difference is that if the effective path is unsuffixed, then the suffix is added to it, and that resulting path is tried first, and if it succeeds, then the file is treated as TXR Pattern Language syntax. If that fails, then the suffix is tried, and so forth, as described for the function. If these initial attempts to find the file fail, and the failure is due to the file not being found rather than some other problem such as a permission error, and isn't an absolute path according to then additional attempts are made by searching for the file in the list of directories given in the variable. Details are given in the description of the TXR Lisp function. Both the and directives bind the variable to the path of the loaded file just before parsing syntax from it, The variable is also given a new dynamic binding, whose value is the same as the existing binding. These bindings are removed when the load operation completes, restoring the prior values of these variables. The variable is given a new dynamic binding, with a value. If the file opened for processing is TXR Lisp source, or a compiled TXR Lisp file, then it is processed in the manner described for the function. Different requirements apply to the processing of the file under the and directives. The directive performs the processing of the file at parse time. If the file being processed is TXR Pattern Language, then it is parsed, and then its syntax replaces the directive, as if it had originally appeared in its place. If a TXR Lisp source or a compiled TXR Lisp file is processed by then the directive is removed from the syntax. The directive performs the processing of the file at evaluation time. Evaluation time occurs after a TXR program is read from beginning to end and parsed. That is to say, when a TXR query is parsed, any embedded forms in it are parsed and constitute part of its syntax tree. They are executed when that query is executed, whenever its execution reaches those directives. When the directive processes TXR Pattern Language syntax, it parses the file in its entirety and then executes that file's directives against the current input position. Repeated executions of the same directive result in repeated processing of the file. Note: the directive is useful for loading TXR files which contain Lisp macros which are needed by the parent program. The parent program cannot use to bring in macros because macros are required during expansion, which takes place prior to evaluation time, whereas doesn't execute until evaluation time. Note: the directive doesn't provide access to the value propagated by a via the block. See also: the function, and the and variables in TXR Lisp. A TXR query may perform custom output. Output is performed by clauses, which may be embedded anywhere in the query, or placed at the end. Output occurs as a side effect of producing a part of a query which contains an directive, and is executed even if that part of the query ultimately fails to find a match. Thus output can be useful for debugging. An clause specifies that its output goes to a file, pipe, or (by default) standard output. If any output clause is executed whose destination is standard output, TXR makes a note of this, and later, just prior to termination, suppresses the usual printing of the variable bindings or the word false. The syntax of the directive is:
  .
  . one or more output directives or lines
  .
  @(end) If the directive has arguments, then the first one is evaluated. If it is an object other than a keyword symbol, then it specifies the optional Any remaining arguments after the optional destination are the keyword list. If the destination is missing, then the entire argument list is a keyword list. The argument, if present, is treated as a TXR Lisp expression and evaluated. The resulting value is taken as the output destination. The value may be a string which gives the pathname of a file to open for output. Otherwise, the destination must be a stream object. The keyword list consists of a mixture of Boolean keywords which do not have an argument, or keywords with arguments. The following Boolean keywords are supported: The directive throws an exception if the output destination cannot be opened, unless the keyword is present, in which case the situation is treated as a match failure. Note that since command pipes are processes that report errors asynchronously, a failing command will not throw an immediate exception that can be suppressed with This is for synchronous errors, like trying to open a destination file, but not having permissions, etc. This keyword is meaningful for files, specifying append mode: the output is to be added to the end of the file rather than overwriting the file.

The following value keywords are supported: The argument can be a symbol, which specifies a filter to be applied to the variable substitutions occurring within the clause. The argument can also be a list of filter symbols, which specifies that multiple filters are to be applied, in left-to-right order. See the later sections Output Filtering below, and The Deffilter Directive. The argument of is a symbol which denotes a variable. The output will go into that variable. If the variable is unbound, it will be created. Otherwise, its contents are overwritten unless the keyword is used. If is used, then the new content will be appended to the previous content of the variable, after flattening the content to a list, as if by the directive. The argument of is a symbol which denotes a variable. The file or pipe stream which is opened for the output is stored in this variable, and is not closed at the end of the output block. This allows a subsequent output block to continue output on the same stream, which is possible using the next two keywords, or A new binding is established for the variable, even if it already has an existing binding. A destination should not be specified if is used. The argument of is an expression, such as a variable name, that evaluates to a stream object. That stream object is used for the output block. At the end of the output block, the stream is flushed, but not closed. A usage example is given in the documentation for the Close Directive below. A destination should not be specified if is used. The argument of is an expression, such as a variable name, that evaluates to a stream object. That stream object is used for the output block. At the end of the output block, the stream is closed. An example is given in the documentation for the Close Directive below. The directive is a variant of which produces lines of text that are pushed back into the input stream. This directive supports only the keyword argument. This directive doesn't take any of the keyword arguments supported by except for the keyword. After the execution of a the next pattern matching syntax that is evaluated now faces the material produced by that followed by the original input. In order to preserve the line numbering of the original input, adjusts the line number for the synthetic input by subtracting the number of synthetic lines from the original input's line number. For instance if the original input is line 5, and 7 lines are prepended by then those lines are numbered -2 to 4. The input-synthesizing effect of is visible to a subsequent form in exactly those situations in which an input-consuming effect of a pattern matching directive would also be visible. For instance, a occurring in the body of a can produce input that is visible to the next iteration. The directive interacts with the parallel matching directives such as When multiple parallel clauses match, the input position is advanced by the longest match. Lines pushed into the input by look like negative advancement. If one clause advances in the input, while another one pushes into it, the push will lose to the advancement and its effect will disappear. If two clauses push varying amounts of material, the shorter push will win. Swap the first two lines if they start with a colon, changing the colon to a period:

Code:
      @(maybe)
      :@a
      :@b
      @ (push)
      .@b
      .@a
      @ (end)
      @(end)
      @(data capture)
      @(do (tprint capture))

Data:
      :hello
      :there
      rest
      of
      data

Output:
      .there
      .hello
      rest
      of
      data Text in an output clause is not matched against anything, but is output verbatim to the destination file, device or command pipe. Variables occurring in an output clause do not match anything; instead their contents are output. A variable being output can be any object. If it is of a type other than a list or string, it will be converted to a string as if by the function in TXR Lisp. A list is converted to a string in a special way: the elements are individually converted to a string and then they are catenated together. The default separator string is a single space: an alternate separation can be specified as an argument in the brace substitution syntax. Empty lists turn into an empty string. Lists may be output within or clauses. Each nesting of these constructs removes one level of nesting from the list variables that it contains. In an output clause, the variable syntax generates fixed-width field, which contains the variable's text. The absolute value of the number specifies the field width. For instance and both specify a field width of twenty. If the text is longer than the field, then it overflows the field. If the text is shorter than the field, then it is left-adjusted within that field, if the width is specified as a positive number, and right-adjusted if the width is specified as negative. An output variable may specify a filter which overrides any filter established for the output clause. The syntax for this is The filter specification syntax is the same as in the output clause. See Output Filtering below. Additional syntax is supported in output variables that does not appear in pattern-matching variables. A square bracket index notation may be used to extract elements or ranges from a variable, which works with strings, vectors and lists. Elements are indexed from zero. This notation is only available in brace-enclosed syntax, and looks like this: Extract the element at the position given by Extract a range of elements from the position given by up to one position less than the position given by If the variable is a list, it is treated as a list substitution, exactly as if it were the value of an unsubscripted list variable. The elements of the list are converted to strings and catenated together with a separator string between them, the default one being a single space. An alternate character may be given as a string argument in the brace notation.

Example:
  @(bind a ("a" "b" "c" "d"))
  @(output)
  @{a[1..3] "," 10}
  @(end) The above produces the text in a field spaces wide. The argument extracts a range of the argument specifies an alternate separator string, and specifies the field width. The brace syntax has another syntactic and semantic extension in clauses. In place of the symbol, an expression may appear. The value of that expression is substituted. Example:
 @(bind a "foo")
 @(output)
 @{`@a:` -10} Here, the quasiliteral expression is evaluated, producing the string This string is printed right-adjusted in a character field. The directive generates repeated text from a "boilerplate", by taking successive elements from lists. The syntax of repeat is like this:
  @(repeat)
  .
  .
  main clause material, required
  .
  .
  special clauses, optional
  .
  .
  @(end) has four types of special clauses, any of which may be specified with empty contents, or omitted entirely. They are described below. takes arguments, also described below. All of the material in the main clause and optional clauses is examined for the presence of variables. If none of the variables hold lists which contain at least one item, then no output is performed, (unless the repeat specifies an clause, see below). Otherwise, among those variables which contain nonempty lists, repeat finds the length of the longest list. This length of this list determines the number of repetitions, R. If the contains only a main clause, then the lines of this clause is output R times. Over the first repetition, all of the variables which, outside of the repeat, contain lists are locally rebound to just their first item. Over the second repetition, all of the list variables are bound to their second item, and so forth. Any variables which hold shorter lists than the longest list eventually end up with empty values over some repetitions. Example: if the list holds and the list holds and the variable holds then
  @(repeat)
  >> @C
  >> @A @B
  @(end) will produce three repetitions (since there are two lists, the longest of which has three items). The output is:
  >> X
  >> 1 A
  >> X
  >> 2 B
  >> X
  >> 3 The last line has a trailing space, since it is produced by where has an empty value. Since is not a list variable, it produces the same value in each repetition. The special clauses are: If the produces exactly one repetition, then the contents of this clause are processed for that one and only repetition, instead of the main clause or any other clause which would otherwise be processed. The body of this clause specifies an alternative body to be used for the first repetition, instead of the material from the main clause. The body of this clause is used instead of the main clause for the last repetition. If the repeat produces no repetitions, then the body of this clause is output. If this clause is absent or empty, the repeat produces no output. The forms and are Lisp expressions that evaluate to integers. The value of should be nonzero. The clause denoted this way is active if the repetition modulo is equal to The first repetition is numbered zero. For instance the clause headed by will be used on repetitions 0, 2, 4, 6, ... and will be used on repetitions 1, 3, 5, 7, ... The meaning of and is the same as in but one more condition is imposed. This clause is used if the repetition modulo is equal to and if it is the last repetition.

The precedence among the clauses which take an iteration is: That is, whenever two or more of these clauses can apply to a repetition, then the leftmost one in this precedence list will be selected. It is possible for all these clauses to be viable for processing the same repetition. If a occurs which has only one repetition, then that repetition is simultaneously the first, only and last repetition. Moreover, it also matches and, because it is the last repetition, it matches In this situation, if there is a clause present, then the repetition shall be processed using that clause. Otherwise, if there is a clause present, that clause is activated. Failing that, is used if there is such a clause, featuring an argument of zero. If there isn't, then the clause is considered, if present. Otherwise, the clause is considered if present with an argument of zero. Otherwise, none of these clauses are present or applicable, and the repetition is processed using the main clause. The clause does not appear in the above precedence list because it is mutually exclusive with respect to the others: it is processed only when there are no iterations, in which case even the main clause isn't active. The clause supports arguments. The argument designates a symbol which will behave as an integer variable over the scope of the clauses inside the repeat. The variable provides access to the repetition count, starting at zero, incrementing with each repetition. If the argument is given as then is a Lisp expression whose value is taken as a displacement value which is added to each iteration of the counter. For instance specifies a counter which counts from 1. The argument specifies a list of variable name symbols or else pairs of the form consisting of a variable name and Lisp expression. Historically, the former syntax informed about references to variables contained in Lisp code. This usage is no longer necessary as of TXR 243, since the construct walks Lisp code, identifying all free variables. The latter syntax introduces a new pattern variable binding for over the scope of the construct. The specifies a Lisp expression which is evaluated to produce the binding's value. The directive then processes the list of variables, selecting from it those which have a binding, either a previously existing binding or the one just introduced. For each selected variable, repeat will assume that the variable occurs in the repeat block and contains a list to be iterated. The variable binding syntax supported by of the form provides a solution for situations when it is necessary to iterate over some list, but that list is the result of an expression, and not stored in any variable. A repeat block iterates only over lists emanating from variables; it does not iterate over lists pulled from arbitrary expressions. Example: output all file names matching the pattern in the current directory:
  @(output)
  @(repeat :vars ((name (glob "*.txr"))))
  @name
  @(end)
  @(end) Prior to TXR 243, the simple variable-binding syntax supported by of the form was needed for situations in which TXR Lisp expressions which referenced variables were embedded in blocks. Variable references embedded in Lisp code were not identified in For instance, the following produced no output, because no variables were found in the body:
  @(bind trigraph ("abc" "def" "ghi"))
  @(output)
  @(repeat)
  @(reverse trigraph)
  @(end)
  @(end) There is a reference to but it's inside the Lisp expression that was not processed by The solution was to mention in the construct:
  @(bind trigraph ("abc" "def" "ghi"))
  @(output)
  @(repeat :vars (trigraph))
  @(reverse trigraph)
  @(end)
  @(end) Then the block would iterate over producing the output
  cba
  fed
  igh This workaround is no longer required as of TXR 243; the output is produced by the first example, without If a clause encloses variables which hold multidimensional lists, those lists require additional nesting levels of (or It is an error to attempt to output a list variable which has not been decimated into primary elements via a construct. Suppose that a variable is two-dimensional (contains a list of lists). must be nested twice in a The outer will traverse the lists contained in The inner will traverse the elements of each of these lists. A nested may be embedded in any of the clauses of a not only in the main clause. The directive is similar to Whereas is line-oriented, generates material within a line. It has all the same clauses, but everything is specified within one line:
  @(rep)... main material ... .... special clauses ...@(end) More than one can occur within a line, mixed with other material. A can be nested within a or within another Also, accepts the same and arguments. Example 1: show the list in parentheses, with spaces between the elements, or the word if the list is empty:
  @(output)
  @(rep)@L @(single)(@L)@(first)(@L @(last)@L)@(empty)EMPTY@(end)
  @(end) Here, the clause specifies So if there are no repetitions, the text is produced. If there is a single item in the list then produces that item between parentheses. Otherwise if there are two or more items, the first item is produced with a leading parenthesis followed by a space by and the last item is produced with a closing parenthesis: All items in between are emitted with a trailing space by the main clause: Example 2: show the list L like Example 1 above, but the empty list is
  @(output)
  (@(rep)@L @(last)@L@(end))
  @(end) This is simpler. The parentheses are part of the text which surrounds the construct, produced unconditionally. If the list is empty, then produces no output, resulting in If the list has one or more items, then they are produced with spaces each one, except the last which has no space. If the list has exactly one item, then the applies to it instead of the main clause: it is produced with no trailing space. The syntax of the directive is: Where evaluates to a stream. The directive can be used to explicitly close streams created using syntax, as an alternative to Examples: Write two lines to over two output blocks using a single stream:
  @(output "foo.txt" :named foo)
  Hello,
  @(end)
  @(output :continue foo)
  world!
  @(end)
  @(close foo) The same as above, using rather than so that the stream is closed at the end of the second block:
  @(output "foo.txt" :named foo)
  Hello,
  @(end)
  @(output :finish foo)
  world!
  @(end) Often it is necessary to transform the output to preserve its meaning under the convention of a given data format. For instance, if a piece of text contains the characters or then if that text is being substituted into HTML, these should be replaced by and This is what filtering is for. Filtering is applied to the contents of output variables, not to any template text. TXR implements named filters. Built-in filters are named by keywords, given below. User-defined filters are possible, however. See notes on the deffilter directive below. Instead of a filter name, the syntax can be used. This denotes that the function called is to be used as a filter. This is described in the next section Function Filters below. Built-in filters named by keywords: Filter text to HTML, representing special characters using HTML ampersand sequences. For instance is replaced by Filter text to HTML, representing special characters using HTML ampersand sequences. Unlike this filter doesn't treat the single and double quote characters. It is not suitable for preparing HTML fragments which end up inserted into HTML tag attributes. Filter text with HTML codes into text in which the codes are replaced by the corresponding characters. For instance is replaced by Convert the 26 lowercase letters of the English alphabet to uppercase. Convert the 26 uppercase letters of the English alphabet to lowercase. Decode percent-encoded text. Character triplets consisting of the character followed by a pair of hexadecimal digits (case insensitive) are are converted to bytes having the value represented by the hexadecimal digits (most significant nybble first). Sequences of one or more such bytes are treated as UTF-8 data and decoded to characters. Convert to percent encoding according to RFC 3986. The text is first converted to UTF-8 bytes. The bytes are then converted back to text as follows. Bytes in the range 0 to 32, and 127 to 255 (note: including the ASCII DEL), bytes whose values correspond to ASCII characters which are listed by RFC 3986 as being in the "reserved set", and the byte value corresponding to the ASCII character are encoded as a three-character sequence consisting of the character followed by two hexadecimal digits derived from the byte value (most significant nybble first, upper case). All other bytes are converted directly to characters of the same value without any such encoding. Decode from URL encoding, which is like percent encoding, except that if the unencoded character occurs, it is decoded to a space character. The sequence still decodes to space, and to the character. Encode to URL encoding, which is like percent encoding except that a space maps to rather than The character, being in the reserved set, encodes to Decode from the Base 64 encoding described in RFC 4648, section 5. Encode to the Base 64 encoding described in RFC 4648, section 5. Decode from the Base64 encoding described in RFC 4648, section 6. This uses the URL and filename safe alphabet, in which the (plus) and (slash) characters used in regular Base 64 are respectively replaced with (minus) and (underscore). Encode to the Base 64 encoding described in RFC 4648, section 6. See above. Converts strings to numbers. Strings that contain a period, or are converted to floating point as if by the Lisp function Otherwise they are converted to integer as if using with a radix of 10. Non-numeric junk results in the object Converts strings to integers as if using with a radix of 10. Non-numeric junk results in the object Converts strings to floating-point values as if using the function Non-numeric junk results in the object Converts strings to integers as if using with a radix of 16. Non-numeric junk results in the object

Examples: To escape HTML characters in all variable substitutions occurring in an output clause, specify in the directive:
  @(output :filter :tohtml)
  ...
  @(end) To filter an individual variable, add the syntax to the variable spec:
  @(output)
  @{x :filter :tohtml}
  @(end) Multiple filters can be applied at the same time. For instance:
  @(output)
  @{x :filter (:upcase :tohtml)}
  @(end) This will fold the contents of to uppercase, and then encode any special characters into HTML. Beware of combinations that do not make sense. For instance, suppose the original text is HTML, containing codes like The compound filter will not work because will turn to which no longer be recognized by the filter, since the entity names in HTML codes are case-sensitive. Capture some numeric variables and convert to numbers:
  @date @time @temperature @pressure
  @(filter :tofloat temperature pressure)
  @;; temperature and pressure can now be used in calculations A function can be used as a filter. For this to be possible, the function must conform to certain rules:

1.  The function must take two special arguments, which may be followed by additional arguments.

2.  When the function is called, the first argument will be bound to a string, and the second argument will be unbound. The function must produce a value by binding it to the second argument. If the filter is to be used as the final filter in a chain, it must produce a string.

For instance, the following is a valid filter function:
  @(define foo_to_bar (in out))
  @ (next :string in)
  @ (cases)
  foo
  @ (bind out "bar")
  @ (or)
  @ (bind out in)
  @ (end)
  @(end) This function binds the parameter to if the in parameter is otherwise it binds the parameter to a copy of the parameter. This is a simple filter. To use the filter, use the syntax in place of a filter name. For instance in the directive:
  @(bind "foo" "bar" :lfilt (:fun foo_to_bar)) The above should succeed since the left side is filtered from to so that there is a match. Function filters can be used in a chain:
  @(output :filter (:downcase (:fun foo_to_bar) :upcase))
  ...
  @(end) Here is a split function which takes an extra argument which specifies the separator:
  @(define split (in out sep))
  @ (next :list in)
  @ (coll)@(maybe)@token@sep@(or)@token@(end)@(end)
  @ (bind out token)
  @(end) Furthermore, note that it produces a list rather than a string. This function separates the argument in into tokens according to the separator text carried in the variable Here is another function, which catenates a list:
  @(define join (in out sep))
  @ (output :into out)
  @ (rep)@in@sep@(last)@in@(end)
  @ (end)
  @(end) Now here is these two being used in a chain:
  @(bind text "how,are,you")
  @(output :filter (:fun split ",") (:fun join "-"))
  @text
  @(end) Output:
  how-are-you When the filter invokes a function, it generates the first two arguments internally to pass in the input value and capture the output. The remaining arguments from the construct are also passed to the function. Thus the string objects and are passed as the argument to and Note that puts out a list, which accepts. So the overall filter chain operates on a string: a string goes into split, and a string comes out of join. The directive allows a query to define a custom filter, which can then be used in clauses to transform substituted data. The syntax of is illustrated in this example:

code:
    @(deffilter rot13
        ("a" "n")
        ("b" "o")
        ("c" "p")
        ("d" "q")
        ("e" "r")
        ("f" "s")
        ("g" "t")
        ("h" "u")
        ("i" "v")
        ("j" "w")
        ("k" "x")
        ("l" "y")
        ("m" "z")
        ("n" "a")
        ("o" "b")
        ("p" "c")
        ("q" "d")
        ("r" "e")
        ("s" "f")
        ("t" "g")
        ("u" "h")
        ("v" "i")
        ("w" "j")
        ("x" "k")
        ("y" "l")
        ("z" "m"))
     @(collect)
     @line
     @(end)
     @(output :filter rot13)
     @(repeat)
     @line
     @(end)
     @(end)

data:
    hey there!

output:
    url gurer!

The symbol must be followed by the name of the filter to be defined, followed by bind expressions which evaluate to lists of strings. Each list must be at least two elements long and specifies one or more texts which are mapped to a replacement text. For instance, the following specifies a telephone keypad mapping from uppercase letters to digits.
  @(deffilter alpha_to_phone ("E" "0")
                        ("J" "N" "Q" "1")
                        ("R" "W" "X" "2")
                        ("D" "S" "Y" "3")
                        ("F" "T" "4")
                        ("A" "M" "5")
                        ("C" "I" "V" "6")
                        ("B" "K" "U" "7")
                        ("L" "O" "P" "8")
                        ("G" "H" "Z" "9"))
  @(deffilter foo (`@a` `@b`) ("c" `->@d`))
  @(bind x ("from" "to"))
  @(bind y ("---" "+++"))
  @(deffilter sub x y) The last has the same effect as the @(deffilter sub ("from" "to") ("---" "+++")) directive. Filtering works using a longest match algorithm. The input is scanned from left to right, and the longest piece of text is identified at every character position which matches a string on the left-hand side, and that text is replaced with its associated replacement text. The scanning then continues at the first character after the matched text. If none of the strings matches at a given character position, then that character is passed through the filter untranslated, and the scan continues at the next character in the input. Filtering is not in-place but rather instantiates a new text, and so replacement text is not re-scanned for more replacements. If a filter definition accidentally contains two or more repetitions of the same left-hand string with different right-hand translations, the later ones take precedence. No warning is issued. The syntax of the directive is:
  @(filter FILTER { VAR }+ ) A filter is specified, followed by one or more variables whose values are filtered and stored back into each variable. Example: convert and to uppercase and HTML encode:
  @(filter (:upcase :tohtml) a b c) The exceptions mechanism in TXR is another disciplined form of nonlocal transfer, in addition to the blocks mechanism (see Blocks above). Like blocks, exceptions provide a construct which serves as the target for a dynamic exit. Both blocks and exceptions can be used to bail out of deep nesting when some condition occurs. However, exceptions provide more complexity. Exceptions are useful for error handling, and TXR in fact maps certain error situations to exception control transfers. However, exceptions are not inherently an error-handling mechanism; they are a structured dynamic control transfer mechanism, one of whose applications is error handling. An exception control transfer (simply called an exception) is always identified by a symbol, which is its type. Types are organized in a subtype-supertype hierarchy. For instance, the exception type is a subtype of the type. This means that a file error is a kind of error. An exception handling block which catches exceptions of type will catch exceptions of type but a block which catches will not catch all exceptions of type A is a kind of error, but not a kind of The symbol is the supertype of every type: every exception type is considered to be a kind of (Mnemonic: stands for type, as in any type). Exceptions are handled using clauses within a directive. In addition to being useful for exception handling, the directive also provides unwind protection by means of a clause, which specifies query material to be executed unconditionally when the clause terminates, no matter how it terminates. The general syntax of the directive is
  @(try)
  ... main clause, required ...
  ... optional catch clauses ...
  ... optional finally clause
  @(end) A clause looks like:
  @(catch TYPE [ PARAMETERS ])
  .
  .
  . and also this simple form:
  @(catch)
  .
  .
  . which catches all exceptions, and is equivalent to A clause looks like:
  @(finally)
  ...
  .
  . The main clause may not be empty, but the catch and finally may be. A try clause is surrounded by an implicit anonymous block (see Blocks section above). So for instance, the following is a no-op (an operation with no effect, other than successful execution):
  @(try)
  @(accept)
  @(end) The causes a successful termination of the implicit anonymous block. Execution resumes with query lines or directives which follow, if any. clauses and blocks interact. For instance, an from within a try clause invokes a

code:
    @(block foo)
     @ (try)
     @ (accept foo)
     @ (finally)
     @ (output)
     bye!
     @ (end)
     @ (end)

output:
    bye!

How this works: the block's main clause is This causes the enclosing block named to terminate, as a successful match. Since the is nested within this block, it too must terminate in order for the block to terminate. But the try has a clause, which executes unconditionally, no matter how the try block terminates. The clause performs some output, which is seen. Note that interacts with in subtle ways not revealed in this example; they are documented in the description of under the directive documentation. A directive can terminate in one of three ways. The main clause may match successfully, and possibly yield some new variable bindings. The main clause may fail to match. Or the main clause may be terminated by a nonlocal control transfer, like an exception being thrown or a block return (like the block foo example in the previous section). No matter how the clause terminates, the clause is processed. The clause is itself a query which binds variables, which leads to questions: what happens to such variables? What if the block fails as a query? As well as: what if a clause itself initiates a control transfer? Answers follow. Firstly, a clause will contribute variable bindings only if the main clause terminates normally (either as a successful or failed match). If the main clause of the block successfully matches, then the block continues matching at the next position in the data, and contributes bindings. If the main clause fails, then the block tries to match at the same position where the main clause failed. The overall directive succeeds as a match if either the main clause or the clause succeed. If both fail, then the directive is a failed match. Example:

code:
    @(try)
     @a
     @(finally)
     @b
     @(end)
     @c

data:
    1
     2
     3

result:
    a="1"
     b="2"
     c="3"

In this example, the main clause of the captures line of the data as variable then the finally clause captures as and then the query continues with the line after try block, so that captures Example:

code:
    @(try)
     hello @a
     @(finally)
     @b
     @(end)
     @c

data:
    1
     2

result:
    b="1"
     c="2"

In this example, the main clause of the fails to match, because the input is not prefixed with However, the clause matches, binding to This means that the try block is a successful match, and so processing continues with which captures When clauses are processed during a nonlocal return, they have no externally visible effect if they do not bind variables. However, their execution makes itself known if they perform side effects, such as output. A clause guards only the main clause and the clauses. It does not guard itself. Once the finally clause is executing, the block is no longer guarded. This means if a nonlocal transfer, such as a block accept or exception, is initiated within the finally clause, it will not re-execute the clause. The clause is simply abandoned. The disestablishment of blocks and clauses is properly interleaved with the execution of clauses. This means that all surrounding exit points are visible in a clause, even if the clause is being invoked as part of a transfer to a distant exit point. The finally clause can make a control transfer to an exit point which is more near than the original one, thereby "hijacking" the control transfer. Also, the anonymous block established by the directive is visible in the clause. Example:
  @(try)
  @ (try)
  @ (next "nonexistent-file")
  @ (finally)
  @ (accept)
  @ (end)
  @(catch file-error)
  @ (output)
  file error caught
  @ (end)
  @(end) In this example, the directive throws an exception of type because the given file does not exist. The exit point for this exception is the clause in the outermost block. The inner block is not eligible because it contains no catch clauses at all. However, the inner try block has a finally clause, and so during the processing of this exception which is headed for the clause performs an anonymous The exit point for that is the anonymous block surrounding the inner So the original transfer to the clause is thereby abandoned. The inner terminates successfully due to the and since it constitutes the main clause of the outer try, that also terminates successfully. The message is never printed. clauses establish their associated blocks as potential exit points for exception-induced control transfers (called "throws"). A clause specifies an optional list of symbols which represent the exception types which it catches. The clause will catch exceptions which are a subtype of any one of those exception types. If a block has more than one clause which can match a given exception, the first one will be invoked. When a is invoked, it is understood that the main clause did not terminate normally, and so the main clause could not have produced any bindings. clauses are processed prior to If a clause itself throws an exception, that exception cannot be caught by that same clause or its siblings in the same try block. The clauses of that block are no longer visible at that point. Nevertheless, the clauses are still protected by the finally block. If a catch clause throws, or otherwise terminates, the block is still processed. If a block throws an exception, then it is simply aborted; the remaining directives in that block are not processed. So the success or failure of the block depends on the behavior of the clause or the clause, if there is one. If either of them succeed, then the try block is considered a successful match. Example:

code:
    @(try)
     @ (next "nonexistent-file")
     @ x
     @ (catch file-error)
     @a
     @(finally)
     @b
     @(end)
     @c

data:
    1
     2
     3

result:
    a="1"
     b="2"
     c="3"

Here, the block's main clause is terminated abruptly by a exception from the directive. This is handled by the clause, which binds variable to the input line Then the clause executes, binding to The block then terminates successfully, and so takes A clause may have parameters following the type name, like this:
  @(catch pair (a b)) To write a catch-all with parameters, explicitly write the master supertype t:
  @(catch t (arg ...)) Parameters are useful in conjunction with The built-in exceptions carry one argument, which is a string containing the error message. Using arbitrary parameters can be passed from the throw site to the catch site. The directive generates an exception. A type must be specified, followed by optional arguments, which are bind expressions. For example,
  @(throw pair "a" `@file.txt`) throws an exception of type with two arguments, being and the expansion of the quasiliteral The selection of the target is performed purely using the type name; the parameters are not involved in the selection. Binding takes place between the arguments given in and the target If any parameter, for which a argument is given, is a bound variable, it has to be identical to the argument, otherwise the catch fails. (Control still passes to the but the catch is a failed match).

code:
    @(bind a "apple")
     @(try)
     @(throw e "banana")
     @(catch e (a))
     @(end)

result:
    [query fails]

If any argument is an unbound variable, the corresponding parameter in the is left alone: if it is an unbound variable, it remains unbound, and if it is bound, it stays as is.

code:
    @(try)
     @(throw e "honda" unbound)
     @(catch e (car1 car2))
     @car1 @car2
     @(end)

data:
    honda toyota

result:
    car1="honda"
     car2="toyota"

If a has fewer parameters than there are throw arguments, the excess arguments are ignored:

code:
    @(try)
     @(throw e "banana" "apple" "pear")
     @(catch e (fruit))
     @(end)

result:
    fruit="banana"

If a has more parameters than there are throw arguments, the excess parameters are left alone. They may be bound or unbound variables.

code:
    @(try)
     @(throw e "honda")
     @(catch e (car1 car2))
     @car1 @car2
     @(end)

data:
    honda toyota

result:
    car1="honda"
     car2="toyota"

A argument passing a value to a parameter which is unbound causes that parameter to be bound to that value. arguments are evaluated in the context of the and the bindings which are available there. Consideration of what parameters are bound is done in the context of the catch.

code:
    @(bind c "c")
     @(try)
     @(forget c)
     @(bind (a c) ("a" "lc"))
     @(throw e a c)
     @(catch e (b a))
     @(end)

result:
    c="c"
     b="a"
     a="lc"

In the above example, has a top-level binding to the string but then becomes unbound via within the construct, and rebound to the value Since the construct is terminated by a these modifications of the binding environment are discarded. Hence, at the end of the query, variable ends up bound to the original value The still takes place within the scope of the bindings set up by the clause, so the values of and that are thrown are and However, at the site, variable does not have a binding. At that point, the binding to established in the has disappeared already. Being unbound, the parameter can take whatever value the corresponding throw argument provides, so it ends up with There is a horizontal form of For instance:
  abc@(throw e 1) throws exception if matches. If is used to generate an exception derived from type and that exception is not handled, TXR will issue diagnostics on the stream and terminate. If an exception derived from is not handled, TXR will generate diagnostics on the stream, after which control returns to the directive, and proceeds with the next directive. If an exception not derived from is thrown, control returns to the directive and proceeds with the next directive. The directive allows the query writer to invent custom exception types, which are arranged in a type hierarchy (meaning that some exception types are considered subtypes of other types). Subtyping means that if an exception type is a subtype of then every exception of type is also considered to be of type So a catch for type will also catch exceptions of type Every type is a supertype of itself: an is a kind of This implies that every type is a subtype of itself also. Furthermore, every type is a subtype of the type which has no supertype other than itself. Type is a subtype of every type, including itself. The subtyping relationship is transitive also. If is a subtype of and is a subtype of then is a subtype of may be invoked with no arguments, in which case it does nothing:
  @(defex) It may be invoked with one argument, which must be a symbol. This introduces a new exception type. Strictly speaking, such an introduction is not necessary; any symbol may be used as an exception type without being introduced by
  @(defex a) Therefore, this also does nothing, other than document the intent to use a as an exception. If two or more argument symbols are given, the symbols are all introduced as types, engaged in a subtype-supertype relationship from left to right. That is to say, the first (leftmost) symbol is a subtype of the next one, which is a subtype of the next one and so on. The last symbol, if it had not been already defined as a subtype of some type, becomes a direct subtype of the master supertype Example:
  @(defex d e)
  @(defex a b c d) The first directive defines as a subtype of and as a subtype of The second defines as a subtype of as a subtype of and as a subtype of which is already defined as a subtype of Thus is now a subtype of The above can be condensed to:
  @(defex a b c d e) Example:

code:
    @(defex gorilla ape primate)
     @(defex monkey primate)
     @(defex human primate)
     @(collect)
     @(try)
     @(skip)
     @(cases)
     gorilla @name
     @(throw gorilla name)
     @(or)
     monkey @name
     @(throw monkey name)
     @(or)
     human @name
     @(throw human name)
     @(end)@#cases
     @(catch primate (name))
     @kind @name
     @(output)
     we have a primate @name of kind @kind
     @(end)@#output
     @(end)@#try
     @(end)@#collect

data:
    gorilla joe
     human bob
     monkey alice

output:
    we have a primate joe of kind gorilla
     we have a primate bob of kind human
     we have a primate alice of kind monkey

Exception types have a pervasive scope. Once a type relationship is introduced, it is visible everywhere. Moreover, the directive is destructive, meaning that the supertype of a type can be redefined. This is necessary so that something like the following works right:
  @(defex gorilla ape)
  @(defex ape primate) These directives are evaluated in sequence. So after the first one, the type has the type as its immediate supertype. But in the second directive, appears again, and is assigned the supertype, while retaining as a subtype. This situation could be diagnosed as an error, forcing the programmer to reorder the statements, but instead TXR obliges. However, there are limitations. It is an error to define a subtype-supertype relationship between two types if they are already connected by such a relationship, directly or transitively. So the following definitions are in error:
  @(defex a b)
  @(defex b c)
  @(defex a c)@# error: a is already a subtype of c, through b
  @(defex x y)
  @(defex y x)@# error: circularity; y is already a supertype of x. The directive requires the remaining query or subquery which follows it to match. If the remainder fails to match, the directive throws an exception. If the directive is simply
  @(assert) Then it throws an assertion of type assert, which is a subtype of error. The directive also takes arguments similar to the directive: an exception symbol and additional arguments which are bind expressions, and may be unbound variables. The following assert directive, if it triggers, will throw an exception of type with arguments and
  @(assert foo 1 "2") Example:
  @(collect)
  Important Header
  ----------------
  @(assert)
  Foo: @a, @b
  @(end) Without the assertion in places, if the part does not match, then the entire interior of the clause fails, and the collect continues searching for another match. With the assertion in place, if the text and its underline match, then the remainder of the collect body must match, otherwise an exception is thrown. Now the program will not silently skip over any Important Header sections due to a problem in its matching logic. This is particularly useful when the matching is varied with numerous cases, and they must all be handled. There is a horizontal directive also. For instance:
  abc@(assert)d@x asserts that if the prefix is matched, then it must be followed by a successful match for or else an exception is thrown. If the exception is not handled, and is derived from then TXR issues diagnostics on the stream and terminates. If the exception is derived from and not handled, TXR issues a diagnostic on after which control returns to the directive. Control silently returns to the directive if an exception of any other kind is not handled. When control returns to due to an unhandled exception, it behaves like a failed match, similarly to the require directive. The TXR language contains an embedded Lisp dialect called TXR Lisp. This language is exposed in TXR in a number of ways. In any situation that calls for an expression, a Lisp expression can be used, if it is preceded by the character. The Lisp expression is evaluated and its value becomes the value of that expression. Thus, TXR directives are embedded in literal text using and Lisp expressions are embedded in directives using also. Furthermore, certain directives evaluate Lisp expressions without requiring These are and TXR Lisp code can be placed into files. On the command line, TXR treats files with a or suffix as TXR Lisp source or compiled code, and the directive does also. TXR also provides an interactive listener for Lisp evaluation. Lastly, TXR Lisp expressions can be evaluated via the command line, using the and options. Examples: Bind variable to the integer 4:
  @(bind a @(+ 2 2)) Bind variable to the standard input stream. Note that is not required on a Lisp variable:
  @(bind a *stdin*) Define several Lisp functions inside
  @(do
    (defun add (x y) (+ x y))
    (defun occurs (item list)
      (cond ((null list) nil)
            ((atom list) (eql item list))
            (t (or (eq (first list) item)
                   (occurs item (rest list))))))) Trigger a failure unless previously bound variable is greater than 42:
  @(require (> (int-str answer) 42) TXR Lisp is a small and simple dialect, like Scheme, but much more similar to Common Lisp than Scheme. It has separate value and function binding namespaces, like Common Lisp (and thus is a Lisp-2 type dialect), and represents Boolean true and false with the symbols and (note the case sensitivity of identifiers denoting symbols!). Furthermore, the symbol is also the empty list, which terminates nonempty lists. TXR Lisp has lexically scoped local variables and dynamic global variables, similarly to Common Lisp, including the convention that marks symbols for dynamic binding in local scopes. Lexical closures are supported. TXR Lisp also supports global lexical variables via Functions are lexically scoped in TXR Lisp; they can be defined in the pervasive global environment using or in local scopes using and Much of the TXR Lisp syntax has been introduced in the previous sections of the manual, since directive forms are based on it. There is some additional syntax that is useful in TXR Lisp programming. The symbol tokens in TXR Lisp, called a (Lisp identifier) has a similar syntax to the (braced identifier) in the TXR pattern language. It may consist of all the same characters, as well as the (slash) character which may not be used in a Thus a may consist of these characters, in addition to letters, numbers and underscores:
 ! $ % & * + - < = > ? \ ~ / and may not look like a number. A may also include all of the Unicode characters which are permitted in a The one character which is allowed in a but not in a is (forward slash). A lone is a valid and consequently a symbol token in TXR Lisp. The token is also a symbol, and, unlike in a braced expression, is not a regular expression. In TXR Lisp expressions, regular expressions are written with a leading If a symbol name contains a colon, the lident characters, if any, before that colon constitute the package prefix. For example, the syntax denotes symbol in the package. It is a syntax error to read a symbol whose package doesn't exist. If the package exists, but the symbol name doesn't exist in that package, then the symbol is interned in that package. If the package name is an empty string (the colon is preceded by nothing), the package is understood to be the package. The symbol is interned in that package. The syntax denotes the symbol in the package, the same as Symbols in the keyword package are self-evaluating. This means that when a keyword symbol is evaluated as a form, the value of that form is the keyword symbol itself. Exactly two non-keyword symbols also have this special self-evaluating behavior: the symbols and in the user package, whose fully qualified names are and The syntax denotes the meta prefix being applied to the symbol, not to a symbol in the package. The syntax denotes an uninterned symbol named described in the next section. In ANSI Common Lisp, the syntax does not intern the symbol in the package; the symbol must exist and be an exported symbol, or else the syntax is erroneous. In ANSI Common Lisp, the syntax does intern in the package. TXR's package system has no double-colon syntax, and lacks the concept of exported symbols. Uninterned symbols are written with the prefix, followed by zero or more lident characters. When an uninterned symbol is read, a new, unique symbol is constructed, with the specified name. Even if two uninterned symbols have the same name, they are different objects. The and functions produce uninterned symbols. "Uninterned" means "not entered into a package". Interning refers to a process which combines package lookup with symbol creation, which ensures that multiple occurrences of a symbol name in written syntax are all converted to the same object: the first occurrence creates the symbol and associates it with its name in a package. Subsequent occurrences do not create a new symbol, but retrieve the existing one. An expression may be preceded by the (at sign) character. If the expression is an then this is a meta-atom, otherwise it is a meta-expression. When the atom is a symbol, this is also called a meta-symbol and in situations when such a symbol behaves like a variable, it is also referred to as a meta-variable. When the atom is an integer, the meta-atom expression is called a meta-number. Meta-atom and meta-expression expressions have no evaluation semantics; evaluating them throws an exception. They play a syntactic role in the operator, which makes use of meta-variables and meta-numbers, and in structural pattern matching, which uses meta-variables as pattern variables and whose operator vocabulary is based on meta-expressions. Meta-expressions also appear in the quasiliteral notation. In other situations, application code may assign meaning to meta syntax as the programmer sees fit. Meta syntax is defined as a shorthand notation, as follows: If is the syntax of an atom, such as a symbol, string or vector, then is a shorthand for the expression Here, refers to the symbol in the If is a compound expression, either or then is a shorthand for the expression The behavior of followed by the syntax of a floating-point constant introduced by a leading decimal point, not preceded by digits, is unspecified. Examples of this are and The behavior of followed by the syntax of a floating-point expression in E notation, which lacks a decimal point, is also unspecified. An example of this is It is a syntax error for to be followed by what appears to be a floating-point constant consisting of a decimal point flanked by digits on both sides. For instance is rejected. A meta-expression followed by a period, and the syntax of another object is otherwise interpreted as a referencing dot expression. For instance denotes which, in turn, denotes even though the unprefixed character sequence is otherwise a floating-point constant. Unlike other major Lisp dialects, TXR Lisp allows a consing dot with no forms preceding it. This construct simply denotes the form which follows the dot. That is to say, the parser implements the following transformation:
  (. expr) -> expr This is convenient in writing function argument lists that only take variable arguments. Instead of the syntax:
  (defun fun args ...) the following syntax can be used:
  (defun fun (. args) ...) When a form is printed, it is printed in the following style.
  (lambda nil ...) -> (lambda () ...)
  (lambda sym ...) -> (lambda (. sym) ...)
  (lambda (sym) ...) -> (lambda (sym) ...) In no other circumstances is printed as or an atom as This notation is implemented for the square brackets, according to this transformation:
  [. expr] -> (dwim . expr) This is useful in Structural Pattern Matching, allowing a pattern like
  [. @args] to match a expression and capture all of its arguments in a variable, without having to resort to the internal notation: Compatibility Note: support for was introduced in TXR 282. Older versions do not read the syntax, but do print as which is then unreadable in those versions, breaking read-print consistency. A dot token which is flanked by expressions on both sides, without any intervening whitespace, is the referencing dot, and not the consing dot. The referencing dot is a syntactic sugar which translated to the syntax ("quoted ref"). When evaluated as a form, this syntax denotes structure access; see Structures. However, it is possible to put this syntax to use for other purposes, in other contexts.
  ;; a.b may be almost any expressions
  a.b <--> (qref a b)
  a.b.c <--> (qref a b c)
  a.(qref b c) <--> (qref a b c)
  (qref a b).c <--> (qref (qref a b) c) That is to say, this dot operator constructs a expression out of its left and right arguments. If the right argument of the dot is already a qref expression (whether produced by another instance of the dot operator, or expressed directly) it is merged. This requires the qref dot operator to be right-to-left associative, so that works by first translating to and then adjoining to produce If the referencing dot is immediately followed by a question mark, it forms a single token, which produces the following syntactic variation, in which the following item is annotated as a list headed by the symbol
  a.?b <--> (t a).b <--> (qref (t a) b)
  a.?b.?c <--> (t a).(t b).c <--> (qref (t a) (t b) c)
  a.?(b) <--> (t a).(b) <--> (qref (t a) (b))
  (a).?b <--> (t (a)).b <--> (qref (t (a)) b) This syntax denotes null-safe access to structure slots and methods. means that may evaluate to in which case the expression yields otherwise, must evaluate to a which has a slot and the expression denotes access to that slot. Similarly, means that if evaluates to the expression yields otherwise, is treated as a struct object whose method is invoked with argument and the value returned by that method becomes the value of the expression. Integer tokens cannot be involved in this syntax, because they form floating-point constants when juxtaposed with a dot. Such ambiguous uses of floating-point tokens are diagnosed as syntax errors:
  (a.4) ;; error: cramped floating-point literal
  (a .4) ;; good: a followed by 0.4 Closely related to the referencing dot syntax is the unbound referencing dot. This is a dot which is flanked by an expression on the right, without any intervening whitespace, but is not preceded by an expression Rather, it is preceded by whitespace, or some punctuation such as or This is a syntactic sugar which translates to syntax:
  .a <--> (uref a)
  .a.b <--> (uref a b)
  .a.?b <--> (uref (t a) b) If the unbound referencing dot is itself combined with a question mark to form the token, then the translation to is as follows:
  .?a <--> (uref t a)
  .?a.b <--> (uref t a b)
  .?a.?b <--> (uref t a (t b)) When the unbound referencing dot is applied to a dotted expression, this can be understood as a conversion of to Indeed, this is exactly what happens if the unbound dot is applied to an explicit expression:
  .(qref a b) <--> (uref a b) The unbound referencing dot takes its name from the semantics of the macro, which produces a function that implements late binding of an object to a method slot. Whereas the expression denotes accessing object to retrieve slot and then accessing slot of the object from that slot, the expression represents a "disembodied" reference: it produces a function which takes an object as an argument and then performs the implied slot referencing on that argument. When the function is called, it is said to bind the referencing to the object. Hence that referencing is "unbound". Whereas the expression produces a function whose argument must be an object, produces a function whose argument may be The function detects this case and returns
    The quote character in front of an expression is used for suppressing evaluation, which is useful for forms that evaluate to something other than themselves. For instance if is evaluated, the value is the three-element list whereas if is evaluated, the value is Similarly, the value of is the symbol itself, whereas the value of is the contents of the variable The caret in front of an expression is a quasiquote. A quasiquote is like a quote, but with the possibility of substitution of material. Under a quasiquote, form is considered to be a quasiquote template. The template is considered to be a literal structure, except that it may contain the notations and which denote non-constant parts. A quasiquote gets translated into code which, when evaluated, constructs the structure implied by taking into account the unquotes and splices. A quasiquote also processes nested quasiquotes specially. If does not contain any unquotes or splices (which match its level of nesting), or is simply an atom, then is equivalent to in other words, it is like an ordinary quote. For instance is equivalent to Although there is an unquote ,d it belongs to the inner quasiquote and the outer quasiquote does not have any unquotes of its own, making it equivalent to a quote. Dialect Note: in Common Lisp and Scheme, is written and quasiquotes are also informally known as backquotes. In TXR, the backquote character used for quasistring literals. The comma character is used within a to denote an unquote. Whereas the quasiquote suppresses evaluation, similarly to the quote, the comma introduces an exception: an element of a form which is evaluated. For example, list is the list Everything in the quasiquote stands for itself, except for the which is evaluated. Note: if a variable is called then the syntax means splice the value of In this situation, whitespace between the comma and the variable name must be used: The comma-star operator is used within quasiquote list to denote a splicing unquote. The form which follows must evaluate to a list. That list is spliced into the structure which the quasiquote denotes. For example: evaluates to The expression is evaluated to produce the list and this list is spliced into the quoted template. This syntax is not a distinct quasiquoting operator, but rather the combination of an unquote occurring as a meta-expression, denoting the structure This structure is treated specially by the quasiquote expander. Code is generated for it such that if evaluates to a value which is an then the result will be the structure. If is a rather than an then the result is the structure. In other words, when quasiquoting is used to insert a value under the meta prefix, the expander generates code to analyze the type of the value, and produce to the form which is most likely intended.
In other Lisp dialects, like Scheme and ANSI Common Lisp, the equivalent syntax is usually (comma at). The character already has an assigned meaning in TXR, so is used. However, is also a character that may appear in a symbol name, which creates a potential for ambiguity. The syntax denotes the application of the splicing operator to the symbolic expression to apply the ordinary non-splicing unquote to the symbol whitespace must be used: In TXR, the unquoting and splicing forms may freely appear outside of a quasiquote template. If they are evaluated as forms, however, they throw an exception:
   ,(+ 2 2) ;; error!
   ',(+ 2 2) --> ,(+ 2 2) In other Lisp dialects, a comma not enclosed by backquote syntax is treated as a syntax error by the reader. TXR's quasiquote supports splicing multiple items into a if that quote is itself evaluated via an unquote. Concretely, these two examples produce the same result:
  (eval
    (eval
      (let ((args '(a b c)))
        ^^(let ((a 1) (b 2) (c 3))
          (list ,',*args)))))
  -> (1 2 3)
  (eval
    (eval
      (let ((args '(a b c)))
        ^^(let ((a 1) (b 2) (c 3))
          (list ,*',args)))))
  -> (1 2 3) The only difference is that the former example uses whereas the latter Thus the former example splices into the quote as if by which is invalid syntax if doesn't expand to exactly one element. This invalid quote syntax is accepted by the quasiquote expander when it occurs in the above unquoting and splicing situation. Effectively, it behaves as if the splice distributes across the quoted unquote, such that all the arguments of the end up individually quoted, and spliced into the surrounding list. The Common Lisp equivalent this combination, works in some Common Lisp implementations, such as CLISP. Quasiquoting is supported over hash table and vector literals (see Vectors and Hashes below). A hash table or vector literal can be quoted, like any object, for instance:
  '#(1 2 3) The literal is turned into a vector atom right in the TXR parser, and this atom is being quoted: this is syntactically, which evaluates to When a vector is quasi-quoted, this is a case of which evaluates to A vector can be quasiquoted, for example:
  ^#(1 2 3) Unquotes can occur within a quasiquoted vector:
  (let ((a 42))
    ^#(1 ,a 3)) ; value is #(1 42 3) In this situation, the notation produces code which constructs a vector. The vector in the following example is also a quasivector. It contains unquotes, and though the quasiquote is not directly applied to it, it is embedded in a quasiquote:
  (let ((a 42))
    ^(a b c #(d ,a))) ; value is (a b c #(d 42)) Hash-table literals have two parts: the list of hash construction arguments and the key-value pairs. For instance:
   #H((:eql-based) (a 1) (b 2)) where indicates that this hash table's keys are treated using equality, and and are the key/value entries. Hash literals may be quasiquoted. In quasiquoting, the arguments and pairs are treated as separate syntax; it is not one big list. So the following is not a possible way to express the above hash:
  ;; not supported: splicing across the entire syntax
  (let ((hash-syntax '((:eql-based) (a 1) (b 2))))
    ^#H(,*hash-syntax)) This is correct:
  ;; fine: splicing hash arguments and contents separately
  (let ((hash-args '(:eql-based))
        (hash-contents '((a 1) (b 2))))
    ^#H(,hash-args ,*hash-contents)) When a quasiliteral is embedded in a quasiquote, it is possible to use splicing to insert material into the quasiliteral. Example:
  (eval (let ((a 3)) ^`abc @,a @{,a} @{(list 1 2 ,a)}`))
  -> "abc 3 3 1 2 3" A hash token followed by a list denotes a vector. For example is a three-element vector containing the numbers and and the symbol The notation followed by a nested list syntax denotes a struct literal. The first item in the syntax is a symbol denoting the struct type name. This must be the name of a struct type, otherwise the literal is erroneous. Followed by the struct type are slot names interleaved with their values. The values are literal expressions, not subject to evaluation. Each slot name which is present in the literal must name a slot in the struct type, though not all slots in the struct type must be present in the literal. When a struct literal is read, the denoted struct type is constructed as if by a call to with an empty argument, followed by a sequence of assignments which store into each the corresponding expression. The notation followed by list syntax denotes a hash-table literal. The first item in the syntax is a list of keywords. These are the same keywords as are used when calling the function hash to construct a hash table. Allowed keywords are: and If the keyword is present, it must be followed by an object; that object specifies the hash table's user data, which can be retrieved using the function. The and keywords are mutually exclusive. An empty list can be specified as or which defaults to a hash table based on the function, with no weak semantics or user data. The entire syntax following may be an empty list; however, that empty list may not be specified as the empty parentheses notation is required. The hash table's key-value contents are specified as zero or more two-element lists, whose first element specifies the and whose second specifies the Both expressions are literal objects, not subject to evaluation. The notation followed by a two-element list syntax denotes a range literal. It combines and expressions, themselves literals not subject to evaluation, producing the range object whose corresponding and fields are the objects denoted by these expressions. The notation introduces a buffer object: a data representation for a block of bytes. This prefix must be followed by a data section and a closing quote. The data section consists of hexadecimal digits, among which may be interspersed whitespace: tabs, spaces and newlines. There must be an even number of digits, or else the notation is ill-formed. The whitespace is ignored, and pairs of successive hex digits specify bytes. If there are no hex digits, then a zero length buffer is specified. Buffers may be constructed by the function, and other means such as the function. Note that the prefix is also used for binary numbers. In that syntax, it is followed by an optional sign, and then a mixture of one or more of the digits or The notation followed by list syntax denotes a tree node literal. The list syntax must be a proper list that has up to three elements. If the list is empty, it may not be written as A tree node is an object of type Every has three elements: a a link and a link. They may be objects of any type. If the tree node literal syntax omits any of these, they default to The notation followed by list syntax denotes a tree literal, which specifies an object of type Objects of type are search trees. The list syntax which follows may be empty. If so, it cannot be written as The first element of the syntax, if present, must be a list of zero to three elements. These elements are symbols giving the names of the object's key abstraction functions. specifies the key function which is applied to each element to retrieve its key. If it is omitted, the object shall use the function as its key. The specifies the name of the comparison function by which keys are compared for inequality. It defaults to The specifies the function by which keys are compared for equality. It defaults to A symbol which is specified as the name of any of these three special functions must be an element of the list stored in the special variable otherwise the string literal is diagnosed as erroneous. Note: this is due to security considerations, since these three functions are executed during the processing of tree syntax. A tree object is constructed from a tree literal by first creating an empty tree endowed with the three key abstraction functions that are indicated in the syntax, either explicitly or as defaults. Then, every object is constructed from its respective literal syntax and inserted into the tree. Duplicate objects are preserved. For instance the tree literal specifies a tree with three nodes which have the same key. Duplicates appear in the tree in the order that they appear in the literal. Introduces a JSON literal. Introduces a JSON quasiliteral, allowing unquoting and splicing of Lisp expressions. The implementation of JSON syntax is based on, and intended to conform with the IETF RFC 8259 document. Only TXR's extensions to JSON syntax are described in this manual, as well as the correspondence between JSON syntax and Lisp. The is translated into a TXR Lisp object as follows. A JSON string corresponds to a Lisp string. A JSON number corresponds to a Lisp floating-point number. A JSON array corresponds to a Lisp vector. A JSON object corresponds to an hash table. The JSON Boolean symbols and translate to the Lisp symbols and respectively, those being the standard ones in the package. The JSON symbol maps to the symbol in the package. The expression produces the object: where is the Lisp value which corresponds to the Similarly, but with a key difference, the expression produces the object: in which has been replaced with The symbol is bound as a macro, which is expanded when a expression is evaluated. The following remarks indicate special treatment and extensions in the processing of JSON. Similar remarks regarding the production of JSON are given under the function. When an invalid UTF-8 byte is encountered inside a JSON string, its value is mapped into the code point range U+DC01 to U+DCFF. That byte is consumed, and decoding continues with the next byte. This treatment is consistent with the treatment of invalid UTF-8 bytes in TXR Lisp literals and I/O streams. If the valid UTF-8 byte U+0000 (ASCII NUL) occurs in a JSON string, it is also mapped to U+DC00, TXR's pseudo-null character. This treatment is consistent with TXR string literals and I/O streams. The JSON escape sequence denoting the U+0000 NUL character is also converted to U+DC00. TXR Lisp does not impose the restriction that the keys in a JSON object must be strings: is accepted. TXR Lisp allows the circle notation to occur within JSON syntax. See the section Notation for Circular and Shared Structure. TXR Lisp supports the extension of Lisp comments in JSON. When the character (semicolon) occurs in the middle of JSON syntax, outside of a token, that character and all characters until the end of the line constitute a comment that is discarded. TXR Lisp never produces comments when printing JSON. TXR Lisp allows for JSON syntax to be quasiquoted, and provides two extensions for writing unquotes and splicing unquotes. Within a JSON quasiquote, the (tilde) character introduces a Lisp expression whose value is to be substituted at that point. Thus, the tilde serves the role of the unquoting comma used in Lisp quasiquotes. Splicing is indicated by the character sequence which introduces a Lisp expression that is expected to produce a list, whose elements are interpolated into the JSON value. Note: quasiquoting allows Lisp values to be introduced into the resulting object which are outside of the JSON type system, such as integers, characters, symbols or structures. These objects have no representation in JSON syntax.
  ;; Basic JSON:
  #Jtrue -> t
  #Jfalse -> nil
  (list #J true #Jtrue #Jfalse) -> (t t nil)
  #J[1, 2, 3.14] -> #(1.0 2.0 3.14)
  #J{"foo":"bar"} -> #H(() ("foo" "bar"))
  ;; Quoting JSON shows the json expression
  '#Jfalse -> (json quote ())
  '#Jtrue -> (json quote t)
  '#J["a", true, 3.0] -> (json quote #("a" t 3.0))
  '#J^[~(+ 2 2), 3] -> (json sys:qquote #(,(+ 2 2) 3.0))
  :; Circle notation:
  #J[#1="abc", #1#, #1#] -> #("abc" "abc" "abc")
  ;; JSON Quasiquote:
  #J^[~*(list 1.0 2.0 3.0), ~(* 2.0 2), 5.0]
  --> #(1.0 2.0 3.0 4.0 5.0)
  ;; Lisp quasiquote around JSON quote: requires evaluation round.
  ^#J[~*(list 1.0 2.0 3.0), ~(* 2.0 2), 5.0]
  --> (json quote #(1.0 2.0 3.0 4.0 5.0))
  (eval ^#J[~*(list 1.0 2.0 3.0), ~(* 2.0 2), 5.0])
  --> #(1.0 2.0 3.0 4.0 5.0)
  ;; Comment extension
  #J[1, ; Comment inside JSON.
     2, ; Another one.
     3] ; Lisp comment outside of JSON.
  --> #(1.0 2.0 3.0) In TXR Lisp, there is a special "dotdot" notation consisting of a pair of dots. This can be written between successive atoms or compound expressions, and is a shorthand for That is to say, translates to and so for instance means The function constructs a range object, which denotes a pair of values. Range objects are most commonly used for referencing subranges of sequences. For instance, if is a list, then computes a sublist of consisting of elements 1 through 2 (counting from zero). Note that if this notation is used in the dot position of an improper list, the transformation still applies. That is, the syntax is valid and produces the object which is another way of writing which is quite probably nonsense. The notation's operator associates right to left, so that denotes Note that range objects are not printed using the dotdot notation. A range literal has the syntax of a two-element list, prefixed by (See Range Literals above.) In any context where the dotdot notation may be used, and where it is evaluated to its value, a range literal may also be specified. If an evaluated dotdot notation specifies two constant expressions, then an equivalent range literal can replace it. For instance the form can also be written The two are syntactically different, and so if these expressions are being considered for their syntax rather than value, they are not the same. TXR Lisp has a square bracket notation. The syntax is a shorthand way of writing The syntax is useful for situations where the expressive style of a Lisp-1 dialect is useful. For instance if is a variable which holds a function object, then can be used to call it, instead of If foo is a vector, then retrieves the fourth element, like Indexing over lists, strings and hash tables is possible, and the notation is assignable. Furthermore, any arguments enclosed in which are symbols are treated according to a modified namespace lookup rule. More details are given in the documentation for the operator. In TXR Lisp, there are two types of compound forms: the Lisp-2 style compound forms, denoted by ordinary lists that are expressed with parentheses. There are Lisp-1 style compound forms denoted by the DWIM Brackets, described in the previous section. The first position of an ordinary Lisp-2 style compound form, is expected to have a function or operator name. Then arguments follow. There may also be an expression in the dotted position, if the form is a function call. If the form is a function call then the arguments are evaluated. If any of the arguments are symbols, they are treated according to Lisp-2 namespacing rules. A function name may be a symbol, or else any of the syntactic forms given in the description of the function If there is an expression in the dotted position of a function call expression, it is also evaluated, and the resulting value is involved in the function call in a special way. Firstly, note that a compound form cannot be used in the dot position, for obvious reasons, namely that does not mean that there is a compound form in the dot position, but denotes an alternate spelling for where foo behaves as a variable. If the dot position of a compound form is an atom, then the behavior may be understood according to the following transformations:
  (f a b c ... . x) --> (apply (fun f) a b c ... x)
  [f a b c ... . x] --> [apply f a b c ... x] In addition to atoms, meta-expressions and meta-symbols can appear in the dot position, even though their underlying syntax is actually a compound expression. This is made to work according to a transformation pattern which superficially resembles the above one for atoms:
  (f a b c ... . @x) --> (apply (fun f) a b c ... @x) However, in this situation, the is a notation denoting the expression and thus the entire form is a proper list, not a dotted list. With the underlying syntax revealed, the transformation looks like this:
  (f a b c ... sys:var x) --> (apply (fun f) a b c ... (sys:var @x)) That is to say, the TXR Lisp form expander reacts to the presence of a or atom in embedded in the form. That symbol and the items which follow it are wrapped in an additional level of nesting, converted into a single compound form element. Effectively, in all these cases, the dot notation constitutes a shorthand for Examples:
  ;; a contains 3
  ;; b contains 4
  ;; c contains #(5 6 7)
  ;; s contains "xyz"
  (foo a b . c) ;; calls (foo 3 4 5 6 7)
  (foo a) ;; calls (foo 3)
  (foo . s) ;; calls (foo #\x #\y #\z)
  (list . a) ;; yields 3
  (list a . b) ;; yields (3 . 4)
  (list a . c) ;; yields (3 5 6 7)
  (list* a c) ;; yields (3 . #(5 6 7))
  (cons a . b) ;; error: cons isn't variadic.
  (cons a b . c) ;; error: cons requires exactly two arguments.
  [foo a b . c] ;; calls (foo 3 4 5 6 7)
  [c 1] ;; indexes into vector #(5 6 7) to yield 6
  (call (op list 1 . @1) 2) ;; yields (1 . 2) Note that the atom in the dot position of a function call may be a symbol macro. Since the semantics works as if by transformation to an apply form in which the original dot position atom is an ordinary argument, the symbol macro may produce a compound form. Thus:
  (symacrolet ((x 2))
    (list 1 . x)) ;; yields (1 . 2)
  (symacrolet ((x (list 1 2)))
    (list 1 . x)) ;; yields (1 1 2) That is to say, the expansion of is not substituted into the form but rather the transformation to syntax takes place first, and so the substitution of takes place in a form resembling Dialect Note: In some other Lisp dialects like ANSI Common Lisp, the improper list syntax may not be used as a function call; a function called apply (or similar) must be used for application even if the expression which gives the trailing arguments is a symbol. Moreover, applying sequences other than lists is not supported. TXR Lisp allows macros to be called using forms which are improper lists. These forms are simply destructured by the usual macro parameter list destructuring. To be callable this way, the macro must have an argument list which specifies a parameter match in the dot position. This dot position must either match the terminating atom of the improper list form, or else match the trailing portion of the improper list form. For instance if a macro mac is defined as
  (defmacro mac (a b . c) ...) then it may not be invoked as because the required argument is not satisfied, and so the argument cannot match the dot position as required. The macro may be called as in which case receives the form If it is called as then receives the improper list form In TXR Lisp, the character can occur in symbol names, and the token is a symbol. Therefore the syntax is not used for denoting regular expressions; rather, the syntax is used. TXR Lisp supports a printed notation called circle notation which accurately articulates the representation of objects which contain shared substructures as well as circular references. The notation is supported as a means of input, and is also optionally produced as output, controlled by the variable. Ordinarily, shared substructure in printed objects is not evident, except in the case of multiple occurrences of interned symbols, in whose semantics it is implicit that they refer to the same object. Other shared structure is printed as separate copies which look like distinct objects. For instance, the object produced by is printed as where it is not clear that the two occurrences of are actually the same object. Under the circle notation, this object can be represented as The part introduces a reference label, associating the arbitrarily chosen nonnegative integer 5 with the object which follows. The subsequent notation simply refers to the object labeled by 5, reproducing that object by reference. The result is a two-element list which has the same in two places. Circular structure presents a greater challenge to printing: namely, if it is printed by a naive recursive descent, it results in infinite output, and possibly stack exhaustion due to recursion. The circle notation detects and handles circular references. For instance, the object produced by produces a circular list which looks like an infinite list of 1's: This cannot be printed. However, under the circle notation, it can be represented as The entire object itself is labeled by the integer 1. Then, enclosed within the syntax of that labeled object itself, a reference occurs to the label. This circular label reference represents the corresponding circular reference in the object. A detailed description of the notational elements follows: The syntax introduces an object label which denotes the object whose printed representation follows. The label is identified by the integer value arising from digits which are one or more decimal digits. Note: the value zero is permitted; even though when the notation is produced by the TXR Lisp printer, labeling begins at 1. Negative values are not possible because a leading sign is not part of the syntax. There may be no more than one definition for a given label within the syntactic scope being parsed, otherwise a syntax error occurs. In TXR pattern language code, an entire source file is parsed as one unit, and so scope for the circular notation's references is the entire source file. Files processed by have their own scope. The scope for labels in TXR Lisp source code is the top-level expression in which they appear. Consequently, references in one TXR Lisp top-level expression cannot reach definitions in another. The syntax denotes a label reference: the repetition of an object that was previously labeled by the integer given by If no such label had been introduced in the syntactic scope, a syntax error occurs. An object was previously labeled by if a definition occurs in the same syntactic scope as the reference, and is applied to an object which either encloses the reference, or lexically precedes the reference. Forward references such as are not supported.

Note: Circular notation can span hash-table literals. The syntax denotes an hash table which contains one entry, in which that same table itself is both the key and value. This kind of circularity is not supported for hash tables. The analogous syntax produces a hash table in an inconsistent state. Dialect Note: Circle notation is taken from Common Lisp, intended to be unsurprising to users familiar with that language. The implementation is based on descriptions in the ANSI Common Lisp document, judiciously taking into account the content of the X3J13 Cleanup Issues named PRINT-CIRCLE-STRUCTURE:USER-FUNCTIONS-WORK and PRINT-CIRCLE-SHARED:RESPECT-PRINT-CIRCLE. The TXR Lisp notation in TXR Lisp indicates that the expression is to be read and then discarded, as if it were replaced by whitespace. This is useful for temporarily "commenting out" an expression.

Notes: Whereas it is valid for a TXR Lisp source file to be empty, it is a syntax error if a TXR Lisp source file contains nothing but one or more objects which are each suppressed by a preceding In the interactive listener, an input line consisting of nothing but commented-out objects is similarly a syntax error. The notation does not cascade; consecutive occurrences of trigger a syntax error. The notation interacts with the circle notation. Firstly, if an object which is erased by contains circular-referencing instances of the label notation, those instances refer to Secondly, commented-out objects may introduce labels which are subsequently referenced in An example of the first situation occurs in:
  #;(#1=(#1#)) Here the label is a circular reference because it refers to an object which is a parent of the object which contains that reference. Such a reference is only satisfied by a "backpatching" process once the entire surrounding syntax is processed to the top level. The erasure perpetrated by causes the label reference to be replaced by and therefore the labeled object is the object An example of the second situation is
  #;(#2=(a b c)) #2# Here, even though the expression is suppressed, the label definition which it has introduced persists into the following object, where the label reference resolves to A combination of the two situations occurs in
  #;(#1=(#1#)) #1# which yields This is because the label is available; but the earlier reference, being a circular reference inside an erased object, had lapsed to In ancient Lisp in the 1960's, it was not possible to apply the operations and to the symbol (empty list), because it is not a cell. In the InterLisp dialect, this restriction was lifted: these operations were extended to accept (and return The convention was adopted in other Lisp dialects such as MacLisp and eventually in Common Lisp. Thus there exists an object which is not a cons, yet which takes and In TXR Lisp, this relaxation is extended further. For the sake of convenience, the operations and are made to work with strings and vectors:
  (cdr "") -> nil
  (car "") -> nil
  (car "abc") -> #\a
  (cdr "abc") -> "bc"
  (cdr #(1 2 3)) -> #(2 3)
  (car #(1 2 3)) -> 1 Moreover, structure types which define the methods and can also be treated in the same way. The function is also extended in a special way. When the right parameter a non-list sequence, then it uses the equal equality test rather than eq for detecting the tail of the list.
  (ldiff "abcd" "cd") -> (#\a #\b) The operation starts with and repeatedly applies to produce and until the suffix is equal to the second argument: (equal "cd" "cd") yields true. Operations based on and such as and extend to strings and vectors. Most derived list processing operations such as or obey the following rule: the returned object follows the type of the leftmost input list object. For instance, if one or more sequences are processed by and the leftmost one is a character string, the function is expected to return characters, which are converted to a character string. However, in the event that the objects produced cannot be assembled into that type of sequence, a list is returned instead. For example [mapcar list "ab" "12"] returns because a string cannot hold lists of characters. However [mappend list "ab" "12"] returns The lazy versions of these functions such as do not have this behavior; they produce lazy lists. TXR Lisp implements a unified paradigm for iterating over sequence-like container structures and abstract spaces such as bounded and unbounded ranges of integers. This concept is based around an iterator abstraction which is directly compatible with Lisp cons-cell traversal in the sense that when iteration takes place over lists, the iterator instance is nothing but a cons cell. An iterator is created using the constructor function which takes a single argument. The argument denotes a space to be traversed; the iterator provides the means for that traversal. When the function is applied to a list (a cell or the object), the return value is that object itself. The remaining functions in the iterator API then behave like aliases for list processing functions. The function behaves like behaves like and behaves like For example, the following loops not only produce identical behavior, but the variable steps through the cells in the same manner in both:
  ;; print all symbols in the list (a b c d):
  (let ((iter '(a b c d)))
    (while iter
      (prinl (car iter))
      (set iter (cdr iter))))
  ;; likewise:
  (let ((iter (iter-begin '(a b c d))))
    (while (iter-more iter)
      (prinl (iter-item iter))
      (set iter (iter-step iter)))) There are three important differences. Firstly, both examples will still work if the list is replaced by a different kind of sequence, such as the string or the vector However, the former example will not execute efficiently on these objects. The reason is that the function will construct successive suffixes of the string and list object. That requires not only the allocation of memory, but changes the running time complexity of the loop from linear to quadratic. Secondly, the former example with will not work correctly if the sequence is an empty non-list sequence, like the null string or empty vector. Rectifying this problem requires the function to be used:
  ;; print all symbols in the list (a b c d):
  (let ((iter (nullify "abcd")))
    (while iter
      (prinl (car iter))
      (set iter (cdr iter)))) The function converts empty sequences of all kinds into the empty list Thirdly, the second example will work even if the input list is replaced with certain objects which are not sequences at all:
  ;; Print the integers from 0 to 3
  (let ((iter (iter-begin 0..4)))
    (while (iter-more iter)
      (prinl (iter-item iter))
      (set iter (iter-step iter))))
  ;; Print incrementing integers starting at 1,
  ;; breaking out of the loop after 100.
  (let ((iter (iter-begin 1)))
    (while (iter-more iter)
      (if (eql 100 (prinl (iter-item iter)))
        (return))
      (set iter (iter-step iter)))) In TXR Lisp, numerous functions that appear as list processing functions in other contemporary Lisp dialects, and historically, are actually sequence processing functions based on the above iterator paradigm. In TXR Lisp, sequences (strings, vectors and lists) as well as hashes and regular expressions can be used as functions everywhere, not just with the DWIM brackets. Sequences work as one- or two-argument functions. With a single argument, an element is selected by position and returned. With two arguments, a range is extracted and returned. Moreover, when a sequence is used as a function of one argument, and the argument is a range object rather than an integer, then the call is equivalent to the two-argument form. This is the basis for array slice syntax like ["abc" 0..1] . Hashes also work as one or two argument functions, corresponding to the arguments of the gethash function. A regular expression behaves as a one, two, or three argument function, which operates on a string argument. It returns the leftmost matching substring, or else Structure objects are callable if they implement the method. Integers and ranges are callable like functions. They take one argument, which must be a sequence or hash. An integer selects the corresponding element position from the sequence, and a range extracts a slice of its argument. Example 1:
  (mapcar "abc" '(2 0 1)) -> (#\c #\a #\b) Here, treats the string as a function of one argument (since there is one list argument). This function maps the indices and to the corresponding characters of string Through this function, the list of integer indices is taken to the list of characters Example 2:
  (call '(1 2 3 4) 1..3) -> (2 3) Here, the shorthand denotes A range used as an argument to a sequence performs range extraction: taking a slice starting at index 1, up to and not including index 3, as if by the call Example 3:
  (call '(1 2 3 4) '(0 2)) -> (1 2) A sequence applied to a list of index arguments is equivalent to using the select function, as if were called. Example 4:
  (call #/b./ "abcd") -> "bc" Here, the regular expression, called as a function, finds the matching substring within the argument Example 5:
  [1 "abcd"] -> #\b
  ["abcd" 1] -> #\b An integer used as function indexes into sequence. This produces the same result as when the sequence is used as a function with an integer argument. Example 6:
  [1..3 '(a b c d)] -> (b c)
  ['(a b c d) 1..3] -> (b c) A range used as a function extracts a slice of its argument. Similarly to Common Lisp, TXR Lisp is lexically scoped by default, but also has dynamically scoped (a.k.a "special") variables. When a variable is defined with or a binding for the symbol is introduced in the global name space, regardless of in what scope the form occurs. Furthermore, at the time the defvar form is evaluated, the symbol which names the variable is tagged as special. When a symbol is tagged as special, it behaves differently when it is used in a lexical binding construct like and all other such constructs such as function parameter lists. Such a binding is not the usual lexical binding, but a "rebinding" of the global variable. Over the dynamic scope of the form, the global variable takes on the value given to it by the rebinding. When the form terminates, the prior value of the variable is restored. (This is true no matter how the form terminates; even if by an exception.) Because of this "pervasive special" behavior of a symbol that has been used as the name of a global variable, a good practice is to make global variables have visually distinct names via the "earmuffs" convention: beginning and ending the name with an asterisk.
  (defvar *x* 42) ;; *x* has a value of 42
  (defun print-x ()
    (format t "~a\n" *x*))
  (let ((*x* "abc")) ;; this overrides *x*
    (print-x)) ;; *x* is now "abc" and so that is printed
  (print-x) ;; *x* is 42 again and so "42" is printed The terms bind and binding are used differently in TXR Lisp compared to ANSI Common Lisp. In TXR Lisp binding is an association between a symbol and an abstract storage location. The association is registered in some namespace, such as the global namespace or a lexical scope. That storage location, in turn, contains a value. In ANSI Lisp, a binding of a dynamic variable is the association between the symbol and a value. It is possible for a dynamic variable to exist, and not have a value. A value can be assigned, which creates a binding. In TXR Lisp, an assignment is an operation which transfers a value into a binding, not one which creates a binding. In ANSI Lisp, a dynamic variable can exist which has no value. Accessing the value signals a condition, but storing a value is permitted; doing so creates a binding. By contrast, in TXR Lisp a global variable cannot exist without a value. If a form doesn't specify a value, and the variable doesn't exist, it is created with a value of Unlike ANSI Common Lisp, TXR Lisp has global lexical variables in addition to special variables. These are defined using and The only difference is that when variables are introduced by these macros, the symbols are not marked special, so their binding in lexical scopes is not altered to dynamic binding. Many variables in TXR Lisp's standard library are global lexicals. Those which are special variables obey the "earmuffs" convention in their naming. For instance and are global lexicals, because they provide constant values for which overriding doesn't make sense. On the other hand the standard output stream variable is special. Overriding it over a dynamic scope is useful, as a means of redirecting the output of functions which write to the stream. In Common Lisp, is known as The TXR Lisp feature known as syntactic places allows programs to use the syntax of a form which is used to access a value from an environment or object, as an expression which denotes a place where a value may be stored. They are almost exactly the same concept as "generalized references" in Common Lisp, and are related to "lvalues" in languages in the C family, or "designators" in Pascal. A symbol is a is a syntactic place if it names a variable. If is a variable, then it may be assigned using the operator: the form causes to have the integer value 42. A compound expression can be a syntactic place, if its leftmost constituent is as symbol which is specially registered, and if the form has the correct syntax for that kind of place, and suitable semantics. Such an expression is a compound place. An example of a compound place is a form. If is an expression denoting a cell, then is not only an expression which retrieves the value of the field of the cell. It is also a syntactic place which denotes that field as a storage location. Consequently, the expression (set (car c) "abc") stores the character string in that location. Although the same effect can be obtained with (rplaca c "abc") the syntactic place frees the programmer from having to remember different update functions for different kinds of places. There are various other advantages. TXR Lisp provides a plethora of operators for modifying a place in addition to Subject to certain usage restrictions, these operators work uniformly on all places. For instance, the expression causes three different kinds of places to exchange contents, while the three expressions denoting those places are evaluated only once. New kinds of place update macros like are quite easily defined, as are new kinds of compound places. When a function call form such as the above is a syntactic place, then the function is called an accessor. This term is used throughout this document to denote functions which have associated syntactic places. Syntactic places can be macros (global and lexical), including symbol macros. So for instance in the place can actually be a symbolic macro which expands to, say, This means that the assignment is effectively Syntactic places, as well as operators upon syntactic places, are both open-ended. Code can be written quite easily in TXR Lisp to introduce new kinds of places, as well as new place-mutating operators. New places can be introduced with the help of the or macros, or possibly the macro in simple cases when a new syntactic place can be expressed as a transformation to the syntax of an existing place. Three ways exist for developing new place update macros (place operators). They can be written using the ordinary macro definer ordinary macro definer with the help of special utility macros called and They can also be written using in conjunction with the operators or Simple update macros similar to and can be written compactly using Unlike generalized references in Common Lisp, TXR Lisp syntactic places support the concept of deletion. Some kinds of places can be deleted, which is an action distinct from (but does not preclude) being overwritten with a value. What exactly it means for a place to be deleted, or whether that is even permitted, depends on the kind of place. For instance a place which denotes a lexical variable may not be deleted, whereas a global variable may be. A place which denotes a hash-table entry may be deleted, and results in the entry being removed from the hash table. Deleting a place in a list causes the trailing items, if any, or else the terminating atom, to move in to close the gap. Users may define new kinds of places which support deletion semantics. To bring about their effect, place operators must evaluate one or more places. Moreover, some of them evaluate additional forms which are not places. Which arguments of a place operator form are places and which are ordinary forms depends on its specific syntax. For all the built-in place operators, the position of an argument in the syntax determines whether it is treated as (and consequently required to be) a syntactic place, or whether it is an ordinary form. All built-in place operators perform the evaluation of place and non-place argument forms in strict left-to-right order. Place forms are evaluated not in order to compute a value, but in order to determine the storage location. In addition to determining a storage location, the evaluation of a place form may possibly give rise to side effects. Once a place is fully evaluated, the storage location can then be accessed. Access to the storage location is not considered part of the evaluation of a place. To determine a storage location means to compute some hidden referential object which provides subsequent access to that location without the need for a reevaluation of the original place form. (The subsequent access to the place through this referential object may still require a multi-step traversal of a data structure; minimizing such steps is a matter of optimization.) Place forms may themselves be compounds, which contain subexpressions that must be evaluated. All such evaluation for the built-in places takes place in left to right order. Certain place operators, such as and exhibit an unspecified behavior with regard to the timing of the access of the prior value of a place, relative to the evaluation of places which occur later in the same place operator form. Access to the prior values may be delayed until the entire form is evaluated, or it may be interleaved into the evaluation of the form. For example, in the form the prior value of can be accessed and saved as soon as is evaluated, prior to the evaluation of Alternatively, may be accessed and saved later, after the evaluation of or after the evaluation of all the forms. This issue affects the behavior of place-modifying forms whose subforms contain side effects. It is recommended that such forms not be used in programs. Certain place forms are required to have one or more arguments which are themselves places. The prime example of this, and the only example from among built-in syntactic places, are DWIM forms. A DWIM form has the syntax and the square-bracket-notation equivalent: Note that not only is the entire form a place, denoting some element or element range of but there is the added constraint that must also itself be a syntactic place. This requirement is necessary, because it supports the behavior that when the element or element range is updated, then is also potentially updated. After the assignment (set [obj 0..3] '("forty" "two")) not only is the range of places denoted by replaced by the list of strings ("forty" "two") but may also be overwritten with a new value. This behavior is necessary because the DWIM brackets notation maintains the illusion of an encapsulated array-like container over several dissimilar types, including Lisp lists. But Lisp lists do not behave as fully encapsulated containers. Some mutations on Lisp lists return new objects, which then have to stored (or otherwise accepted) in place of the original objects in order to maintain the array-like container illusion. The following is a summary of the built-in place forms, in addition to symbolic places denoting variables. New syntactic place forms can be defined by TXR programs. The following is a summary of the built-in place mutating macros. They are described in detail in their own sections. Assigns the values of expressions to places, performing assignments in left-to-right order, returning the value assigned to the rightmost place. Assigns the values of expressions to places, performing the determination of places and evaluation of the expressions left to right, but the assignment in parallel. Returns the value assigned to the rightmost place. Assigns to place, defaulting to and returns the prior value. Logically toggles the Boolean value of and returns the new value. If contains stores into the place and returns to indicate that the store took place. Otherwise does nothing and returns If contains a Boolean true value, stores into the place and returns to indicate that the store took place. Otherwise does nothing and returns Examines the value of and compares it to using the comparison function given by the function name If the comparison is false, returns Otherwise, stores the value into and returns If the place is evaluates stores that value into and returns it. Otherwise, returns the value of without changing its value or evaluating Increments by which defaults to 1, and returns the new value. Decrements by which defaults to 1, and returns the new value. Increments by which defaults to 1, and returns the old value. Decrements by which defaults to 1, and returns the old value. Increments by and returns if the previous value was to where defaults to 1 and defaults to zero. Decrements by and returns if the new value is to where defaults to 1 and defaults to 0. Exchanges the values of and Adds to the front of the list which is currently stored in then stores the extended list back into and returns it. Pop the list stored in and returns the popped value. Treats one or more places as a "multi-place shift register". Values are shifted to the left among the places. The rightmost place receives and the value of the leftmost place emerges as the return value. Treats zero or more places as a "multi-place rotate register". The places exchange values among themselves, by a rotation by one place to the left. The value of the leftmost place goes to the rightmost place, and that value is returned. Deletes a place which supports deletion, and returns the value which existed in that place prior to deletion. Sets multiple places to values obtained from successive elements of Applies an operational pipeline to the value of and stores the result back into Sets to 1 the bits in corresponding to bits that are equal to 1 in the mask made up of the arguments (by combining them together with the inclusive or operation). Clears (sets to 0) the bits in corresponding to bits that are equal to 1 in the mask made up of the arguments (by combining them together with the inclusive or operation).

TXR Lisp is a Lisp-2 dialect: it features separate namespaces for functions and variables. In TXR Lisp, global functions and operator macros coexist, meaning that the same symbol can be defined as both a macro and a function. There is a global namespace for functions, into which functions can be introduced with the macro. The global function environment can be inspected and modified using the accessor. There is a global namespace for macros, into which macros are introduced with the macro. The global function environment can be inspected and modified using the accessor. If a name is defined as both a function and a macro, then an expression of the form is expanded by the macro, whereas an expression of the form refers to the function. Moreover, the macro can produce a call to the function. The expression will retrieve the function object. There is a global namespace for variables also. The operators and introduce bindings into this namespace. These operators have the side effect of marking a symbol as a special variable, of the symbol are treated as dynamic variables, subject to rebinding. The global variable namespace together with the special dynamic rebinding is called the dynamic environment. The dynamic environment can be inspected and modified using the accessor. The operators and introduce bindings into the global namespace without marking symbols as special variables. Such bindings are called global lexical variables. Symbol macros may be defined over the global variable namespace using Note that whereas a symbol may simultaneously have both a function and macro binding in the global namespace, a symbol may not simultaneously have a variable and symbol macro binding. In addition to global and dynamic namespaces, TXR Lisp provides lexically scoped binding for functions, variables, macros, and symbol macros. Lexical variable binding are introduced with or various binding macros derived from these. Lexical functions are bound with and Lexical macros are established with and lexical symbol macros with Macros receive an environment parameter with which they may expand forms in their correct environment, and perform some limited introspection over that environment in order to determine the nature of bindings, or the classification of forms in those environments. This introspection is provided by and Lexical operator macros and lexical functions can also coexist in the following way. A lexical function shadows a global or lexical macro completely. However, the reverse is not the case. A lexical macro shadows only those uses of a function which look like macro calls. This is succinctly demonstrated by the following form:
  (flet ((foo () 43))
    (macrolet ((foo () 44))
      (list (fun foo) (foo) [foo])))
  -> (#<interpreted fun: lambda nil> 44 43) The and expressions are oblivious to the macro; the macro expansion process process the symbol in those contexts. However the form is subject to macro-expansion and replaced with If the and are reversed, the behavior is different:
  (macrolet ((foo () 44))
    (flet ((foo () 43))
      (list (fun foo) (foo) [foo])))
  -> (#<interpreted fun: lambda nil> 43 43) All three forms refer to the function, which lexically shadows the macro. TXR Lisp expressions can be embedded in the TXR pattern language in various ways. Likewise, the pattern language can be invoked from TXR Lisp. This brings about the possibility that Lisp code attempts to access pattern variables bound in the pattern language. The TXR pattern language can also attempt to access TXR Lisp variables. The rules are as follows, but they have undergone historic changes. See the COMPATIBILITY section, in particular notes under 138 and 121, and also 124. A Lisp expression evaluated from the TXR pattern language executes in a null lexical environment. The current set of pattern variables captured up to that point by the pattern language are installed as dynamic variables. They shadow any Lisp global variables (whether those are defined by or In the reverse direction, a variable reference from the TXR pattern language searches the pattern variable space first. If a variable doesn't exist there, then the lookup refers to the TXR Lisp global variable space. The pattern language doesn't see Lisp lexical variables. When Lisp code is evaluated from the pattern language, the pattern variable bindings are not only installed as dynamic variables for the sake of their visibility from Lisp, but they are also specially stored in a dynamic environment frame. When TXR pattern code is reentered from Lisp, these bindings are picked up from the closest such environment frame, allowing the nested invocation of pattern code to continue with the bindings captured by outer pattern code. Concisely, in any context in which a symbol has both a binding as a Lisp global variable as well as a pattern variable, that symbol refers to the pattern variable. Pattern variables are propagated through Lisp evaluation into nested invocations of the pattern language. The pattern language can also reference Lisp variables using the prefix, which is a consequence of that prefix introducing an expression that is evaluated as Lisp, the name of a variable being such an expression. The following sections list all of the special operators, macros and functions in TXR Lisp. In these sections, syntax is indicated using these conventions: A symbol in angle brackets denotes some syntactic unit: it may be a symbol or compound form. The syntactic unit is explained in the corresponding Description section. This indicates a repetition of zero or more of the given syntax enclosed in the braces or syntactic unit. The curly braces may be omitted if the scope of the is clear. This indicates a repetition of one or more of the given syntax enclosed in the braces or syntactic unit. The curly braces may be omitted if the scope of the is clear. This indicates a single, mandatory element, which is selected from among the indicated alternatives. May be combined with or repetition. Square brackets indicate optional syntax. Square brackets containing piped elements indicate an optional element, which, if present, must be chosen from among the indicated alternatives. The quoted square brackets indicate literal brackets which appear in the syntax, which they do without quotes. For instance is a pattern denotes the two possible expressions and The arrow notation is used in examples to indicate that the evaluation of the given syntax produces a value, whose printed representation is A compound expression with a symbol as its first element, if intended to be evaluated, denotes either an operator invocation or a function call. This depends on whether the symbol names an operator or a function. When the form is an operator invocation, the interpretation of the meaning of that form is under the complete control of that operator. If the compound form is a function call, the remaining forms, if any, denote argument expressions to the function. They are evaluated in left-to-right order to produce the argument values, which are passed to the function. An exception is thrown if there are not enough arguments, or too many. Programs can define named functions with the defun operator Some operators are macros. There exist predefined macros in the library, and macro operators can also be user-defined using the macro-defining operator Operators that are not macros are called special operators. Macro operators work as functions which are given the source code of the form. They analyze the form, and translate it to another form which is substituted in their place. This happens during a code walking phase called the expansion phase, which is applied to each top-level expression prior to evaluation. All macros occurring in a form are expanded in the expansion phase, and subsequent evaluation takes place on a structure which is devoid of macros. All that remains are the executable forms of special operators, function calls, symbols denoting either variables or themselves, and atoms such as numeric and string literals. Special operators can also perform code transformations during the expansion phase, but that is not considered macroexpansion, but rather an adjustment of the representation of the operator into a required executable form. In effect, it is post-macro compilation phase. Note that Lisp forms occurring in TXR pattern language are not individual top-level forms. Rather, the entire TXR query is parsed at the same time, and the macros occurring in its Lisp forms are expanded at that time. The operator, when evaluated, suppresses the evaluation of and instead returns itself as an object. For example, if is a symbol then the value of is itself. Without would evaluate to the value held by the variable which is named or else throw an error if there is no such variable. The operator never raises an error, if it is given exactly one argument, as required. The notation is translated to the object providing a shorthand for quoting. Likewise, when an object of the form is printed, it appears as
  ;; yields symbol a itself, not value of variable a
  (quote a) -> a
  ;; yields three-element list (+ 2 2), not 4.
  (quote (+ 2 2)) -> (+ 2 2) Variables are associations between symbols and storage locations which hold values. These associations are called bindings. Bindings are held in a context called an environment. Lexical environments hold local variables, and nest according to the syntactic structure of the program. Lexical bindings are always introduced by a some form known as a binding construct, and the corresponding environment is instantiated during the evaluation of that construct. There also exist bindings outside of any binding construct, in the so-called global environment. Bindings in the global environment can be temporarily shadowed by lexically-established binding in the dynamic environment. See the Special Variables section above. Certain special symbols cannot be used as variable names, namely the symbols and and all of the keyword symbols (symbols in the keyword package), which are denoted by a leading colon. When any of these symbols is evaluated as a form, the resulting value is that symbol itself. It is said that these special symbols are self-evaluating or self-quoting, similarly to all other atom objects such as numbers or strings. When a form consisting of a symbol, other than the above special symbols, is evaluated, it is treated as a variable, and yields the value of the variable's storage location. If the variable doesn't exist, an exception is thrown. Note: symbol forms may also denote invocations of symbol macros. (See the operators and All macros, including symbol macros, which occur inside a form are fully expanded prior to the evaluation of a form, therefore evaluation does not consider the possibility of a symbol being a symbol macro. The operator binds a name in the variable namespace of the global environment. Binding a name means creating a binding: recording, in some namespace of some environment, an association between a name and some named entity. In the case of a variable binding, that entity is a storage location for a value. The value of a variable is that which has most recently been written into the storage location, and is also said to be a value of the binding, or stored in the binding. If the variable named already exists in the global environment, the form has no effect; the form is not evaluated, and the value of the variable is unchanged. If the variable does not exist, then a new binding is introduced, with a value given by evaluating the form. If the form is absent, the variable is initialized to The form is evaluated in the environment in which the form occurs, not necessarily in the global environment. The symbols and may not be used as variables, nor can they be keyword symbols (symbols denoted by a leading colon). In addition to creating a binding, the operator also marks as the name of a special variable. This changes what it means to bind that symbol in a lexical binding construct such as the operator, or a function parameter list. See the section "Special Variables" far above. The macro behaves like when a variable named doesn't already exist. If already denotes a variable binding in the global namespace, evaluates the form and assigns the resulting value to the variable. The following equivalence holds:
  (defparm x y) <--> (prog1 (defvar x) (set x y)) The and forms return The and macros behave, respectively, almost exactly like and The difference is that these operators do not mark as special. If a global variable does not previously exist, then after the evaluation of either of these forms is true, but isn't. If had been already introduced as a special variable, it stays that way after the evaluation of or The and operators introduce a new scope with variables and evaluate forms in that scope. The operator symbol, either or is followed by a list which can contain any mixture of or pairs. Each must be a symbol, and specifies the name of variable to be instantiated and initialized. The variant specifies that the new variable receives an initial value from the evaluation of The plain variant specifies a variable which is initialized to The are evaluated in order, by both and The symbols and may not be used as variables, and neither can be keyword symbols: symbols denoted by a leading colon. The difference between and is that in later are in scope of the variables established by earlier variables in the same construct. In plain the are evaluated in a scope which does not include any of the variables. When the variables are established, the are evaluated in order. The value of the last becomes the return value of the If there are no then the return value is produced. The list of variables may be empty. The list of variables may contain duplicate if the operator is In that situation, a given has in scope the rightmost duplicate of any given that has been previously established. The have in scope the rightmost duplicate of any in the construct. Therefore, the following form calculates the value 3:
  (let* ((a 1)
         (a (succ a))
         (a (succ a)))
    a) Each duplicate is a separately instantiated binding, and may be independently captured by a lexical closure placed in a subsequent
  (let* ((a 0)
         (f1 (lambda () (inc a)))
         (a 0)
         (f2 (lambda () (inc a))))
    (list [f1] [f1] [f1] [f2] [f2] [f2]))
  --> (1 2 3 1 2 3) The preceding example shows that there are two mutable variables named in independent scopes, each respectively captured by the separate closures and Three calls to increment the first while the second retains its initial value. Under the behavior of duplicate variables is unspecified. Implementation note: the TXR compiler diagnoses and rejects duplicate symbols in whereas the interpreter ignores the situation. When the names of a special variables is specified in or remain, a new binding is created for them in the dynamic environment, rather than the lexical environment. In later are evaluated in a dynamic scope in which previous dynamic variables are established, and later dynamic variables are not yet established. A special variable may appear multiple times in a just like a lexical variable. Each duplicate occurrence extends the dynamic environment with a new dynamic binding. All these dynamic environments are removed when the or form terminates. Dynamic environments aren't captured by lexical closures, but are captured in delimited continuations.
  (let ((a 1) (b 2)) (list a b)) -> (1 2)
  (let* ((a 1) (b (+ a 1))) (list a b (+ a b))) -> (1 2 3)
  (let ()) -> nil
  (let (:a nil)) -> error, :a and nil can't be used as variables TXR Lisp follows ANSI Common Lisp in making the parallel binding construct, and the sequential one. In that language, the situation exists for historic reasons: mainly that was initially understood as being a macro for an immediately-called where the parameters come into existence simultaneously, receiving the evaluated values of all the argument expressions. The need for sequential binding was recognized later, by which time was cemented as a parallel binding construct. There are very good arguments for, in a new design, using the name for the construct which has sequential semantics. Nevertheless, in this matter, TXR Lisp remains compatible with dialects like ANSI CL and Emacs Lisp. The operator binds dynamic variables, and evaluates the in the dynamic scope of those bindings. The bindings are removed when the form terminates. The result value is that of the last or else if there are no forms. The and are expressions which are evaluated. Their values are expected to be lists, of bindable symbols and arbitrary values, respectively. The symbols coming from one list are bound to the values coming from the other list. If there are more symbols than values, then the extra symbols will appear unbound, as if they were first bound and then hidden using the function. If there are more values than symbols, the extra values are ignored. Note that dynamic binding takes place for the symbols even if they have not been introduced as special variables via or However, if those symbols appear as expressions denoting variables inside the they will not necessarily be treated as dynamic variables. If they have lexical definitions in scope, those will be referenced. Furthermore, the compiler treats undefined variables as global references, and not dynamic.
  (progv '(a b) '(1 2) (cons a b)) -> (1 . 2)
  (progv '(x) '(1) (let ((x 4)) (symbol-value 'x))) -> 1
  (let ((x 'lexical)
        (vars (list 'x))
        (vals (list 'dynamic)))
    (progv vars vals (list x (symbol-value 'x))))
  --> (lexical dynamic) The operator introduces a new function in the global function namespace. The function is similar to a lambda, and has the same parameter syntax and semantics as the operator. Note that the above syntax synopsis describes only the canonical parameter syntax which remains after parameter list macros are expanded. See the section Parameter List Macros. Unlike in the of a are surrounded by a block. The name of this block is the same as the name of the function, making it possible to terminate the function and return a value using For more information, see the definition of the block operator. A function may call itself by name, allowing for recursion. The special symbols and may not be used as function names. Neither can keyword symbols. It is possible to define methods as well as macros with as an alternative to the and forms. To define a method, the syntax should be used as the argument to the parameter. This gives rise to the syntax which is equivalent to the syntax. Macros can be defined using as the parameter of This way of defining a macro doesn't support destructuring; it defines the expander as an ordinary function with an ordinary argument list. To work, the function must accept two arguments: the entire macro call form that is to be expanded, and the macro environment. Thus, the macro definition syntax is which is equivalent to the syntax. In ANSI Common Lisp, keywords may be used as function names. In TXR Lisp, they may not. A function defined by may coexist with a macro defined by This is not permitted in ANSI Common Lisp. The operator produces a value which is a function. Like in most other Lisps, functions are objects in TXR Lisp. They can be passed to functions as arguments, returned from functions, aggregated into lists, stored in variables, etc. Note that the above syntax synopsis describes only the canonical parameter syntax which remains after parameter list macros are expanded. See the section Parameter List Macros. The first argument of is the list of parameters for the function. It may be empty, and it may also be an improper list (dot notation) where the terminating atom is a symbol other than It can also be a single symbol. The second and subsequent arguments are the forms making up the function body. The body may be empty. When a function is called, the parameters are instantiated as variables that are visible to the body forms. The variables are initialized from the values of the argument expressions appearing in the function call. The dotted notation can be used to write a function that accepts a variable number of arguments. There are two ways write a function that accepts only a variable argument list and no required arguments: (These notations are syntactically equivalent because the list notation actually denotes the object which isn't wrapped in any list). The keyword symbol (colon) can appear in the parameter list. This is the symbol in the keyword package whose name is the empty string. This symbol is treated specially: it serves as a separator between required parameters and optional parameters. Furthermore, the symbol has a role to play in function calls: it can be specified as an argument value to an optional parameter by which the caller indicates that the optional argument is not being specified. It will be processed exactly that way. An optional parameter can also be written in the form In this situation, if the call does not specify a value for the parameter, or specifies a value as the (colon) keyword symbol, then the parameter takes on the value of the expression This expression is only evaluated when its value is required. If is specified, then will be introduced as an additional binding with a Boolean value which indicates whether or not the optional parameter had been specified by the caller. Each that is evaluated is evaluated in an environment in which all of the previous parameters are visible, in addition to the surrounding environment of the For instance:
  (let ((default 0))
    (lambda (str : (end (length str)) (counter default))
      (list str end counter))) In this the initializing expression for the optional parameter end is and the variable it refers to is the previous argument. The initializer for the optional variable counter is the expression default, and it refers to the binding established by the surrounding let. This reference is captured as part of the lexical closure. Keyword symbols, and the symbols and may not be used as parameter names. The behavior is unspecified if the same symbol is specified more than once anywhere in the parameter list, whether as a parameter name or as the indicator in an optional parameter or any combination. Implementation note: the TXR compiler diagnoses and rejects duplicate symbols in whereas the interpreter ignores the situation. Note: it is not always necessary to use the operator directly in order to produce an anonymous function. In situations when is being written in order to simulate partial evaluation, it may be possible to instead make use of the macro. For instance the function which adds the values of all of its arguments together, and to the lexically captured variable can be written more succinctly as The operator is the main representative of a family of operators: and In situations when functions are simply combined together, the effect may be achieved using some of the available functional combinators, instead of a For instance chaining together functions as in is achievable using the function: The operator can also be used: Numerous combinators are available; see the section Partial Evaluation and Combinators. When a function is needed which accesses an object, there are also alternatives. Instead of and it is simpler to use the and notations. See the section Unbound Referencing Dot. Also see the functions and as well as the related convenience macros and If a function is needed which partially applies, to some arguments, a method invoked on a specific object, the function or macro may be used. For instance, instead of it is possible to write except that the latter produces a variadic function. The following expression returns a function which captures the variable Whenever the returned function is called, it increments by one, and returns the incremented value.
  (let ((counter 0))
    (lambda () (inc counter))) The following produces a variadic function which requires at least two arguments. The third and subsequent arguments are aggregated into a list passed as the single parameter
  (lambda (x y . z) (list 'my-arguments-are x y z)) A variadic function with no required arguments. The parameter name for the received arguments appears alone in place of the parameter list.
  (lambda args (list 'my-list-of-arguments args)) Same as the previous example, using a dotted notation specific to TXR Lisp.
  (lambda (. args) (list 'my-list-of-arguments args)) Note that is just a written notation equivalent to and not a different object structure. Optional arguments:
  [(lambda (x : y) (list x y)) 1] -> (1 nil)
  [(lambda (x : y) (list x y)) 1 2] -> (1 2) Passing (colon symbol) to request default value of optional parameter:
  [(lambda (x : (y 42) z) (list x y z)) 1 2 3] -> (1 2 3)
  [(lambda (x : (y 42) z) (list x y z)) 1 : 3] -> (1 42 3)
  [(lambda (x : (y 42) z) (list x y z)) 1] -> (1 42 nil) Presence-indicating variable accompanying optional parameter:
  [(lambda (x : (y 42 have-y)) (list x y have-y)) 1 2]
  -> (1 2 t)
  [(lambda (x : (y 42 have-y)) (list x y have-y)) 1]
  -> (1 42 nil)
  ;; defaulting via : is indistinguishable from missing
  [(lambda (x : (y 42 have-y)) (list x y have-y)) 1 :]
  -> (1 42 nil) The and macros bind local, named functions in the lexical scope. Note that the above syntax synopsis describes only the canonical parameter syntax which remains after parameter list macros are expanded. See the section Parameter List Macros. The difference between and is that a function defined by can see itself, and therefore recurse directly by name. Moreover, if multiple functions are defined by the same labels construct, they all have each other's names in scope of their bodies. By contrast, a function does not have itself in scope and cannot recurse. Multiple functions in the same do not have each other's names in their scopes. More formally, the and of the functions defined by are in a scope in which all of the function names being defined by that same construct are visible. Under both and the local functions that are defined are lexically visible to the main Note that and are properly scoped with regard to macros. During macro expansion, function bindings introduced by these macro operators shadow macros defined by and Furthermore, function bindings introduced by and also shadow symbol macros defined by when those symbol macros occur as arguments of a form. See also: the operator. The and macros do not establish named blocks around the body forms of the local functions which they bind. This differs from ANSI Common Lisp, whose local function have implicit named blocks, allowing for to be used.
  ;; Wastefully slow algorithm for determining evenness.
  ;; Note:
  ;; - mutual recursion between labels-defined functions
  ;; - inner is-even bound by labels shadows the outer
  ;; one bound by defun so the (is-even n) call goes
  ;; to the local function.
  (defun is-even (n)
   (labels ((is-even (n)
              (if (zerop n) t (is-odd (- n 1))))
            (is-odd (n)
              (if (zerop n) nil (is-even (- n 1)))))
     (is-even n))) The function invokes passing it the given arguments, if any. need not be a function; other kinds of objects can be used in place of functions with various semantics. The details are given in the description of the operator. Apply to arguments, adding them to produce
  (call (lambda (a b) (+ a b)) 1 2) Useless use of on a named function; equivalent to
  (call (fun list) 1 2) The function invokes optionally passing to it an argument list. The return value of the call is that of If no arguments are present after then is invoked without arguments. If one argument is present after then it is interpreted as If this is a sequence (a list, vector or string), then the elements of the sequence are passed as individual arguments to If is not a sequence, then is invoked with an improper argument list, terminated by the atom. If two or more arguments are present after then the last of these arguments is interpreted as The previous arguments represent leading arguments. When the argument list is formed to which is applied, the leading arguments become individual arguments presented in the same order, followed by arguments taken from the list. Note that if value is an atom or an improper list, the function is then invoked with an improper argument list. Only a variadic function may be invoked with an improper argument list. Moreover, all of the function's required and optional parameters must be satisfied by elements of the improper list, such that the terminating atom either matches the directly (see the operator) or else the receives an improper list terminated by that atom. To treat the terminating atom of an improper list as an ordinary element which can satisfy a required or optional function parameter, the function may be used, described next. The function ("improper apply") is similar to except with regard to the treatment of Firstly, under if is an atom other than (possibly a sequence, such as a vector or string), then it is treated as an ordinary argument: is invoked with a proper argument list, whose last element is Secondly, if is a list, but an improper list, then the terminating atom of becomes an individual argument. This terminating atom is not split into multiple arguments, even if it is a sequence. Thus, in all possible cases, treats an extra atom as an argument, and never calls with an improper argument list.
  ;; '(1 2 3) becomes arguments to list, thus (list 1 2 3).
  (apply (fun list) '(1 2 3)) -> (1 2 3)
  ;; this effectively invokes (list 1 2 3 4)
  (apply (fun list) 1 2 '(3 4)) -> (1 2 3 4)
  ;; this effectively invokes (list 1 2 . 3)
  (apply (fun list) 1 2 3)) -> (1 2 . 3)
  ;; "abc" is separated into characters
  ;; which become arguments of list
  (apply (fun list) "abc") -> (#\a #\b #\c) Note that some uses of this function that are necessary in other Lisp dialects are not necessary in TXR Lisp. The reason is that in TXR Lisp, improper list syntax is accepted as a compound form, and performs application:
  (foo a b . x) Here, the variables and supply the first two arguments for In the dotted position, must evaluate to a list or vector. The list or vector's elements are pulled out and treated as additional arguments for This syntax can only be used if is a symbolic form or an atom. It cannot be a compound form, because and are equivalent structures. The operator retrieves the function object corresponding to a named function in the current lexical environment. The may be a symbol denoting a named function: a built in function, or one defined by The may also take any of the forms specified in the description of the function. If such a refers to a function which exists, then the operator yields that function. Note: the operator does not see macro bindings via their symbolic names with which they are defined by However, the name syntax may be used to refer to macros. This syntax is documented in the description of It is also possible to retrieve a global macro expander using the function The operator's name is an acronym: DWIM may be taken to mean "Do What I Mean", or alternatively, "Dispatch, in a Way that is Intelligent and Meaningful". The notation is a shorthand which denotes Note that since the and are used in this document for indicating optional syntax, in the above Syntax synopsis the quoted notation and denotes bracket tokens which literally appear in the syntax. The operator takes a variable number of arguments, which are treated as expressions to be individually macro-expanded and evaluated, using the same rules. This means that the first argument isn't a function name, but an ordinary expression which can simply compute a function object (or, more generally, a callable object). Furthermore, for those arguments of which are symbols (after all macro-expansion is performed), the evaluation rules are altered. For the purposes of resolving symbols to values, the function and variable binding namespaces are considered to be merged into a single space, creating a situation that is similar to a Lisp-1 style dialect. This special Lisp-1 evaluation is not recursively applied. All arguments of which, after macro expansion, are not symbols are evaluated using the normal Lisp-2 evaluation rules. Thus, the DWIM operator must be used in every expression where the Lisp-1 rules for reducing symbols to values are desired. If a symbol has bindings both in the variable and function namespace in scope, and is referenced by a dwim argument, this constitutes a conflict which is resolved according to two rules. When nested scopes are concerned, then an inner binding shadows an outer binding, regardless of their kind. An inner variable binding for a symbol shadows an outer or global function binding, and vice versa. If a symbol is bound to both a function and variable in the global namespace, then the variable binding is favored. Macros do not participate in the special scope conflation, with one exception. What this means is that the space of symbol macros is not folded together with the space of operator macros. An argument of that is a symbol might be symbol macro, variable or function, but it cannot be interpreted as the name of a operator macro. The exception is this: from the perspective of a form, function bindings can shadow symbol macros. If a function binding is defined in an inner scope relative to a symbol macro for the same symbol, using or the function hides the symbol macro. In other words, when macro expansion processes an argument of a form, and that argument is a symbol, it is treated specially in order to provide a consistent name lookup behavior. If the innermost binding for that symbol is a function binding, it refers to that function binding, even if a more outer symbol macro binding exists, and so the symbol is not expanded using the symbol macro. By contrast, in an ordinary form, a symbolic argument never resolves to a function binding. The symbol refers to either a symbol macro or a variable, whichever is nested closer. If, after macro expansion, the leftmost argument of the is the name of a special operator or macro, the form doesn't denote an invocation of that operator or macro. A form is an invocation of the operator, and the leftmost argument of that operator, if it is a symbol, is treated as a binding to be resolved in the variable or function namespace, like any other argument. Thus is an invocation of the function, not the operator. How many arguments are required by the operator depends on the type of object to which the first argument expression evaluates. The possibilities are:
    Call the given function object with the given arguments. If the first expression evaluates to a symbol, that symbol is resolved in the function namespace, and then the resulting function, if found, is called with the given arguments. Retrieve an element from at the specified which is a nonnegative integer. This form is also a syntactic place. If a value is stored to this place, it replaces the element. The place may also be deleted, which has the effect of removing the element from the sequence, shifting the elements at higher indices, if any, down one element position, and shortening the sequence by one. If the place is deleted, and if is a list, then the form itself must be a place. This form is implemented using the accessor such that, except for the argument evaluation semantics of the DWIM brackets, it is equivalent to using the syntax. Retrieve the specified range of elements. The range of elements is specified in the and fields of a range object. The (dotdot) syntactic sugar denotes the construction of the range object via the function. See the section on Range Indexing below. This form is also a syntactic place. Storing a value in this place has the effect of replacing the subsequence with a new subsequence. Deleting the place has the effect of removing the specified subsequence from If is a list, then the form must itself be a place. The argument in a range assignment can be a string, vector or list, regardless of whether the target is a string, vector or list. If the target is a string, the replacement sequence must be a string, or a list or vector of characters. The semantics is implemented using the accessor, such that the following equivalence holds:
      [seq from..to] <--> (sub seq from..to) For this reason, may be any object that is iterable by Elements of specified by elements of are extracted and returned as a sequence of the same kind as This form is equivalent to except when the target of an assignment operation. This form is a syntactic place if is one. If a sequence is assigned to this place, then elements of the sequence are distributed to the specified locations. The following equivalences hold between index-sequence-based indexing and the and functions, except that always returns the value assigned, whereas returns its first argument:
      [seq idx-seq] <--> (select seq idx-seq)
      (set [seq idx-seq] new) <--> (replace seq new idx-seq) Note that unlike the select function, this does not support because since hash keys may be lists, that syntax is indistinguishable from a simple hash lookup where is the key. Retrieve a value from the hash table corresponding to or else return if there is no such entry. The expression is always evaluated, whether or not its value is used. Retrieves an element from the search tree as if by applying the function to Retrieves a list of elements from the search tree as if by evaluating the expression. Determine whether regular expression matches and in that case return the (possibly empty) leftmost matching substring. Otherwise, return If is specified, it gives the starting position where the search begins, and if is given, and has a value other than it specifies a search from right to left. These optional arguments have the same conventions and semantics as their equivalents in the function. Note that is always required, and is always the rightmost argument. The structure instance is inquired whether it supports a method named by the symbol If so, that method is invoked on the object. The method receives followed by the value of every If this form is used as a place, then the object must support a method. Element and range indexing is possible on object of type which manipulate arrays in a foreign ("C language") representation, and are closely associated with the Foreign Function Interface (FFI). Just like in the case of sequences, the semantics of referencing objects with the bracket notation is based on the functions and These, in turn, rely on the specialized functions. and Indexing is supported for objects of type This provides a way to access and store the individual bytes of a buffer. If the left argument is an integer, it denotes selection of an element from The value acts as the index into a vector-like or list-like sequence, or a key into a hash table. If the left argument is a range, and there is one argument, the semantics is that of the function: either the selection of a point from the range by an integer index or the selection of a subrange of sequence according to the endpoints of

Note that the various above forms are not actually cases of the operator but the due to the semantics of the left argument objects being used as functions. All of the semantics described above is available in any situation in which an object is used as a function: for instance, as an argument of the or operators, or the functional argument in Vector and list range indexing is based from zero, meaning that the first element is numbered zero, the second one and so on. zero. Negative values are allowed; the value refers to the last element of the vector or list, and to the second last and so forth. Thus the range means "everything except for the first element and the last two". The symbol represents the position one past the end of the vector, string or list, so denotes the entire list or vector, and the range represents the empty range just beyond the last element. It is possible to assign to For instance:
  (defvar list '(1 2 3))
  (set [list t..t] '(4)) ;; list is now (1 2 3 4) The value zero has a "floating" behavior when used as the end of a range. If the start of the range is a negative value, and the end of the range is zero, the zero is interpreted as being the position past the end of the sequence, rather than the first element. For instance the range means the same thing as Zero at the start of a range always means the first element, so that refers to all the elements except for the last one. The dwim operator allows for a Lisp-1 flavor of programming in TXR Lisp, which is principally a Lisp-2 dialect. A Lisp-1 dialect is one in which an expression like treats both a and b as expressions subject to the same evaluation rules—at least, when isn't an operator or an operator macro. This means that the symbols and are resolved to values in the same namespace. The form denotes a function call if the value of variable is a function object. Thus in a Lisp-1, named functions do not exist as such: they are just variable bindings. In a Lisp-1, the form means that there is a variable called which holds a function, which is retrieved from that variable and applied to the argument. In the expression both occurrences of refer to the variable, and so this form applies the function to itself. It is almost certainly meaningless. In a Lisp-2 means that there is a function called in the function namespace. In the expression the two occurrences refer to different bindings: one is a function and the other a variable. Thus there can exist a variable which holds a cons-cell object, rather than the function, and the form makes sense. The Lisp-1 approach is useful for functional programming, because it eliminates cluttering occurrences of the call and fun operators. For instance:
  ;; regular notation
  (call foo (fun second) '((1 a) (2 b)))
  ;; [] notation
  [foo second '((1 a) (2 b))] Lisp-1 dialects can also provide useful extensions by giving a meaning to objects other than functions in the first position of a form, and the syntax does exactly this. TXR Lisp is a Lisp-2 because Lisp-2 also has advantages. Lisp-2 programs which use macros naturally achieve hygiene because lexical variables do not interfere with the function namespace. If a Lisp-2 program has a local variable called this does not interfere with the hidden use of the function in a macro expansion in the same block of code. Lisp-1 dialects have to provide hygienic macro systems to attack this problem. Furthermore, even when not using macros, Lisp-1 programmers have to avoid using the names of functions as lexical variable names, if the enclosing code might use them. The two namespaces of a Lisp-2 also naturally accommodate symbol macros and operator macros. Whereas functions and variables can be represented in a single namespace readily, because functions are data objects, this is not so with symbol macros and operator macros, the latter of which are distinguished syntactically by their position in a form. In a Lisp-1 dialect, given either of the two symbols could be a symbol macro, but only can possibly be an operator macro. Yet, having only a single namespace, a Lisp-1 doesn't permit where is simultaneously a symbol macro and an operator macro, though the situation is unambiguous by syntax even in Lisp-1. In other words, Lisp-1 dialects do not entirely remove the special syntactic recognition given to the leftmost position of a compound form, yet at the same time they prohibit the user from taking full advantage of it by providing only one namespace. TXR Lisp provides the "best of both worlds": the DWIM brackets notation provides a model of Lisp-1 computation that is purer than Lisp-1 dialects (since the leftmost argument is not given any special syntactic treatment at all) while the Lisp-2 foundation provides a traditional Lisp environment with its "natural hygiene". The function returns if is a function, otherwise it returns The function produces and returns a duplicate of which must be a function. A duplicate of a function is a distinct function object not to the original function, yet which accepts the same arguments and behaves exactly the same way as the original. If a function contains no captured environment, then a copy made of that function by is indistinguishable from the original function in every regard, except for being a distinct object that compares unequal to the original under the function. If a function contains a captured environment, then a copy of that function made by has its own copy of that environment. If the copied function changes the values of captured lexical variables, the original function is not affected by these changes and vice versa. The entire lexical environment is copied; the copy and original function do not share any portion of the environment at any level of nesting. The operator evaluates each in left-to-right order, and returns the value of the last form. The value of the form is The operator evaluates each in left-to-right order, and returns the value of the first form. The value of the form is Various other operators such as also arrange for the evaluation of a body of forms, the value of the last of which is returned. These operators are said to feature an implicit These special operators are also functions. The function accepts zero or more arguments. It returns its last argument, or if called with no arguments. The function likewise accepts zero or more arguments. It returns its first argument, or if called with no arguments. In ANSI Common Lisp, requires at least one argument. Neither nor exist as functions. The evaluates each in left-to-right order. The value is that of the second form, if present, otherwise it is The form yields The value of is also and yield The symbol also has a function binding. The function accepts any number of arguments. If invoked with at least two arguments, it returns the second one. Otherwise it returns In ANSI Common Lisp, requires at least two arguments. It does not exist as a function. The operator provides a multi-branching conditional evaluation of forms. Enclosed in the cond form are groups of forms expressed as lists. Each group must be a list of at least one form. The forms are processed from left to right as follows: the first form, in each group is evaluated. If it evaluates true, then the remaining forms in that group, if any, are also evaluated. Processing then terminates and the result of the last form in the group is taken as the result of cond. If is the only form in the group, then result of is taken as the result of If the first form of a group yields then processing continues with the next group, if any. If all form groups yield then the cond form yields This holds in the case that the syntax is empty: yields These three macros arrange for the evaluation of whose value is then compared against the key or keys in each When the value matches a key, then the remaining forms of are evaluated, and the value of the last form is returned; subsequent clauses are not evaluated. If no matches, and there is no then the value nil is returned. Otherwise, the forms in the are evaluated, and the value of the last one is returned. If there are no forms, then is returned. If duplicates keys are present in such a way that the value of the matches multiple it is unspecified which of those clauses is evaluated. The syntax of a takes on these two forms: where may be an atom which denotes a single key, or else a list of keys. There is a restriction that the symbol may not be used as The form may be used as a key to match that symbol. The syntax of an is: which resembles a form that is often used as the final clause in the syntax. The three forms of the case construct differ from what type of test they apply between the value of and the keys. The macro generates code which uses the function's equality. The macro uses and uses
  (let ((command-symbol (casequal command-string
                        (("q" "quit") 'quit)
                        (("a" "add") 'add)
                        (("d" "del" "delete") 'delete)
                        (t 'unknown))))
    ...) The and macros are similar to the macros and differing from them in only the following regard. The of these macros has the form where is either an atom, which is evaluated to produce a key, or else else a compound form, whose elements are evaluated as forms, producing multiple keys. This evaluation takes place at macro-expansion time, in the global environment. The works the same way under these macros as under et al. Note that although in a must not be the atom there is no restriction against it being an atom which evaluates to In this situation, the value has no special meaning. The expressions are evaluated in the order in which they appear in the construct, at the time the or macro is expanded. Note: these macros allow the use of variables and global symbol macros as case keys.
  (defvarl red 0)
  (defvarl green 1)
  (defvarl blue 2)
  (let ((color blue))
    (caseql* color
      (red "hot")
      ((green blue) "cool")))
  --> "cool" These macros are error-catching variants of, respectively, and If the is present in the invocation of an error-catching case macro, then the the invocation is precisely equivalent to the corresponding non-error-trapping variant. If the is missing in the invocation of an error-catching variant, then a default is inserted which throws an exception of type derived from After this insertion, the semantics follows that of the non-error-trapping variant. For instance, which has no is equivalent to where indicates the inserted expression which throws However, is simply equivalent to since it has an Note: the error-catching case macros are intended for situations in which it is a matter of program correctness that every possible value of matches a such that if a failure to match occurs, it indicates a software defect. The error-throwing helps to ensure that the error situation is noticed. Without this clause, the case macro terminates with a value of which may conceal the defect and delay its identification. There exist both an operator and an function. A list form with the symbol in the first position is interpreted as an invocation of the operator. The function can be accessed using the DWIM bracket notation and in other ways. The operator provides a simple two-way-selective evaluation control. The form is evaluated. If it yields true then is evaluated, and that form's return value becomes the return value of the If yields false, then is evaluated and its return value is taken to be that of If is omitted, then the behavior is as if were specified as The function provides no evaluation control. All of its arguments are evaluated from left to right. If the argument is true, then it returns the argument, otherwise it returns the value of the argument if present, otherwise it returns There exist both an operator and an function. A list form with the symbol in the first position is interpreted as an invocation of the operator. The function can be accessed using the DWIM bracket notation and in other ways. The operator provides three functionalities in one. It computes the logical "and" function over several forms. It controls evaluation (a.k.a. "short-circuiting"). It also provides an idiom for the convenient substitution of a value in place of when some other values are all true. The operator evaluates as follows. First, a return value is established and initialized to the value The if any, are evaluated from left to right. The return value is overwritten with the result of each Evaluation stops when all are exhausted, or when is stored in the return value. When evaluation stops, the operator yields the return value. The function provides no evaluation control: it receives all of its arguments fully evaluated. If it is given no arguments, it returns If it is given one or more arguments, and any of them are it returns Otherwise, it returns the value of the last argument.
  (and) -> t
  (and (> 10 5) (stringp "foo")) -> t
  (and 1 2 3) -> 3 ;; shorthand for (if (and 1 2) 3). There exist both a macro and a function. A list form with the symbol in the first position is interpreted as an invocation of the macro. The function can be accessed using the DWIM bracket notation and in other ways. The macro and function are the logical negation of the operator and function. They are related according to the following equivalences:
  (nand f0 f1 f2 ...) <--> (not (and f0 f1 f2 ...))
  [nand f0 f1 f2 ...] <--> (not [and f0 f1 f2 ...]) There exist both an operator and an function. A list form with the symbol in the first position is interpreted as an invocation of the operator. The function can be accessed using the DWIM bracket notation and in other ways. The operator provides three functionalities in one. It computes the logical "or" function over several forms. It controls evaluation (a.k.a. "short-circuiting"). The behavior of also provides an idiom for the selection of the first value from a sequence of forms. The operator evaluates as follows. First, a return value is established and initialized to the value The if any, are evaluated from left to right. The return value is overwritten with the result of each Evaluation stops when all are exhausted, or when a true value is stored into the return value. When evaluation stops, the operator yields the return value. The function provides no evaluation control: it receives all of its arguments fully evaluated. If it is given no arguments, it returns If all of its arguments are it also returns Otherwise, it returns the value of the first argument which isn't
  (or) -> nil
  (or 1 2) -> 1
  (or nil 2) -> 2
  (or (> 10 20) (stringp "foo")) -> t There exist both a macro and a function. A list form with the symbol in the first position is interpreted as an invocation of the macro. The function can be accessed using the DWIM bracket notation and in other ways. The macro and function are the logical negation of the operator and function. They are related according to the following equivalences:
  (nor f0 f1 f2 ...) <--> (not (or f0 f1 f2 ...))
  [nor f0 f1 f2 ...] <--> (not [or f0 f1 f2 ...]) The macro operator evaluates If yields true, and there are additional forms, then each is evaluated. The value of the last form becomes the result value of the form. If there are no forms, then the result is The operator is similar to except that it reverses the logic of the test. The forms, if any, are evaluated if and only if is false. The macro operator provides a looping construct. It evaluates If yields then the evaluation of the form terminates, producing the value Otherwise, if there are additional forms, then each is evaluated. Next, evaluation returns to repeating all of the previous steps. The macro operator is similar to except that the form terminates when evaluates true, rather than false. These operators arrange for the evaluation of all their enclosed forms in an anonymous block. Any of the or may use the operator to terminate the loop, and optionally to specify a result value for the form. The only way these forms can yield a value other than is if the operator is used to terminate the implicit anonymous block, and is given an argument, which becomes the result value. The and macros are similar, respectively, to the macros and They differ in one respect: they begin by evaluating the one time unconditionally, without first evaluating After this evaluation, the subsequent behavior is like that of or Another way to regard the behavior is that that these forms execute one iteration unconditionally, without evaluating the termination test prior to the first iteration. Yet another view is that these constructs relocate the test from the top of the loop to the bottom of the loop. The macro provides a construct which combines iteration with variable binding. The evaluation of the form takes place as follows. First, fresh bindings are established for as if by the operator. It is an error for the list of variable bindings to be empty. After the establishment of the bindings, the value of the last is tested. If the value is then terminates. Otherwise, are evaluated in the scope of the variable bindings, and then iterates from the beginning, again establishing fresh bindings for the and testing the value of the last All evaluation takes place in an anonymous block, which can be terminated with the operator. Doing so terminates the loop. If the loop is thus terminated by an explicit a return value can be specified. Under normal termination, the return value is In the syntax, a small convenience is permitted. Instead of the last it is permissible for the syntax to appear, the being omitted. A machine-generated variable is substituted in place of the missing and that variable is then initialized from and used as the basis of the test.
  ;; read lines of text from *stdin* and print them,
  ;; until the end-of-stream condition:
  (whilet ((line (get-line)))
    (put-line line))
  ;; read lines of text from *stdin* and print them,
  ;; until the end-of-stream condition occurs or
  ;; a line is identical to the character string "end".
  (whilet ((line (get-line))
           (more (and line (nequal line "end"))))
    (put-line line)) The and macros combine the variable binding of with conditional evaluation of and respectively. In either construct's syntax, a non-compound form may appear in place of the variable binding list. In this case, is evaluated as a form, and the construct is equivalent to its respective ordinary or counterpart. If the list of variable bindings is empty, it is interpreted as the atom and treated as an If one or more bindings are specified rather than then the evaluation of these forms takes place as follows. First, fresh bindings are established for as if by the operator. Then, the last variable's value is tested. If it is not then the test is true, otherwise false. In the syntax, a small convenience is permitted. Instead of the last it is permissible for the syntax to appear, the being omitted. A machine-generated variable is substituted in place of the missing and that variable is then initialized from and used as the basis of the test. This is intended to be useful in situations in which or do not require access to the tested value. In the case of the operator, if the test is true, the operator evaluates and yields its value. Otherwise the test is false, and if the optional is present, that is evaluated instead and its value is returned. If this form is missing, then is returned. In the case of the operator, if the test is true, then the if any, are evaluated. The value of the last one is returned, otherwise if the forms are missing. If the test is false, then evaluation of is skipped, and is returned.
  ;; dispose of foo-resource if present
  (whenlet ((foo-res (get-foo-resource obj)))
    (foo-shutdown foo-res)
    (set-foo-resource obj nil))
  ;; Contrast with: above, using when and let
  (let ((foo-res (get-foo-resource obj)))
    (when foo-res
      (foo-shutdown foo-res)
      (set-foo-resource obj nil)))
  ;; print frobosity value if it exceeds 150
  (whenlet ((fv (get-frobosity-value))
            (exceeds-p (> fv 150)))
    (format t "frobosity value ~a exceeds 150\n" fv))
  ;; same as above, taking advantage of the
  ;; last variable being optional:
  (whenlet ((fv (get-frobosity-value))
            ((> fv 150)))
    (format t "frobosity value ~a exceeds 150\n" fv))
  ;; yield 4: 3 interpreted as atom-form
  (whenlet 3 4)
  ;; yield 4: nil interpreted as atom-form
  (iflet () 3 4) The macro generalizes Each argument is a compound consisting of at least one item: a list of bindings or This item is followed by zero or more If there are no then the situation is treated as if there were a single specified as The arguments of are considered in sequence, starting with the leftmost. If the argument's left item is an then the form is evaluated. If it yields true, then the next to it are evaluated in order, and the form terminates, yielding the value obtained from the last If yields false, then the next argument is considered, if there is one. If the argument's left item is a list of bindings, then it is processed with exactly the same logic as under the macro. If the last binding contains a true value, then the adjoining are evaluated in a scope in which all of the bindings are visible, and terminates, yielding the value of the last Otherwise, the next argument of is considered (processed in a scope in which the bindings produced by the current item are no longer visible). If runs out of arguments, it terminates and returns
  (let ((l '(1 2 3)))
    (condlet
      ;; first arg
      (((a (first l) ;; a binding gets 1
        (b (second l)) ;; b binding gets 2
        (g (> a b)))) ;; last variable g is nil
       'foo) ;; not evaluated
      ;; second arg
      (((b (second l) ;; b gets 2
        (c (third l)) ;; c gets 3
        (g (> b c)))) ;; last variable g is true
       'bar))) ;; condlet terminates
  --> bar ;; result is bar The macro provides an anaphoric conditional operator resembling the operator. Around the evaluation of the and forms, the symbol is implicitly bound to a subexpression of a subexpression which is thereby identified as the it-form. This alias provides a convenient reference to that place or value, similar to the word "it" in the English language, and similar anaphoric pronouns in other languages. If is bound to a place form, the binding is established as if using the operator: the form is evaluated only once, even if the alias is used multiple times in the or expressions. Furthermore, the place form is implicitly surrounded with so that the place's value is accessed only once, and multiple references to refer to a copy of the value cached in a hidden variable, rather than generating multiple accesses to the place. Otherwise, if the form is not a syntactic place is bound as an ordinary lexical variable to the form's value. An it-candidate is an an expression viable for having its value or storage location bound to the symbol. An it-candidate is any expression which is not a constant expression according to the function, and not a symbol. The macro imposes applies several rules to the expression:

    1.  The expression must be either an atom, a function call form, or a form. Otherwise the expression is ill-formed, and throws an exception at macro-expansion time. For the purposes of these rules, a form is considered as a function call expression, whose first argument is the second element of the form. That is to say, which is equivalent to is treated similarly to as a one-argument call.

    2.  If the expression is a function call with two or more arguments, at most one of them may be an it-candidate. If two or more arguments are it-candidates, the situation is ambiguous. The expression is ill-formed and throws an exception at macro-expansion time.

    3.  If is an atom, or a function call expression with no arguments, then the symbol is not bound. Effectively, macro behaves like the ordinary operator.

    4.  If is a function call or expression with exactly one argument, then the variable is bound to the argument expression, except when the function being called is or This binding occurs regardless of whether the expression is an it-candidate.

    5.  If is a function call with exactly one argument to the Boolean negation function which goes by one of the three names or then that situation is handled by a rewrite according to the following pattern: which applies likewise for or substituted for The Boolean inverse function is removed, and the and expressions are exchanged.

    6.  If is a function call with two or more arguments, then it is only well-formed if at most one of those arguments is an it-candidate. If there is one such argument, then the variable is bound to it.

    7.  Otherwise the variable is bound to the leftmost argument expression, regardless of whether that argument expression is an it-candidate.

    In all other regards, the macro behaves similarly to The expression is evaluated, and, if applicable, the value of, or storage location denoted by the appropriate argument is captured and bound to the variable whose scope extends over the form, as well as over if present. If yields a true value, then is evaluated and the resulting value is returned, otherwise is evaluated if present and its value is returned. A missing is treated as if it were the form.
      (ifa t 1 0) -> 1
      ;; Rule 6: it binds to (* x x), which is
      ;; the only it-candidate.
      (let ((x 6) (y 49))
        (ifa (> y (* x x)) ;; it binds to (* x x)
          (list it)))
      -> (36)
      ;; Rule 4: it binds to argument of evenp,
      ;; even though 4 isn't an it-candidate.
      (ifa (evenp 4)
        (list it))
      -> (4)
      ;; Rule 5:
      (ifa (not (oddp 4))
        (list it))
      -> (4)
      ;; Rule 7: no candidates: choose leftmost
      (let ((x 6) (y 49))
        (ifa (< 1 x y)
          (list it)))
      -> (1)
      -> (4)
      ;; Violation of Rule 1:
      ;; while is not a function
      (ifa (while t (print 42))
        (list it))
      --> exception!
      ;; Violation of Rule 2:
      (let ((x 6) (y 49))
        (ifa (> (* y y y) (* x x)))
          (list it))
      --> exception! The operator provides a multi-branching conditional evaluation of forms, similarly to the operator. Enclosed in the cond form are groups of forms expressed as lists. Each group must be a list of at least one form. The operator is anaphoric: it expands into a nested structure of zero or more invocations, according to these patterns:
      (conda) -> nil
      (conda (x y ...) ...) -> (ifa x (progn y ...) (conda ...)) Thus, inherits all the restrictions on the expressions from as well as the anaphoric variable feature. The macro is similar to the macro, except that it is anaphoric in exactly the same manner as the macro. It may be understood as conforming to the following equivalence:
      (whena x f0 f2 ...) <--> (if x (progn f0 f2 ...)) The macro implements a simple counting loop. is established as a variable, and initialized to zero. is evaluated one time to produce a limiting value, which should be a number. Then, if the value of is less than the limiting value, the are evaluated, is incremented by one, and the process repeats with a new comparison of against the limiting value possibly leading to another evaluation of the forms. If is found to equal or exceed the limiting value, then the loop terminates. When the loop terminates, its return value is unless a is present, in which case the value of that form specifies the return value. as well as are evaluated in the scope in which the binding of is visible. These operators establish a loop for iterating over the elements of one or more sequences. Each must evaluate to an iterable object that is suitable as an argument for the function. The sequences are then iterated in parallel over repeated evaluations of the with each variable being assigned to successive elements of its sequence. The shortest list determines the number of iterations, so if any of the evaluate to an empty sequence, the body is not executed. If the list of pairs itself is empty, then an infinite loop is specified. The body forms are enclosed in an anonymous block, allowing the operator to terminate the loop prematurely and optionally specify the return value. The and variants are like and except that for each iteration, the resulting value of the body is collected into a list. When the iteration terminates, the return value of the or operator is this collection. The and variants are like and except that for each iteration other than the last, the resulting value of the body must be a list. The last iteration may produce either an atom or a list. The objects produced by the iterations are combined together as if they were arguments to the append function, and the resulting value is the value of the or operator. The alternate forms denoted by the adorned symbols and differ from and in the following way. The plain forms evaluate the in an environment in which none of the variables are yet visible. By contrast, the alternate forms evaluate each in an environment in which bindings for the previous variables are visible. In this phase of evaluation, variables are list-valued: one by one they are each bound to the list object emanating from their corresponding Just before the first loop iteration, however, the variables are assigned the first item from each of their lists. The semantics of may be understood in terms of an equivalence to a code pattern involving
      (collect-each ((x xinit) (mapcar (lambda (x y)
                     (y yinit)) <--> body)
        body) xinit yinit) The variant may be understood in terms of the following equivalence involving for sequential binding and
      (collect-each* ((x xinit) (let* ((x xinit)
                      (y yinit)) <--> (y yinit))
        body) (mapcar (lambda (x y)
                            body)
                            x y)) However, note that the as well as each invocation of the binds fresh instances of the variables, whereas these operators are permitted to bind a single instance of the variables, which are first initialized with the initializing expressions, and then reused as iteration variables which are stepped by assignment. The other operators may be understood likewise, with the substitution of the function in the case of and and of the function in the case of and
     ;; print numbers from 1 to 10 and whether they are even or odd
     (each* ((n 1..11) ;; n is just a range object in this scope
             (even (collect-each ((m n)) (evenp m))))
       ;; n is an integer in this scope
       (format t "~s is ~s\n" n (if even "even" "odd")))
     1 is "odd"
     2 is "even"
     3 is "odd"
     4 is "even"
     5 is "odd"
     6 is "even"
     7 is "odd"
     8 is "even"
     9 is "odd"
     10 is "even" The macros and combine variable binding with loop iteration. The first argument is a list of variables with optional initializers, exactly the same as in the and operators. Furthermore, the difference between and is like that between and with regard to this list of variables. The second variant in the above syntax synopsis shows that when are absent, then a list of which is empty may be omitted from the syntax. The and macros execute these steps:

    1.  Establish an anonymous block over the entire form, allowing the operator to be used to terminate the loop.

    2.  Establish bindings for the specified variables similarly to and The variable bindings are visible over the each each and each

    3.  Evaluate If yields then the loop terminates. Each is evaluated, and the value of the last of these forms is the result value of the loop. If there are no then the result value is If the is omitted, then the test is taken to be true, and the loop does not terminate.

    4.  Otherwise, if yields true, then each is evaluated in turn. Then, each is evaluated in turn and processing resumes at step 2.
The and macros provide an iteration construct inspired by the ANSI Common Lisp and macros. Each element in the form must be a symbol suitable for use as a variable name. The are placed into an implicit meaning that a which is an integer, character or symbol is interpreted as a label which may be the target of a control transfer via the macro. The macro binds each to the value produced by evaluating the adjacent Then, in the environment in which these variables now exist, is evaluated. If that form yields then the loop terminates. The are evaluated, and the value of the last one is returned. If are absent, then is returned. If is also absent, then the loop terminates and returns If produces a true value, then are not evaluated. Instead, the implicit consisting of the is evaluated. If that evaluation terminates normally, the loop variables are then updated by assigning to each the value of The following defaulting behaviors apply in regard to the variable syntax. For each which has an associated but no the is duplicated and taken as the Thus a variable specification like is equivalent to If both forms are omitted, then the is taken to be and the is taken to be This means that the variable form is equivalent to which has the effect that retains its current value when the next loop iteration begins. Lastly, the variant is equivalent to so that is also equivalent to The differences between and are: binds the variables in parallel, similarly to whereas binds sequentially, like moreover, performs the assignments in parallel as if using a single form, whereas performs the assignment sequentially as if using rather than The and macros establish an anonymous allowing early return from the loop, with a value, via the operator. These macros are substantially different from the ANSI Common Lisp and macros. Firstly, the termination logic is inverted; effectively they implement "while" loops, whereas their ANSI CL counterparts implement "until" loops. Secondly, in the ANSI CL macros, the defaulting of the missing is different. Variables with no are not updated. In particular, this means that the form is not equivalent to the ANSI CL macros do not feature the automatic replication of into the position. The macros and behave very similarly to the operator. Whereas the operator form returns as its result, the and forms, if they execute to completion and return normally, return an accumulated value. The macro initializes newly instantiated, hidden accumulator variable to the value For each iteration of the loop, the are evaluated, and are expected to produce a value. This value is added to the current value of the hidden accumulator using the function, and the result is stored into the accumulator. If returns normally, then the value of this accumulator becomes its resulting value. The macro similarly initializes a hidden accumulator to the value The value from each iteration of the body is multiplied with the accumulator using the function, and the result is stored into the accumulator. If returns normally, then the value of this accumulator becomes its resulting value. The and variants of the macros implement the sequential scoping rule for the variable bindings, exactly the way alters the semantics of The are enclosed in an implicit anonymous block. If the forms terminate by returning from the anonymous block then these macros terminate with the specified value. When and are specified with variables whose values specify zero iterations, or with no variables at all, the form terminates with a value of In this situation, and terminate with Note that this behavior differs from and its closely-related operators, which loop infinitely when no variables are specified. It is unspecified whether and continue iterating when the accumulator takes on a value satisfying the predicate. These macros iterate zero or more variables over sequences, similarly to the operator, and calculate logical results, with short-circuiting semantics. The macro initializes an internal result variable to the value. It then evaluates the for each tuple of variable values, replacing the result variable with the value produced by these forms. If that value is the iteration stops. When the iteration terminates normally, the value of the result variable is returned. If no variables are specified, termination occurs immediately. Note that this is different from the operator, which iterates indefinitely if no variables are specified. The are surrounded by an implicit anonymous block, making it possible to terminate via or In these cases, the form terminates with or the specified return value. The internal result is ignored. The macro is similar to with the following differences. The internal result variable is initialized to rather than The iteration stops whenever the produce a true value, and that value is returned. The and macros are, respectively, similar to and with one difference. After each iteration, the value produced by the is logically inverted using the function prior to being assigned to the result variable.
  (each-true ()) -> t
  (each-true ((a ()))) -> t
  (each-true ((a '(1 2 3))) a) -> 3
  (each-true ((a '(1 2 3))
              (b '(4 5 6)))
    (< a b))
  -> t
  (each-true ((a '(1 2 3))
              (b '(4 0 6)))
    (< a b))
  -> nil
  (some-true ((a '(1 2 3))) a) -> 1
  (some-true ((a '(nil 2 3))) a) -> 2
  (some-true ((a '(nil nil nil))) a) -> nil
  (some-true ((a '(1 2 3))
              (b '(4 0 6)))
    (< a b))
  -> t
  (some-true ((a '(1 2 3))
              (b '(0 1 2)))
    (< a b))
  -> nil
  (each-false ((a '(1 2 3))
               (b '(4 5 6)))
    (> a b))
  -> t
  (each-false ((a '(1 2 3))
               (b '(4 0 6)))
    (> a b))
  -> nil
  (some-false ((a '(1 2 3))
               (b '(4 0 6)))
    (> a b))
  -> t
  (some-false ((a '(1 2 3))
               (b '(0 1 2)))
    (> a b))
  -> nil The macros and have a similar syntax to and However, instead of iterating over sequences in parallel, they iterate over the Cartesian product of the elements from the sequences. The difference between and is analogous to that between the functions and Like in the operator family, the are surrounded by an anonymous block. If these forms execute a return from this block, then these macros terminate with the specified return value. When no iterations are performed, including in the case when an empty list of variables is specified, all these macro forms terminate and return Note that this behavior differs from and its closely-related operators, which loop infinitely when no variables are specified. With one caveat noted below, these macros can be understood as providing syntactic sugar according to the pattern established by the following equivalences:
  (each-prod (block nil
    ((x xinit) (let ((#:gx xinit) (#:gy yinit))
     (y yinit)) <--> (maprodo (lambda (x y)
    body) body)
                        #:gx #:gy))
  (collect-each-prod (block nil
    ((x xinit) (let ((#:gx xinit) (#:gy yinit))
     (y yinit)) <--> (maprod (lambda (x y)
    body) body)
                        #:gx #:gy))
  (append-each-prod (block nil
    ((x xinit) (let ((#:gx xinit) (#:gy yinit))
     (y yinit)) <--> (maprend (lambda (x y)
    body) body)
                        #:gx #:gy)) However, note that each invocation of the binds fresh instances of the variables, whereas these operators are permitted to bind a single instance of the variables, which are then stepped by assignment.
  (collect-each-prod ((a '(a b c))
                      (n #(1 2)))
    (cons a n))
 --> ((a . 1) (a . 2) (b . 1) (b . 2) (c . 1) (c . 2)) The macros and are variants of and with sequential binding. These macros can be understood as providing syntactic sugar according to the pattern established by the following equivalences:
  (each-prod* (let* ((x xinit)
    ((x xinit) (y yinit))
     (y yinit)) <--> (maprodo (lambda (x y) body)
    body) x y)
  (collect-each-prod* (let* ((x xinit)
    ((x xinit) (y yinit))
     (y yinit)) <--> (maprod (lambda (x y) body)
    body) x y)
  (append-each-prod* (let* ((x xinit)
    ((x xinit) (y yinit))
     (y yinit)) <--> (maprend (lambda (x y) body)
    body) x y) However, note that the as well as each invocation of the binds fresh instances of the variables, whereas these operators are permitted to bind a single instance of the variables, which are first initialized with the initializing expressions, and then reused as iteration variables which are stepped by assignment.
  (collect-each-prod* ((a "abc")
                       (b (upcase-str a)))
    `@a@b`)
  --> ("aA" "aB" "aC" "bA" "bB" "bC" "cA" "cB" "cC") The macros and have a similar syntax to and However, instead of iterating over sequences in parallel, they iterate over the Cartesian product of the elements from the sequences. The macros and variants perform sequential variable binding when establishing the initial values of the variables, similarly to the operator. The are surrounded by an implicit anonymous block. If these forms execute a return from this block, then these macros terminate with the specified return value. When no iterations are specified, including in the case when an empty list of variables is specified, the summing macros terminate, yielding and the multiplicative macros terminate with Note that this behavior differs from and its closely-related operators, which loop infinitely when no variables are specified.
  ;; Inefficiently calculate (+ (* 1 2 3) (* 4 3 2)).
  ;; Every value from (1 2 3) is paired with every value
  ;; from (4 3 2) to form a partial products, and
  ;; sum-each-prod adds these together implicitly:
  (sum-each-prod ((x '(1 2 3))
                  (y '(4 3 2)))
    (* x y))
  -> 54 The operator introduces a named block around the execution of some forms. The argument may be any object, though block names are usually symbols. Two block objects are considered to be the same name according to equality. Since a block name is not a variable binding, keyword symbols are permitted, and so are the symbols and A block named by the symbol nil is slightly special: it is understood to be an anonymous block. The operator differs from in that it evaluates which is expected to produce a symbol. The resulting symbol is used for the name of the block. A named or anonymous block establishes an exit point for the or operator, respectively. These operators can be invoked within a block to cause its immediate termination with a specified return value. A block also establishes a prompt for a delimited continuation. Anywhere in a block, a continuation can be captured using the function. Delimited continuations are described in the section Delimited Continuations. A delimited continuation allows an apparently abandoned block to be restarted at the capture point, with the entire call chain and dynamic environment between the prompt and the capture point intact. Blocks in TXR Lisp have dynamic scope. This means that the following situation is allowed:
  (defun func () (return-from foo 42))
  (block foo (func)) The function can return from the block even though the block does not lexically surround It is because blocks are dynamic that the variant exists; for lexically scoped blocks, it would make little sense to have support a dynamically computed name. Thus blocks in TXR Lisp provide dynamic nonlocal returns, as well as returns out of lexical nesting. It is permitted for blocks to be aggressively by compilation. This means that a form which meets certain criteria is converted to a form which surrounds the and thus no longer establishes an exit point. A form will be spared from by the compiler if it meets the following rules.

    1.  Any references the block's in a or expression.

    2.  The form contains at least one direct call to a function not in the standard TXR Lisp library.

    3.  The form contains at least one direct call to the functions or

    4.  The form references any of the functions in rules 2 and 3 as a function binding via the operator (or the DWIM brackets notation) or via the operator.

    5.  The form is a form; these are spared from the optimization.

    Removal of blocks under the above rules means that some use of blocks which works in interpreted code will not work in compiled programs. Programs which adhere to the rules are not affected by such a difference. Additionally, the compiler may blocks in contravention of the above rules, but only if doing so makes no difference to visible program behavior.
      (defun helper ()
        (return-from top 42))
      ;; defun implicitly defines a block named top
      (defun top ()
        (helper) ;; function returns 42
        (prinl 'notreached)) ;; never printed
      (defun top2 ()
        (let ((h (fun helper)))
          (block top (call h)) ;; may progn-convert
          (block top (call 'helper)) ;; may progn-convert
          (block top (helper)))) ;; not removed In the above examples, the block containing may be converted to because it doesn't express a direct call to the function. The block which calls using is also not considered to be making a direct call. In Common Lisp, blocks are lexical. A separate mechanism consisting of catch and throw operators performs nonlocal transfer based on symbols. The TXR Lisp example:
      (defun func () (return-from foo 42))
      (block foo (func)) is not allowed in Common Lisp, but can be transliterated to:
      (defun func () (throw 'foo 42))
      (catch 'foo (func)) Note that foo is quoted in CL. This underscores the dynamic nature of the construct. itself is a function and not an operator. Also note that the CL example, in turn, is even more closely transcribed back into TXR Lisp simply by replacing its and with and
      (defun func () (return* 'foo 42))
      (block* 'foo (func)) Common Lisp blocks also do not support delimited continuations. The operator must be dynamically enclosed within an anonymous block (a block named by the symbol It immediately terminates the evaluation of the innermost anonymous block which encloses it, causing it to return the specified value. If the value is omitted, the anonymous block returns The operator must be dynamically enclosed within a named block whose name matches the argument. It immediately terminates the evaluation of the innermost such block, causing it to return the specified value. If the value is omitted, that block returns
        (block foo
          (let ((a "abc\n")
                (b "def\n"))
            (pprint a *stdout*)
            (return-from foo 42)
            (pprint b *stdout*))) Here, the output produced is The value of is not printed because. terminates block and so the second pprint form is not evaluated. The function is similar to the operator, except that is an ordinary function parameter, and so when is used, an argument expression must be specified which evaluates to a symbol. Thus allows the target block of a return to be dynamically computed. The following equivalence holds between the operator and function:
      (return-from a b) <--> (return* 'a b) Expressions used as arguments to which do not simply quote a symbol have no equivalent in The macro provides a form of the "go to" control construct. The arguments of a form are a mixture of zero or more forms and go labels. The latter consist of those arguments which are symbols, integers or characters. Labels are not considered by and to be forms, and are not subject to macro expansion or evaluation. The macro is available inside It is erroneous for a form to occur outside of a This situation is diagnosed by global macro called which unconditionally throws an error. In the absence of invocations of or other control transfers, the macro evaluates each in left-to-right order. The labels are ignored. After the last is evaluated, the form terminates, and yields Any itself, or else any of its subforms, may be the form where matches one of the labels of a surrounding When this form is evaluated, then the evaluation of is immediately abandoned, and control transfers to the specified label. The forms are then evaluated in left-to-right order starting with the form immediately after that label. If the label is not followed by any forms, then the terminates. If doesn't match to any label in any surrounding the form is erroneous. The abandonment of a by invocation of is a dynamic transfer. All necessary unwinding inside takes place. The labels are lexically scoped, but dynamically bound. Their scope being lexical means that the labels are not visible to forms which are not enclosed within the even if their evaluation is invoked from that The dynamic binding means that the labels of a form are established when it begins evaluating, and removed when that form terminates. Once a label is removed, it is not available to be the target of a control transfer, even if that form has the label in its lexical scope. Such an attempted transfer is erroneous. It is permitted for forms to nest arbitrarily. The labels of an inner are not visible to an outer However, the reverse is true: a form in an inner may branch to a label in an outer in which case the entire inner terminates. In cases where the same objects are used as labels by an inner and outer the inner labels shadow the outer labels. There is no restriction on what kinds of symbols may be labels. Symbols in the package as well as the symbols and are valid labels. ANSI Common Lisp supports only symbols and integers as labels (which are called "go tags"); characters are not supported.
      ;; print the numbers 1 to 10
      (let ((i 0))
        (tagbody
          (go skip) ;; forward goto skips 0
         again
          (prinl i)
         skip
          (when (<= (inc i) 10)
            (go again))))
      ;; Example of erroneous usage: by the time func is invoked
      ;; by (call func) the tagbody has already terminated. The
      ;; lambda body can still "see" the label, but it doesn't
      ;; have a binding.
      (let (func)
        (tagbody
          (set func (lambda () (go label)))
          (go out)
         label
          (prinl 'never-reached)
          out)
        (call func))
      ;; Example of unwinding when the unwind-protect
      ;; form is abandoned by (go out). Output is:
      ;; reached
      ;; cleanup
      ;; out
      (tagbody
         (unwind-protect
           (progn
             (prinl 'reached)
             (go out)
             (prinl 'notreached))
           (prinl 'cleanup))
       out
         (prinl 'out)) The and macros combine the features of and respectively, anonymous block and The macro treats the and expressions similarly to establishing variable bindings in parallel. The macro treats these expressions in a similar way to The forms enclosed are treated like the argument forms of the macro: labels are permitted, along with use of Finally, an anonymous block is established around all of the enclosed forms (both the and allowing the use of to terminate evaluation with a value. The macro may be understood according to the following equivalence:
       (prog vars forms ...) <--> (block nil
                            (let vars
                            (tagbody forms ...))) Likewise, the macro follows an analogous equivalence, with replaced by The function treats the object as a Lisp expression, which is expanded and evaluated. The side effects implied by the form are performed, and the value which it produces is returned. The optional argument specifies an environment for resolving the function and variable references encountered in If this argument is omitted, then evaluation takes place in the global environment. The optional object specifies a macro environment for expanding macros encountered in If this argument is omitted, then may refer to only global macros. If both and are specified, then takes precedence over behaving like a more nested scope. Definitions contained in shadow same-named definitions in The is not expanded all at once. Rather, it is treated by the following algorithm:

    1.  First, if is a macro, it is macro-expanded as if by an application of the function (with a suitable environment argument, calculated by a combination of and

    2.  If the resulting expanded form is a or form, then iterates over that form's argument expressions, passing each expression to a recursive call to using the same

    3.  Otherwise, if the expanded form isn't one of the above three kinds of expressions, it is subject to a full expansion and evaluation.

    This algorithm allows a sequence of top-level forms to be combined into a single top-level form, even when the expansion of forms occurring later in the sequence depends on the evaluation effects of forms earlier in the sequence. For instance, a form like may be processed with because the above algorithm ensures that the expression is fully evaluated first, thereby providing the macro definition required by This expansion and evaluation order is important because the semantics of forms the reference model for how the function processes top-level forms. Moreover, file compilation perform a similar treatment of top-level forms and incremental macro compilation. The result is that the behavior is consistent between source files and compiled files. See the sections Top-Level Forms and File Compilation Model. Note that, according to these rules, the constituent body forms of a or top-level form are not individual top-level forms, even if the expansion of the construct combines the expanded versions of those forms with The form will therefore not work correctly. However, the specific problem in this situation can be be resolved by rewriting as a macro: See also: the function. The function determines whether is a constant form, with respect to environment If is absent, the global environment is used. The argument is used for fully expanding prior to analyzing. Currently, returns true for any form which, after macro-expansion, is any of the following: a compound form with the symbol in its first position; a non-symbolic atom; or one of the symbols which evaluate to themselves and cannot be bound as variables. These symbols are the keyword symbols, and the symbols and Additionally, returns true for a compound form, or a DWIM form, whose symbol is the member of a set a large number of constant-foldable library functions, and whose arguments are, recursively, expressions for the same environment. The arithmetic functions are members of this set. For all other inputs, returns Note: some uses of require manual expansion.
      (constantp nil) -> t
      (constantp t) -> t
      (constantp :key) -> t
      (constantp :) -> t
      (constantp 'a) -> nil
      (constantp 42) -> t
      (constantp '(+ 2 2 [* 3 (/ 4 4)])) -> t
      ;; symacrolet form expands to 42, which is constant
      (constantp '(symacrolet ((a 42)) a))
      (defmacro cp (:env e arg)
        (constantp arg e))
      ;; macro call (cp 'a) is replaced by t because
      ;; the symbol a expands to (+ 2 2) in the given environment,
      ;; and so (* a a) expands to (* (+ 2 2) (+ 2 2)) which is constantp.
      (symacrolet ((a (+ 2 2)))
        (cp '(* a a))) -> t The function creates an environment object suitable as the parameter. The and parameters, if specified, should be association lists, mapping symbols to objects. The objects in should be functions, or objects callable as functions. The argument, if specified, should be an environment. Note: bindings can also be added to an environment using the and functions. These functions bind a symbol to a value in either the function or variable space of environment Values established in the function space should be functions or objects that can be used as functions such as lists, strings, arrays or hashes. If already exists in the environment, in the given space, then its value is updated with If is specified as then the binding takes place in the global environment. These function retrieve the components of which must be an environment. The function retrieves the association list representing variable bindings. Similarly, the retrieves the association list of function bindings. The function retrieves the next environment, if has one, otherwise If is an environment constructed by the expression then retrieves retrieves and returns If given a argument, the function retrieves the value of the global function binding of the given if it has one: that is, the function object bound to the If has no global function binding, then is returned. The function supports method names of the form where names a struct type, and is either a static slot or one of the keyword symbols or which refer to special functions associated with a structure type. Names in this format are returned by the function. The function also supports names of the form which denote macros. Thus, provides unified access to functions, methods and macros. If a expression is passed to then the expression is macro-expanded and if that is successful, the function implied by that expression is returned. It is unspecified whether this function is interpreted or compiled. The function retrieves the value of the global macro binding of if it has one. Note: the name of this function has nothing to do with symbol macros; it is named for consistency with and referring to the "macro-expander binding of the symbol cell". The value of a macro binding is a function object. Intrinsic macros are C functions in the TXR kernel, which receive the entire macro call form and macro environment, performing their own destructuring. Currently, macros written in TXR Lisp are represented as curried C functions which carry the following list object in their environment cell: Local macros created by have in place of the environment object. This representation is likely to change or expand to include other forms in future TXR versions. The function retrieves the value stored in the dynamic binding of that is apparent in the current context. If the variable has no dynamic binding, then retrieves its value in the global environment. If has no variable binding, but is defined as a global symbol macro, then the value of that symbol macro binding is retrieved. The value of a symbol macro binding is simply the replacement form. Rather than throwing an exception, each of these functions returns if the argument symbol doesn't have the binding in the respective namespace or namespaces which that function searches. A or form denotes a place, if has a binding of the respective kind. This place may be assigned to or deleted. Assignment to the place causes the denoted binding to have a new value. Deleting a place with the macro removes the binding, and returns the previous contents of that binding. A binding denoted by a form is removed using one denoted by by is removed using and a binding denoted by is removed using Deleting a method via is not possible; an attempt to do so has no effect. Storing a value, using any one of these three accessors, to a nonexistent variable, function or macro binding, is not erroneous. It has has the effect of creating that binding. Using accessor to assign to a lambda expression is erroneous. Deleting a binding, using any of these three accessors, when the binding does not exist, also isn't erroneous. There is no effect and the operator yields as the prior value, consistent with the behavior when accessors are used to retrieve a nonexistent value. In ANSI Common Lisp, the function retrieves a function, macro or special operator binding of a symbol. These are all in one space and may not coexist. In TXR Lisp, it retrieves a symbol's function binding only. Common Lisp has an accessor named similar to returns if the is bound as a variable or symbol macro in the global environment, otherwise returns if the has a function binding in the global environment, the method specified by exists, or a lambda expression argument is given. Otherwise it returns returns if the symbol has an operator macro binding in the global environment, otherwise The function in ANSI Common Lisp doesn't report that global symbol macros have a binding. They are not considered bindings. In TXR Lisp, they are considered bindings. The ANSI Common Lisp yields true if its argument has a function, macro or operator binding, whereas the TXR Lisp does not consider operators or macros. The ANSI CL does not yield true for lambda expressions. Behavior similar to the Common Lisp expression in Common Lisp can be obtained in TXR Lisp using the
      (or (fboundp x) (mboundp x) (special-operator-p x)) expression, except that this will also yield true when is a lambda expression. The function doesn't exist in ANSI Common Lisp. The function removes the binding of from either the dynamic environment or the global symbol macro environment. After the call to appears to be unbound. If the call takes place in a scope in which there exists a dynamic rebinding of the information for restoring the previous binding is not affected by When that scope terminates, the previous binding will be restored. If the call takes place in a scope in which the dynamic binding for is the global binding, then the global binding is removed. When the global binding is removed, then if was previously marked as special (for instance by this marking is removed. Otherwise if has a global symbol macro binding, that binding is removed. If has no apparent dynamic binding, and no global symbol macro binding, does nothing. In all cases, returns The behavior of differs from its counterpart in ANSI Common Lisp. The function in Common Lisp only removes a value from a dynamic variable. The dynamic variable does not cease to exist, it only ceases to have a value (because a binding is a value). In TXR Lisp, the variable ceases to exist. The binding of a variable isn't its value, it is the variable itself: the association between a name and an abstract storage location, in some environment. If the binding is undone, the variable disappears. The function in Common Lisp does not remove global symbol macros, which are not considered to be bindings in the variable namespace. That is to say, the Common Lisp does not report true for symbol macros. The Common Lisp also doesn't remove the special attribute from a symbol. If a variable is introduced with and then removed with the symbol continues to exhibit dynamic binding rather than lexical in subsequent scopes. In TXR Lisp, if a global binding is removed, so is the special attribute. The function removes any binding for from the function namespace of the global environment. If has no such binding, it does nothing. In either case, it returns The function removes any binding for from the operator macro namespace of the global environment. If has no such binding, it does nothing. In either case, it returns The behavior of differs from its counterpart in ANSI Common Lisp. The function in Common Lisp removes a function or macro binding, which do not coexist. The function doesn't exist in Common Lisp. The function retrieves a source code form of which must be an interpreted function. The source code form has the syntax The tries to resolve the function object to a name. If that is not possible, it returns The resolution is performed by an exhaustive search through up to three spaces. If an environment is specified by then this is searched first. If a binding is found in that environment which resolves to the function, then the search terminates and the binding's symbol is returned as the function's name. If the search through environment fails, or if that argument is not specified, then the global environment is searched for a function binding which resolves to If such a binding is found, then the search terminates, and the binding's symbol is returned. If two or more symbols in the global environment resolve to the function, it is not specified which one is returned. If the global function environment search fails, then the function is considered as a possible macro. The global macro environment is searched for a macro binding whose expander function is similarly to the way the function environment was searched. If a binding is found, then the syntax is returned, where is the name of the global macro binding that was found which resolves to If two or more global macro bindings share it is not specified which of those bindings provides If the global macro search fails, then is considered as a possible method. The static slot space of all struct types is searched for a slot which contains If such a slot is found, then the method name is returned, consisting of the syntax where is a symbol denoting the struct type and is the static slot of the struct type which holds A check is also performed whether might be equal to one of the two special functions of a structure type: its or in which case it is returned as either the or the syntax. If is an interpreted function not found under any name, then a lambda expression denoting that function is returned in the syntax If cannot be identified as a function, then is returned. The function retrieves the environment object associated with function The environment object holds the captured bindings of a lexical closure. The reports number of fixed parameters. The fixed parameters consist of the required parameters and the optional parameters. Variadic functions have a parameter which captures the remaining arguments which are in excess of the fixed parameters. That parameter is not considered a fixed parameter and therefore doesn't contribute to this count. The reports number of optional parameters. The argument must be a function. Note: if a function isn't variadic (see the function) then the value reported by represents the maximum number of arguments which can be passed to the function. The minimum number of required arguments can be calculated for any function by subtracting the value from from the value from The function returns if is a variadic function, otherwise The argument must be a function. The function returns if is an interpreted function, otherwise it returns The function returns if a function compiled for the virtual machine: a function representation produced by means of the functions or If is of any other type, the function returns The function returns if is a symbol marked for special variable binding, otherwise it returns Symbols are marked special by and The function returns if is a symbol which names a special operator, otherwise it returns The symbol macro indicates the current function name, There is a global symbol macro which expands to Around certain kinds of named functions, a local binding for is established which provides the function name. The purpose of this name is for use in diagnostic messages; therefore it is an abbreviated name. The macro is established for and forms. It is also established for methods defined inside a form including the methods and The macro is visible not only to the its function's body, but also to the expressions inside the parameter list which compute the default values for optional parameters. The name provided by is intended for use in diagnostic messages and is therefore an informal name, and not the formal name which can be passed to to retrieve the function. In the case of a function named the name is that symbol, Thus, in this case, the name is the same as the formal name. In the case of a named also expands to the symbol x but that is the formal name of the macro, which is In the case of a method of a structure type is the two-element list rather than the formal name
      ;; log a message naming the function
      (defun connect-to-host (addr)
        (format t "~s: connecting to host ~s" %fun% addr)) In TXR Lisp, objects obey the following type hierarchy. In this type hierarchy, the internal nodes denote abstract types: no object is an instance of an abstract type. Nodes in square brackets indicate an internal structure in the type graph, invisible to programs, and angle brackets indicate a plurality of types which are not listed by name:
      t ----+--- [cobj types] ---+--- hash
            | |
            | +--- hash-iter
            | |
            | +--- stream
            | |
            | +--- random-state
            | |
            | +--- regex
            | |
            | +--- buf
            | |
            | +--- tree
            | |
            | +--- tree-iter
            | |
            | +--- seq-iter
            | |
            | +--- cptr
            | |
            | +--- dir
            | |
            | +--- struct-type
            | |
            | +--- <all structures>
            | |
            | +--- ... others
            |
            |
            +--- sequence ---+--- string ---+--- str
            | | |
            | | +--- lstr
            | | |
            | | +--- lit
            | |
            | +--- list ---+--- null
            | | |
            | | +--- cons
            | | |
            | | +--- lcons
            | |
            | +--- vec
            | |
            | +--- <structures with car or length methods>
            |
            +--- number ---+--- float
            | |
            | +--- integer ---+--- fixnum
            | |
            | +--- bignum
            |
            +--- chr
            |
            +--- sym
            |
            +--- env
            |
            +--- range
            |
            +--- tnode
            |
            +--- pkg
            |
            +--- fun
            |
            +--- args In addition to the above hierarchy, the following relationships also exist:
      t ---+--- atom --- <any type other than cons> --- nil
           |
           +--- cons ---+--- lcons --- nil
                        |
                        +--- nil
      sym --- null
      struct ---- <all structures> That is to say, the types are exhaustively partitioned into atoms and conses; an object is either a or else it isn't, in which case it is the abstract type The type is odd in that it is both an abstract type, serving as a supertype for the type and it is also a concrete type in that regular conses are of this type. The type is an abstract type which is empty. That is to say, no object is of type This type is considered the abstract subtype of every other type, including itself. The type is not to be confused with the type which is the type of the symbol. Because the type of is the type and is also a symbol, the type is a subtype of Lastly, the symbol serves as the supertype of all structures. The function returns a symbol representing the type of The core types are identified by the following symbols: Cons cell. String. Literal string embedded in the TXR executable image. Character. Fixnum integer: an integer that fits into the value word, not having to be heap-allocated. A bignum integer: arbitrary precision integer that is heap-allocated. Floating-point number. Symbol. Symbol package. Function. Vector. Lazy cons. Range object. Lazy string. Function/variable binding environment. Hash table. I/O stream of any kind. Regular-expression object. A structure type: the type of any one of the values which represents a structure type. Binary search tree node. Binary search tree. Function argument list represented as an object.

There are more kinds of objects, such as user-defined structures. The function tests whether and name a pair of types, such that the left type is a subtype of the right type. The arguments are either type symbols, or structure type objects, as returned by the function. Thus, the symbol which is the name of a predefined struct type, and the object returned by are considered equivalent argument values. If either argument doesn't name a type, the behavior is unspecified. Each type is a subtype of itself. Most other type relationships can be inferred from the type hierarchy diagrams given in the introduction to this section. In addition, there are inheritance relationships among structures. If and are both structure types, then yields true if the types are the same struct type, or if the right type is a direct or indirect supertype of the left. The type symbol is a supertype of all structure types. The function tests whether the type of is a subtype of the type named by The following equivalence holds:
  (typep a b) --> (subtypep (typeof a) b) The macro evaluates and then successively tests its type against each clause. Each clause consists of a type symbol and zero or more The first clause whose is a supertype of the type of value is considered to be the matching clause. That clause's are evaluated, and the value of the last form is returned. If there is no matching clause, or there are no clauses present, or the matching clause has no then is returned. Note: since is the supertype of every type, a clause whose is the symbol always matches. If such a clause is placed as the last clause of a it provides a fallback case, whose forms are evaluated if none of the previous clauses match. The macro is the error-catching variant of similar to the relationship between the and families of macros. If one of the clauses has a which is the symbol then is precisely equivalent to Otherwise, a clause with a of and which throws an exception of type derived from is appended to the existing clauses, after which the semantics follows that of The function returns if is a symbol which is the name of a built-in type. For all other objects it returns The function returns its argument. If the function is given at least one argument, then it returns its leftmost argument, otherwise it returns The function is a synonym of The function is useful as a functional argument, when a transformation function is required, but no transformation is actually desired. In this role, the synonym leads to readable code. For instance:
  ;; construct a function which returns its integer argument
  ;; if it is odd, otherwise it returns its successor.
  ;; "If it's odd, use it, otherwise take its successor".
  [iff oddp use succ]
  ;; Applications of the function:
  [[iff oddp use succ] 3] -> 3 ;; use applied to 3
  [[iff oddp use succ] 2] -> 3 ;; succ applied to 2 The and functions are synonyms. They tests whether is the object They return if this is the case, otherwise.
  (null '()) -> t
  (null nil) -> t
  (null ()) -> t
  (false t) -> nil
  (if (null x) (format t "x is nil!"))
  (let ((list '(b c d)))
    (if (not (memq 'a list))
      (format t "list ~s does not contain the symbol a\n"))) The function is the complement of the and functions. The function is a synonym for It return if the is any object other than If is it returns Note: programs should avoid explicitly testing values with true. For instance should be favored over However, the latter is useful with the macro because binds the variable to the value of no matter what kind of form is, which is not true in the form.
   ;; Compute indices where the list '(1 nil 2 nil 3)
   ;; has true values:
   [where '(1 nil 2 nil 3) true] -> (1 3) The principal equality test functions and test whether two objects are equivalent, using different criteria. They return if the objects are equivalent, and otherwise. The function uses the strictest equivalence test, called implementation equality. The eq function returns if and only if, and are actually the same object. The test is implemented by comparing the raw bit pattern of the value, whether or not it is an immediate value or a pointer to a heaped object. Two character values are if they are the same character, and two fixnum integers are if they have the same value. All other object representations are actually pointers, and are if and only if they point to the same object in memory. So, for instance, two bignum integers might not be even if they have the same numeric value, two lists might not be even if all their corresponding elements are and two strings might not be eq even if they hold identical text. The function is slightly less strict than The difference between and is that if and are numbers which are of the same kind and have the same numeric value, returns even if they are different objects. Note that an integers and a floating-point number are not even if one has a value which converts to the other: thus, yields a comparison expression which finds these numbers equal is The function also specially treats range objects. Two distinct range objects are if their corresponding and fields are For all other object types, behaves like The function is less strict still than In general, it recurses into some kinds of aggregate objects to perform a structural equivalence check. For struct types, it also supports customization via equality substitution. See the Equality Substitution section under Structures. Firstly, if and are then they are also though the converse isn't necessarily the case. If two objects are both cons cells, then they are equal if their fields are and their fields are If two objects are vectors, they are if they have the same length, and their corresponding elements are If two objects are strings, they are if they are textually identical. If two objects are functions, they are if they have environments, and if they have the same code. Two compiled functions are considered to have the same code if and only if they are pointers to the same function. Two interpreted functions are considered to have the same code if their list structure is Two hashes are if they use the same equality (both are or both are or else both are if their associated user data elements are equal (see the function if their sets of keys are identical, and if the data items associated with corresponding keys from each respective hash are objects. Two ranges are if their corresponding and fields are equal. For some aggregate objects, there is no special semantics. Two arguments which are symbols, packages, or streams are if and only if they are the same object. Certain object types have a custom function. The functions and are logically negated counterparts of, respectively, and If returns for a given pair of arguments and then returns Vice versa, if returns returns The same relationship exits between and and between and The functions and ("member equal" or "multi-equal") provide a particular kind of a generalization of the binary equality functions and to multiple arguments. The value is compared to each value using the corresponding binary equality function. If a match occurs, then is returned, otherwise The traversal of the argument values proceeds from left to right, and stops when a match is found. The function, when called with two arguments, determines whether compares less than in a generic way which handles arguments of various types. The argument syntax of is generalized. It can accept one argument, in which case it unconditionally returns regardless of that argument's value. If more than two arguments are given, then generalizes in a way which can be described by the following equivalence pattern, with the understanding that each argument expression is evaluated exactly once:
  (less a b c) <--> (and (less a b) (less b c))
  (less a b c d) <--> (and (less a b) (less b c) (less c d)) The function is used as the default for the argument of the functions and as well as the argument of the and The function is capable of comparing numbers, characters, symbols, strings, as well as lists and vectors of these. It can also compare buffers. If both arguments are the same object so that holds true, then the function returns regardless of the type of even if the function doesn't handle comparing different instances of that type. In other words, no object is less than itself, no matter what it is. The function pairs with the function. If values and are objects which are of suitable types to the function, then exactly one of the following three expressions must be true: or The relation is: antisymmetric, such that if is true, then then is false; irreflexive, such that is false; and transitive, such that and imply The following are detailed criteria that applies to arguments of different types and combinations thereof. If both arguments are numbers or characters, they are compared as if using the function. If both arguments are strings, they are compared as if using the function. If both arguments are symbols, the following rules apply. If the symbols have names which are different, then the result is that of their names being compared by the function. If is passed symbols which have the same name, and neither of these symbols has a home package, then the raw bit patterns of their values are compared as integers: effectively, the object with the lower machine address is considered lesser than the other. If only one of the two same-named symbols has no home package, then if that symbol is the left argument, returns otherwise If both same-named symbols have home packages, then the result of is that of applied to the names of their respective packages. Thus is less than If both arguments are conses, then they are compared as follows:

    1.  The function is recursively applied to the fields of both arguments. If it yields true, then is deemed to be less than

    2.  Otherwise, if the fields are unequal under the function, returns

    3.  If the fields are then is recursively applied to the fields of the arguments, and the result of that comparison is returned.

    This logic performs a lexicographic comparison on ordinary lists such that for instance is less than but not less than or Note that the empty list nil compared to a cons is handled by type-based precedence, described below. Two vectors are compared by lexicographically, similarly to strings. Corresponding elements, starting with element 0, of the vectors are compared until an index position is found where corresponding elements of the two vectors are not If this differing position is beyond the end of one of the two vectors, then the shorter vector is considered to be lesser. Otherwise, the result of is the outcome of comparing those differing elements themselves with Two buffers are also compared by lexicographically, as if they were vectors of integer byte values. Two ranges are compared by using lexicographic logic similar to conses and vectors. The fields of the ranges are first compared. If they are not equal then is applied to those fields and the result is returned. If the fields are then is applied to the fields and that result is returned. If the two arguments are of the above types, but of different types from each other, then resolves the situation based on the following precedence: numbers and characters are less than ranges, which are less than strings, which are less than symbols, which are less than conses, which are less than vectors, which are less than buffers. Note that since is a symbol, it is ranked lower than a cons. This interpretation ensures correct behavior when is regarded as an empty list, since the empty list is lexicographically prior to a nonempty list. If either argument is a structure for which the method is defined, the method is invoked on that argument, and the value returned is used in place of that argument for performing the comparison. Structures with no method cannot participate in a comparison, resulting in an error. See the Equality Substitution section under Structures. Finally, if either of the arguments has a type other than the above types, the situation is an error. The function is equivalent to with the arguments reversed. That is to say, the following equivalences hold:
      (greater a) <--> (less a) <--> t
      (greater a b) <--> (less b a)
      (greater a b c ...) <--> (less ... c b a) The function is used as the default for the argument of the and functions. The functions and are similar to and respectively, but differ in the following respect: when called with two arguments which compare true under the function, the and functions return When called with only one argument, both functions return and both functions generalize to three or more arguments in the same way as do and The function duplicates objects of various supported types: sequences, hashes, structures and random states. If is it returns Otherwise, is equivalent to invoking a more specific copying function according to the type of the argument, as follows:

    struct type

    For all other types of the invocation is erroneous. Except in the case when is returns a value that is distinct from (not to) This is different from the behavior of or which recognize that they need not make a copy of and just return it. Note however, that the elements of the returned sequence may be eq to elements of the original sequence. In other words, copy is a deeper copy than just duplicating the value itself, but it is not a deep copy. The function allocates, initializes and returns a single cons cell. A cons cell has two fields called and which are accessed by functions of the same name, or by the functions and which are synonyms for these. Lists are made up of conses. A (proper) list is either the symbol denoting an empty list, or a cons cell which holds the first item of the list in its and the list of the remaining items in The expression allocates and returns a single cons cell which denotes the one-element list The is so there are no additional items. A cons cell whose is an atom other than is printed with the dotted pair notation. For example the cell produced by is denoted The notation is perfectly valid as input, but the cell which it denotes will print back as The notations are equivalent. The dotted pair notation can be used regardless of what type of object is the cons cell's so that for instance denotes the cons cell whose is the symbol a and whose is the list This is exactly the same thing as In other words is exactly the same as Every list, and more generally cons-cell tree structure, can be written in a "fully dotted" notation, such that there are as many dots as there are cells. For instance the cons structure of the nested list can be made more explicit using The structure contains eight conses, and so there are eight dots in the fully dotted notation. The number of conses in a linear list like is simply the number of items, so that list in particular is made of three conses. Additional nestings require additional conses, so for instance requires four conses. A visual way to count the conses from the printed representation is to count the atoms, then add the count of open parentheses, and finally subtract one. A list terminated by an atom other than is called an improper list, and the dot notation is extended to cover improper lists. For instance is an improper list of two elements, terminated by and can be constructed using The fully dotted notation for this list is The function tests whether is an atom. It returns if this is the case, otherwise. All values which are not cons cells are atoms. is equivalent to
      (atom 3) -> t
      (atom (cons 1 2)) -> nil
      (atom "abc") -> t
      (atom '(3)) -> nil The function tests whether is a cons. It returns if this is the case, otherwise. is equivalent to Nonempty lists test positive under because a list is represented as a reference to the first cons in a chain of one or more conses. Note that a lazy cons is a cons and satisfies the test. See the function and the macro
      (consp 3) -> nil
      (consp (cons 1 2)) -> t
      (consp "abc") -> nil
      (consp '(3)) -> t The functions and are synonyms. If is a cons cell, these functions retrieve the field of that cons cell. yields For programming convenience, may be of several other kinds in addition to conses. is allowed, and returns may also be a vector or a string. If it is an empty vector or string, then is returned. Otherwise the first character of the string or first element of the vector is returned. may be a structure. The operation is possible if the object has a method. If so, invokes that method and returns whatever the method returns. If the structure has no method, but has a method, then the function calls that method with one argument, that being the integer zero. Whatever the method returns, returns. If neither method is defined, an error exception is thrown. A form denotes a valid place whenever is a valid argument for the function. Modifying the place denoted by the form is equivalent to invoking with as the left argument, and the replacement value as the right argument. It takes place in the manner given under the description function, and obeys the same restrictions. A form supports deletion. The following equivalence then applies:
      (del (car place)) <--> (pop place) This implies that deletion requires the argument of the form to be a place, rather than the whole form itself. In this situation, the argument place may have a value which is because is defined on an empty list. The abstract concept behind deleting a is that physically deleting this field from a cons, thereby breaking it in half, would result in just the remaining. Though fragmenting a cons in this manner is impossible, deletion simulates it by replacing the place which previously held the cons, with that cons' field. This semantics happens to coincide with deleting the first element of a list by a operation. The functions and are synonyms. If is a cons cell, these functions retrieve the field of that cons cell. yields For programming convenience, may be of several other kinds in addition to conses. is allowed, and returns may also be a vector or a string. If it is a nonempty string or vector containing at least two items, then the remaining part of the object is returned, with the first element removed. For example (cdr "abc") yields If is a one-element vector or string, or an empty vector or string, then is returned. Thus (cdr "a") and (cdr "") both result in If is a structure, then requires it to support either the method or the method. If both are present, is used. When the function uses the method, it invokes it with no arguments. Whatever value the method returns becomes the return value of When invokes a structure's method, it passes as the argument the range object Whatever the method returns becomes the return value of The invocation syntax of a or form is a syntactic place. The place is semantically correct if is a valid argument for the function. Modifying the place denoted by the form is equivalent to invoking with as the left argument, and the replacement value as the right argument. It takes place in the manner given under the description function, and obeys the same restrictions. A place supports deletion, according to the following near equivalence:
      (del (cdr place)) <--> (prog1 (cdr place)
                            (set place (car place))) The expression is evaluated only once. Note that this is symmetric with the delete semantics of in that the cons stored in goes away, as does the field, leaving just the which takes the place of the original cons. Example: Walk every element of the list using a loop:
        (for ((i '(1 2 3))) (i) ((set i (cdr i)))
          (print (car i) *stdout*)
          (print #\newline *stdout*)) The variable marches over the cons cells which make up the "backbone" of the list. The elements are retrieved using the function. Advancing to the next cell is achieved using If is the last cell in a (proper) list, yields and so becomes the loop guard expression fails and the loop terminates. If is a cons cell or lazy cons cell, then and functions assign new values into the and fields of the In addition, these functions are meaningful for other kinds of objects also. Note that, except for the difference in return value, is the same as the more generic and likewise can be written as The and functions return Note: In TXR versions 89 and earlier, these functions returned the new value. The behavior was undocumented. The argument does not have to be a cons cell. Both functions support meaningful semantics for vectors and strings. If is a string, it must be modifiable. The function replaces the first element of a vector or first character of a string. The vector or string must be at least one element long. The function replaces the suffix of a vector or string after the first element with a new suffix. The must be a sequence, and if the suffix of a string is being replaced, it must be a sequence of characters. The suffix here refers to the portion of the vector or string after the first element. It is permissible to use on an empty string or vector. In this case, specifies the contents of the entire string or vector, as if the operation were done on a nonempty vector or string, followed by the deletion of the first element. The argument may be a structure. In the case of the structure must have a defined method or else, failing that, a method. The first of these methods which is available, in the given order, is used to perform the operation. Whatever the respective method returns, If the method is used, it is called with two arguments (in addition to the integer zero, and In the case of the structure must have a defined method or else, failing that, a method. The first of these methods which is available, in the given order, is used to perform the operation. Whatever the respective method returns, If the method is used, it is called with two arguments (in addition to the range value and Used as functions, these accessors retrieve the elements of a sequence by position. If the sequence is shorter than implied by the position, these functions return When used as syntactic places, these accessors denote the storage locations by position. The location must exist, otherwise an error exception results. The places support deletion.
      (third '(1 2)) -> nil
      (second "ab") -> #\b
      (third '(1 2 . 3)) -> **error, improper list*
      (let ((x (copy "abcd")))
        (inc (third x))
        x) -> "abce" The function creates a new object which is a catenation of the arguments. All arguments are optional; produces the empty list, and if a single argument is specified, that argument is returned. If two or more arguments are present, then the situation is identified as one or more arguments followed by The arguments must be sequences; may be a sequence or atom. The operation over three or more arguments is left-associative, such that is equivalent to both and This allows the catenation of an arbitrary number of arguments to be understood in terms of a repeated application of the two-argument case, whose semantics is given by these rules:

    1.  catenates with to produce
          (append nil nil) -> nil

    2.  catenates with a proper or improper list, producing that list itself:
          (append nil '(1 2)) -> (1 2)
          (append nil '(1 2 . 3)) -> (1 2 . 3)

    3.  A proper list catenates with producing that list itself:
          (append '(1 2) nil) -> (1 2)

    4.  A proper list catenates with an atom, producing an improper list terminated by that atom, whether or not that atom is a sequence:
          (append '(1 2) #(3)) -> (1 2 . #(3))
          (append '(1 2) 3) -> (1 2 . 3)

    5.  A non-list sequence catenates with another sequence into a sequence, producing a sequence which contains the elements of both, of the same kind as the left sequence. The elements must be compatible; a string can only catenate with a sequence of characters.
          (append #(1 2) #(3 4)) -> #(1 2 3 4)
          (append "ab" "cd") -> "abcd"
          (append "ab" #(#\c #\d)) -> "abcd"
          (append "ab" #(3 4)) -> ;; error

    6.  A non-list sequence catenates with an atom if it is a suitable element type for that kind of sequence. The resulting sequence is of the same kind, and includes that atom:
          (append #(1 2) 3) -> #(1 2 3)
          (append "ab" #
          (append "ab" 3) -> ;; error

    7.  If an improper list is catenated with any object, the catenation takes place between the terminating atom of that list and that object. This requires the terminating atom to be a sequence. If the catenation is possible, then the result is a new improper list which is a copy of the original, but with the terminating atom replaced by a catenation of that atom and the object:
          (append '(1 2 . "ab") "c") -> (1 2 . "abc")
          (append '(1 2 . "ab") '(2 3)) -> ;; error

    8.  A non-sequence atom doesn't catenate; the situation is erroneous:
          (append 1 2) -> ;; error
          (append '(1 . 2) 3) -> ;; error

    If N arguments are specified, where N > 1, then the first N-1 arguments must be proper lists. Copies of these lists are catenated together. The last argument N, shown in the above syntax as may be any kind of object. It is installed into the field of the last cons cell of the resulting list. Thus, if argument N is also a list, it is catenated onto the resulting list, but without being copied. Argument N may be an atom other than in that case produces an improper list. The function works like but may destructively manipulate any of the input objects.
      ;; An atom is returned.
      (append 3) -> 3
      ;; A list is also just returned: no copying takes place.
      ;; The eq function can verify that the same object emerges
      ;; from append that went in.
      (let ((list '(1 2 3)))
        (eq (append list) list)) -> t
      (append '(1 2 3) '(4 5 6) 7) -> '(1 2 3 4 5 6 . 7))
      ;; the (4 5 6) tail of the resulting list is the original
      ;; (4 5 6) object, shared with that list.
      (append '(1 2 3) '(4 5 6)) -> '(1 2 3 4 5 6)
      (append nil) -> nil
      ;; (1 2 3) is copied: it is not the last argument
      (append '(1 2 3) nil) -> (1 2 3)
      ;; empty lists disappear
      (append nil '(1 2 3) nil '(4 5 6)) -> (1 2 3 4 5 6)
      (append nil nil nil) -> nil
      ;; atoms and improper lists other than in the last position
      ;; are erroneous
      (append '(a . b) 3 '(1 2 3)) -> **error**
      ;; sequences other than lists can be catenated.
      (append "abc" "def" "g" #\h) -> "abcdefgh"
      ;; lists followed by non-list sequences end with non-list
      ;; sequences catenated in the terminating atom:
      (append '(1 2) '(3 4) "abc" "def") -> (1 2 3 4 . "abcdef") The function lazily catenates lists. If invoked with no arguments, it returns If invoked with a single argument, it returns that argument. Otherwise, it returns a lazy list consisting of the elements of every argument from left to right. Arguments other than the last are treated as lists, and traversed using and functions to visit their elements. The last argument isn't traversed: rather, that object itself becomes the field of the last cons cell of the lazy list constructed from the previous arguments. The function returns a list consisting of appended to a reversed copy of The returned object shares structure with which is unmodified. The function behaves similarly, except that the returned object may share structure with not only but also which is modified. The function creates a new list, whose elements are the argument values.
      (list) -> nil
      (list 1) -> (1)
      (list 'a 'b) -> (a b) The function is a generalization of cons. If called with exactly two arguments, it behaves exactly like cons: is identical to If three or more arguments are specified, the leading arguments specify additional atoms to be consed to the front of the list. So for instance is the same as and produces the improper list Generalizing in the other direction, can be called with just one argument, in which case it returns that argument, and can also be called with no arguments in which case it returns
      (list*) -> nil
      (list* 1) -> 1
      (list* 'a 'b) -> (a . b)
      (list* 'a 'b 'c) -> (a b . c) Note that unlike in some other Lisp dialects, the effect of can also be obtained using However, cannot be rewritten as because the latter is equivalent to The function has the same parameters and semantics as the function, except that it operates on its argument using list operations, and assumes that is terminated by If a form is used as a place, then the argument form must also be a place. The place denotes a subrange of as if it were a storage location. The previous value of this location, if needed, is fetched by a call to Storing to the place is performed by a call to The return value of is stored into In an update operation which accesses the prior value and stores a new value, the arguments and are evaluated once. The function is like the function, except that it operates on its argument using list operations. It assumes that is terminated by and that it is made of cells which can be mutated using The and functions test, respectively, whether is a list, or a proper list, and return or accordingly. The test is weaker, and executes without having to traverse the object. The value produced by the expression is the same as that of except that is evaluated only once. The empty list is a list, and a cons cell is a list. The function returns only for proper lists. A proper list is either or a cons whose is a proper list. traverses the list, and its execution will not terminate if the list is circular. These functions return for list-like sequences that are not made of actual cells. Dialect Note: in TXR 137 and older, is called The name was changed for adherence to conventions and compatibility with other Lisp dialects, like Common Lisp. However, the function continues to be available under the old name. Code that must run on TXR 137 and older installations should use but its use going forward is deprecated. The function returns if is the object If is a cons cell, then returns Otherwise, function throws an exception. The function returns the length of which may be a proper or improper list. The length of a list is the number of conses in that list. The function which returns a list similar to but with a newly allocated cons-cell structure. If is an atom, it is simply returned. Otherwise, is a cons cell, and returns the same object as the expression Note that the object is not deeply copied, but only propagated by reference into the new list. produces a new list structure out of the same items that are in Common Lisp does not allow the argument to be an atom, except for the empty list The function determines whether the length of is less than the integer The expression
      (length-list-< x y) is similar to, but usefully different from
      (< (length-list x) y) because is required to only traverses far enough to be able to determine the return value. If the end of the list is reached before conses are encountered, the function returns otherwise if conses are encountered, the function terminates immediately and returns The function is therefore safe to use with infinite lazy lists and circular lists, for which would not terminate. Note: there is more generic function which works with efficiently with different kinds of sequences. Note: the is useful in situations when a decision must be made between two algorithms based on the length of one or more input lists. The decision can be made without wastefully performing a full pass over the input lists to measure their length. The function creates and returns a new object that is a replica of The argument must be either a cell, or else a lazy cons: an object of type A new cell of the same type as is created, and all of its fields are initialized by copying the corresponding fields from If is lazy, the newly created object is in the same state as the original. If the original has not yet been updated and thus has an update function, the copy also has not yet been updated and has the same update function. The function returns a copy of which represents an arbitrary structure. The cell structure of is traversed and a similar structure is constructed, but without regard for substructure sharing or circularity. More precisely, if is an atom, then it is returned. If it is an ordinary cell, then is recursively applied to the and fields to produce their individual replicas. A new cell is then produced from the replicated and If is a lazy then just like in the ordinary case, the and fields are duplicated with a recursive call to Then, a lazy is created from these replicated fields. If has an update function, then the newly created lazy has the same update function; the function isn't copied. Like the function doesn't trigger the update of lazy conses. The copies of lazy conses which have not been updated are also conses which have not been updated. Description: The functions and produce an object which contains the same items as proper list but in reverse order. If is then both functions return The function is non-destructive: it creates a new list. The function creates the structure of the reversed list out of the cons cells of the input list, thereby destructively altering it (if it contains more than one element). How uses the material from the original list is unspecified. It may rearrange the cons cells into a reverse order, or it may keep the structure intact, but transfer the values among cons cells into reverse order. Other approaches are possible. The function retrieves the n-th last cons cell of a list, indexed from one. The parameter must be a an integer. If is positive and so large that it specifies a nonexistent cons beyond the beginning of the list, returns Effectively, values of larger than the length of the list are clamped to the length. If is negative, then yields nil. An value of zero retrieves the terminating atom of or else the value itself, if is an atom. The following equivalences hold:
      (nthlast 1 list) <--> (last list) An place designates the storage location which holds the n-th cell, as indicated by the value of A negative doesn't denote a place. A positive greater than the length of the list is treated as if it were equal to the length of the list. If is itself a syntactic place, then the value n is permitted for a list of length n. This index value denotes the place itself. Storing to this value overwrites If isn't a syntactic place, then storing to position n isn't permitted. If is of length zero, or an atom (in which case its length is considered to be zero) then the above remarks about position n apply to an value of zero: if is a syntactic place, then the position denotes itself, otherwise the position doesn't exist as a place. If contains one or more elements, then value of zero denotes the field of its last cons cell. Storing a value to this place overwrites the terminating atom. The function calculates that initial portion of which excludes the last elements. Note: the function doesn't support non-list sequences as sequences; it treats them as the terminating atom of a zero-length improper list. The sequence function supports non-list sequences. If is a list, then the following equivalence holds:
      (butlastn n x) <--> (butlast x n) If is zero, or negative, then returns If is positive, and meets or exceeds the length of then returns If a form is used as a syntactic place, then must be a place. Assigning to the form causes to be replaced with a new list which is a catenation of the new value and the last elements of the original list, according to the following equivalence:
      (set (butlastn n x) v)
      <-->
      (progn (set x (append v (nthlast n x))) v) except that and are evaluated only once, in left-to-right order. The function performs random access on a list, retrieving the n-th element indicated by the zero-based index value given by The argument must be a nonnegative integer. If indicates an element beyond the end of the list, then the function returns The following equivalences hold:
      (nth 0 list) <--> (car 0) <--> (first list)
      (nth 1 list) <--> (cadr list) <--> (second list)
      (nth 2 list) <--> (caddr list) <--> (third list)
      (nth x y) <--> (car (nthcdr x y)) The function retrieves the n-th cons cell of a list, indexed from zero. The parameter must be a nonnegative integer. If specifies a nonexistent cons beyond the end of the list, then yields nil. The following equivalences hold:
      (nthcdr 0 list) <--> list
      (nthcdr 1 list) <--> (cdr list)
      (nthcdr 2 list) <--> (cddr list)
      (car (nthcdr x y)) <--> (nth x y) An place designates the storage location which holds the n-th cell, as indicated by the value of Indices beyond the last cell of do not designate a valid place. If is itself a place, then the zeroth index is permitted and the resulting place denotes Storing a value to overwrites Otherwise if isn't a syntactic place, then the zeroth index does not designate a valid place; must have a positive value. A place does not support deletion. In Common Lisp, is only a function, not an accessor; forms do not denote places. The function tests whether is a tail of This means that is either itself, or else one of the cells of or else the terminating atom of More formally, a recursive definition follows. If and are the same object (thus equal under the function) then returns If is an atom, and is not then the function returns Otherwise, is a that is not and yields the same value as the expression. The a-d accessors provide a shorthand notation for accessing two to five levels deep into a cons-cell-based tree structure. For instance, the the equivalent of the nested function call expression can be achieved using the single function call The symbol names of the a-d accessors are a generalization of the words "car" and "cdr". They encode the pattern of and traversal of the structure using a sequence of the letters and placed between and The traversal is encoded in right-to-left order, so that indicates a traversal of the link, followed by the This order corresponds to the nested function call notation, which also encodes the traversal right-to-left. The following diagram illustrates the straightforward relationship:
      (cdr (car (cdr x)))
        ^ ^ ^
        | / |
        | / /
        | / ____/
        || /
      (cdadr x) TXR Lisp provides all possible a-d accessors up to five levels deep, from all the way through Expressions involving a-d accessors are places. For example, denotes the same place as and denotes the same place as The a-d accessor places support deletion, with semantics derived from the deletion semantics of the and places. For example, means the same as The and functions provide navigation of tree structure driven by numeric address given by the argument. The argument can express any combination of the application of and functions, including none at all. The difference between and is the bit order of the encoding. Under the most significant bit of the encoding given in indicates the initial navigation, and the least significant bit gives the final one. Under it is opposite. Both functions require to be a positive integer. Any other argument raises an error. Under both functions, the value encodes the operation: no The function recursively traverses a nested returning a list whose elements are all of the atoms contained in at any level of nesting. If the argument is an rather than a then it is returned. Otherwise, the argument must be a proper list, as must all lists nested within it. The function calculates the same result as except that it produces a lazy list. It can be used to lazily flatten an infinite lazy list.
      (flatten 42) -> 42
      (flatten '(1 2 () (3 4))) -> (1 2 3 4)
      ;; equivalent to previous, since
      ;; nil is the same thing as ()
      (flatten '(1 2 nil (3 4))) -> (1 2 3 4)
      (flatten nil) -> nil
      (flatten '(((()) ()))) -> nil
      (flatten '(a (b . c))) -> ;; error The function produces a list of all the atoms contained in the tree structure in the order in which they appear, when the structure is traversed left to right. This list includes those atoms which appear in fields. The list excludes atoms which appear in fields. If the argument is an atom, it is returned. The function works like except that it produces a lazy list. It can be used to lazily flatten an infinite lazy structure.
      (flatcar '(1 2 () (3 4))) -> (1 2 nil 3 4)
      (flatcar '(a (b . c) d (e) (((f)) . g) (nil . z) nil . h))
      --> (a b c d e f g nil z nil h) The and function search for an occurrence of Tree can be any atom, or a cons. If it is a cons, it is understood to be a proper list whose elements are also trees. The equivalence test is performed by which must take two arguments, and has conventions similar to or If an argument is omitted, the default function is Under both and if is equivalent to under then is returned to announce a successful finding. Next, if the mismatched is an atom, both functions return to indicate that the search failed. If none of the above cases occur, the semantics of the functions diverge, as follows. In the case of is taken to be a proper list, and is recursively applied to each element of the list in turn, using the same and arguments, stopping at the first element which returns a value. In the case of is taken to be tree structure. The function is recursively applied to the and fields of Thus a match may be found in any position in the structure, including the dotted position of a list. The and functions search for a member which is, respectively, or to (See the and functions above.) If no such element found, is returned. Otherwise, that suffix of is returned whose first element is the matching object. The and functions search through for an item which matches a key, or satisfies a predicate function, respectively. The argument specifies a function which is applied to the elements of the sequence to produce the comparison key. If this argument is omitted, then the untransformed elements of the sequence themselves are examined. The function's argument specifies the test function which is used to compare the comparison keys taken from the sequence to the search key. If this argument is omitted, then the function is used. If does not find a matching element, it returns Otherwise it returns the suffix of which begins with the matching element. The function's argument specifies a predicate function which is applied to the successive comparison keys pulled from the sequence by applying the key function to successive elements. If no match is found, then is returned, otherwise what is returned is the suffix of which begins with the matching element. These functions are counterparts to and which look for the rightmost element which matches rather than for the leftmost element. These functions return a list whose elements are the conses which make up The function does this in a lazy way, avoiding the computation of the entire list: it returns a lazy list of the conses of The function computes the entire list before returning. The input may be proper or improper. The first cons of is that itself. The second cons is the rest of the list, or The third cons is and so on.
      (conses '(1 2 3)) -> ((1 2 3) (2 3) (3)) These functions are useful for simulating the function found in other dialects like Common Lisp. TXR Lisp's can be expressed in Common Lisp as Conversely, the Common Lisp operation can be computed in TXR Lisp as More generally, the Common Lisp operation
      (maplist function list0 list1 ... listn) can be expressed as:
      (mapcar function (conses list0)
                       (conses list1) ... (conses listn)) The function destructively removes a cons cell from a list. The is searched to see whether one of its cons cells is the same object as If so, that cell is removed from the list. The argument may be a proper or improper list, possibly empty. It may also be an atom other than which is regarded as being, effectively, an empty improper list terminated by that atom. The operation of is divided into the following three cases. If is the first cons cell of then the of is returned. If is the second or subsequent cons of then is destructively altered to remove and then returned. This means that the field of the predecessor of is altered from referencing to referencing instead. The returned value is the same cons cell as The third case occurs when is not found in In this situation, is returned unchanged.
      (let ((x (list 1 2 3)))
        (delcons x x))
      -> (2 3)
      (let ((x (list 1 2 . 3)))
        (delcons (cdr x) x))
      -> (1 . 3) Association lists are ordinary lists formed according to a special convention. Firstly, any empty list is a valid association list. A nonempty association list contains only cons cells as the key elements. These cons cells are understood to represent key/value associations, hence the name "association list". The function searches an association list for a cons cell whose field is equivalent to under the function. The first such cons is returned. If no such cons is found, is returned. The and functions are very similar to with the only difference being that they determine equality using, respectively, the and functions rather than The and functions are reverse lookup counterparts to and When searching, they examine the field of the pairs of rather than the field. The function searches association list for a cons whose field equivalent to according to the function. If such a cons is found, it is returned. Otherwise is returned. The and functions search in the same way as but compares values using, respectively, and The function constructs a new alist by consing a new cons to the front of The following equivalence holds:
      (acons car cdr alist) <--> (cons (cons car cdr) alist) The function searches as if using the assoc function, for an existing cell which matches the key provided by the car argument. If such a cell exists, then its cdr field is overwritten with the argument, and then the is returned. If no such cell exists, then a new list is returned by adding a new cell to the input list consisting of the and values, as if by the function. The function has similar same parameters and semantics as except that the function is used for equality testing. Thus, the list is searched for an existing cell as if using the function rather than The function takes association list and produces a duplicate from which cells matching any of the specified have been removed. The function is like but potentially destructive. The input list may be destroyed and its structural material reused to form the output list. The application should not retain references to the input list. The function duplicates Unlike which only duplicates list structure, also duplicates each cons cell of the input alist. That is to say, each element of the output list is produced as if by the function applied to the corresponding element of the input list. The function returns an association list consisting of pairs formed from the elements of and prepended to the existing If an argument is omitted, it defaults to Pairs of elements are formed by taking successive elements from the and sequences in parallel. If the sequences are not of equal length, the excess elements from the longer sequence are ignored. The pairs appear in the resulting list in the original order in which their constituents appeared in and The ANSI CL requires and to be lists, not sequences. The behavior of the ANSI CL is undefined of those lists are of different lengths. Finally, the elements are permitted to appear in either the original order or reverse order.
      (pairlis nil nil) -> nil
      (pairlis "abc" #(1 2 3 4)) -> ((#\a . 1) (#\b . 2) (#\c . 3))
      (pairlis '(1 2 3) '(a b c) '((x . y) (z . w)))
      -> ((1 . a) (2 . b) (3 . c) (x . y) (z . w)) A property list, also referred to as a plist, is a flat list of even length consisting of interleaved pairs of property names (usually symbols) and their values (arbitrary objects). An example property list is (:a 1 :b "two") which contains two properties, :a having value 1, and :b having value "two". An improper plist represents Boolean properties in a condensed way, as property indicators which are not followed by a value. Such properties only indicate their presence or absence, which is useful for encoding a Boolean value. If it is absent, then the property is false. Correctly using an improper plist requires that the exact set of Boolean keys is established by convention. In this document, the unqualified terms property list and plist refer strictly to an ordinary plist, not to an improper plist. Unlike in some other Lisp dialects, including ANSI Common Lisp, symbols do not have property lists in TXR Lisp. Improper plists aren't a concept in ANSI CL. The function searches property list for key If the key is found, then the value next to it is returned. Otherwise is returned. It is ambiguous whether is returned due to the property not being found, or due to the property being present with a value. The indicators in are compared with using equality, allowing them to be symbols, characters or integers. The function searches property list for key using equality. If the key is found, then the entire suffix of beginning with the indicator is returned, such that the first element of the returned list is and the second element is the property value. Note the reversed argument convention relative to the function, harmonizing with functions in the family. The functions and convert, respectively, a property list and improper property list to an association list. The function scans and returns the indicator-property pairs as a list of cons cells, such that each is the indicator, and each is the value. The is similar, except that it handles the Boolean properties which, by convention, aren't followed by a value. The list of all such indicators is specified by the argument.
      (plist-to-alist '(a 1 b 2)) --> ((a . 1) (b . 2))
      (improper-plist-to-alist '(:x 1 :blue :y 2) '(:blue))
      --> ((:x . 1) (:blue) (:y . 2)) Note: these functions operate on lists. The principal sorting function in TXR Lisp is described under Sequence Manipulation. The function described here provides access to an elementary step of the algorithm used internally by when operating on lists. The operation sorts multiple lists in parallel. It is implemented using The function merges two sorted sequences and into a single sorted sequence. The semantics and defaulting behavior of the and arguments are the same as those of the sort function. The sequence which is returned is of the same kind as This function is destructive of any inputs that are lists. If the output is a list, it is formed out of the structure of the input lists. The function regards a list of lists to be the columns of a database. The corresponding elements from each list constitute a record. These records are to be sorted, producing a new list of lists. The argument supplies the list of lists which comprise the columns of the database. The lists should ideally be of the same length. If the lists are of different lengths, then the shortest list is taken to be the length of the database. Excess elements in the longer lists are ignored, and do not appear in the sorted output. The argument supplies a list of comparison functions which are applied to the columns. Successive functions correspond to successive columns. If is an empty list, then the sorted database will emerge in the original order. If contains exactly one function, then the rows of the database are sorted according to the first column. The remaining columns simply follow their row. If contains more than one function, then additional columns are taken into consideration if the items in the previous columns compare For instance if two elements from column one compare then the corresponding second column elements are compared using the second column comparison function. The argument may be a function object, in which case it is treated as if it were a one-element list containing that function object. The optional argument supplies transformation functions through which column entries are converted to comparison keys, similarly to the single key function used in the sort function and others. If there are more key functions than less functions, the excess key functions are ignored. The function makes a special kind of cons cell called a lazy cons, whose type is Lazy conses are useful for implementing lazy lists. Lazy lists are lists which are not allocated all at once. Rather, the elements of its structure materialize just before they are accessed. A lazy cons has and fields like a regular cons, and those fields are initialized to the values of the and arguments of when the lazy cons is created. These arguments default to if omitted. A lazy cons also has an update function, which is specified by the argument to The argument must be a function that may be called with exactly one parameter. When either the and fields of a cons are accessed for the first time to retrieve their value, is automatically invoked first, and is given the lazy cons as a parameter. That function has the opportunity to store new values into the and fields. Once the function is called, it is removed from the lazy cons: the lazy cons no longer has an update function. If the update function itself attempts to retrieve the value of the lazy cons cell's or field, it will be recursively invoked. The functions and may be used to access the fields of a lazy cons without triggering the update function. Storing a value into either the or field does not have the effect of invoking the update function. If the function terminates by returning normally, the access to the value of the field then proceeds in the ordinary manner, retrieving whatever value has most recently been stored. The return value of the function is ignored. To perpetuate the growth of a lazy list, the function can make another call to and install the resulting cons as the of the lazy cons.
      ;;; lazy list of integers between min and max
      (defun integer-range (min max)
        (let ((counter min))
          ;; min is greater than max; just return empty list,
          ;; otherwise return a lazy list
          (if (> min max)
            nil
            (make-lazy-cons
              (lambda (lcons)
                ;; install next number into car
                (rplaca lcons counter)
                ;; now deal wit cdr field
                (cond
                  ;; max reached, terminate list with nil!
                  ((eql counter max)
                   (rplacd lcons nil))
                  ;; max not reached: increment counter
                  ;; and extend with another lazy cons
                  (t
                    (inc counter)
                    (rplacd lcons
                            (make-lazy-cons
                            (lcons-fun lcons)))))))))) The function returns if is a lazy cons cell. Otherwise it returns even if is an ordinary cons cell. The function retrieves the update function of a lazy cons. Once a lazy cons has been accessed, it no longer has an update function and returns While the update function of a lazy cons is executing, it is still accessible. This allows the update function to retrieve a reference to itself and propagate itself into another lazy cons (as in the example under The functions and retrieve the and fields of without triggering the invocation of its associated update function. The argument must be an object of type Unlike the functions and These functions cannot be applied to any other type of object. Note: these functions may be used by the update function to retrieve the values which were stored into by the constructor, without triggering recursion. The function may then overwrite either or both of these values. This allows the fields of the lazy cons to store state information necessary for the propagation of a lazy list. If that state information consists of no more than two values, then no additional context object need be allocated. The macro simplifies the construction of structures based on lazy conses. Syntactically, it resembles the function. However, the arguments are expressions rather than values. The macro generates code which, when evaluated, immediately produces a lazy cons. The expressions and are not immediately evaluated. Rather, when either the or field of the lazy cons cell is accessed, these expressions are both evaluated at that time, in the order that they appear in the expression, and in the original lexical scope in which that expression was evaluated. The return values of these expressions are used, respectively, to initialize the corresponding fields of the lazy cons. Note: the macro may be understood in terms of the following reference implementation, as a syntactic sugar combining the constructor with a lexical closure provided by a function:
      (defmacro lcons (car-form cdr-form)
        (let ((lc (gensym)))
           ^(make-lazy-cons (lambda (,lc)
                            (rplaca ,lc ,car-form)
                            (rplacd ,lc ,cdr-form)))))
      ;; Given the following function ...
      (defun fib-generator (a b)
        (lcons a (fib-generator b (+ a b))))
      ;; ... the following function call generates the Fibonacci
      ;; sequence as an infinite lazy list.
      (fib-generator 1 1) -> (1 1 2 3 5 8 13 ...) The and functions are synonyms, except that the argument is optional in and defaults to Thus, the following description of also applies to The returns a lazy cons which generates a lazy list based on reading lines of text from input stream which form the elements of the list. The function is called on demand to add elements to the list. The function itself makes the first call to on the stream. If this returns then the stream is closed and is returned. Otherwise, a lazy cons is returned whose update function will install that line into the field of the lazy cons, and continue the lazy list by making another call to installing the result into the field. When this lazy list obtains an end-of-file indication from the stream, it closes the stream. inspects the real-time property of a stream as if by the function. This determines which of two styles of lazy list are returned. For an ordinary (non-real-time) stream, the lazy list treats the end-of-file condition accurately: an empty file turns into the empty list a one line file into a one-element list which contains that line and so on. This accuracy requires one line of lookahead which is not acceptable in real-time streams, and so a different type of lazy list is used, which generates an extra item after the last line. Under this type of lazy list, an empty input stream translates to the list a one-line stream translates to ("line" nil) and so forth. If and when is closed by the function directly, or else by the returned lazy list, the Boolean argument, defaulting to controls the argument of the call to the function. These arguments have opposite polarity: if is true, then shall be false, and vice versa. The macro establishes a dynamic environment in which zero or more are evaluated, yielding the value of the last or else if there are no arguments. In this regard, the macro operator resembles The environment established by sets up special monitoring of the the functions and Whenever these functions register an I/O stream with a lazy list, that stream is recorded in a hidden. When the form terminates, it invokes the on each stream in the hidden list. Note: the macro provides a possible solution for situations in which a body of code, possibly consisting of nested functions, manipulates lazy lists of lines coming from from I/O streams, such that these lists are not completely forced. Incompletely processed lazy lists will not close their associated streams until they are reclaimed by garbage collection, which could cause the application to run out of file descriptors. The macro allows the application to delineate a dynamic contour of code upon whose termination all such stream associations generated within that contour will be duly cleaned up. Collect list of names of files which contain the string
      ;; Incorrect version: could run out of open files if there are many
      ;; files which contain a match processed, because find-if will stop
      ;; traversing the list of lines when it finds a match:
      (build
        (each ((file (glob "*.tl")))
          (if (find-if #/\(cons / (file-get-lines file))
            (add file))))
      ;; Addressed with close-lazy-streams: after each iteration, the
      ;; stream created by file-get-lines is closed.
      (build
        (each ((file (glob "*.tl")))
          (close-lazy-streams
            (if (find-if #/\(cons / (file-get-lines file))
              (add file))))) The delay operator arranges for the delayed (or "lazy") evaluation of This means that the expression is not evaluated immediately. Rather, the delay expression produces a promise object. The promise object can later be passed to the function (described later in this document). The force function will trigger the evaluation of the expression and retrieve the value. The expression is evaluated in the original scope, no matter where the takes place. The expression is evaluated at most once, by the first call to Additional calls to only retrieve a cached value.
      ;; list is popped only once: the value is computed
      ;; just once when force is called on a given promise
      ;; for the first time.
      (defun get-it (promise)
        (format t "*list* is ~s\n" *list*)
        (format t "item is ~s\n" (force promise))
        (format t "item is ~s\n" (force promise))
        (format t "*list* is ~s\n" *list*))
      (defvar *list* '(1 2 3))
      (get-it (delay (pop *list*)))
      Output:
      *list* is (1 2 3)
      item is 1
      item is 1
      *list* is (2 3) The function accepts a promise object produced by the macro. The first time is invoked, the which was wrapped inside by the macro is evaluated (in its original lexical environment, regardless of where in the program the call takes place). The value of is cached inside and returned, becoming the return value of the function call. If the function is invoked additional times on the same promise, the cached value is retrieved. A form is a syntactic place, denoting the value cache location within Storing a value in a place causes future accesses to the to return that value. If the promise had not yet been forced, then storing a value into it prevents that from ever happening. The delayed will never be evaluated. If, while a promise is being forced, the evaluation of itself causes an assignment to the promise, it is not specified whether the promise will take on the value of or the assigned value. The function returns if is a promise object: an object created by the macro. Otherwise it returns Note: promise objects are conses. The function applied to a promise returns The macro ("magic let" or "mutual let") implements a variable binding construct similar to and Under the scope of the bindings of the variables extends over the as well as the Unlike the construct, each has each in scope. That is to say, an can refer not only to previous variables, but also to later variables as well as to its own variable. The variables are not initialized until their values are accessed for the first time. Any whose value is not accessed is not initialized. Furthermore, the evaluation of each does not take place until the time when its value is needed to initialize the associated This evaluation takes place once. If a given is not accessed during the evaluation of the construct, then its is never evaluated. The bound variables may be assigned. If, before initialization, a variable is updated in such a way that its prior value is not needed, it is unspecified whether initialization takes place, and thus whether its is evaluated. Direct circular references are erroneous and are diagnosed. This takes place when the macro-expanded form is evaluated, not during the expansion of
      ;; Dependent calculations in arbitrary order
      (mlet ((x (+ y 3))
             (z (+ x 1))
             (y 4))
        (+ z 4)) --> 12
      ;; Error: circular reference:
      ;; x depends on y, y on z, but z on x again.
      (mlet ((x (+ y 1))
             (y (+ z 1))
             (z (+ x 1)))
        z)
      ;; Okay: lazy circular reference because lcons is used
      (mlet ((list (lcons 1 list)))
        list) --> (1 1 1 1 1 ...) ;; circular list In the last example, the variable is accessed for the first time in the body of the form. This causes the evaluation of the form. This form evaluates its arguments lazily, which means that it is not a problem that is not yet initialized. The form produces a lazy cons, which is then used to initialize When the or fields of the lazy cons are accessed, the expression in the argument is accessed. By that time, the variable is initialized and holds the lazy cons itself, which creates the circular reference, and a circular list. The function produces a lazy list which dynamically produces items according to the following logic. The arguments to are functions which do not take any arguments. The return value of generate is a lazy list. When the lazy list is accessed, for instance with the functions car and cdr, it produces items on demand. Prior to producing each item, is called. If it returns a true Boolean value (any value other than then the function is called, and its return value is incorporated as the next item of the lazy list. But if yields then the lazy list immediately terminates. Prior to returning the lazy list, generate invokes the one time. If yields then returns the empty list instead of a lazy list. Otherwise, it instantiates a lazy list, and invokes the to populate it with the first item. The function is similar to except that and are functions of one argument rather than functions of no arguments. The optional argument defaults to and is threaded through the function calls. That is to say, the lazy list returned is The lazy list terminates when a value fails to satisfy That is to say, prior to generating each value, the lazy list tests the value using If that function returns then the item is not added, and the sequence terminates. Note: could be written in terms of like this:
      (defun giterate (w g v)
         (generate (lambda () [w v])
                   (lambda () (prog1 v (set v [g v]))))) The function is a variant of which includes the test-failing item in the generated sequence. That is to say generates the next value and adds it to the lazy list. The value is then tested using If that function returns then the list is terminated, and no more items are produced.
      (giterate (op > 5) (op + 1) 0) -> (0 1 2 3 4)
      (ginterate (op > 5) (op + 1) 0) -> (0 1 2 3 4 5) The function is a complement to with lazy semantics. The parameter is a function, which must accept a single argument, and return either a cons pair or The parameter is any value. The first call to receives The return value is interpreted as follows. If returns a cons-cell pair then specifies the element to be added to the lazy list, and specifies the value to be passed to the next call to If returns then the lazy list ends.
      ;; Count down from 5 to 1 using explicit lambda
      ;; for gen-fun:
      (expand-right
        (lambda (item)
          (if (zerop item) nil
            (cons item (pred item))))
        5)
      --> (5 4 3 2 1)
      ;; Using functional combinators:
      [expand-right [iff zerop nilf [callf cons identity pred]] 5]
      --> (5 4 3 2 1)
      ;; Include zero:
      [expand-right
        [iff null
           nilf
           [callf cons identity [iff zerop nilf pred]]] 5]
      --> (5 4 3 2 1 0) The function is a companion to Unlike it has eager semantics: it calls repeatedly and accumulates an output list, not returning until returns The semantics is as follows. initializes an empty accumulation list. Then is called, with as its argument. If it returns a cons cell, then the of that cons cell is pushed onto the accumulation list, and the procedure is repeated: is called again, with taking the place of If returns then the accumulation list is returned. If the expression produces a terminating list, then the following equivalence holds:
      (expand-left f v) <--> (reverse (expand-right f v)) The equivalence cannot hold for arguments to which produce an infinite list. The function is a destructive version of The list returned by is composed of the cons cells returned by whereas the list returned by is composed of freshly allocated cons cells. If is empty, then repeat returns an empty list. If is omitted, the function produces an infinite lazy list formed by catenating together copies of If is specified and is zero or negative, then an empty list is returned. Otherwise a list is returned consisting of repetitions of catenated together. The function produces a lazy list which consists of all of the elements of followed by repetitions of If is omitted, it defaults to If is omitted, then the repetition of is infinite. Otherwise the specified number of repetitions occur. Note that may be a lazy list which is infinite. In that case, the repetitions of will never occur. The function interleaves elements from the sequences given as arguments. If called with no arguments, it returns the empty list. If called with a single sequence, it returns the elements of that sequence as a new lazy list. When called with two or more sequences, returns a lazy list which draws elements from the sequences in a round-robin fashion, repeatedly scanning the sequences from left to right, and taking an item from each one, removing it from the sequence. Whenever a sequence runs out of items, it is deleted; the weaving then continues with the remaining sequences. The weaved sequence terminates when all sequences are eliminated. (If at least one of the sequences is an infinite lazy list, then the weaved sequence is infinite.)
      ;; Weave negative integers with positive ones:
      (weave (range 1) (range -1 : -1)) -> (1 -1 2 -2 3 -3 ...)
      (weave "abcd" (range 1 3) '(x x x x x x x))
      --> (#\a 1 x #\b 2 x #\c 3 x #\d x x x x) The macro operator produces a lazy list, in a manner similar to the function. Whereas the function takes functional arguments, the operator takes two expressions, which is often more convenient. The return value of is a lazy list. When the lazy list is accessed, for instance with the functions and it produces items on demand. Prior to producing each item, the is evaluated, in its original lexical scope. If the expression yields a value, then is evaluated, and its return value is incorporated as the next item of the lazy list. If the expression yields then the lazy list immediately terminates. The operator itself immediately evaluates before producing the lazy list. If the expression yields then the operator returns the empty list Otherwise, it instantiates the lazy list and invokes the to force the first item. The macro similarly creates a lazy list according to the following rules. Each successive item of the lazy list is obtained as a result of evaluating However, when yields then the list terminates (without adding that as an item). Note 1: the form can be implemented as a macro-expanding to an instance of the operator, like this:
      (defmacro gun (expr)
        (let ((var (gensym)))
          ^(let (,var)
             (gen (set ,var ,expr)
                  ,var)))) This exploits the fact that the operator returns the value that is assigned, so the set expression is tested as a condition by while having the side effect of storing the next item temporarily in a hidden variable. In turn, can be implemented as a macro expanding to some functions which are passed to the function:
      (defmacro gen (while-expr produce-expr)
        ^(generate (lambda () ,while-expr)
                   (lambda () ,produce-expr))) Note 2: can be considered as an acronym for Generate, testing Expression before Next item, whereas stands for Generate Until Null.
      ;; Make a lazy list of integers up to 1000
      ;; access and print the first three.
      (let* ((counter 0)
             (list (gen (< counter 1000) (inc counter))))
        (format t "~s ~s ~s\n" (pop list) (pop list) (pop list)))
      Output:
      1 2 3 The and functions generate a lazy, potentially infinite list, according to several disciplines. There is a major division in behavior depending on whether or not the argument, which specifies the initial item, is an arithmetic type according to the function. The following remarks describe the arithmetic case. A description of the non-arithmetic behavior follows. The difference between and is that excludes the endpoint. For instance generates the list whereas generates All arguments are optional. If the argument is omitted, then it defaults to 1 if the argument is omitted, or else if it is greater than or equal to according to the function. If is given, and is less than then a missing argument defaults to -1. Each value in the list is obtained from the previous by adding the value. Positive or negative values are allowed. There is no check for a step size of zero, or for a step direction which cannot meet the endpoint. The argument may be a function. The function must accept one argument. That argument is an element of the list, from which the function calculates the next element. The argument specifies the endpoint value, which, if it occurs in the list, is excluded from it by the function, but included by the range function. If is missing, or specified as then there is no endpoint, and the list which is generated is infinite, regardless of If is omitted, then the list begins at zero, otherwise must be an arithmetic object which specifies the initial value. The list stops if it reaches the endpoint value (which is included in the case of and excluded in the case of However, depending on the arguments, it is possible that the generated list doesn't contain the endpoint value, yet steps over it. This occurs when the previous value of the list is less than the endpoint value, but the next value is greater, or vice versa. In this situation, the list also stops, and the excess value which surpasses the endpoint is excluded from the list. The rest of the description applies to the case when the argument is a non-arithmetic type. In the non-arithmetic case, the argument unconditionally defaults to 1. If it is given, it must either be a function, or else a positive integer. If is a function, that function is used to determine each successive value from the previous similarly to the arithmetic case. If the value is omitted, an infinite list is generated this way. If the argument is present, the list stops if it attains the endpoint value. No provision is made for the endpoint value being skipped, like in the arithmetic case. When the endpoint value is reached, function omits that value from the list. If is a positive integer, then range iteration is used. A range value is constructed from the and arguments as if by the expression. Here, the argument defaults to if it is missing. An iterator is created for the resulting range object as if by and this iterator is then used to obtain values for the lazy list returned by or The list ends when the iterator indicates that no more items are available. In the case of the function, the last value produced by the iterator is omitted from the list. The size is used to skip items from the iterator. For instance, if the value is 3, then the sequence begins with the value. The next two values from the sequence are omitted, The fourth item from the sequence is included in the list, (unless there either is no such item, or the function is and that item is the last one).
      (range 1 1) -> (1)
      (range 0 4) -> (0 1 2 3 4)
      (range 4 0) -> (4 3 2 1 0)
      (range 0.0 2.0 0.5) (0.0 0.5 1.0 1.5 2.0)
      (range #R(0 1) #R(3 4)) (#R(0 1) #R(1 2) #R(3 4))
      (range 0 4 2) -> (0 2 4)
      (range #\a #\e 2) (#\a #\c #\e)
      (range 1 32 (op * 2)) -> (1 2 4 8 16 32))
      (range* 1 1) -> nil
      (range* 0 4) -> (0 1 2 3)
      (range* 4 0 -2) -> (4 2)
      (range 0 1.25 0.5) -> (0 0.5 1.0)
      (range* 0 1.25 0.5) -> (0 0.5 1.0))
      (range "A" "A") -> nil
      (range "AA" "BC") -> ("AA" "AB" "AC" "BA" "BB" "BC")
      (range "AA" "BC" 2) -> ("AA" "AC" "BB")
      [range* "ABCD" nil rest] -> ("ABCD" "BCD" "CD" "D") The ("range list") function is useful for producing a list consisting of a mixture of discontinuous numeric or character ranges and individual items. The function returns a lazy list of elements. The items are produced by converting the function's successive arguments into lists, which are lazily catenated together to form the output list. Each is transformed into a list as follows. Any item which is not a range object is trivially turned into a one-element list as if by the expression. Any item which is a range object, whose field isn't a range is turned into a lazy list as if by evaluating the expression. Thus for instance the argument turns into the (lazy) list Any item which is a range object such that its field is also a range is turned into a lazy list as if by evaluating the expression. Thus for instance the argument expression produces an which turns into the lazy list as if by the call Note that the expression stands for the expression which evaluates to The range literal syntax can be passed as an argument to with the same result as The function differs from in one regard: under the ranges denoted by the range notation exclude the endpoint. That is, the ranges are generated as if by the function rather than Note: it is permissible for objects to specify infinite ranges. It is also permissible to apply to an infinite argument list.
      (rlist 1 "two" :three) -> (1 "two" :three)
      (rlist 10 15..16 #\a..#\d 2) -> (10 15 16 #\a #\b #\c #\d 2)
      (take 7 (rlist 1 2 5..:)) -> (1 2 5 6 7 8 9) Ranges are objects that aggregate two values, not unlike cells. However, they are atoms, and are primarily intended to hold numeric or character values in their two fields. These fields are called and which are the names of the functions which access them. These fields are not mutable; a new value cannot be stored into either field of a range. The printed notation for a range object consists of the prefix (hash R) followed by the two values expressed as a two-element list. Ranges can be constructed using the function. The notation corresponds to Ranges behave as a numeric type and support a subset of the numeric operations. Two ranges can be added or subtracted, which obeys these equivalences:
      (+ a..b c..d) <--> (+ a c)..(+ b d)
      (- a..b c..d) <--> (- a c)..(- b d) A range can be combined with a character or number using addition or subtractions, which obeys these equivalences:
      (+ a..b n) <--> (+ n a..b) <--> (+ a n)..(+ b n)
      (- a..b n) <--> (- a n)..(- b n)
      (- n a..b) <--> (- n a)..(- n b) A range can be multiplied by a number:
      (* a..b n) <--> (* n a..b) <--> (* a n)..(* b n) A range can be divided by a number using the or functions, but a number cannot be divided by a range:
      (trunc a..b n) <--> (trunc a n)..(trunc b n)
      (/ a..b n) <--> (/ a n)..(/ b n) Ranges can be compared using the equality and inequality functions and Equality obeys this equivalence:
      (= a..b c..d) <--> (and (= a c) (= b d)) Inequality comparisons treat the component with precedence over such that only if the components of the two ranges are not equal under the function, then the inequality is based solely on them. If they are equal, then the inequality is based on the components. This gives rise to the following equivalences:
      (< a..b c..d) <--> (if (= a c) (< b d) (< a c))
      (> a..b c..d) <--> (if (= a c) (> b d) (> a c))
      (>= a..b c..d) <--> (if (= a c) (>= b d) (> a c))
      (<= a..b c..d) <--> (if (= a c) (<= b d) (< a c)) Ranges can be negated with the one-argument form of the function, which is equivalent to subtraction from zero: the negation distributes over the two range components. The function also applies to ranges and distributes into their components. The and family of functions also operate on ranges. The length of a range may be obtained with the function; The length of the range is defined as and may be obtained using the function. The function accepts ranges and tests them for zero length. The function constructs a range object which holds the values and Though range objects are effectively binary cells like conses, they are atoms. They also aren't considered sequences, nor are they structures. Range objects are used for indicating numeric ranges, such as substrings of lists, arrays and strings. The dotdot notation serves as a syntactic sugar for The syntax denotes the expression Note that ranges are immutable, meaning that it is not possible to replace the values in a range. The function returns if is a range. Otherwise it returns The and functions retrieve, respectively, the from and to fields of a range. Note that these functions are not accessors, which is because ranges are immutable. The and functions test whether the argument lies in the range represented by the argument, indicating the Boolean result using one of the values or The argument must be a range object. It is expected that the range object's value does not exceed the value; a reversed range is considered empty. The function differs from in that it excludes the upper endpoint. The implicit comparison against the range endpoints is performed using the and functions, as appropriate. The following equivalences hold:
      (in-range r x) <--> (and (lequal (from r) x)
                            (lequal x (to r)))
      (in-range* r x) <--> (and (lequal (from r) x)
                            (less x (to r))) The function requires its argument to be a range object. It supports two semantics, based on the type of the second argument. If the second argument is an integer, then it is interpreted as The function then treats the as if it were a sequence. The must be a numeric or character range. The field of is added to to form the tentative return value. If the field is a value other than or the (colon) symbol, then the tentative value must be less than the value of this field, or an exception is thrown. In other words, must indicate a point within the range. After the above range check is performed, if applicable, the tentative value is returned. If the second argument isn't an integer, it is interpreted as a sequence The object's values are used to extract a subrange of according to the following equivalence:
      (rangeref r s) <--> (sub s (from r) (to r)) except that and are evaluated only once, in that order. The function constructs a string object of a length specified by the parameter. The parameter must be non-negative. Every position in the string is initialized with which must be a character value. If the optional argument is not specified, it defaults to the space character. The function resembles and behaves the same way when the second argument is omitted, and when it is a character value. The second argument of may be a in which case the newly created string is filled by taking successive characters from If is longer than its excess characters are ignored. If is shorter, then characters are taken from the beginning again; is effectively taken as a fill pattern to be repeated as many times as necessary to provide the required number of characters. If is empty, fills the new string with spaces. The function constructs a new string whose contents are identical to If is a lazy string, then a lazy string is constructed with the same attributes as The new lazy string has its own copy of the prefix portion of which has been forced so far. The unforced list and separator string are shared between and the newly constructed lazy string. The function produces a copy of such that all lowercase characters of the English alphabet are mapped to their uppercase counterparts. The function produces a copy of such that all uppercase characters of the English alphabet are mapped to their lowercase counterparts. The function destructively increases the length of which must be an ordinary dynamic string. It is an error to invoke this function on a literal string or a lazy string. The argument can be a character, string or integer. If it is a string or character, it specifies material which is to be added to the end of the string: either a single character or a sequence of characters. If it is an integer, it specifies the number of characters to be added to the string. If is an integer, the newly added characters have indeterminate contents. The string appears to be the original one because of an internal terminating null character remains in place, but the characters beyond the terminating zero are indeterminate. The optional Boolean argument defaulting to is a hint which indicates whether this call is expected to be the last time that the function is invoked on the given If is true, then the object's underlying memory allocation is trimmed to fit the actual string data. If the argument is false, the object may be given a larger allocation intended to improves the performance of subsequent calls. The function removes excess allocation from that may have been produced by previous calls to Note: if the most recent call to string specified a true value for the parameter, then calling is unnecessary and does nothing. The function returns if is one of the several kinds of strings. Otherwise it returns The function returns the length in characters. The argument must be a string. The function returns the number of bytes required to encode in UTF-8. The argument must be a character string. If the string contains only characters in the ASCII range U+0001 to U+007F range, then the value returned shall be the same as that returned by the function. The function finds an occurrence of the string inside the string and returns its position. If no such occurrence exists, it returns If a argument is not specified, it defaults to zero. If it is a nonnegative integer, it specifies the starting character position for the search. Negative values of indicate positions from the end of the string, such that is the last character of the string. If the argument is specified and is not it means that the search is conducted right-to-left. If multiple matches are possible, it will find the rightmost one rather than the leftmost one. The function is similar to except that instead of searching for the occurrence of a single needle string, it searches for the occurrence of numerous strings at the same time. These search strings are specified, via the argument, as an arbitrarily structured tree whose leaves are strings. The function finds the earliest possible match, in the given search direction, from among all of the needle strings. If is a single string, the semantics is equivalent to Without the argument, the function determines whether is a prefix of If the argument is specified, and is a nonnegative integer, then the function tests whether matches a prefix of that portion of which starts at the given position. If the argument is a negative integer, then determines whether is a suffix of ending on that position of bigstring, where denotes the last character of the second last one and so on. If is then this corresponds to testing whether is a suffix of The function returns if there is no match. If a prefix match is successful, then an integer value is returned indicating the position, inside one character past the matching prefix. If the entire string is matched, then this value corresponds to the length of If a suffix match is successful, the return value is the position within where the leftmost character of matched. The function is a generalization of which matches multiple test strings against at the same time. The value reported is the longest match from among any of the strings. The strings are specified as an arbitrarily shaped tree structure which has strings at the leaves. If is a single string atom, then the function behaves exactly like The function has the same parameters and semantics as the function, function, except that the first argument is operated upon using string operations. If a form is used as a place, it denotes a subrange of as if it were a storage location. The previous value of this location, if needed, is fetched by a call to Storing to the place is performed by a call to In an update operation which accesses the prior value and stores a new value, the arguments and are evaluated once. The argument is not itself required to be a place; it is not updated when a value is written to the storage location. The function has the same parameters and semantics as the function, except that the first argument is operated upon using string operations. The and functions combine items into a single string, which is returned. Every argument must be a character, string or else a possibly empty sequence of items. This rule applies recursively. If a argument is present, it must be a character or string. The argument must be a sequence of any mixture of items which are characters, strings or sequences of items. Note that this means that if is a character string, it is a valid argument, since it is a sequence of characters. If is empty, or no arguments are present, then all three functions return an empty string. All three functions operate on an abstract sequence of character and string items, produced by a left-to-right recursive traversal of their or arguments. Under the function, as well as the function a argument is given to it, the items are catenated together such that is interposed between them. If there are n character or string items, then n - 1 copies of occur in the resulting string, which is returned. Under the function, or function invoked without a argument, the items are catenated together directly, without any separator. The resulting string is returned. The function breaks the into pieces, returning a list thereof. The argument must be one of three types: a string, a character or a regular expression. It determines the separator character sequences within The following describes the behavior of in the case when the integer parameter is omitted. The semantics of are then given. All non-overlapping matches for within are identified in left-to-right order, and are removed from The string is broken into pieces according to the gaps left behind by the removed separators, and a list of the remaining pieces is returned. If is the empty string, then the separator pieces removed from the string are considered to be the empty strings between its characters. In this case, if is of length one or zero, then it is considered to have no such pieces, and a list of one element is returned containing the original string. These remarks also apply to the situation when is a regular expression which matches only an empty substring of If a match for is not found in the string at all (not even an empty match), then the string is not split at all: a list of one element is returned containing the original string. If matches the entire string, then a list of two empty strings is returned, except in the case that the original string is empty, in which case a list of one element is returned, containing the empty string. Whenever two adjacent matches for occur, they are considered separate cuts with an empty piece between them. This operation is nondestructive: is not modified in any way. If the optional argument is specified and is not If an argument is given and is true, then incorporates the matching separating pieces of into the resulting list, such that if the resulting list is catenated, a string equivalent to the original string will be produced. Note: to split a string into pieces of length one such that an empty string produces rather than use the pattern. Note: the function call produces a resulting list identical to for all values of and provided that does not match empty strings. If matches empty strings, then the call returns extra elements compared to because allows empty matches to take place and extract empty tokens before the first character of the string, and after the last character, whereas does not recognize empty separators at these outer limits of the string. If the parameter is present, it must be a non-negative integer. This value specifies the maximum number of pieces of the input which are extracted by the splitting process. The returned list consists of these pieces, followed by the remainder of the string, if the remainder is nonempty. If is true, then separators appear between the pieces, and if the remainder piece is present, the separator between the last piece and the remainder is included. If is zero, then returns a list of one element, which is The function performs the same computation as The same-named parameters of and have the same semantics. The difference is the argument order. The function takes the argument first. The last argument is always whether or not there are two arguments or three. If there are three arguments, then is the middle one. Note: the argument conventions of facilitate less verbose partial application, such as with macros in the family, in the common situation when is the unbound argument. The function is similar to taking a required argument which behaves exactly like the same-named argument of The function breaks the into pieces, returning a list thereof. The argument must be a string. It specifies a set of characters. All occurrences of any of these characters within are identified, and are removed from The string is broken into pieces according to the gaps left behind by the removed separators. Adjacent occurrences of characters from within are considered to be separate gaps which come between empty strings. This operation is nondestructive: is not modified in any way. The function performs the same operation; the only difference between and is argument order. The function searches for tokens, which are defined as substrings of which match the regular expression in the longest possible way, and do not overlap. These tokens are extracted from the string and returned as a list. Whenever matches an empty string, then an empty token is returned, and the search for another token within resumes after advancing by one character position. However, if an empty match occurs immediately after a nonempty token, that empty match is not turned into a token. So for instance, (tok-str "abc" #/a?/) returns ("a" "" ""). After the token is extracted from a nonempty match for the regex, an empty match for the regex occurs just before the character This match is discarded because it is an empty match which immediately follows the nonempty match. The character is skipped. The next match is an empty match between the and characters. This match causes an empty token to be extracted. The character is skipped, and one more empty match occurs after that character and is extracted. If the argument is true, then the behavior of changes in the following way. The pieces of which are skipped by the search for tokens are included in the output. If no token is found in then a list of one element is returned, containing Generally, if N tokens are found, then the returned list consists of 2N + 1 elements. The first element of the list is the (possibly empty) substring which had to be skipped to find the first token. Then the token follows. The next element is the next skipped substring and so on. The last element is the substring of between the last token and the end. If is specified, it must be a nonnegative integer. The value limits the number of tokens which are extracted. The returned list then includes one more item: the remainder of the string after the last extracted token. This item is omitted if the rest of the string is empty, unless is true. The function works similarly to but instead of returning the extracted tokens themselves, it returns a list of the character position ranges within where matches for occur. The ranges are pairs of numbers, represented as cons cells, where the first number of the pair gives the starting character position, and the second number is one position past the end of the match. If a match is empty, then the two numbers are equal. The function does not support the parameter. The function performs the same computation as The same-named parameters of and have the same semantics. The difference is the argument order. The function takes the argument first. The last argument is always whether or not there are two arguments or three. If there are three arguments, then is the middle one. Note: the argument conventions of facilitate less verbose partial application, such as with macros in the family, in the common situation when is the unbound argument. The function is similar to taking a required argument which behaves exactly like the same-named argument of The function converts a string into a list of characters. The function produces a copy of from which leading and trailing tabs, spaces and newlines are removed. The function performs a character escaping transformation on the input string The argument is a string containing zero or more characters. The argument is a character or string. The function returns a transformed version of in which every character of which occurs in is preceded by
      (str-esc "$@#" "$" "$foo @abc #1") -> "$$foo $@abc $#1"
      (str-esc "'" "'\\'" "foo 'bar' baz") -> "foo '\\''bar'\\'' baz" The and functions provide a mechanism for associating an integer code with a string. Note: this mechanism is the basis for associating system error messages passed in exceptions with the values of the failed system library calls which precipitated these error exceptions. Not all string types can have an integer code: lazy strings and literal strings do not have this capability. The argument must be of type The argument must be an integer or character. It is recommended that its value be confined to the non-negative range of the platform's C type. Otherwise it is unspecified whether the same value shall be observed by as what was stored with The function associates the integer with the given and returns Any previously associated value is overwritten. The function retrieves the value most recently associated with If has no associated value, then is returned. If the is invoked on a then it is unspecified whether or not has an associated value and, if so, what value that is, except in the following case: if is invoked with a argument which is true, then is caused not to have an associated value. If the function is invoked on a that string is caused not to have an associated value. Returns if is a character, otherwise nil. Returns if is an alphanumeric character, otherwise nil. Alphanumeric means one of the uppercase or lowercase letters of the English alphabet found in ASCII, or an ASCII digit. This function is not affected by locale. Returns if is an alphabetic character, otherwise Alphabetic means one of the uppercase or lowercase letters of the English alphabet found in ASCII. This function is not affected by locale. The function returns if the code of character is in the range 0 to 127 inclusive. For characters outside of this range, it returns The function returns if the character is a control character. For all other character, it returns A control character is one which belongs to the Unicode C0 or C1 block. C0 consists of the characters U+0000 through U+001F, plus the character U+007F. These are the original ASCII control characters. Block C1 consists of U+0080 through U+009F. If is an ASCII decimal digit character, returns the value and returns the integer value corresponding to that digit character, a value in the range 0 to 9. Otherwise, both functions return The function returns if is a non-space printable ASCII character. It returns if it is a space or control character. It also returns for non-ASCII characters: Unicode characters with a code above 127. The function returns if is an ASCII lowercase letter. Otherwise it returns The function returns if is an ASCII character which is not a control character. It also returns for all non-ASCII characters: Unicode characters with a code above 127. The function returns if is an ASCII character which is not a control character. It also returns for all non-ASCII characters: Unicode characters with a code above 127. The function returns if is an ASCII whitespace character: any of the characters in the set and For all other characters, it returns The function returns if is a space or tab: the character or For all other characters, it returns The function returns if is a Unicode whitespace character. This the case for all the characters for which returns It also returns for these additional characters: and For all other characters, it returns The function returns if is an ASCII uppercase letter. Otherwise it returns If is a hexadecimal digit character, returns the value and returns the integer value corresponding to that digit character, a value in the range 0 to 15. Otherwise, both functions returns A hexadecimal digit is one of the ASCII digit characters through or else one of the letters through or their lowercase equivalents through denoting the values 10 to 15. If character is a lowercase ASCII letter character, this function returns the uppercase equivalent character. If it is some other character, then it just returns If character is an uppercase ASCII letter character, this function returns the lowercase equivalent character. If it is some other character, then it just returns The argument must be a character. The function returns that character's Unicode code point value as an integer. The argument must be a fixnum integer in the range to The function interprets as a Unicode code point value and returns the corresponding character object. Note: these functions are also known by the obsolescent names and The function performs random access on string to retrieve the character whose position is given by integer which must be within range of the string. The index value 0 corresponds to the first (leftmost) character of the string and so nonnegative values up to one less than the length are possible. Negative index values are also allowed, such that -1 corresponds to the last (rightmost) character of the string, and so negative values down to the additive inverse of the string length are possible. An empty string cannot be indexed. A string of length one supports index 0 and index -1. A string of length two is indexed left to right by the values 0 and 1, and from right to left by -1 and -2. If the element of string exists, and the string is modifiable, then the form denotes a place. A place supports deletion. When a deletion takes place, then the character at is removed from the string. Any characters after that position move by one position to close the gap, and the length of the string decreases by one. Direct use of is equivalent to the DWIM bracket notation except that must be a string. The following relation holds:
      (chr-str s i) --> [s i] since this also holds:
      (chr-str s i) --> (ref s i) However, note the following difference. When the expression is used as a place, then the subexpression must be a place. When is used as a place, need not be a place. The function performs random access on string to overwrite the character whose position is given by integer which must be within range of the string. The character at is overwritten with character The argument works exactly as in The argument must be a modifiable string. Direct use of is equivalent to the DWIM bracket notation provided that is a string and an integer. The following relation holds:
      (chr-str-set s i c) --> (set [s i] c) Since for an integer index this also holds:
      (chr-str s i) --> (refset s i c) The function determines the longest prefix of string which consists only of the characters in string in any combination. If both arguments are strings, the function returns an integer between 0 and the length of
      (span-str "abcde" "ab") -> 2
      (span-str "abcde" "z") -> 0
      (span-str "abcde" "") -> 0
      (span-str "abcde" "edcba") -> 5 The function determines the longest prefix of string which consists only of the characters which do not appear in in any combination. If both arguments are strings, the function returns an integer between 0 and the length of
      (compl-span-str "abc,def" ",") -> 3
      (compl-span-str "abc," ",") -> 3
      (compl-span-str "abc" ",") -> 3
      (compl-span-str "abc3" "0123456789") -> 3
      (compl-span-str "3" "0123456789") -> 0 The function returns an integer which represents the position of the first character in string which appears in string If there is no such character, then is returned.
      (break-str "abc,def.ghi" ",.:") -> 3
      (break-str "abc,def.ghi" ".:") -> 6
      (break-str "abc,def.ghi" ":") -> nil Lazy strings are objects that were developed for the TXR pattern-matching language, and are exposed via TXR Lisp. Lazy strings behave much like strings, and can be substituted for strings. However, unlike regular strings, which exist in their entirety, first to last character, from the moment they are created, lazy strings do not exist all at once, but are created on demand. If character at index N of a lazy string is accessed, then characters 0 through N of that string are forced into existence. However, characters at indices beyond N need not necessarily exist. A lazy string dynamically grows by acquiring new text from a list of strings which is attached to that lazy string object. When the lazy string is accessed beyond the end of its hitherto materialized prefix, it takes enough strings from the list in order to materialize the index. If the list doesn't have enough material, then the access fails, just like an access beyond the end of a regular string. A lazy string always takes whole strings from the attached list. Lazy string growth is achieved via the function which forces a string to exist up to a given character position. This function is used internally to handle various situations. The function forces the entire string to materialize. If the string is connected to an infinite lazy list, this will exhaust all memory. Lazy strings are specially recognized in many of the regular string functions, which do the right thing with lazy strings. For instance when is invoked on a lazy string, a special version of the logic is used which handles various lazy string cases, and can potentially return another lazy string. Taking a of a lazy string from a given character position to the end does not force the entire lazy string to exist, and in fact the operation will work on a lazy string that is infinite. Furthermore, special lazy string functions are provided which allow programs to be written carefully to take better advantage of lazy strings. What carefully means is code that avoids unnecessarily forcing the lazy string. For instance, in many situations it is necessary to obtain the length of a string, only to test it for equality or inequality with some number. But it is not necessary to compute the length of a string in order to know that it is greater than some value. The function constructs a lazy string which draws material from which is a list of strings. If the optional argument is given, then it specifies a string which is appended to every string from before that string is incorporated into the lazy string. If is not given, then it defaults to the string and so the strings from are effectively treated as lines which get terminated by newlines as they accumulate into the growing prefix of the lazy string. To avoid the use of a terminator string, a null string argument must be explicitly passed. In that case, the lazy string grows simply by catenating elements from If the argument is specified, it must be a positive integer. It expresses a maximum limit on how many elements will be consumed from in order to feed the lazy string. Once that many elements are drawn, the string ends, even if the list has not been exhausted. However, that remaining list, though not contributing to the string, is still incorporated into the value returned by The function returns if is a lazy string. Otherwise it returns The function tries to instantiate the lazy string such that the position given by materializes. The is a character position, exactly as used in the function. It is an error if the argument isn't a lazy string. Some positions beyond may also materialize, as a side effect, because the operation takes only whole strings from the internal list, according to the algorithm described below. If the string is already materialized through to at least or if it is possible to materialize the string that far, then the value is returned to indicate success. If there is sufficient material to force the lazy string through to the position, then is returned, otherwise The object's is observed: a total of no more than elements are taken from the object's list. The algorithm is as follows:

    1.  While the length of the materialized prefix of the string is less than or equal to and while elements are available in the list, subject to observance of the perform the following steps 2 and 3:

    2.  Remove the next available string from the list, and add it as a suffix to the materialized prefix.

    3.  Add the string to the materialized prefix.

    4.  Return if the length of the materialized prefix exceeds otherwise

    The algorithm does not take portions of strings from the list, and always adds the terminator after incorporating each piece into the materialized prefix. The argument must be a lazy string. The lazy string is forced to fully materialize. The return value is an ordinary, non-lazy string equivalent to the fully materialized lazy string. The object's is observed: a total of no more than elements are taken from the object's list. The algorithm that is followed by is similar to the one followed by with only the following modification. The test in step 1 isn't concerned with the length of the materialized prefix, since the goal is to materialize all available characters. Steps 2 and 3 are performed while elements are available in the list, subject to observance of the The function can be considered, in some way, an inverse operation to the production of the lazy string from its associated list. Note: the behavior of this function changed in TXR 274. This is subject to a note in the COMPATIBILITY section. First, the lazy string is forced up through the position as if by a call to If consists of or more characters, then after the forcing operation, it is guaranteed that at least characters of the string have been materialized into a single string, called the materialized prefix of the lazy string. If fewer than characters are available, taking into account the contribution of the terminator string, then the number of characters in the materialized prefix fall short of The materialized prefix never takes fractional strings from the lazy string's list, and is always terminated by the terminator string. Next, the materialized prefix is split into pieces on occurrences of terminator string, as if by using function. If the terminator string is empty, it is split into individual characters, in accordance with the semantics of that function. Then, if the last piece of the split prefix is an empty string, it is removed. This situation occurs in two cases: the materialized prefix is empty, or else it ends in the terminating string. For example, if the terminating string is a single newline, and the prefix is In this case, produces from which the trailing empty string is removed, leaving Finally, a list is formed by appending the split piece of the materialized prefix, calculated as described above, with remaining list of strings which have not been pulled into the materialized prefix. This list is returned. These functions compare the lengths of two strings. The following equivalences hold, as far as the resulting value is concerned:
      (length-str-> s l) <--> (> (length-str s) l)
      (length-str->= s l) <--> (>= (length-str s) l)
      (length-str-< s l) <--> (< (length-str s) l)
      (length-str-<= s l) <--> (<= (length-str s) l) The difference between the functions and the equivalent forms is that if the string is lazy, the function will fully force it in order to calculate and return its length. These functions only force a string up to position so they are not only more efficient, but on infinitely long lazy strings they are usable. cannot compute the length of a lazy string with an unbounded length; it will exhaust all memory trying to force the string. These functions can be used to test such as string whether it is longer or shorter than a given length, without forcing the string beyond that length. The function returns -1 if is lexicographically prior to If the reverse relationship holds, it returns 1. Otherwise the strings are equal and zero is returned. If either or both of the strings are lazy, then they are only forced to the minimum extent necessary for the function to reach a conclusion and return the appropriate value, since there is no need to look beyond the first character position in which they differ. The lexicographic ordering is naive, based on the character code point values in Unicode taken as integers, without regard for locale-specific collation orders. Note: in TXR 232 and earlier versions, conforms to a weaker requirements: any negative integer value may be returned rather than -1, and any positive integer value can be returned instead of 1. These functions compare and lexicographically, as if by the function. The function returns if the two strings are exactly the same, character for character, otherwise it returns The function returns if is lexicographically before otherwise nil. The function returns if is lexicographically after otherwise The function returns if is lexicographically before or if they are exactly the same, otherwise The function returns if is lexicographically after or if they are exactly the same, otherwise The is a deprecated alias for The function creates and returns a vector object of the specified length. The elements of the vector are initialized to or to nil if is omitted. The function creates a vector out of its arguments. The function returns if is a vector, otherwise it returns The modifies the length of making it longer or shorter. If the vector is made longer, then the newly added elements are initialized to nil. The argument must be nonnegative. The return value is The function performs indexing into a vector. It retrieves an element of at position counted from zero. The value must range from 0 to one less than the length of the vector. The specified element is returned. If the element of vector exists, then the form denotes a place. A place supports deletion. When a deletion takes place, then if denotes the last element in the vector, the vector's length is decreased by one, so that the vector no longer has that element. Otherwise, if isn't the last element, then each elements values at a higher index than shifts by one one element position to the adjacent lower index. Then, the length of the vector is decreased by one, so that the last element position disappears. The function extends the length of a vector by one element, and sets the new element to the value The previous length of the vector (which is also the position of is returned. The function returns the length of vector It performs similarly to the generic function, except that the argument must be a vector. The function returns the number of elements for which storage is reserved in the vector The of the vector can be extended up to this size without any memory allocation operations having to be performed. The function returns a vector which contains all of the same elements and in the same order as list Note: this function is also known by the obsolescent name The function returns a list of the elements of vector Note: this function is also known by the obsolescent name The function returns a new vector object of the same length as and containing the same elements in the same order. The function has the same parameters and semantics as the function except that the argument must be a vector. If a form is used as a place, it denotes a subrange of as if it were a storage location. The previous value of this location, if needed, is fetched by a call to Storing to the place is performed by a call to In an update operation which accesses the prior value and stores a new value, the arguments and are evaluated once. The argument is not itself required to be a place; it is not updated when a value is written to the storage location. The is like the function except that the argument must be a vector. The function overwrites a range of the vector with copies of the value. The and index arguments follow the same range indexing conventions as the and functions. If is omitted, it defaults to zero. If is omitted, it defaults to the length of Negative values of and are adjusted by adding the length of the vector to them, once. If the adjusted value of either or is negative, or exceeds the length of an error exception is thrown. The adjusted values of and specify a range of vec starting at the index, and ending at the index, which is excluded from the range. If the adjusted is less than or equal to the adjusted then is unaltered. Otherwise, copies of element are stored into starting at the index, ending just before the index is reached. The function returns
      (defvarl v (vec 1 2 3))
      v --> #(1 2 3)
      (fill-vec v 0) --> #(0 0 0)
      (fill-vec v 3 1) --> #(0 3 3)
      (fill-vec v 4 -1) --> #(0 3 4)
      (fill-vec v 5 -3 -1) --> #(5 5 4) The argument is a list of vectors. The function produces a catenation of the vectors listed in It returns a single large vector formed by catenating those vectors together in order. The function constructs a nested vector according to the arguments, described in detail below. The function is equivalent to with an argument of When there are no arguments, returns If there is exactly one argument, it must be a nonnegative integer. A newly created having that many elements is returned, with each element of the vector being If there are two or more arguments, nested vector is returned. The first argument specifies the outermost dimension: a vector of that many elements are returned. Each element of that vector is a vector whose length is given by the second dimension. This nesting pattern continues through the remaining dimensions. The last dimension specifies the length of vectors which are filled with From the above it follows that if a zero-valued is encountered, every vector corresponding to that level of nesting shall be empty, and that shall be the last dimension regardless of the presence of additional arguments.
      (nested-vec) -> nil
      (nested-vec-of 0 4) -> #(0 0 0 0)
      (nested-vec-of 0 4 3) -> #(#(0 0 0)
                            #(0 0 0)
                            #(0 0 0)
                            #(0 0 0))
      (nested-vec-of 'a 4 3 2) -> #(#(#(a a) #(a a) #(a a))
                            #(#(a a) #(a a) #(a a))
                            #(#(a a) #(a a) #(a a))
                            #(#(a a) #(a a) #(a a)))
      (nested-vec-of 'a 1 1 1) -> #(#(#(a)))
      (nested-vec-of 'a 1 1 0) -> #(#(#()))
      (nested-vec-of 'a 1 0 1) -> #(#())
      (nested-vec-of 'a 1 0) -> #(#())
      (nested-vec-of 'a 0 1) -> #()
      (nested-vec-of 'a 0) -> #()
      (nested-vec-of 'a 4 0 1) #(#() #() #() #())
      (nested-vec-of 'a 4 0) #(#() #() #() #())) Object of the type are buffers: vector-like objects specialized for holding binary data represented as a sequence of 8-bit bytes. Buffers support operations specialized toward the encoding of Lisp values into machine-oriented data types, and decoding such data types into Lisp values. Buffers are particularly useful in conjunction with the Foreign Function Interface (FFI), since they can be used to prepare arbitrary data which can be passed into and out of a function by pointer. They are also useful for binary I/O. Buffers support a number of similar functions for converting Lisp numeric values into common data types, which are placed into the buffer. These functions are named starting with the prefix, followed by an abbreviated type name. Each of these functions takes three arguments: specifies the buffer, specifies the byte offset position into the buffer which receives the low-order byte of the data transfer, and indicates the value. If has a value such that any portion of the data transfer would like outside of the buffer, the buffer is automatically extended in length to contain the data transfer. If this extension causes any padding bytes to appear between the previous length of the buffer and those bytes are initialized to zero. The argument giving the value to be stored must be an integer or character, except in the case of the types and functions and for which it is required to be of type and in case of the function which expects the argument to be a object. The argument must be in range for the data type, or an exception results. Unless otherwise indicated, the stored datum is in the local format used by the machine with regard to byte order and other representational details. Buffers support a number of similar functions for extracting common data types, and converting them into Lisp values. These functions are named starting with the prefix, followed by an abbreviated type name. Each of these functions takes two arguments: specifies the buffer and specifies the byte offset position into the buffer which holds the low-order byte of the datum to be extracted. If any portion of requested datum lies outside of the boundaries of the buffer, an error exception is thrown. The extracted value is converted to a Lisp datum. For the majority of these functions, the returned value is of type integer. The and return a floating-point value. The function returns a value of type The function creates a new buffer object which holds bytes. This argument may be zero. If is present, it specifies the value with which the first bytes of the buffer are initialized. If omitted, it defaults to zero. The value of must lie in the range 0 to 255. The parameter indicates how much memory to actually allocate for the buffer. If an argument is not given, the parameter takes on the same value as If an argument is given, its value must not be less than The function returns if is a otherwise it returns The function retrieves the buffer length: how many bytes are stored in the buffer. Note: the generic function is also applicable to buffers. The function retrieves the allocation size of the buffer. The function reduces the amount of memory allocated to the buffer to the minimum required to hold it contents, effectively setting the allocation size to the current length. The previous allocation size is returned. The function changes the length of the buffer. If the buffer is made longer, the newly added bytes appear at the end, and are initialized to the value given by If is specified, its value must be in the range 0 to 255. It defaults to zero. The function returns a duplicate of an object distinct from which has the same length and contents, and compares to The function has the same semantics as the function, except that the first argument must be a buffer. The extracted sub-range of a buffer is itself a buffer object. If is used as a syntactic place, the argument expressions and are evaluated just once. The prior value, if required, is accessed by calling and is then stored via The function has the same semantics as the function, except that the first argument must be a buffer. The elements of are stored into as if using the function and therefore must be suitable arguments for that function. The of the arguments, semantics and return value given for apply to The function creates and returns a new buffer, whose contents are derived from the elements of which may be any kind of sequence. The elements of must be integers whose values lie in the range 0 to 255, or else characters whose code point values lie in that range. These values are placed into the newly created buffer, which therefore has the same length as The function stores a copy of buffer into at the offset indicated by The source and destination memory regions may overlap. The return value is Note: the effect of a operation may also be performed by a suitable call to however, is less general: it doesn't insert or delete by replacing destination ranges with data of differing length, and requires a source operand of buffer type. The converts into an 8-bit signed integer, and stores it into the buffer at the offset indicated by The return value is The converts into an 8-bit unsigned integer, and stores it into the buffer at the offset indicated by The return value is The converts into a sixteen bit signed integer, and stores it into the buffer at the offset indicated by The return value is The converts into a sixteen bit unsigned integer, and stores it into the buffer at the offset indicated by The return value is The converts into a 32-bit signed integer, and stores it into the buffer at the offset indicated by The return value is The converts into a 32-bit unsigned integer, and stores it into the buffer at the offset indicated by The return value is The converts into a 64-bit signed integer, and stores it into the buffer at the offset indicated by The return value is The converts the value into a 64-bit unsigned integer, and stores it into the buffer at the offset indicated by The return value is The converts into a value of the C type and stores it into the buffer at the offset indicated by The return value is Note that the type may be signed or unsigned. The converts into a value of the C type and stores it into the buffer at the offset indicated by The converts into a value of the C type and stores it into the buffer at the offset indicated by The converts into a value of the C type and stores it into the buffer at the offset indicated by The converts into a value of the C type and stores it into the buffer at the offset indicated by The converts into a value of the C type and stores it into the buffer at the offset indicated by The converts into a value of the C type and stores it into the buffer at the offset indicated by The converts into a value of the C type and stores it into the buffer at the offset indicated by The converts into a value of the C type and stores it into the buffer at the offset indicated by Note: the conversion of a TXR Lisp floating-point value to the C type float may be inexact, reducing the numeric precision. The converts into a value of the C type and stores it into the buffer at the offset indicated by The expects to be of type It stores the object's pointer value into the buffer at the offset indicated by The function extracts and returns signed 8-bit integer from at the offset given by The function extracts and returns an unsigned 8-bit integer from at the offset given by The function extracts and returns a signed 16-bit integer from at the offset given by The function extracts and returns an unsigned 16-bit integer from at the offset given by The function extracts and returns a signed 32-bit integer from at the offset given by The function extracts and returns an unsigned 32-bit integer from at the offset given by The function extracts and returns a signed 64-bit integer from at the offset given by The function extracts and returns an unsigned 64-bit integer from at the offset given by The function extracts and returns a value of the C type from at the offset given by Note that may be signed or unsigned. The function extracts and returns a value of the C type from at the offset given by The function extracts and returns a value of the C type from at the offset given by The function extracts and returns a value of the C type from at the offset given by The function extracts and returns a value of the C type from at the offset given by The function extracts and returns a value of the C type from at the offset given by The function extracts and returns a value of the C type from at the offset given by The function extracts and returns a value of the C type from at the offset given by The function extracts and returns a value of the C type from at the offset given by returning that value as a Lisp floating-point number. The function extracts and returns a value of the C type from at the offset given by returning that value as a Lisp floating-point number. The function extracts a C pointer from at the offset given by returning that value as a Lisp object of type The function writes the contents of buffer starting at position to a stream, through to the last byte, if possible. Successive bytes from the buffer are written to the stream as if by a operation. If is omitted, it defaults to If is omitted, it defaults to zero. It indicates the starting position within the buffer. The stream must support the operation. Streams which support can be expected to support and, conversely, streams which do not support do not support The function returns the position of the last byte that was successfully written. If the buffer was written through to the end, then this value corresponds to the length of the buffer. If an error occurs before any bytes are written, the function throws an error. The reads bytes from and writes them into consecutive locations in buffer starting at position The bytes are read as if using the function. If the argument is omitted, it defaults to If is omitted, it defaults to zero. It indicates the starting position within the buffer. The stream must support the operation. Buffers which support can be expected to support and, conversely, streams which do not support do not support The function returns the position that is one byte past the last byte that was successfully read. If an end-of-file or other error condition occurs before the buffer is filled through to the end, then the value returned is smaller than the buffer length. In this case, the area of the buffer beyond the read size retains its previous content. If an error situation occurs other than a premature end-of-file before any bytes are read, then an exception is thrown. If an end-of-file condition occurs before any bytes are read, then zero is returned. The differs usefully from as follows. Whereas doesn't manipulate the length of the buffer at any stage of the operation, the begins by adjusting the length of the buffer to the underlying allocated size. Then it performs the fill operation in exactly the same manner as Finally, if the operation succeeds, then adjusts the length of the buffer to match the position that is returned. The reads bytes from as if using the function, until either a the newline character is encountered, or else the end of input is encountered. The bytes which are read, exclusive of the newline character, are returned in a new buffer object. The newline character, if it occurs, is consumed. If is omitted, it defaults to The stream is required to support byte input. The function opens a binary stream over the file indicated by the string argument for reading. By default, the entire file is read and its contents are returned as a buffer object. The buffer's length corresponds to the number of bytes read from the file. The function opens a binary stream over an input command pipe created for the command string as if by the function. It read bytes from the pipe until the indication that no more input is available. The bytes are returned aggregated into a buffer object. If the parameter is given an argument, it must be a nonnegative integer. That value specifies a limit on the number of bytes to read. A buffer no longer than shall be returned. If the parameter is given an argument, it must be a nonnegative integer. That value specifies how many initial bytes of the input should be discarded before accumulation of the buffer begins. If possible, the semantics of this parameter is achieved by performing a operation, falling back on reading and discarding bytes if the stream doesn't support seeking. If is specified, then the stream is opened in unbuffered mode, so that bytes beyond the specified range shall not be requested from the underlying file, device or process. The function opens the file as if using the function, using a of If the is present, it specifies to be added to the string. These must be compatible with the implicit mode. The function opens a text stream over the file indicated by the string argument writes the contents of the buffer object into the file, and then closes the file. If the file doesn't exist, it is created. If it exists, it is truncated to zero length and overwritten. The default value of the optional parameter is zero. If an argument is given, it must be a nonnegative integer. If it is nonzero, then after opening the file, before writing the buffer, the function will seek to an offset of that many bytes from the start of the file. The contents of will be written at that offset. The function does not truncate an existing file to zero length. In all other regards, it is equivalent to The function is similar to except that if the file exists, it isn't overwritten. Rather, the buffer is appended to the file. The function opens an output text stream over an output command pipe created for the command specified in the string argument as if by the function. It then writes the contents of buffer into the stream and closes the stream. The and functions open a file as if using the function using, respectively, values of and The argument, if present, specifies additional to be added to these modes. The return value of all three functions is that of the operation which is implicitly performed. The and functions perform UTF-8 conversion between the character string and buffer data types. The function UTF-8-encodes and returns a buffer containing the converted representation. If a true argument is given to the parameter, then a null terminating byte is added to the buffer. This byte is added even if the previous byte is already a null byte from the conversion of a pseudo-null character occurring in The function takes the contents of buffer to be UTF-8 data, which is converted to a character string and returned. Null bytes in the buffer are mapped to the pseudo-null character If a true argument is given to the parameter, then if the contents of end in a null byte, that byte is not included in the conversion. The and functions convert a signed and unsigned integer, respectively, or else a character, into a binary representation, which is returned as a buffer object. Under both functions, the representation uses big endian byte order: most significant byte first. The function requires a nonnegative argument, which may be a character. The representation stored in the buffer is a pure binary representation of the value using the smallest number of bytes required for the given value. The function requires an integer or character argument. The representation stored in the buffer is a two's complement representation of using the smallest number of bytes which can represent that value. If is nonnegative, then the first byte of the buffer lies in the range 0 to 127. If is negative, then the first byte of the buffer lies in the range 128 to 255. The integer 255 therefore doesn't convert to the buffer but rather The buffer represents -1. If the argument is a character object, it is taken to be its Unicode code point value, as returned by the function. The and functions recover an integer value from its binary form which appears inside which must be a buffer object. These functions expect to contain the representation produced by, respectively, the functions and If holds the representation of an integer value as produced by then returns The same relationship holds between and Thus, these equalities hold: provided that is of integer type and, in the case of nonnegative. The and functions perform compression using the Deflate algorithm, via Zlib. These functions are only available if TXR is built with Zlib support. More specifically, uses Zlib's function; therefore it can be expected to interoperate with other software which uses the same function. The function compresses the entire contents of and returns new buffer with the compressed contents. The optional argument specifies the compression level as an integer. Valid values range from 0 (no compression) to 9 (maximum compression). The value -1 selects a default compression determined internally by Zlib. The function reverses the operation: it takes a compressed and returns a buffer containing the original uncompressed data. The function throws an error exception if the value is unacceptable to Zlib. The function throws an error exception if doesn't contain a compressed image. TXR supports user-defined types in the form of structures. Structures are objects which hold multiple storage locations called slots, which are named by symbols. Structures can be related to each other by inheritance. Multiple inheritance is permitted. The type of a structure is itself an object, of type When the program defines a new structure type, it does so by creating a new instance, with properties which describe the new structure type: its name, its list of slots, its initialization and "boa constructor" functions, and the structures type it inherits from (the supertypes). The object is then used to generate instances. Structures instances are not only containers which hold named slots, but they also indicate their struct type. Two structures which have the same number of slots having the same names are not necessarily of the same type. Structure types and structures may be created and manipulated using a programming interface based on functions. For more convenient and clutter-free expression of structure-based program code, macros are also provided. Furthermore, concise and expressive slot access syntax is provided courtesy of the referencing dot and unbound referencing dot syntax, a syntactic sugar for the and macros. Structure types have a name, which is a symbol. The function, when applied to any struct type, returns the symbol When is applied to a struct instance, it returns the name of the struct type. Effectively, struct names are types. The consequences are unspecified if an existing struct name is reused for a different struct type, or an existing type name is used for a struct type. Structure slots can be of two kinds: they can be the ordinary instance slots or they can be static slots. The instances of a given structure type have their own instance of a given instance slot. However, they all share a single instance of a static slot. Static slots are allocated in a global area associated with a structure type and are initialized when the structure type is created. They are useful for efficiently representing properties which have the same value for all instances of a struct. These properties don't have to occupy space in each instance, and time doesn't have to be wasted initializing them each time a new instance is created. Static slots are also useful for struct-specific global variables. Lastly, static slots are also useful for holding methods and functions. Although structures can have methods and functions in their instances, usually, all structures of the same type share the same functions. The macro supports a special syntax for defining methods and struct-specific functions at the same time when a new structure type is defined. The macro can be used for adding new methods and functions to an existing structure and its descendants. Static slots may be assigned just like instance slots. Changing a static slot changes that slot in every structure of the same type. Static slots are not listed in the notation when a structure is printed. When the structure notation is read from a stream, if static slots are present, they will be processed and their values stored in the static locations they represent, thus changing their values for all instances. Static slots are inherited just like instance slots. The following simplified discussion is restricted to single inheritance. A detailed description of multiple inheritance is given in the Multiple Inheritance section below. If a given structure has some static slot and a new structure is derived from using and does not define a slot then inherits This means that shares the static slot with both types share a single instance of that slot. On the other hand if defines a static slot then that slot will have its own instance in the structure type; will not inherit the instance of slot Moreover, if the definition of omits the for slot then that slot will be initialized with a copy of the current value of slot of the base type, which allows derived types to obtain the value of base type's static slot, yet have that in their own instance. The slot type can be overridden. A structure type deriving from another type can introduce slots which have the same names as the supertype, but are of a different kind: an instance slot in the supertype can be replaced by a static slot in the derived type or vice versa. Note that, in light of the above type overriding possibility, the static slot value propagation happens only from the immediate supertype. If is derived from which has a static slot whereas specifies as an instance slot, but then again specifies a static slot then slot will not inherit the value from slot. Simply, supertype is and that supertype is not considered to have a static slot A structure type is associated with a static initialization function which may be used to store initial values into static slots. This function is invoked once in a type's life time, when the type is created. The function is also inherited by derived struct types and invoked when they are created. When a structure type is defined, two or more supertypes may be specified. The new structure type then potentially inherits instance and static slots from all of the specified supertypes, and is considered to be a subtype of all of them. This situation with two or more supertypes is called multiple inheritance. The contrasting term is single inheritance, denoting the situation when a structure has exactly one supertype. TXR Lisp's struct types initially permitted only single inheritance. Multiple inheritance support was introduced in version 229, as a straightforward extension of single inheritance semantics. In the function and macro, a list of supertypes can be given instead of just one. The type then inherits slots from all of the specified types. If any conflicts arise among the supertypes due to slots having the same name, the leftmost supertype dominates: that type's slot will be inherited. If the leftmost slot is static, then that static slot will be inherited. Otherwise, the instance slot will be inherited. Of course, any slot which is specified in the newly defined type itself dominates over any same-named slots among the supertypes. The new structure type inherits all of the slot initializing expressions, as well as and methods of all of its supertypes. Each time the structure is instantiated, the initializing expressions inherited from the supertypes, together with the slot initializing expressions, are all evaluated, in right-to-left order: the initializations contributed by each supertype are performed before considering the next supertype to the left. The methods are similarly invoked in right-to-left order, before the methods of the new type itself. Thus the order is: supertype inits, own inits, supertype post-inits, own post-inits. Multiple inheritance makes it possible for a type to inherit the same supertype more than once, either directly (by naming it more than once as a direct supertype) or indirectly (by inheriting two or more different types, which have a common ancestor). The latter situation is sometimes referred to as the diamond problem. Until TXR 242, the situation of duplicate supertypes was ignored for the purposes of object initialization. It was documented that if a supertype is referenced by inheritance, directly or indirectly, two or more times, then its initializing expressions are evaluated that many times. Starting in TXR 243, duplicate supertypes no longer give rise to duplicate initialization. When an object is instantiated, only one initialization of a duplicated supertype occurs. The subsequent initializations that would take place in the absence of duplicate detection are suppressed. Note also that the mechanism is tied to initialization. Initialization of an object registers the finalizers, and so in TXR 242, finalizers are also executed multiple times, if initializers are. Consider following program:
      (defstruct base ()
        (:init (me) (put-line "base init"))
        (:fini (me) (put-line "base fini")))
      (defstruct d1 (base)
        (:init (me) (put-line "d1 init"))
        (:fini (me) (put-line "d1 fini")))
      (defstruct d2 (base)
        (:init (me) (put-line "d2 init"))
        (:fini (me) (put-line "d2 fini")))
      (defstruct s (d1 d2))
      (call-finalizers (new s)) Under TXR 242, and earlier versions that support multiple inheritance, it produces the output:
      base init
      d2 init
      base init
      d1 init
      d1 fini
      base fini
      d2 fini
      base fini The supertypes are initialized in a right-to-left traversal of the type lattice, without regard for being duplicated. Starting with TXR 243, the output is:
      base init
      d2 init
      d1 init
      d1 fini
      d2 fini
      base fini The rightmost duplicate of the base is initialized, so that the initialization is complete prior to the initializations of any dependent types. Likewise, the same rightmost duplicate of the base is finalized, so that finalization takes place after that of any dependent struct types. Note, however, that the function function mechanism is not required to detect duplicated direct supertypes. If a supertype implements the function to detect situations when it is the target of inheritance, and some subtype inherits that type more than once, that function may be called more than once. The behavior is unspecified. All structure instances contain a Boolean flag called the dirty flag. This flag is not a slot, but rather a meta-data property that is exposed to program access. When the flag is set, an object is said to be dirty; otherwise it is clean. Newly constructed objects come into existence dirty. The dirty flag state can be tested with the function An object can be marked as clean by clearing its dirty flag with A combined operation is provided which clears the dirty flag, and returns its previous value. The dirty flag is set whenever a new value is stored into the instance slot of an object. Note: the dirty flag can be used to support support the caching of values derived from an object's slots. The derived values don't have to be recomputed while an object remains clean. In object-based or object-oriented programming, sometimes it is necessary for a new data type to provide its own notion of equality: its own requirements for when two distinct instances of the type are considered equal. Furthermore, types sometimes have to implement their own notion, also, of inequality: the requirements for the manner in which one instance is considered lesser or greater than another. TXR Lisp structures implement a concept called equality substitution which provides a simple, unified way for the implementor of an object to encode the requirements for both equality and inequality. Equality substitution allows for objects to be used as keys in a hash table according to the custom equality, without the programmer being burdened with the responsibility of developing a custom hashing function. An object participates in equality substitution by implementing the method. The method takes no arguments other than the object itself. It returns a representative value which is used in place of that object for the purposes of comparison. Whenever an object which supports equality substitution is used as an argument of any of the functions or the method of that object is invoked, and the return value of that method is taken in place of that object. The same is true if an object which supports equality substitution is used as a key in an hash table. The substitution is applied repeatedly: if the return value of the object's method is an object which itself supports equality substitution, than that returned object's method is invoked on that object to fetch its equality substitute. This repeats as many times as necessary until an object is determined which isn't a structure that supports equality substitution. Once the equality substitute is determined, then the given function proceeds with the replacement object. Thus for example compares the replacement object in place of the original, and an hash table uses the replacement object as the key for the purposes of hashing and comparison. The macro has a provision for for application-defined clauses, which may be defined using the macro. This macro associates new clause keywords with custom expansion. The clause of is in fact implemented externally to using The macro has a provision for implicit inclusion of application-defined clauses called preludes, which are previously defined via the macro. During macro-expansion, checks whether the structure being defined is the target of one or more preludes. If so, it includes the clauses from those preludes as if they were written directly in the syntax. The macro defines a new structure type and registers it under which must be a bindable symbol, according to the function. Likewise, the name of every must also be a bindable symbol. The argument must either be or a symbol which names an existing struct type, or else a list of such symbols. The newly defined struct type will inherit all slots, as well as initialization behaviors from the specified struct types. The macro is implemented using the function, which is more general. The macro analyzes the argument syntax, and synthesizes arguments which are then used to call the function. Some remarks in the description of only apply to structure types defined using that macro. Slots are specified using zero or more clauses. Application-defined clauses are possible via The macro may bring in prelude clauses which are not specified in its syntax, but that have been specified using The following built-in clauses are supported:
    The simplest slot specifier is just a name, which must be a bindable symbol, as defined by the function. This form is a short form for the syntax. This syntax is a short form for the syntax. This syntax specifies an instance slot called whose initial value is obtained by evaluating whenever a new instance of the structure is created. This evaluation takes place in the original lexical environment in which the form occurs. If is omitted, the slot is initialized to This syntax specifies a static slot called whose initial value is obtained by evaluating once, during the evaluation of the form in which it occurs, if the is present. If is absent, and a static slot with the same name exists in the base type, then this slot is initialized with the value of that slot. Otherwise it is initialized to The definition of a static slot in a causes the new type to have its own instance that slot, even if a same-named static slot occurs in the base type, or its bases. This syntax creates a static slot called which is initialized with an anonymous function. The anonymous function is created during the evaluation of the form. The function takes the arguments specified by the symbols, and its body consists of the There must be at least one When the function is invoked as a method, as intended, the leftmost receives the structure instance. The are evaluated in a context in which a block named is visible. Consequently, may be used to terminate the execution of a method and return a value. Methods are invoked using the syntax, which implicitly inserts the instance into the argument list. Due to the semantics of static slots, methods are naturally inherited from a base structure to a derived one, and defining a method in a derived class which also exists in a base class performs OOP-style overriding. This syntax creates a static slot called which is initialized with an anonymous function. The anonymous function is created during the evaluation of the form. The function takes the arguments specified by the symbols, and its body consists of the This specifier differs from only in one respect: there may be zero parameters. A structure function defined this way is intended to be used as a utility function which doesn't receive the structure instance as an argument. The are evaluated in a context in which a block named is visible. Consequently, may be used to terminate the execution of the function and return a value. Such functions are called using the or else the DWIM brackets syntax The remarks about inheritance and overriding in the description of also apply to The specifier doesn't describe a slot. Rather, it specifies code which is executed when a structure is instantiated, before the slot initializations specific to the structure type are performed. The code consists of which are evaluated in order in a lexical scope in which the variable is bound to the structure object. Multiple specifiers may appear in the same form. They are executed in their order of appearance, left to right. When an object with one or more levels of inheritance is instantiated, the code of a base structure type, if any, is executed before any initializations specific to a derived structure type. Under multiple inheritance, the code of the rightmost base type is executed first, then that of the remaining bases in right-to-left order. The initializations are executed before any other slot initializations. The argument values passed to the or operator or the function are not yet stored in the object's slots, and are not accessible. Initialization code which needs these values to be stable can be defined with Initializers in base structures must be careful about assumptions about slot kinds, because derived structures can alter static slots to instance slots or vice versa. To avoid an unwanted initialization being applied to the wrong kind of slot, initialization code can be made conditional on the outcome of applied to the slot. (Code generated by for initializing instance slots performs this kind of check). The of an specifier are not surrounded by an implicit The specifier is similar to Both specify forms which are evaluated during object instantiation. The difference is that the of a are evaluated after other initializations have taken place, including the initializations, as a second pass. By the time initialization runs, the argument material from the or invocation has already been processed and stored into slots. Like actions, actions registered at different levels of the type's inheritance hierarchy are invoked in the base-to-derived order, in right-to-left order among multiple bases at the same level. Multiple form in the same are invoked in left-to-right order. The specifier doesn't describe a slot. Rather, it specifies a finalization function which is associated with the structure instance, as if by use of the function. This finalization registration takes place as the first step when an instance of the structure is created, before the slots are initialized and the code, if any, has been executed. The registration takes place as if by the evaluation of the form where denotes the structure instance. Note the argument which requests reverse order of registration, ensuring that if an object has multiple finalizers registered at different levels of inheritance hierarchy, the finalizers specified for a derived structure type are called before inherited finalizers. The of a specifier are not surrounded by an implicit Multiple clauses may be specified in the same in which case they are invoked in reverse, right-to-left order. Note that an object's finalizers can be called explicitly with Note: the macro arranges for finalizers to be called on objects when the execution of a scope terminates by any means. Like specifier doesn't describe a slot. The syntax is identical to Independently of whether is specified, at most one may be specified. The only difference between and is that arranges for a finalizer to be registered as if by the evaluation of the form where denotes the structure instance. Note the that unlike this omits the parameter, which means that finalizers of derived structures execute after the execution of inherited finalizers. It also means that multiple finalizers appearing in the same execute in left-to-right order unlike the reverse right-to-left order of finalizers. When both and clauses are specified in the same form, all the finalizers execute after all the finalizers regardless of the order in which they appear. The clause specifies zero or more types to be inherited. Each argument must be a symbol which is the name of an existing struct type. These symbols are appended to the list of supertypes coming from the argument Note: the motivation behind is to make it possible for struct clauses defined by to inject supertypes. Developers are encouraged to use the regular argument of to declare inheritance of supertypes, rather than writing visible clauses that can be moved into the argument.

    The slot names given in a must all be unique among themselves, but they may match the names of existing slots in the base type. A given structure type can have only one slot under a given symbolic name. If a newly specified slot matches the name of an existing slot in the type or that type's chain of ancestors, it is called a repeated slot. The kind of the repeated slot (static or instance) is not inherited; it is established by the and may be different from the type of the same-named slot in the supertype or its ancestors. If a repeated slot is introduced as a static slot, and has no then it receives the current of the a static of the same name from the nearest supertype which has such a slot. If a repeated slot is an instance slot, no such inheritance of value takes place; only the local applies to it; if it is absent, the slot it initialized to in each newly created instance of the new type. However, and initializations are inherited from a base type and they apply to the repeated slots, regardless of their kind. These initializations take place on the instantiated object, and the slot references resolve accordingly. The initialization for slots which are specified using the or specifiers is reordered with regard to slots. Regardless of their placement in the form, and slots are initialized before slots. This ordering is useful, because it means that when the initialization expression for a given static slot constructs an instance of the struct type, any instance initialization code executing for that instance can use all functions and methods of the struct type. However, note the static slots which follow that slot in the syntax are not yet initialized. If it is necessary for a structure's initialization code to have access to all static slots, even when the structure is instantiated during the initialization of a static slot, a possible solution may be to use lazy instantiation using the operator, rather than ordinary eager instantiation via It is also necessary to ensure that that the instance isn't accessed until all static initializations are complete, since access to the instance slots of a lazily instantiated structure triggers its initialization. The structure name is specified using two forms, plain or the syntax If the second form is used, then the structure type will support "boa construction", where "boa" stands for "by order of arguments". The specify the list of slot names which are to be initialized in the by-order-of-arguments style. For instance, if three slot names are given, then those slots can be optionally initialized by giving three arguments in the macro or the function. Slots are first initialized according to their regardless of whether they are involved in boa construction. A slot initialized in this style still has a which is processed independently of the existence of, and prior to, boa construction. The boa constructor syntax can specify optional parameters, delimited by a colon, similarly to the syntax. However, the optional parameters may not be arbitrary symbols; they must be symbols which name slots. Moreover, the optional parameter syntax isn't supported. When boa construction is invoked with optional arguments missing, the default values for those arguments come from the in the remaining syntax.
      (defvar *counter* 0)
      ;; New struct type foo with no super type:
      ;; Slots a and b initialize to nil.
      ;; Slot c is initialized by value of (inc *counter*).
      (defstruct foo nil (a b (c (inc *counter*))))
      (new foo) -> #S(foo a nil b nil c 1)
      (new foo) -> #S(foo a nil b nil c 2)
      ;; New struct bar inheriting from foo.
      (defstruct bar foo (c 0) (d 100))
      (new bar) -> #S(bar a nil b nil c 0 d 100)
      (new bar) -> #S(bar a nil b nil c 0 d 100)
      ;; counter was still incremented during
      ;; construction of d:
      *counter* -> 4
      ;; override slots with new arguments
      (new foo a "str" c 17) -> #S(foo a "str" b nil c 17)
      *counter* -> 5
      ;; boa initialization
      (defstruct (point x : y) nil (x 0) (y 0))
      (new point) -> #S(point x 0 y 0)
      (new (point 1 1)) -> #S(point x 1 y 1)
      ;; property list style initialization
      ;; can always be used:
      (new point x 4 y 5) -> #S(point x 4 y 5)
      ;; boa applies last:
      (new (point 1 1) x 4 y 5) -> #S(point x 1 y 1)
      ;; boa with optional argument omitted:
      (new (point 1)) -> #S(point x 1 y 0)
      ;; boa with optional argument omitted and
      ;; with property list style initialization:
      (new (point 1) x 5 y 5) -> #S(point x 1 y 5) Unless is one of the two symbols or the macro installs a function into the static slot named by the symbol in the struct type indicated by If the structure type doesn't already have such a static slot, it is first added, as if by the function, subject to the same checks. If the function has at least one argument, it can be used as a method. In that situation, the leftmost argument passes the structure instance on which the method is being invoked. The function takes the arguments specified by the symbols, and its body consists of the The are placed into a named A method named allows a structure to be used as if it were a function. When a structure is applied to arguments, as if it were a function, the method is invoked with those arguments, with the object itself inserted into the leftmost argument position. If is used to redefine an existing method, the semantics can be inferred from that of In particular, the method will be imposed into all subtypes which inherit (do not override) the method. If is the keyword symbol then instead of operating on a static slot, the macro redefines the of the given structure type, as if by a call to the function Similarly, if is the keyword symbol then the macro redefines the of the given structure type, as if by a call to the function When redefining the admonishments given in the description of apply: if the type has an generated by the macro, then that is what implements all of the slot initializations given in the slot specifier syntax. These initializations are lost if the is overwritten. The macro returns a method name: a unit of syntax of the form which can be used as an argument to the accessor and other situations. The macro creates a new instance of the structure type named by If the structure supports "boa construction", then, optionally, the arguments may be given using the syntax instead of Slot values may also be specified by the and arguments. Note: the evaluation order in is surprising: namely, are evaluated before if both are present. When the object is constructed, all default initializations take place first. If the object's structure type has a supertype, then the supertype initializations take place. Then the type's initializations take place, followed by the overrides from the macro, and lastly the "boa constructor" overrides. If any of the initializations abandon the evaluation of by a nonlocal exit such as an exception throw, the object's finalizers, if any, are invoked. The macro differs from new in that it specifies the construction of a lazy struct, as if by the function. When is used to construct an instance, a lazy struct is returned immediately, without evaluating any of the and expressions. The expressions are evaluated when any of the object's instance slots is accessed for the first time. At that time, these expressions are evaluated (in the same order as under and initialization proceeds in the same way. If any of the initializations abandon the delayed initializations steps arranged by by a nonlocal exit such as an exception throw, the object's finalizers, if any, are invoked. Lazy initialization does not detect cycles. Immediately prior to the lazy initialization of a struct, the struct is marked as no longer requiring initialization. Thus, during initialization, its instance slots may be freely accessed. Slots not yet initialized evaluate as The and macros are variants, respectively, of and The difference in behavior in these macros relative to and is that the argument is replaced with an expression which is evaluated. The value of must be a struct type, or a symbol which is the name of a struct type. With one exception, if is a compound expression, then is interpreted as where the head of is actually the expression which is evaluated to produce the type, and the remaining constituents of become the boa arguments. The same requirement applies to The exception is that if is the symbol this interpretation does not apply. Thus evaluates the expression, rather than treating it as where would be evaluated as a variable reference expected to produce a type.
      ;; struct with boa constructor
      (defstruct (ab a : b) () a b)
      ;; error: find-struct-type is interpreted as a variable
      (new* (find-struct-type 'ab) a 1) -> ;; error
      ;; OK: extra nesting.
      (new* ((find-struct-type 'ab)) a 1) -> #S(ab a 1 b nil)
      ;; OK: dwim brackets without nesting.
      (new* [find-struct-type 'ab] a 1) -> #S(ab a 1 b nil)
      ;; boa construction
      (new* ([find-struct-type 'ab] 1 2)) -> #S(ab a 1 b 2)
      (new* ((find-struct-type 'ab) 1 2)) -> #S(ab a 1 b 2)
      ;; mixed construction
      (new* ([find-struct-type 'ab] 1) b 2) -> #S(ab a 1 b 2)
      (let ((type (find-struct-type 'ab)))
        (new* type a 3 b 4))
      -> #S(ab a 3 b 4)
      (let ((type (find-struct-type 'ab)))
        (new* (type 3 4)))
      -> #S(ab a 3 b 4) The binds lexical macros to serve as aliases for the slots of a structure. The argument is expected to be an expression which evaluates to a struct object. It is evaluated once, and its value is retained. The aliases are then established to the slots of the resulting struct value. The aliases are specified as zero or more expressions which consist of either a single symbol or a pair. The simple form binds a macro named to a slot also named The pair form binds a macro named to a slot named The lexical aliases are syntactic places: assigning to an alias causes the value to be stored into the slot which it denotes. After evaluating the macro arranges for the evaluation of in the lexical scope in which the aliases are visible. The intent of the macro is to help reduce the verbosity of code which makes multiple references to the same slot. Use of is less necessary in TXR Lisp than other Lisp dialects thanks to the dot operator for accessing struct slots. Lexical aliases to struct places can also be arranged with considerable convenience using the operator. However, will not bind multiple aliases to multiple slots of the same object such that the expression which produces the object is evaluated only once.
      (defstruct point nil x y)
      ;; Here, with-slots introduces verbosity because
      ;; each slot is accessed only once. The function
      ;; is equivalent to:
      ;;
      ;; (defun point-delta (p0 p1)
      ;; (new point x (- p1.x p0.x) y (- p1.y p0.y)))
      ;;
      ;; Also contrast with the use of placelet:
      ;;
      ;; (defun point-delta (p0 p1)
      ;; (placelet ((x0 p0.x) (y0 p0.y)
      ;; (x1 p1.x) (y1 p1.y))
      ;; (new point x (- x1 x0) y (- y1 y0)))))
      (defun point-delta (p0 p1)
        (with-slots ((x0 x) (y0 y)) p0
          (with-slots ((x1 x) (y1 y)) p1
            (new point x (- x1 x0) y (- y1 y0))))) The macro ("quoted reference") performs structure slot access. Structure slot access is more conveniently expressed using the referencing dot notation, which works by translating to qref syntax, according to the following equivalence:
      a.b.c.d <--> (qref a b c d) ;; a b c d must not be numbers (See the Referencing Dot section under Additional Syntax.) The leftmost argument of is an expression which is evaluated. This argument is followed by one or more reference designators. If there are two or more designators, the following equivalence applies:
      (qref obj d1 d2 ...) <---> (qref (qref obj d1) d2 ...) That is to say, is applied to the object and a single designator. This must yield an object, which to which the next designator is applied as if by another operation, and so forth. If the null-safe syntax is present, the equivalence becomes more complicated:
      (qref (t obj) d1 d2 ...) <---> (qref (qref (t obj) d1) d2 ...)
      (qref obj (t d1) d2 ...) <---> (qref (t (qref obj d1)) d2 ...) Thus, can be understood in terms of the semantics of the binary form Designators come in three basic forms: a lone symbol, an ordinary compound expression consisting of a symbol followed by arguments, or a DWIM expression consisting of a symbol followed by arguments. A lone symbol designator indicates the slot of that name. That is to say, the following equivalence applies:
      (qref o n) <--> (slot o 'n) where is the structure slot accessor function. Because is an accessor, this form denotes the slot as a syntactic place; slots can be modified via assignment to the form and the referencing dot syntax. The slot name being implicitly quoted is the basis of the term "quoted reference", giving rise to the name. A compound designator indicates that the named slot is a function, which is to be applied to arguments. The following equivalence applies in this case, except that is evaluated only once:
      (qref o (n arg ...)) <--> (call (slot o 'n) o arg ...) A DWIM designator similarly indicates that the named slot is a function, which is to be applied to arguments. The following equivalence applies:
      (qref obj [name arg ...]) <--> [(slot obj 'name) o arg ...] Therefore, under this equivalence, this syntax provides the usual Lisp-1-style evaluation rule via the operator. If the has the syntax this indicates null-safe access: if evaluates to then the entire expression form yields This syntax is produced by the notation. The null-safe access notation prevents not only slot access, but also method or function calls on When a method or function call is suppressed due to the object being no aspect of the method or function call is evaluated; not only is the slot not accessed, but the argument expressions are not evaluated.
      (defstruct foo nil
        (array (vec 1 2 3))
        (increment (lambda (self index delta)
                     (inc [self.array index] delta))))
      (defvarl s (new foo))
      ;; access third element of s.array:
      [s.array 2] --> 3
      ;; increment first element of array by 42
      s.(increment 0 42) --> 43
      ;; access array member
      s.array --> #(43 2 3) Note how behaves much like a single-argument-dispatch object-oriented method. Firstly, the syntax s.(increment 0 42) effectively selects the function which is particular to the object. Secondly, the object is passed to the selected function as the leftmost argument, so that the function has access to the object. The macro ("unbound reference") expands to an expression which evaluates to a function. The function takes exactly one argument: an object. When the function is invoked on an object, it references slots or methods relative to that object. Note: the syntax may be used directly, but it is also produced by the unbound referencing dot syntactic sugar:
      .a --> (uref a)
      .?a --> (uref t a)
      .(f x) --> (uref (f x))
      .(f x).b --> (uref (f x) b)
      .a.(f x).b --> (uref a (f x) b) The macro may be understood in terms of the following translation scheme:
      (uref a b ...) --> (lambda (o) (qref o a b ...))
      (uref t a b ...) --> (lambda (o) (if o (qref o a b ...))) where is understood to be a unique symbol (for instance, as produced by the function). When only one argument is present, these equivalences also hold:
      (uref (f a b c ...)) <--> (umeth f a b c ...)
      (uref s) <--> (usl s) The terminology "unbound reference" refers to the property that expressions produce a function which isn't bound to a structure object. The function binds a slot or method; the call to that function then binds an object to that function, as an argument. Suppose that the objects in have slots and Then, a list of the slot values may be obtained using:
      (mapcar .a list) because this is equivalent to
      (mapcar (lambda (o) o.a) list) Because produces a function, its result can be operated upon by functional combinators. For instance, we can use the combinator to produce a list of two-element lists, which hold the and slots from each object in
      (mapcar (juxt .a .b) list) The macro allows indirection upon a method-like function stored in a function slot. The macro binds as the leftmost argument of the function stored in returning a function which takes the remaining arguments. That is to say, it returns a function such that calls except that is evaluated only once. If one or more expressions are present, their values are bound inside also, and when is invoked, these are passed to the function stored in the slot. Thus if is produced by then calls except that is evaluated only once, and and are the values of expressions and The argument must be an expression which evaluates to a struct. The argument is not evaluated, and must be a symbol denoting a slot. The syntax can be understood as a translation to a call of the function:
      (meth a b) <--> (method a 'b) If expressions are present, the translation may be be understood as:
      (meth a b c1 c2 ...) <--> [(fun method) a 'b c1 c2 ...] In other words the expressions are evaluated under the operator evaluation rules.
      ;; struct for counting atoms eq to key
      (defstruct (counter key) nil
        key
        (count 0)
        (:method increment (self key)
          (if (eq self.key key)
            (inc self.count))))
      ;; pass all atoms in tree to func
      (defun map-tree (tree func)
        (if (atom tree)
          [func tree]
          (progn (map-tree (car tree) func)
                 (map-tree (cdr tree) func))))
      ;; count occurrences of symbol a
      ;; using increment method of counter,
      ;; passed as func argument to map-tree.
      (let ((c (new (counter 'a)))
            (tr '(a (b (a a)) c a d)))
        (map-tree tr (meth c increment))
        c)
      --> #S(counter key a count 4
                     increment #<function: type 0>) The macro binds the symbol to a function and returns that function. The arguments, if present, are evaluated as if they were arguments to the operator. When that function is called, it expects at least one argument. The leftmost argument must be an object of struct type. The slot named is retrieved from that object, and is expected to be a function. That function is called with the object, followed by the values of the if any, followed by that function's arguments. The syntax can be understood as a translation to a call of the function:
      (umeth s ...) <--> [umethod 's ...] The macro merely provides the syntactic sugar of not having to quote the symbol, and automatically treating the curried argument expressions using Lisp-1 semantics of the operator.
       ;; seal and dog are variables which hold structures of
       ;; different types. Both have a method called bark.
       (let ((bark-fun (umeth bark)))
         [bark-fun dog] ;; same effect as dog.(bark)
         [bark-fun seal]) ;; same effect as seal.(bark) The in stands for "unbound". The function produced by is not bound to any specific object; it binds to an object whenever it is invoked by retrieving the actual method from the object's slot at call time. The macro binds the symbol to a function and returns that function. When that function is called, it expects exactly one argument. That argument must be an object of struct type. The slot named is retrieved from that object and returned. The name stands for "unbound slot". The term "unbound" refers to the returned function not being bound to a particular object. The binding of the slot to an object takes place whenever the function is called. The function creates a new struct type. The argument must be a bindable symbol, according to the function. It specifies the name property of the struct type as well as the name under which the struct type is globally registered. The argument indicates the supertype for the struct type. It must be either a value of type a symbol which names a struct type, or else indicating that the newly created struct type has no supertype. The argument is a list of symbol which specify static slots. The symbols must be bindable and the list must not contain duplicates. The argument is a list of symbols which specifies the instance slots. The symbols must be bindable and there must not be any duplicates within the list, or against entries in the list. The new struct type's effective list of slots is formed by appending together and and then appending that to the list of the supertype's slots, and de-duplicating the resulting list as if by the function. Thus, any slots which are already present in the supertype are removed. If the structure has no supertype, then the list of supertype slots is taken to be empty. When a structure is instantiated, it shall have all the slots specified in the effective list of slots. Each instance slot shall be initialized to the value prior to the invocation of and The argument either specifies an initialization function, or is which is equivalent to specifying a function which does nothing. Prior to the invocation of each new static slot shall be initialized the value Inherited static slots retain their values from the supertype. If specified, function must accept one argument. When the structure type is created (before the function returns) the function is invoked, passed the newly created structure type as its argument. The argument either specifies an initialization function, or is which is equivalent to specifying a function which does nothing. If specified, this function must accept one argument. When a structure is instantiated, every in its chain of supertype ancestry is invoked, in order of inheritance, so that the root supertype's is called first and the structure's own specific is called last. These calls occur before the slots are initialized from the arguments or the of Each function is passed the newly created structure object, and may alter its slots. If multiple inheritance occurs, the functions of multiple supertypes are called in right-to-left order. The argument either specifies a by-order-of-arguments initialization function ("boa constructor") or is which is equivalent to specifying a constructor which does nothing. If specified, it must be a function which takes at least one argument. When a structure is instantiated, and boa arguments are given, the is invoked, with the structure as the leftmost argument, and the boa arguments as additional arguments. This takes place after the processing of functions, and after the processing of the specified in the call. Note that the functions of the supertypes are not called, only the specific to the type being constructed. The argument either specifies an initialization function, or is which is equivalent to specifying a function which does nothing. If specified, this function must accept one argument. The function is similar to The difference is that functions are called after all other initialization processing, rather than before. They are are also called in order of inheritance: the of a structure's supertype is called before its own, and in right-to-left order among multiple supertypes under multiple inheritance. The returns a object corresponding to the symbol If no struct type is registered under then it returns A object exists for each structure type and holds information about it. These objects are not themselves structures and are all of the same type, The function returns if is a structure type, otherwise it returns A structure type is an object of type returned by The function determines a structure type from the argument and returns that structure type's symbolic name. The argument must be either a struct type object (such as the return value of a successful lookup via a symbol which names a struct type, or else a struct instance. The function determines a structure type from the argument and returns the struct type object which is the supertype of that type, or else if that type has no supertype. The argument must be either a struct type object, a symbol which names a struct type, or else a struct instance. The function returns a new object which is an instance of the structure type The argument must either be a object, or else a symbol which is the name of a structure. The argument gives a list of slot initializations in the style of a property list, as defined by the function. It may be empty, in which case it has no effect. Otherwise, it specifies slot names and their values. Each slot name which is given must be a slot of the structure type. The corresponding value will be stored into the slot of the newly created object. If a slot is repeated, it is unspecified which value takes effect. The optional specify arguments to the structure type's boa constructor. If the arguments are omitted, the boa constructor is not invoked. Otherwise the boa constructor is invoked on the structure object and those arguments. The argument list must match the trailing parameters of the boa constructor (the remaining parameters which follow the leftmost argument which passes the structure to the boa constructor). When a new structure is instantiated by its slot values are first initialized by the structure type's registered functions as described under Then, the is processed, if not empty, and finally, the are processed, if present, and passed to the boa constructor. If any of the initializations abandon the evaluation of by a nonlocal exit such as an exception throw, the object's finalizers, if any, are invoked. The function returns a new object which is an instance of the structure type The argument must either be a object, or else a symbol which is the name of a structure. The argument should be a function which can be called with no parameters and returns a cons cell. More requirements are specified below. The object returned by is a lazily-initialized struct instance, or lazy struct. A lazy struct remains uninitialized until just before the first access to any of its instance slots. Just before an instance slot is accessed, initialization takes place as follows. The function is invoked with no arguments. Its return value must be a cons cell. The of the cons cell is taken to be a property list, as defined by the function. The field is taken to be a list of arguments. These values are treated as if they were, respectively, the and the boa constructor arguments given in a invocation. Initialization of the structure proceeds as described in the description of The and functions are interfaces to the function. The function passes its and arguments as the argument of It passes no boa constructor arguments. The function calls with an empty passing down the list of The following equivalences hold:
      (struct-from-plist a s0 v0 s1 v1 ...)
      <--> (make-struct a (list s0 v0 s1 v1 ...))
      (struct-from-args a v0 v1 v2 ...)
      <--> (make-struct a nil v0 v1 v2 ...) The provides a low-level allocator for structure objects. The argument must either be a object, or else a symbol which is the name of a structure. The creates and returns a new instance of all of whose instance slots take on the value No initializations are performed. The struct type's registered initialization functions are not invoked. The function creates and returns a new object which is a duplicate of which must be a structure. The duplicate object is a structure of the same type as and has the same slot values. The creation of a duplicate does not involve calling any of the struct type's initialization functions. Only instance slots participate in the duplication. Since the original structure and copy are of the same structure type, they already share static slots. This is a low-level, "shallow" copying mechanism. If an object design calls for a higher level cloning mechanism with deep copying or other additional semantics, one can be built on top of For instance, a structure can have a method similar to the following:
      (:method copy (me)
        (let ((my-copy (copy-struct me)))
          ;; inform the copy that it has been created
          ;; by invoking its copied method.
          my-copy.(copied)
          my-copy)) which can then be invoked on whatever object needs copying. (Note that this method is not a special structure function, and is thus not taken into account by the function.) Since this logic is generic, it can be placed in a base method. The method which it calls is the means by which the new object is notified that it is a copy. This method takes on whatever special responsibilities are required when a copy is produced, such as registering the object in various necessary associations, or performing a deeper copy of some of the objects held in the slots. The handler can be implemented at multiple levels of an inheritance hierarchy. The initial call to from will call the most derived override of that method. To call the corresponding method in the base class, a given derived method can use the function, or else the syntax in the first position of a compound form, in place of a function name. Examples of both are given in the documentation for Thus derived structs can inherit the copy handling logic from base structs, and extend it with their own. The function retrieves a structure's slot. The argument must be a structure, and must be a symbol which names a slot in that structure. Because is an accessor, a form is a syntactic place which denotes the slot's storage location. A syntactic place expressed by does not support deletion. The function stores a value in a structure's slot.
     The argument must be a structure, and must be a symbol which names a slot in that structure. The argument specifies the value to be stored in the slot. If a successful store takes place to an instance slot of then the dirty flag of that object is set, causing the function to report true for that object. The function returns The and functions comprise the interface for interacting with structure dirty flags. Each structure instance has a dirty flag. When this flag is set, the structure instance is said to be dirty, otherwise it is said to be clean. A newly created structure is dirty. A structure remains dirty until its dirty flag is explicitly reset. If a structure is clean, and one of its instance slots is overwritten with a new value, it becomes dirty. The function returns the dirty flag of if is dirty, otherwise The function clears the dirty flag of and returns itself. The flag combines these operations: it makes a note of the dirty flag of and clears it. Then it returns the noted value, or The function returns if is a structure, otherwise it returns The function returns the structure type object which represents the type of the structure object instance The replaces all instance slots of with which defaults to if omitted. Note that finalizers are not executed prior to replacing the slot values. The function reinitializes the structure object as if it were being newly created. First, all the slots are set to as if by the function. Then the slots are initialized by invoking the initialization functions, in order of the supertype ancestry, just as would be done for a new structure object created by with an empty and no boa arguments. Note that finalizers registered against are not invoked prior to the reset operation, and remain registered. If the structure has state which is cleaned up by finalizers, it is advisable to invoke them using prior to using or to take other measures to deal with the situation. If the structure specifies handlers, then the reinitialization will cause these to registered, just like when a new object it constructed. Thus if is not used prior to this will result in the existence of duplicate registrations of the finalization functions. Finalizers registered against are invoked if an exception is thrown during the reinitialization, just like when a new structure is being constructed. The function causes to take on the attributes of without changing its identity. The type of is changed to that of All instance slots of are discarded, and it is given new slots, which are copies of the instance slots of Because of the type change, implicitly loses all of its original static slots, and acquires those of Note that finalizers registered against are not invoked, and remain registered. If has state which is cleaned up by finalizers, it is advisable to invoke them using prior to using or to take other measures to handle the situation. If the and arguments are the same object, has no effect. The return value is The function retrieves a function from a structure's slot and returns a new function which binds that function's left argument. If arguments are present, then they are also stored in the returned function. These are the curried arguments. The argument must be a structure, and must be a symbol denoting a slot in that structure. The slot must hold a function of at least one argument. The function which function returns, when invoked, calls the function previously retrieved from the object's slot, passing to that function as the leftmost argument, followed by the curried arguments, followed by all of own arguments. Note: the macro is an alternative interface which is suitable if the slot name isn't a computed value. The function retrieves a function from a static slot belonging to one of the direct supertypes of the structure type of It then returns a function which binds that function's left argument to the structure. The argument must be a structure which has at least one supertype, and must be a symbol denoting a static slot in one of those supertypes. The slot must hold a function of at least one argument. The supertypes are searched from left to right for a static slot named when the first such slot is found, its value is used. The function returns a function which, when invoked, calls the function previously retrieved from the supertype's static slot, passing to that function as the leftmost argument, followed by the function's own arguments. The returns a function which represents the set of all methods named by the slot in all structure types, including ones not yet defined. The argument must be a symbol. If one or more argument are present, these values represent the curried arguments which are stored in the function object which is returned. This returned function must be called with at least one argument. Its leftmost argument must be an object of structure type, which has a slot named The function will retrieve the value of the slot from that object, expecting it to be a function, and calls it, passing to it the following arguments: the object itself; all of the curried arguments, if any; and all of its remaining arguments. Note: the name stands for "unbound method". Unlike the function, doesn't return a method whose leftmost argument is already bound to an object; the binding occurs at call time. The returns a function which represents all slots named in all structure types, including ones not yet defined. The argument must be a symbol. The returned function must be called with exactly one argument. The argument must be a structure which has a slot named The function will retrieve the value of the slot from that object and return it. Note: the name stands for "unbound slot". The returned function isn't bound to a particular object. The binding of to a slot in the structure object occurs when the function is called. The function returns a list of all of the slots of struct type The argument must be a structure type, or else a symbol which names a structure type. The function returns if name is a symbol which names a slot in the structure type Otherwise it returns The argument must be a structure type, or else a symbol which names a structure type. The function returns if name is a symbol which names a slot in the structure type and if that slot is a static slot. Otherwise it returns The argument must be a structure type, or else a symbol which names a structure type. The function retrieves the value of the static slot named by symbol of the structure type The argument must be a structure type or a symbol which names a structure type, and must be a static slot of this type. The function stores into the static slot named by symbol of the structure type It returns The argument must be a structure type or the name of a structure type, and must be a static slot of this type. The ensures, if possible, that the struct type as well as possibly one or more struct types derived from it, have a static slot called that this slot is not shared with a supertype, and that the value stored in it is Note: this function supports the redefinition of methods, as the implementation underlying the macro; its semantics is designed to harmonize with expected behaviors in that usage. The function operates as follows. If itself already has an instance slot called then an error is thrown, and the function has no effect, unless a true argument is specified for the Boolean parameter. In that case, in the same situation, the function has no effect and simply returns If already has a non-inherited static slot called then this slot is overwritten with and the function returns Types derived from may also have this slot, via inheritance; consequently, its value changes in those types also. If already has an inherited static slot called then its inheritance is severed; the slot is converted to a non-inherited static slot of and initialized with Then all struct types derived from are scanned. In each such type, if the original inherited static slot is found, it is replaced with the same newly converted static slot that was just introduced into so that all these types now inherit this new slot from rather than the original slot from some supertype of These types all share a single instance of the slot with but not with supertypes of In the remaining case, has no slot called The slot is added as a static slot to Then it is added to every struct type derived from which does not already have a slot by that name, as if by inheritance. That is to say, types to which this slot is introduced share a single instance of that slot. The value of the new slot is which is also returned from the function. Any subtypes of which already have a slot called are ignored, as are their subtypes. The method determines which structure type actually defines the static slot present in struct type If isn't a struct type, or the name of a struct type, the function throws an error. Likewise, if isn't a static slot of If is a static slot of then the function returns a struct type name symbol which is either then name of itself, if the slot is defined specifically for or else the most distant ancestor of from which the slot is inherited. The function is deprecated. Solutions involving should be reworked in terms of The retrieves the function stored in the static slot of one of the direct supertypes of and invokes it, passing to that function as the leftmost argument, followed by the given if any. The argument must be of structure type. Moreover, that structure type must be derived from one or more supertypes, and must name a static slot available from at least one of those supertypes. The supertypes are searched left to right in search of this slot. The object retrieved from that static slot must be callable as a function, and accept the arguments. Note that it is not correct for a method that is defined against a particular type to use to call the same method (or any other method) in the supertype of that particular type. This is because refers to the type of the object instance not to the type against which the calling method is defined. The retrieves the function stored in the slot of one of the supertypes of and invokes it, passing to that function the given if any. The argument must be a structure type. Moreover, that structure type must be derived from one or more supertypes, and must name a static slot available from at least one of those supertypes. The supertypes are searched left to right in search of this slot. The object retrieved from that static slot must be callable as a function, and accept the arguments. Print a message and call supertype method:
      (defstruct base nil)
      (defstruct derived base)
      (defmeth base fun (obj arg)
        (format t "base fun method called with arg ~s\n" arg))
      (defmeth derived fun (obj arg)
        (format t "derived fun method called with arg ~s\n" arg)
        (call-super-fun 'derived 'fun obj arg))
      ;; Interactive Listener:
      1> (new derived).(fun 42)
      derived fun method called with arg 42
      base fun method called with arg 42 Note that a static method or function in any structure type can be invoked by using the name syntax in the first position of a compound form, as a function name. Thus, the above can also be written:
      (defmeth derived fun (obj arg)
        (format t "derived fun method called with arg ~s\n" arg)
        ((meth base fun) obj arg)) The and functions retrieve, respectively, a structure type's and functions. These are the functions which are initially configured in the call to via the and arguments. Either one may be indicating that the type has no or The and functions overwrite, respectively, a structure type's and functions. These are the functions which are initially configured in the call to via the and arguments. The argument must either be or else a function which accepts one argument. Note that has the responsibility for all instance slot initializations. The syntax compiles the initializing expressions in the slot specifier syntax into statements which are placed into a function, which becomes the of the struct type. The macro provides a binding construct similar to Each must be a symbol suitable for use as a variable name. Each is evaluated in sequence, and a binding is established for its corresponding which is initialized with the value of that form. The binding is visible to subsequent Additionally, the values of the are noted as they are produced. When the form terminates, by any means, the function is invoked on each value which was returned by an and had been noted. These calls are performed in the reverse order relative to the original evaluation of the forms. After the variables are established and initialized, the are evaluated in the scope of the variables. The value of the last form is returned, or else if there are no forms. The invocations of take place just before the value of the last form is returned. The macro makes available a new, application-defined clause. The clause is named by which must be a keyword symbol, and is implemented as a macro transformation by the and of the definition. The definition established by is called a struct clause macro. A struct clause macro is invoked when syntax is processed which contains one or more clauses which are headed by the matching symbol. The comprise a macro-style parameter list which must match the invoking clause, otherwise an error exception is thrown. When successfully matches the clause parameters, the parameters are destructured into the parameters and the are evaluated in the scope of those parameters. The must return a possibly list of clauses, not a single clause. Each of the returned clauses is examined for the possibility that it may be a struct clause macro; if so, it is expanded. The built-in clause keywords and may not be used as the names of a struct clause macro; if any of these symbols is used as the parameter of an error exception is thrown. The return value of a macro invocation is the argument.
      ;; Trivial struct clause macro which consumes any number of
      ;; arguments and produces no slots:
      (define-struct-clause :nothing (. ignored-args))
      ;; Consequently, the following defines a struct with one slot, x:
      ;; The (:nothing ...) clause disappears by producing no clauses.
      (defstruct foo ()
        (:nothing 1 2 3 beeblebrox)
        x)
      ;; struct clause macro called :multi which takes an initial value
      ;; and zero or more slot names. It produces instance slot definitions
      ;; which all use that same initial value.
      (define-struct-clause :multi (init-val . names)
        (mapcar (lop list init-val) names))
      ;; define a struct with three slots initialized to zero:
      (defstruct bar ()
        (:multi 0 a b c)) ;; expands to (a 0) (b 0) (c 0)
      ;; struct clause macro to define a slot along with a
      ;; get and set method.
      (define-struct-clause :getset (slot getter setter : init-val)
        ^((,slot ,init-val)
          (:method ,getter (obj) obj.,slot)
          (:method ,setter (obj new) (set obj.,slot new))))
      ;; Example use:
      (defstruct point ()
        (:getset x get-x set-x 0)
        (:getset y get-y set-y 0))
      ;; This has exactly the same effect as the following defstruct:
      (defstruct point ()
        (x 0)
        (y 0)
        (:method get-x (obj) obj.x)
        (:method set-x (ob new) (set obj.x new))
        (:method get-y (obj) obj.y)
        (:method set-y (ob new) (set obj.y new))) The :delegate struct clause macro provides a way to define a method which is implemented entirely by delegation to a different object. The name of the method is and its parameter list is specified in the same way as in the clause. Instead of a method body, the clause has an expression and an optional which defaults to The must be an expression which the delegate method can evaluate to produce a delegate object. The delegate method then passes its arguments to the target method, given by the argument, invoked on the delegate object. If the delegate method specifies an optional parameter without a default initializing expression, and that optional parameter is not given an argument value, it receives the colon symbol as its argument. That value is passed on to the corresponding parameter of the delegate target method. Thus, if the target method has an optional parameter in that same parameter position, that colon symbol argument then has the effect of requesting the default value. If the target method has an ordinary parameter in that position, then the colon symbol is received as an ordinary argument value. If the delegate method specifies an optional parameter with a default initializing expression, and that optional parameter is not given an argument value, then the expression is evaluated to produce a value for that parameter, in the usual manner, and that value is passed as an argument to the corresponding parameter of the delegate target. Thus, delegates are able to specify different optional argument defaulting from their targets. A delegate may have an optional parameter in a position where the target has a required parameter and vice versa. The three-element optional parameter expression, specifying a Boolean variable which indicates whether the optional parameter has been given an argument, is not supported by the clause, and is diagnosed. If the delegate method has variadic parameters, they are passed on to the target after the fixed parameters. Structure definitions:
      (defstruct worker ()
        name
        (:method work (me)
          `worker @{me.name} works`)
        (:method relax (me : (min 15))
          `worker @{me.name} relaxes for @min min`))
      ;; "contractor" class has a sub ("subcontractor") slot
      ;; which is another contractor of the same type.
      ;; The subcontractor's own sub slot, however is going
      ;; to be a worker.
      (defstruct contractor ()
        sub
        (:delegate work (me) me.sub.sub)
        (:delegate break (me : min) me.sub.sub relax)) The structure's and methods delegate to the sub-subcontractor, which is going to be instantiated as a object. Note that the method delegates to a differently named method
      ;; The objects are set up as described above.
      ;; general contractor co has a co.sub subcontractor,
      ;; and co.sub.sub is a worker:
      (defvar co (new contractor
                      sub (new contractor
                            sub (new worker name "foo"))))
      ;; Call work method on general contractor:
      ;; this invokes co.sub.sub.(work) on the worker.
      co.(work) -> "worker foo works"
      ;; Call break method on general contractor with
      ;; no argument. This causes co.sub.sub.(relax :)
      ;; to be invoked, triggering argument defaulting:
      co.(break) -> "worker foo relaxes for 15 min"
      ;; Call break method with argument. This
      ;; invokes co.sub.sub.(relax 5), specifying a
      ;; value for the default argument:
      co.(break 5) -> "worker foo relaxes for 5 min" The :mass-delegate struct macro provides a way to define multiple methods which are implemented as delegates to corresponding methods on another object. The implementation of depends on the macro. The argument must be a bindable symbol. In each generated delegate method, this symbol will be the first argument. The purpose of this symbol is to enable the to refer to the delegating object. The is an expression which is inserted into every method. Its evaluation is expected to produce the delegate object. This expression may reference in order to retrieve or otherwise obtain the delegate from the delegating object. The argument is a symbol naming an existing structure type. If no such structure type has been defined, an error exception is thrown. After the argument, either zero or more slot names appear, optionally preceded by the (asterisk) symbol. If the symbol is present, and isn't followed by any other symbols, it indicates that all methods from are to be delegated. If symbols appear after the then those specify exceptions: methods not to be delegated. No validation is performed on the exception list; it may specify nonexistent method names which have no effect. If the symbol is absent, then every symbol specifies a method to be delegated. It is consequently expected to name a method of the a static slot which contains a function. If any isn't a static slot of or not a static slot which contains a function, an error exception is thrown. The struct macro iterates over all of the methods of that selected for delegation, and for each one it generates a macro clause based on the existing method's parameter list. For instance, the delegate for a method which has two required arguments and one optional will itself have two required arguments and one optional. Delegates are not simply wrapper functions which take any number of arguments and try to pass them to the target. The generated clauses are then processed by that struct clause macro. Note: composition with delegation is a useful alternative when multiple inheritance is not applicable or desired for various reasons. One such reason is that structures that would be used as multiple inheritance bases use the same symbols for certain slots, and the semantics of those slots conflict. Under inheritance, same-named slots coming from different bases become one slot, Note: a particular being nominated in the clause doesn't mean that the specific methods of that type shall be called by the generated delegates. The methods that shall be called are those of the calculated delegate object selected by the The is used as a source of the argument info, and method existence validation. It is up to the application to ensure that the delegation using makes sense with respect to the delegate object that is selected by the for instance, by ensuring that this object is an instance of or a subtype thereof.
      (defstruct foo-api ()
        name
        (:method begin (me) ^(foo ,me.name begin))
        (:method increment (me delta) ^(foo ,me.name increment ,delta))
        (:method end (me) ^(foo ,me.name end)))
      (defstruct bar-api ()
        name
        (:method open (me) ^(bar ,me.name open))
        (:method read (me buf) ^(bar ,me.name read ,buf))
        (:method write (me buf) ^(bar ,me.name write ,buf))
        (:method close (me) ^(bar ,me.name close)))
      ;; facade holds the two API objects by composition:
      (defstruct facade ()
        (foo (new foo-api name "foo"))
        (bar (new bar-api name "bar"))
        ;; delegate foo-api style calls via me.foo
        (:mass-delegate me me.foo foo-api *)
        ;; delegate bar-api style calls via me.bar
        ;; exclude the write method.
        (:mass-delegate me me.bar bar-api * write))
      ;; instantiate facade as variable fa
      (defvar fa (new facade)) -> fa
      ;; begin call on facade delegates through foo-api object.
      fa.(begin) -> (foo "foo" begin)
      fa.(increment) -> ;; error: too few arguments
      fa.(increment 3) -> (foo "foo" increment 3)
      fa.(open) -> (bar "bar" open)
      fa.(write 4) -> ;; error: fa has no such method If is a compound expression whose operator symbol was defined by then expands the clause and returns the expansion, which is a list of zero or more clauses. Otherwise, the function returns a one-element list containing the argument, as if by the expression. The parameter, if present, is used for reporting errors. Note: clauses are usually expanded during the processing of a macro; in that situation, the entire unexpanded form serves the role,
      ;; try to expand :delegate, using incorrect syntax.
      (macroexpand-struct-clause '(:delegate x (a b)))
      --> error "** source location n/a: nil: too few elements ..."
      ;; same, but with error reporting form.
      (macroexpand-struct-clause '(:delegate x (a b)) '(abc xyz))
      --> error: "** expr-1:1: abc: too few elements ..."
      ;; correct :delegate syntax
      (macroexpand-struct-clause '(:delegate x (a b) a.foo))
      --> ((:method x (a b) (qref a.foo (x b))))
      ;; not a defstruct macro clause
      (macroexpand-struct-clause '(1 2 3))
      -> ((1 2 3)) The special variable holds the hash table of associations between keyword symbols and struct clause expander functions, defined by If the expression yields a function, then symbol has a binding as a struct clause macro. If that expression yields then there is no such binding. The macro expanders in are two-parameter functions. The first parameter accepts the clause to be expanded. The second parameter accepts the form in which that clause is found; this is useful for error reporting. An expander function returns a list of clauses, which may be any, possibly empty mixture of primary clauses accepted by and clause macros. The macro defines a prelude. A prelude is a named entity which implicitly provides clauses to macro invocations. Preludes are processed during the macroexpansion of prelude definitions have no effect on previously compiled forms loaded from a file. A prelude has a which must be a bindable symbol. The purpose of this name is that if multiple forms are evaluated which specify the same they replace each others' definition. Only the most recent prelude of a given is retained; the previous definitions are overwritten. The argument is either a symbol or a list of symbols, which are valid for use as structure names. The prelude being defined shall be applicable to each of the structures whose names are given by this argument. The zero or more arguments give the clauses which comprise the prelude. In the future, when a form is macroexpanded which targets any of the structures given by the argument, the specified clauses will be inserted into that definition, as if they appeared in the form literally. Multiple preludes may be defined with different names, which each target the same structure. When the structure is defined, or redefined, it will receive all those preludes, in the order in which they were defined.
      ;; define init-fini-log prelude which targets fox and bear structs
      (define-struct-prelude init-fini-log (fox bear)
        (:init (me) (put-line `@me created`))
        (:fini (me) (put-line `@me finalized`)))
      ;; The behavior is as if the following defstruct forms included
      ;; the above :init and :fini clauses
      (defstruct fox ())
      (defstruct bear ())
      (with-object ((f (new fox))
                    (b (new bear)))
        (put-line "inside with-object")) Output:
      #S(fox) created
      #S(bear) created
      inside with-object
      #S(bear) finalized
      #S(fox) finalized Special structure functions are user-defined methods or structure functions which are specially recognized by certain functions in TXR Lisp. They endow structure objects with the ability to participate in certain usage scenarios, or to participate in a customized way. Special functions are required to bound to static slots, which is the case if the macro is used, or when methods or functions are defined using syntax inside a form. If a special function or method is defined as an instance slot, then the behavior of library functions which depend on this method is unspecified. Special functions introduced below by the word "Method" receive an object instance as an argument. Their syntax is indicated using the same notation which may be used to invoke them, such as: However, those introduced as "Function" do not operate on an instance. Their syntax is likewise indicated using the notation that may be used to invoke them: If such a invocation is actually used, the instance only serves for identifying the struct type whose static slot provides the function; doesn't participate in the call. An object is not strictly required since the function can be called using which looks up the function in the struct directly. Normally, two struct values are not considered the same under the function unless they are the same object. However, if the method is defined for a structure type, then instances of that structure type support equality substitution. The method must not require any arguments other than Moreover, the method must never return When a struct which supports equality substitution is compared using or its method is invoked, and the return value is used in place of that structure for the purposes of the comparison. The same applies when a struct is hashed using the function, or implicitly by an hash table. Note: if an method is defined or redefined with different semantics for a struct type whose instances have already been inserted as keys in an hash table, the behavior of subsequent insertion and lookup operations on that hash table becomes unspecified. If a method named by the symbol is defined for a structure type, then it is used for printing instances of that type. The argument specifies the output stream to which the printed representation is to be written. The argument is a Boolean flag indicating whether pretty-printing is requested. Its value may simply be passed to recursive calls to or used to select between or formatting if is used. The value returned by the method is significant. If the special keyword symbol (colon) is returned, then the system will print the object in the default way, as if no method existed: it is understood that the method declined the responsibility for printing the object. If any other value is returned, then it is understood that the method method accepted the responsibility for printing the object, and the system consequently will generate into any output output pertaining to representation. If a structure type provides a method called then it can be used as a function. This method can be called by name, using the syntax given in the above syntactic description. However, the intended use is that it allows the structure instance itself to be used as a function. When a structure is applied to arguments as if it were a function, this is erroneous, unless that object has a method. In that case, the arguments are passed to the lambda method. The leftmost argument of the method is the structure instance itself. That is to say, the following equivalences apply, except that is evaluated only once:
      (call s args ...) <--> s.(lambda args ...)
      [s args ...] <--> [s.lambda s args ...]
      (mapcar s list) <--> (mapcar (meth s lambda) list) Note: a form such as where is a structure can be treated as a place if the method is also implemented. The method, in conjunction with a method, allows structures to be used as place accessors. If structure supports a with four arguments, then the following use of the operator is possible:
      (set [s a b c d] v)
      (set (dwim s a b c d) v) ;; precisely equivalently This has an effect which can be described by the following code:
      (progn
        s s.(lambda-set a b c d v)
        v) except that and are evaluated only once, and through are evaluated using the Lisp-1 semantics due the operator. If a place-mutating operator is used on this form which requires the prior value, such as the macro, then the structure must support the function also. If takes n arguments, then should take n+1 arguments. The first n arguments of these two methods are congruent; the extra rightmost argument of is the new value to be stored into the place denoted by the prior arguments. The return value of is ignored. Note: the method is also used by the function, if no method exists. The following defines a structure with a single instance slot which holds a hash table, as well as and methods:
      (defstruct hash-wrapper nil
        (hash (hash))
        (:method lambda (self key)
          [self.hash key])
        (:method lambda-set (self key new-val)
          (set [self.hash key] new-val) self)) An instance of this structure can now be used as follows:
       (let ((s (new hash-wrapper)))
         (set [s "apple"] 3
              [s "orange] 4)
         [s "apple"]) -> 3 If a structure has method, then it can be used as an argument to the function. Structures which implement the methods and can be treated as abstract vector-like sequences, because such structures support the and functions. For instance, the function will operate on such objects. Note: a structure which supports the method also supports the function, in a different way. Such a structure is treated by as a list-like sequence, and its length is measured by walking the sequence with operations. If a structure supports both and preference is given to which is likely to be much more efficient. If a structure has method, then it can be used as the left argument to the function. The argument receives the right argument. If an object doesn't implement the method, but does implement the it can also be used as an argument to the function. In that situation, the function will call the method instead, and then compare the returned value against the parameter. Structures may be treated as sequences if they define methods named by the symbols and If a structure supports these methods, then these methods are used by the functions and various other sequence manipulating functions derived from them, when those functions are applied to that object. An object which implements these three methods can be considered to represent a list-like abstract sequence. The object's method should return the first value in that abstract sequence, or else if that sequence is empty. The object's method should return an object denoting the remainder of the sequence, or else if the sequence is empty or contains only one value. This returned object can be of any type: it may be of the same structure type as that object, a different structure type, a list, or whatever else. If a non-sequence object is returned. The method should return if the object is considered to denote an empty sequence. Otherwise it should either return that object itself, or else return the sequence which that object represents. If a structure type defines the methods and then, respectively, the and functions will use these methods if they are applied to instances of that type. That is to say, when the function call is evaluated, and is a structure type, the function inquires whether supports a method. If so, then, effectively, is invoked. The return value of this method call is ignored; returns The analogous requirements apply to in relation to the method. Note: if the method doesn't exist, the function falls back on trying to store by means of the structure type's method, using an index of zero. That is to say, if the type has no method, but does have a method, then is invoked. If a structure function is defined for a structure type, it is called in certain situations with an argument which is a list object. The function's purpose is to construct a new instance of the structure type, derived from that list. The purpose of this function is to allow sequence processing operations such as and to operate on a structure object as if it were a sequence, and return a transformed sequence of the same type. This is analogous to the way such functions can operate on a vector or string, and return a vector or string. If a structure object behaves as a sequence thanks to providing and methods, but does not have a function, then those sequence-processing operations which return a sequence will always return a plain list of items. If a structure type supports a function called this function is called whenever a new type is defined which names that type as its supertype. The function is called with two arguments which are both struct types. The argument gives the type that is being inherited from. The gives the new type that is inheriting from When a new structure type is defined, its list of immediate supertypes is considered. For each of those supertypes which defines the function, the function is invoked. The function is not retroactively invoked. If it is defined for a structure type from which subtypes have already been derived, it is not invoked for those existing subtypes. If directly inherits more than once, it is not specified whether this function is called once, or multiple times. Note: the parameter exists because the function is itself inherited. If the same version of this function is shared by multiple structure types due to inheritance, this argument informs the function which of those types it is being invoked for. If an object supports the method, it is considered iterable; the function will return if invoked on this object. The responsibility of the method is to return an iterator object: an object which supports certain special methods related to iteration, according to one of two protocols, described below. The method is optional. It is similar to but takes an additional argument, an iterator object that was previously returned by the method of the same If determines that can be reused for a new iteration, then it can suitably mutate the state of and return it. Otherwise, it behaves like and returns a new iterator. There are two protocols for iteration: the fast protocol, and the canonical protocol. Both protocols require the iterator object returned by the method to provide the methods and If the iterator also provides the method, then the protocol which applies is the canonical protocol. If that method is absent, then the fast protocol is followed. Under the fast protocol, the method does not exist and is not involved. The iterable object's method must return if the abstract sequence is empty. If an iterator is returned, it is assumed that an object can be retrieved from the iterator by invoking its method. The iterator's method should return if there are no more objects in the abstract sequence, or else it should return an iterator that obeys the fast protocol (possibly itself). Under the canonical protocol, the iterator implements the function. The iterable object's always returns an iterator object. The iterator object's method is always invoked to determine whether another item is available from the sequence. The iterator object's method is expected to return an iterator object which conforms to the canonical protocol. The method is invoked on an iterator to retrieve the next item in the sequence. Under the fast protocol, it is assumed that if was returned by an iterable object's method, or by an iterator's method, that an item is available. This method will be unconditionally invoked. Under the canonical protocol for iteration, the method will be invoked on first. If that method yields true, then is expected to yield the next available item in the sequence. Note: calls to the function, with as its argument, invoke the method. It is possible for an application to call through this function or directly as a method call without first calling No iteration mechanism in the TXR Lisp standard library behaves this way. If the iterator has no more items available and is invoked anyway, no requirements apply to its behavior or return value. The method is invoked on an iterator object to produce an iterator object for the remainder of the sequence, excluding the current item. Under the fast iteration protocol, this method returns if there are no more items in the sequence. Under the canonical iteration protocol, this method always returns an iterator object. If no items remain in the sequence, then that iterator object's method returns Furthermore, under this protocol, is not called if returns Note: calls to the function, with as its argument, invoke the method. It is possible for an application to call through this function or directly as a method call without first calling No iteration mechanism in the TXR Lisp standard library behaves this way. If the iterator has no more items available and is invoked anyway, no requirements apply to its behavior or return value. If an iterator returned by supports the method, then the canonical iteration protocol applies to that iteration session. All subsequent iterators that are involved in the iteration are assumed to conform to the protocol and should implement the method also. The behavior is unspecified otherwise. The method is used to interrogate an iterator whether more unvisited items remain in the sequence. This method does not advance the iteration, and does not change the state of the iterator. It is idempotent: if it is called multiple times without any intervening call to any other method, it yields the same value. If an iterator does not implement the method, then if the function is applied to that iterator, it unconditionally returns Functions in this category uniformly manipulate abstract sequences. Lists, strings and vectors are sequences. Structure objects can behave like sequences, either list-like or vector-like sequences, if they have certain methods: see the previous section Special Structure Functions. Moreover, hash tables behave like sequences of key-value entries represented by pairs. Not all sequence-processing functions accept hash-table sequences. Additionally, some sequence-processing functions work not only with sequences but with all iterable objects: objects that can be used as arguments to the function. Such arguments are called rather than possibly abbreviated to with or without a numeric suffix. Hash tables are always supported if they appear as arguments. The function returns if is a sequence, otherwise Lists, vectors and strings are sequences. The object denotes the empty list and so is a sequence. Objects of type and are sequences, as are hash tables. Structures which implement the or methods are considered sequences. No other objects are sequences. However, future revisions of the language may specify additional objects that are sequences. The function returns if is iterable, otherwise If is a sequence according to the function, then it is iterable. If is a structure which supports the method, then it is iterable. Additional objects that are not sequences are also iterable: numeric or character ranges, and numbers. Future revisions of the language may specify additional iterable objects. The argument must be a list. If is a sequence type, then is converted to the same type of sequence and returned. Otherwise the original is returned. Conversion is supported to string and vector type. Conversion to a structure type is possible for structures. If is an object of a structure type which has a static function then calls that function, passing to it, and the resulting value is returned. and returns whatever value that function returns. If is a then is passed to the function, and the resulting value is returned. The second argument in the call is the element type taken from The third argument is indicating that the resulting is not to be null terminated. The may be an iterator returned by In this situation, if that object makes the original sequence available, then takes that sequence in place of Note: the function is a helper which supports the development of unoptimized versions of a generic function that accepts any type of sequence as input, and produces a sequence of the same type as output. The implementation of such a function can internally accumulate a list, and then convert the resulting list to the same type as an input value by using The and functions convert an iterable object of any type into a list, vector or string, respectively. The list returned by is lazy. The and iterate the items of and accumulate these items into a new list or vector. The similarly iterates the items of requiring them to be a mixture of characters and strings. The function returns the number of items contained in The function is a synonym of An attempt to calculate the length of infinite lazy lists will not terminate. Iterable objects representing infinite ranges, such as integers and characters are invalid arguments. The function efficiently determines whether is less than the integer value In cases when would have to be fully traversed in order to measure its length, the function avoids this traversal, by making use of the functions or as appropriate. Note: this function is useful when a decision must be made between two algorithms, depending on whether the length is less than a certain small constant. It is also safe on lazy, infinite sequences and circular lists, for which will fail to terminate. If is a suitable argument for the function, then the Returns if is zero, otherwise The function also supports certain objects not suitable as arguments for An infinite lazy list is not empty, and so returns for such an object. The function also returns for iterable objects representing nonempty spaces, even if those spaces are infinite. For instance yields because the set of integers beginning with 0 isn't empty. The function returns if denotes an empty sequence. Otherwise, if is not an empty sequence, or isn't a sequence, then itself is returned. If is a structure object which supports the method, then that method is called. If it returns then is returned. If the method returns a substitute object other than the object itself, then is invoked on that returned substitute object. Note: the function is a helper to support unoptimized generic traversal of sequences. Thanks to the generalized behavior of non-list sequences can be traversed using similarly to proper lists, by checking for returning the terminating value However, empty non-list sequences are handled incorrectly because since they are not the object, they look nonempty under this paradigm of traversal. The function provides a correction: if the input sequence is filtered through then the subsequent list-like iteration works correctly. Examples:
      ;; Incorrect for empty strings:
      (defun print-chars (string)
        (while string
          (prinl (pop string))))
      ;; Corrected with nullify:
      (defun print-chars (string)
        (let ((s (nullify string)))
          (while s
            (prinl (pop s))))) Note: optimized generic iteration is available in the form of iteration based on rather than and Examples:
      ;; Efficient with iterators,
      ;; at the cost of verbosity:
      (defun print-chars (string)
        (let ((i (iter-begin string)))
          (while (iter-more i)
            (prinl (iter-item s))
            (set s (iter-step s)))))
      ;; Using mapping function built on iterators:
      (defun print-chars (string)
        [mapdo prinl string]) The function extracts a slice from input sequence The slice is a sequence of the same type as If the argument is omitted, it defaults to If the parameter is omitted, it defaults to Thus means The following semantic equivalence exists between a call to the function and the DWIM-bracket syntax, except that is an ordinary function call form, which doesn't apply the Lisp-1 evaluation semantics to its arguments:
      ;; from is not a list
      (sub seq from to) <--> [seq from..to] The description of the operator—in particular, the section on Range Indexing—explains the semantics of the range specification. The output sequence may share structure with the input sequence. If is a object, then the function behaves like If is a object, then the function behaves like If is a object, then the function behaves like Note: because is not an accessor, assigning to the syntax in this case will produce an error. The argument may also be any other object type that is suitable as input to the function. In this situation, assigning to syntax produces an error. The behavior is complex. In cases where the and arguments imply that a suffix of is required, an iterator may be returned which traverses the suffix of the sequence. In other cases, a list of the elements selected by is returned. If is a structure, it must support the method. The operation is transformed into a call to the method according to the following equivalence:
      (sub o from to) <--> o.(lambda (rcons from to))
      (sub o : to) <--> o.(lambda (rcons : to))
      (sub o from) <--> o.(lambda (rcons from :))
      (sub o) <--> o.(lambda (rcons : :)) That is to say, the and arguments are converted to range object. If either argument is missing, the (colon) keyword symbol is used for the corresponding element of the range. When a form is used as a syntactic place, that place denotes a slice of The argument must be itself be syntactic place, because receives a new value, which may be different from its original value in cases when is a list. Overwriting that slice is equivalent to using the function. The following equivalences give the semantics, except that and are evaluated only once, in left-to-right order:
      (set (sub x a b) v) <--> (progn (set x (replace x v a b))
                            v)
      (del (sub x a b)) <--> (prog1 (sub x a b)
                            (set x (replace x nil a b))) Note that the value of is overwritten with the value returned by If is a vector or string, then the return value of is just the identity of the object doesn't change under mutation. However, if is a list, its identity changes when items are added to or removed from the front of the list, and in those cases will return a value different from its first argument. Similarly, if is an object with a method, that method's return value becomes the return value of and must be taken into account. The function modifies in the ways described below. The operation is destructive: it may work "in place" by modifying the original sequence. The caller should retain the return value and stop relying on the original input sequence. The return value of is the modified version of This may be the same object as or it may be a newly allocated object. Note that the form:
      (set seq (replace seq new fr to)) has the same effect on the variable as the form:
      (set [seq fr..to] new) except that the former form returns the entire modified sequence, whereas the latter returns the value of the argument. The function has two invocation styles, distinguished by the type of the third argument. If the third argument is a sequence, then it is deemed to be the parameter of the second form. Otherwise, if the third argument is missing, or is not a sequence, then it is deemed to be the argument of the first form. The first form of the replace function replaces a contiguous subsequence of the with The replaced subsequence may be empty, in which case an insertion is performed. If is empty (for example, the empty list then a deletion is performed. If the and arguments are omitted, their values default to and respectively. The description of the dwim operator—in particular, the section on Range Indexing—explains the semantics of the range specification. The second form of the replace function replaces a subsequence of elements from given by with their counterparts from If has at least as many elements as are indicated in then the indicated elements of are overwritten with successive elements from If contains fewer elements than then the excess elements indicated in which have no counterparts in the are deleted. Whenever a negative value occurs in the original length of (before any deletions) is added to that value. Furthermore, similar restrictions apply on as under the select function. Namely, the replacement stops when an index value in is encountered which is out of range for furthermore, if is a list, or if any deletions take place, then must be monotonically increasing, after consideration of the displacement of negative values, or else the behavior is unspecified. If shares storage with the target range of or, in the case when that range is resized by the operation, shares storage with any portion of above that range, then the effect of on either object is unspecified. If is a object, then behaves like If is a object, then behaves like If is a structure, then the structure must support the method. The operation is translated into a call of the method according to the following equivalences:
      (replace o items from to)
      <--> o.(lambda-set (rcons from to) items)
      (replace o items index-seq)
      <--> o.(lambda-set index-seq items) Thus, the and arguments are converted to single range object, whereas an is passed as-is. It is an error if the argument is a sequence, indicating an and a argument is also given; the situation is diagnosed. If either or are omitted, the range object contains the (colon) keyword symbol in the corresponding place:
      (replace o items from)
      <--> o.(lambda-set (rcons from :) items)
      (replace o items : to)
      <--> o.(lambda-set (rcons : to) items)
      (replace o items)
      <--> o.(lambda-set (rcons : :) items) It is the responsibility of the object's method to implement semantics consistent with the description of The function returns with all except the first items removed. If is a list, then returns a lazy list which produces the first items of sequence. For other kinds of sequences, including lazy strings, works eagerly. If exceeds the length of then a sequence is returned which has all the items. This object may be itself, or a copy. If is negative, it is treated as zero. The and functions return a prefix of whose items satisfy certain conditions. The function returns the longest prefix of whose elements, accessed through satisfy the function The argument defaults to the identity function: the elements of are examined themselves. The function returns the longest prefix of which consists of elements, accessed through that do not satisfy followed by an element which does satisfy If has no such prefix, then an empty sequence is returned of the same kind as If is a list, then these functions return a lazy list. The function returns with the first items removed. If is negative, it is treated as zero. If is zero, then is returned. If exceeds the length of then an empty sequence is returned of the same kind as The and functions return with a prefix of that sequence removed, according to conditions involving and The function removes the longest prefix of whose elements, accessed through satisfy the function and returns the remaining sequence. The argument defaults to the identity function: the elements of are examined themselves. The function removes the longest prefix of which consists of elements, accessed through that do not satisfy followed by an element which does satisfy A sequence of the remaining elements is returned. If has no such prefix, then a sequence same as is returned, which may be itself or a copy. The function returns a subsequence of consisting of the last of its elements, where defaults to 1. If is zero or negative, then an empty sequence is returned. If is positive, and greater than or equal to the length of sequence, then sequence is returned. If a form is used as a place, then must be a place. The following equivalence gives the semantics of assignment to a
      (set (last x n) v) <--> (set (sub x (- (max n 0)) t) v) A place is deletable. The semantics of deletion may be understood in terms of the following equivalence:
      (del (last x n)) <--> (del (sub x (- (max n 0)) t)) The function returns the prefix of consisting of a copy of it, with the last items removed. The parameter defaults to 1 if an argument is omitted. If is empty, an empty sequence is returned. If is zero or negative, then is returned. If is positive, and meets or exceeds the length of then an empty sequence is returned. If a form is used as a place, then must itself be a place. The following equivalence gives the semantics of assignment to a
      (set (butlast x n) v) <--> (set (sub x 0 (- (max n 0))) v) A place is deletable. The semantics of deletion may be understood in terms of the following equivalence:
      (del (last x n)) <--> (del (sub x 0 (- (max n 0)))) Note: the TXR Lisp function also computes the prefix of a list; however, it counts items from the beginning, and provides lazy semantics which allow it to work with infinite lists. See also: the accessor, which operates on lists. That function has useful semantics for improper lists and treats an atom as the terminator of a zero-length improper list. Dialect Note: a destructive function similar to Common Lisp's isn't provided. Assignment to a form is destructive; Common Lisp doesn't support as a place. The function is a somewhat generalized version of the same-named classic Lisp function found in traditional Lisp dialects. The function supports the original semantics when both inputs are lists. It determines whether the list is a structural suffix of which is to say: is one of the cells which comprise If so, then a list is returned consisting of all the items of before a copy of with the part removed, and replaced by the terminator. If is or the lists are unrelated, then is returned. The TXR Lisp function supports the following additional semantics.

    1.  The basic description of is extended to work with list-like sequences, not merely lists; that is to say, objects which support the method.

    2.  If is any kind of sequence, and is any kind of empty sequence, then is returned.

    3.  If either argument is an atom that is not a sequence, returns

    4.  If is a list-like sequence, and isn't, then the terminating atom of is determined. This atom is compared using to the object. If they are equal, then a proper list is returned containing the items of excluding the terminating atom.

    5.  If both arguments are vector-like sequences, then determines whether has a suffix which is to If this is the case, then a sequence is returned, of the same kind as consisting of the items of before that suffix. If is not to a suffix of then is returned.

    6.  In all other cases, and are compared with If the comparison is true, is returned, otherwise is returned.
  ;;; unspecified: the compiler could make
  ;;; '(2 3) a suffix of '(1 2 3),
  ;;; or they could be separate objects.
  (ldiff '(1 2 3) '(2 3)) -> either (1) or (1 2 3)
  ;; b is the (1 2) suffix of a, so the ldiff is (1)
  (let* ((a '(1 2 3)) (b (cdr a)))
    (ldiff a b))
  -> (1)
  ;; Rule 5: strings and vector
  (ldiff "abc" "bc") -> "a"
  (ldiff "abc" nil) -> "abc"
  (ldiff #(1 2 3) #(3)) -> #(1 2)
  ;; Rule 5: mixed vector kinds
  (ldiff "abc" #(#\b #\c)) -> "abc"
  ;; Rule 6:
  (ldiff #(1 2 3) '(3)) -> #(1 2 3)
  ;; Rule 4:
  (ldiff '(1 2 3) #(3)) -> '(1 2 3)
  (ldiff '(1 2 3 . #(3)) #(3)) -> '(1 2 3)
  (ldiff '(1 2 3 . 4) #(3)) -> '(1 2 3 . 4)
  ;; Rule 6
  (ldiff 1 2) -> 1
  (ldiff 1 1) -> nil The function determines whether the sequence occurs as substring within under the given comparison function and key function If this is the case, then the zero-based position of the leftmost occurrence of within is returned. Otherwise is returned to indicate that does not occur within If is empty, then zero is always returned. The arguments and are sequences. They may not be hash tables. If is not empty, then it occurs at some position N within if the first element of matches the element at position N of the second element of matches the element at position N+1 of and so forth, for all elements of A match between elements is determined by passing each element through and then comparing the resulting values using If is supplied, it must be a function which can be called with two arguments. If it is not supplied, it defaults to If is supplied, it must be a function which can be called with one argument. If it is not supplied, it defaults to
  ;; fails because 3.0 doesn't match 3
  ;; under the default eql function
  [search #(1.0 3.0 4.0 7.0) '(3 4)] -> nil
  ;; occurrence found at position 1:
  ;; (3.0 4.0) matches (3 4) under =
  [search #(1.0 3.0 4.0 7.0) '(3 4) =] -> 1
  ;; "even odd odd odd even" pattern
  ;; matches at position 2
  [search #(1 1 2 3 5 7 8) '(2 1 1 1 2) : evenp] -> 2
  ;; Case insensitive string search
  [search "abcd" "CD" : chr-toupper] -> 2
  ;; Case insensitive string search
  ;; using vector of characters as key
  [search "abcd" #(#\C #\D) : chr-toupper] -> 2 The syntax of the function differs from that of that the and arguments are reversed. The semantics is identical. The function is like except for two differences. Firstly, if matches in multiple places, returns the rightmost matching position rather than the leftmost. Secondly, if is an empty sequence, then returns the length of thereby effectively declaring that the rightmost match for an empty key occurs at the imaginary position past the element of The function is closely related to the and functions. Whereas those two functions return the leftmost or rightmost position, respectively, of within the function returns a list of all the positions where occurs. The positions of overlapping matches are included in the list. If is not found in returns the empty list If is empty, then returns a list of all positions in including the one position past the last element. In this situation, if is empty, the list is returned. If contains one item, then the list is returned and so forth. In all situations in which returns a non-empty list, the first element of that list is what would return for the same arguments, and the last element is what would return. The accessor performs array-like indexing into sequences, as well as hash tables and objects of type as well as structure objects which define a method. If the parameter is a hash, then these functions perform has retrieval and storage; in that case isn't restricted to an integer value. If is a structure, it supports directly if it has a method. The argument is passed to that method, and the resulting value is returned. If a structure lacks a method, but has a method, then treats it as a list, traversing the structure using operations. In the absence of support for these operations, the function fails with an error exception. If is a sequence then argument must be an integer. The first element of the sequence is indexed by zero. Negative values are permitted, denoting backward indexing from the end of the sequence, such that the last element is indexed by -1, the second last by -2 and so on. See also the Range Indexing section under the description of the operator. If is a list, then out-of-range indices, whether positive or negative, are treated leniently by such accesses produce the value rather than an error. For other sequence types, such accesses are erroneous. For hashes, accesses to nonexistent elements are treated leniently, and produce If is a search tree, then behaves like If is a range object, then behaves like A expression may be used as a place. Storing a value into a place is performed using the function. When the operator is used to delete an index value from a place, the itself must be a place. The deletion calculates a new sequence with the item at deleted; that new sequence is stored back into the place. Deletion does not use but rather the function. The function performs indexing into in a manner identical to with the purpose of overwriting the indexed element with It is a companion function to which is used in the implementation of the place. The return value of is If is a structure, it supports directly if it has a method. This gets called with and as arguments. Then is returned. If a structure lacks a method, then treats it as a list, traversing the structure using operations, and storing using In the absence of support for these operations, the function fails with an error exception. The function is not supported by search trees. The function is strict for out-of-range indices over all sequences, including lists. In the case of hashes, a of a nonexistent key creates the key. The accessor provides a mechanism for invoking a curried function. Its name reflects its usefulness for multi-dimensional indexing into nested sequences. The associated place which makes the operator an accessor provides in-place replacement of values in multi-dimensional sequences. There are some restrictions on the arguments when is used as a place. The argument is not necessarily a sequence, but may be object that can be called as a function with one argument. Except that isn't a place, the expression is equivalent to invoke the function/object with argument When multiple arguments are present, the return value of each previous application is expected to be another callable object, to which the next argument is applied. Thus is equivalent to This is also equivalent to provided that under the Lisp-1-style name resolution semantics of the DWIM brackets, the symbols and all resolve to bindings in the variable namespace. The expression is not equivalent to rather, it is equivalent to there are no arguments and so the object is taken as-is, not being applied to any index. In more detail, the function begins by taking as its an accumulator object. Then if there are arguments, it iterates over them. At each iteration step, it replaces the accumulator by treating the accumulator as a callable object and applying it to value and taking the resulting value as the new accumulator. After the iteration, the accumulator becomes the return value of the function. When is used as a place, only the rightmost argument may be a range. If any other argument is a range object, the behavior is unspecified. When is used as a place, and there is only one which is a range object, then the expression is also required to be a place, if it denotes a list or range object. If there are no augments then is unconditionally required to be a place. Note: the functions and may be used to create nested vectors which simulate multi-dimensional arrays.
  ;; Indexing:
  (let ((ar '((1 2 3)
              (4 5 6)
              (7 8 9))))
    (mref ar 1 1))
  --> 5
  ;; Updating value in nested sequence:
  (let ((ar (vec (vec (vec 0 1 2 3)
                      (vec 4 5 6 7))
                 (vec (vec 8 9 10 11)
                      (vec 12 13 14 15)))))
    (set (mref ar 0 0 1..3) "AB")
    ar)
  --> #(#(#( 0 #\A #\B 3)
          #( 4 5 6 7))
        #(#( 8 9 10 11)
          #(12 13 14 15)))
  ;; Invoking curried function:
  (let ((cf (lambda (x)
              (lambda (y)
                (lambda (z)
                  (+ x y z))))))
    [mref cf 1 2 3])
  --> 6 The function replaces each elements in in a hash table, with the result of being applied to that element value. The is returned. The may be a hash table. In that case, is invoked with each hash value, which is replaced with the function's return value. The and functions produce a new sequence based on removing the elements whose associated keys are or to The input is unmodified, but the returned sequence may share substructure with it. If no items are removed, it is possible that the return value is itself. If is omitted, then the element keys compared to are the elements themselves. Otherwise, is applied to each element and the resulting value is that element's key which is compared to The and functions are lazy analogs of and Rather than computing the entire new sequence prior to returning, these functions return a lazy list. Caution: these functions can still get into infinite looping behavior. For instance, in will keep consuming the values coming out of the infinite list, looking for the first item that does not have to be deleted, in order to instantiate the first lazy value.
  ;; Return a list of all the natural numbers, excluding 13,
  ;; then take the first 100 of these.
  ;; If remql is used, it will loop until memory is exhausted,
  ;; because (range 1) is an infinite list.
  [(remql* 13 (range 1)) 0..100] The and functions produce a new sequence based on removing the items whose keys are not or to The input is unmodified, but the returned sequence may share substructure with it. If no items are removed, it is possible that the return value is itself. The optional is applied to each element from the to convert it to a key which is compared to If is omitted, then each element itself of is compared to The function produces a sequence whose contents are those of but with those elements removed which satisfy Those elements which are not removed appear in the same order. The result sequence may share substructure with the input sequence, and may even be the same sequence object if no items are removed. The optional specifies how each element from the is transformed to an argument to If this argument is omitted then the predicate function is applied to the elements directly, a behavior which is identical to being The function is exactly like except the sense of the predicate is inverted. The function retains those items which will delete, and removes those that will preserve. The function combines and into one, returning a list of two elements whose and are the result of calling and respectively, on (with the and arguments passed through). One of the two elements may share substructure with the input sequence, and may even be the same sequence object if all items are either kept or removed (in which case the other element will be Note: the function may be understood in terms of the following reference implementation:
  (defun separate (pred seq : (keyfun :))
    [(juxt (op keep-if pred @1 keyfun)
           (op remove-if pred @1 keyfun))
     seq]) The and functions are like and but produce lazy lists.
  ;; remove any element numerically equal to 3.
  (remove-if (op = 3) '(1 2 3 4 3.0 5)) -> (1 2 4 5)
  ;; remove those pairs whose first element begins with "abc"
  [remove-if (op equal [@1 0..3] "abc")
             '(("abcd" 4) ("defg" 5))
             car]
  -> (("defg" 5))
  ;; equivalent, without test function
  (remove-if (op equal [(car @1) 0..3] "abc")
             '(("abcd" 4) ("defg" 5)))
  -> (("defg" 5)) The functions and are derived, respectively, from the functions and and have the same syntax and argument semantics. They differ in that rather than accumulating the elements of the input they accumulate the transformed values of those elements, as projected through the Thus when is omitted, thus defaulting to or else explicitly specified as or equivalent function, the behavior of these functions is the almost the same as that of and However, there may be a difference in whether the output shares structure with
  ;; square the values 1 to 20, keeping the even squares
  [keep-keys-if evenp (range 1 20) square]
  -> (4 16 36 64 100 144 196 256 324 400)
  ;; square the values 1 to 20 separating into even and odd:
  [separate-keys evenp (range 1 20) square]
  -> ((4 16 36 64 100 144 196 256 324 400)
      (1 9 25 49 81 121 169 225 289 361))
  ;; contrast with keep-if: values are of input sequence
  [keep-if evenp (range 1 20) square]
  -> (2 4 6 8 10 12 14 16 18 20) The and functions count the number of objects in which are or to and return the count. The and functions search through for items which match or satisfy the predicate function respectively. They return the number of matching or predicate-satisfying items. The argument specifies a function which is applied to the elements of to produce the comparison key. If this argument is omitted, then the untransformed elements of are examined. The function's argument specifies the test function which is used to compare the comparison keys from to If this argument is omitted, then the function is used. The function returns the number of elements of whose comparison key (as retrieved by matches the object, as compared by The function's argument specifies a predicate function which is applied to the successive comparison keys taken from The function returns the count of the number keys for which returns true. The function returns the number of times the object occurs in the cell structure under the equality imposed by the If the optional argument is omitted, it defaults to First, and are compared using If they are equal, that counts as one occurrence. Then, if is a cell, the function recurses over the and fields. The sum of all these counts is returned. The and functions return the zero-based position of the first item in which is, respectively, or to The and functions search through for an item which matches or satisfies the predicate function respectively. They return the zero-based position of the matching item. The argument specifies a function which is applied to the elements of to produce the comparison key. If this argument is omitted, then the untransformed elements of are examined. The function's argument specifies the test function which is used to compare the comparison keys from to If this argument is omitted, then the function is used. The function returns the position of the first element of whose comparison key (as retrieved by matches as compared by the function. If no such element is found, is returned. The function's argument specifies a predicate function which is applied to the successive comparison keys taken from by applying to successive elements. The position of the first element for which yields true is returned. If no such element is found, is returned. These functions are counterparts of and which report position of the rightmost matching item, rather than the leftmost. The and functions implement exactly the same algorithm; they differ only in their defaulting behavior with regard to the argument. If is not given, then the function defaults to the function, whereas defaults it to the function. If is empty, both functions return Without a argument, the function finds the zero-based position index of the numerically maximum value occurring in whereas without a argument finds the index of the minimum value. If a argument is given, the two functions are equivalent. The function must be callable with two arguments. If behaves like a greater-than comparison, then and return the index of the maximum element. If behaves like a comparison, then the functions return the index of the minimum element. The argument defaults to the function. Each element from is passed through this one-argument function, and the resulting value is used in its place. If a sequence contains multiple equivalent maxima, whether the position of the leftmost or rightmost such maximum is reported depends on whether compares for strict inequality, or whether it reports true for equal arguments also. Under the default which is the function will return the position leftmost of a duplicate set of maximum elements. To find the rightmost of the maxima, the function can be substituted. Analogous reasoning applies to other test functions. The function returns a sequence of the same type as in which elements of which match the object have been replaced with the object. To form the comparison keys, the elements of are projected through the function, which defaults to so the items themselves are used as keys by default. Keys are compared to the value using which defaults to
  (subst "brown" "black" #("how" "now" "brown" "cow"))
  -> #("how" "now" "black" "cow"))
  ;; elements are converted to lower case to form keys
  [subst "brown" "black"
         #("how" "now" "BROWN" "cow") : downcase-str]
  -> #("how" "now" "black" "cow")
  ;; using < instead of equality, replace elements
  ;; greater than 5 with 0
  [subst 5 0 '(1 2 3 4 5 6 7 8 9 10) <] (1 2 3 4 5 0 0 0 0 0)) The and functions return a sequence of the same kind as in which elements matching the object are replaced by object. The matching elements are identified by comparing with using, respectively, the functions and
  (subq #\b #\z "abc") -> "azc"
  (subql 1 3 #(0 1 2)) -> #(0 3 2)
  (subqual "are" "do" '#"how are you")
  -> ("how" "do" "you") The function compares corresponding elements from the sequences and returning the position at which the first mismatch occurs. If the sequences are of the same length, and their corresponding elements are the same, then is returned. If one sequence is shorter than the other, and matches a prefix of the other, then the mismatching position returned is one position after the last element of the shorter sequence, the same value as its length. An empty sequence is a prefix of every sequence. The argument defaults to the function. Each element from is passed to and the resulting value is used in its place. After being converted through items are then compared using which must accept two arguments, and defaults to If is a sequence, the function returns a lazy list of the numeric indices of those of its elements which satisfy The numeric indices appear in increasing order. If is a hash, the following special behavior applies: returns a lazy list of of keys which have values which satisfy These keys are not subject to an order. must be a function that can be called with one argument. For each element of is called with that element as an argument. If a value is returned, then the zero-based index of that element is added to a list. Finally, the list is returned. Similarly to the function compares corresponding elements from the sequences and returning the position at which the first mismatch occurs. All of the arguments have the same semantics as that of Unlike compares the sequences right-to-left, finding the suffix which they have in common, rather than prefix. If the sequences match, then is returned. Otherwise, a negative index is returned giving the mismatching position, regarded from the end. If the sequences match only in the rightmost element, then -1 is returned. If they match in two elements then -2 and so forth. The and functions compare corresponding elements from sequences and The function returns if is prefix of otherwise, it returns The function returns if is suffix of otherwise, it returns Element from both sequences are mapped to comparison keys using which defaults to Comparison keys are compared using which defaults to The function returns a sequence, of the same kind as which consists of those elements of which are identified by the indices in which is required to be a sequence. If a argument is given instead of then is invoked with as its argument. The return value is then taken as if it were the argument . If is a sequence, then consists of numeric indices. The length of the sequence, as reported by the function, is added to every value which is negative. The function stops collecting values upon encountering an index value which is greater than or equal to the length of the sequence. (Rationale: without this strict behavior, would not be able to terminate if is infinite.) If is, more specifically, a list-like sequence, then must contain monotonically increasing numeric values, even if no value is out of range, since the function makes a single pass through the list based on the assumption that indices are ordered. (Rationale: optimization.) This requirement for monotonicity applies to the values which result after negative indices are displaced by the sequence length Also, in this list-like sequence case, values taken from which are still negative after being displaced by the sequence length are ignored. If is a hash, then is a list of keys. A new hash is returned which contains those elements of whose keys appear in All of is processed, even if it contains keys which are not in The nonexistent keys are ignored. The function also supports objects of type in a manner similar to vectors. The indicated elements are extracted from the input sequence, and a new is returned whose storage is initialized by converting the extracted values back to the foreign representation. The function returns a sequence, of the same kind as which consists of all those elements of which are not identified by the indices in which may be a list or a vector. If is given instead of then is invoked with as its argument. The return value is then taken as if it were the argument . If is a hash, then represents a list of keys. The function returns a duplicate of the hash, in which the keys specified in do not appear. Otherwise if is a vector-like sequence, then the behavior of may be understood by the following equivalence:
  (reject seq idx) --> (make-like
                        [apply append (split* seq idx)]
                        seq) where it is to be understood that is evaluated only once. If is a list, then, similarly, the following equivalence applies:
  (reject seq idx) --> (make-like
                        [apply append* (split* seq idx)]
                        seq) The input sequence is split into pieces at the indicated indices, such that the elements at the indices are removed and do not appear in the pieces. The pieces are then appended together in order, and the resulting list is coerced into the same type of sequence as the input sequence. The function returns a one-argument function which implements the relation formed by mapping the elements of to the positionally corresponding elements of That is to say, the function searches through the sequence to determine the position where its argument occurs, using as the comparison function. Then it returns the element from that position in the sequence. This returned function is called the relation function. If the relation function's argument is not found in then the behavior depends on the optional parameter If an argument is given for then the relation function returns that value. Otherwise, the relation function returns its argument. Note: the function may be understood in terms of the following equivalences:
  (relate d r) <--> (lambda (arg)
                        (iflet ((p (posqual arg d)))
                        [r p]
                        arg))
  (relate d r v) <--> (lambda (arg)
                        (iflet ((p (posqual arg d)))
                        [r p]
                        v)) Note: may return a hash table instead of a function, if such an object can satisfy the semantics required by the arguments.
  (mapcar (relate "_" "-") "foo_bar") -> "foo-bar"
  (mapcar (relate "0123456789" "ABCDEFGHIJ" "X") "139D-345")
  -> "BJDXXDEF"
  (mapcar (relate '(nil) '(0)) '(nil 1 2 nil 4)) -> (0 1 2 0 4) The function tests whether is found inside or If the argument is specified, it specifies the function which is used to comparison keys from the sequence to Otherwise the function is used. If the argument is specified, it specifies a function which is applied to the elements of to produce the comparison keys. Without this argument, the elements themselves are taken as the comparison keys. If the object being searched is a hash, then if neither of the arguments nor is specified, performs a hash lookup for returning if the key is found, otherwise. If either of or is specified, then performs an exhaustive search of the hash table, as if it were a sequence of cells whose fields are keys, and whose keys are values. Thus to search by key, the function must be specified as The function returns if it finds in or otherwise If is empty, then returns an empty list, and the second argument is ignored; if it is it is not called. Otherwise, returns a lazy list of partitions of Partitions are consecutive, non-overlapping, nonempty substrings of of the same kind as such that if these substrings are catenated together in their order of appearance, a sequence to the original is produced. If the second argument is of the form or if an was produced from the or arguments, each value in that sequence must be an integer. Each integer value which is nonnegative specifies the index position given by its value. Each integer value which is negative specifies an index position given by adding the length of to its value. The sequence index positions thus denoted by shall be strictly nondecreasing. Each successive element is expected to designate an index position at least as high as all previous elements, otherwise the behavior is unspecified. Leading index positions which are (still) negative, or zero, are effectively ignored. If is empty then a one-element list containing the entire is returned. If is an infinite lazy list, the function shall terminate if that list eventually produces an index position which is greater than or equal to the length of If the second argument is a function, then this function is applied to and the return value of this call is then used in place of the second argument, which must either be a single index value, which is then taken as if it were the argument, or else a sequence of indices, which are taken as the argument. If the second argument is neither a sequence, nor a function, then it is assumed to be an integer index, and is turned into an sequence containing one element. After the is obtained as an argument, or determined from the or arguments, the function then divides according to the indices. The first partition begins with the first element of The second partition begins at the first position in and so on. Indices beyond the length of the sequence are ignored, as are indices less than or equal to zero.
  (partition '(1 2 3) 1) -> ((1) (2 3))
  ;; split the string where there is a "b"
  (partition "abcbcbd" (op where (op eql #\b))) -> ("a" "bc"
                        "bc" "bd") If is empty, then both and return an empty list, and the second argument is ignored; if it is it is not called. Otherwise, returns a lazy list of pieces of consecutive, non-overlapping, possibly empty substrings of of the same kind as A catenation of these pieces in the order they appear would produce a sequence that is to the original sequence. The function differs from in that the elements indicated by the split indices are removed. The and arguments are subject to the same restrictions and treatment as the corresponding arguments of the function, with the following difference: the index positions indicated by are required to be strictly increasing, rather than nondecreasing. If the second argument is of the form or if an was produced from the or arguments, then the function divides according to the indices indicated in The first piece always begins with the first element of Each subsequent piece begins with the position indicated by an element of Negative indices are ignored. If includes index zero, then an empty first piece is generated. If includes an index greater than or equal to the length of (equivalently, an index beyond the last element of the sequence) then an additional empty last piece is generated. The length of is added to any negative indices. An index which is still negative after being thus displaced is discarded. Note: the principal difference between and is that does not produce empty pieces.
  (split '(1 2 3) 1) -> ((1) (2 3))
  (split "abc" 0) -> ("" "abc")
  (split "abc" 3) -> ("abc" "")
  (split "abc" 1) -> ("a" "bc")
  (split "abc" '(0 1 2 3)) -> ("" "a" "b" "c" "")
  (split "abc" '(1 2)) -> ("a" "b" "c")
  (split "abc" '(-1 1 2 15)) -> ("a" "b" "c")
  ;; triple split at makes two additional empty pieces
  (split "abc" '(1 1 1)) -> ("a" "" "" "bc")
  (split* "abc" 0) -> ("" "bc") ;; "a" is removed
  ;; all characters removed
  (split* "abc" '(0 1 2)) -> ("" "" "" "") If is empty, then returns an empty list, and the second argument is ignored; if it is it is not called. The and arguments are subject to the same restrictions and treatment as the corresponding arguments of the function, with the following difference: the index positions indicated by are required to be strictly increasing, rather than nondecreasing. If the second argument is of the form then produces a lazy list of pieces taken from The pieces are formed by deleting from the elements at the positions given in such that the pieces are the remaining nonempty substrings from between the deleted elements, maintaining their order. If is empty then a one-element list containing the entire is returned.
  (partition* '(1 2 3 4 5) '(0 2 4)) -> ((2) (4))
  (partition* "abcd" '(0 3)) -> "bc"
  (partition* "abcd" '(0 1 2 3)) -> nil The and functions search through a sequence for an item which matches a key, or satisfies a predicate function, respectively. The function is a variant of which returns the value of the predicate function instead of the item. The argument specifies a function which is applied to the elements of to produce the comparison key. If this argument is omitted, then the untransformed elements of the are searched. The function's argument specifies the test function which is used to compare the comparison keys from to the search key. If this argument is omitted, then the function is used. The first element from the list whose comparison key (as retrieved by matches the search (under is returned. If no such element is found, is returned. The function's argument specifies a predicate function which is applied to the successive comparison keys pulled from the list by applying to successive elements. The first element for which yields true is returned. If no such element is found, is returned. In the case of a hash table may be specified instead of a sequence. The is treated as if it were a sequence of hash key and hash value pairs represented as cons cells, the slots of which are the hash keys, and the of which are the hash values. If the caller doesn't specify a then these cells are taken as their keys. The function's argument conventions and search semantics are identical to those of but the return value is different. Instead of returning the found item, returns the value which returned for the found item's key. The and functions are almost exactly like and except that if there are multiple matches for in they return the rightmost element rather than the leftmost. In the case of when a is specified instead of a the function searches through the hash entries in the same order as but finds the last match rather than the first. Note: hashes are inherently not ordered; the relative order of items in a hash table can change when other items are inserted or deleted. The and function implement exactly the same algorithm; they differ only in their defaulting behavior with regard to the argument. If is not given, then the function defaults it to the function, whereas defaults it to the function. Without a argument, the function finds the numerically maximum value occurring in whereas without a argument finds the minimum value. If a argument is given, the two functions are equivalent. The function must be callable with two arguments. If behaves like a greater-than comparison, then and both return the maximum element. If behaves like a less-than comparison, then the functions return the minimum element. The argument defaults to the function. Each element from is passed through this one-argument function, and the resulting value is used in its place for the purposes of the comparison. However, the original element is returned. If there are multiple equivalent maxima, then under the default that being the first one encountered while traversing is the one that is reported. See the notes under regarding duplicate maxima. The and functions have the same argument conventions as, respectively, and and agree with those functions in regard to which element of the input sequence is identified: all these functions identify the element which maximizes or minimizes the value of Whereas and return the maximizing or minimizing element itself, the and functions return the value of applied to the element. Under the default value, that being the function, these functions behave the same as and The functions and treat the sequences and as if they were sets. They, respectively, compute the set union, set intersection, set difference and symmetric difference of and returning a new sequence. The is Boolean: it returns for those arguments for which returns a non-empty list, otherwise The arguments and need not be of the same kind. They may be hash tables. The returned sequence is of the same kind as If is a hash table, the returned sequence is a list. For the purposes of these functions, an input which is a hash table is considered as if it were a sequence of hash key and hash value pairs represented as cons cells, the slots of which are the hash keys, and the of which are the hash values. This means that if no is specified, these pairs are taken as keys. Since the input sequences are defined as representing sets, they are assumed not to contain duplicate elements. These functions are not required, but may, de-duplicate the sequences. The union sequence produced by contains all of the elements which occur in both and If a given element occurs exactly once only in or exactly once only in or exactly once in both sequences, then it occurs exactly once in the union sequence. If a given element occurs at least once in either or both, then it occurs at least once in the union sequence. The intersection sequence produced by contains all of the elements which occur in both and If a given element occurs exactly once in and exactly once in then in occurs exactly once in the intersection sequence. If a given element occurs at least once in and at least once in then in occurs at least once in the intersection sequence. The difference sequence produced by contains all of the elements which occur in but do not occur in If an element occurs exactly once in and does not occur in then it occurs exactly once in the difference sequence. If an element occurs at least once in and does not occur in then it occurs at least once in the difference sequence. If an element occurs at least once in then it does not occur in the difference sequence. The symmetric difference sequence produced by contains all of the elements of which do not occur in and vice versa: it also contains all of the elements of which do not occur in Element equivalence is determined by a combination of and Elements are compared pairwise, and each element of a pair is passed through function to produce a comparison value. The comparison values are compared using If is omitted, then the untransformed elements themselves are compared, and if is omitted, then the function is used. Note: a function similar to named exists. This became deprecated starting in TXR 184. For the function, the requirement was specified to preserve the original order of items from that survive into the output sequence. This requirement is not documented for the function, but is de facto honored by the implementation for at as long as the synonym continues to be available. The function doesn't support hash tables and is inefficient for vectors and strings. Note: these functions are not efficient for the processing of hash tables, even when both inputs are hashes, the argument is and matches the equality used by both hash-table inputs. If applicable, the operations and should be used instead. When given only one argument, the function returns is never called. When given two arguments, the function applies to each elements of and returns a sequence of the resulting values in the same order as the original values. The returned sequence is the same kind as if possible. If the accumulated values cannot be elements of that type of sequence, then a list is returned. When additional sequences are given as arguments, this filtering behavior is generalized in the following way: traverses the sequences in parallel, taking a value from each sequence as an argument to the function. If there are two lists, is called with two arguments and so forth. The traversal is limited by the length of the shortest sequence. The return values of the function are collected into a new sequence which is returned. The returned sequence is of the same kind as the leftmost input sequence, unless the accumulated values cannot be elements of that type of sequence, in which case a list is returned. The function works like with the following difference. Rather than accumulating the values returned by the function into a sequence, mappend expects the items returned by the function to be sequences which are catenated with and the resulting sequence is returned. The returned sequence is of the same kind as the leftmost input sequence, unless the values cannot be elements of that type of sequence, in which case a list is returned. The and functions work like and respectively. However, they return lazy lists rather than generating the entire output list prior to returning. Like must "consume" empty lists. For instance, if the function being mapped puts out a sequence of then the result must be the empty list because is But suppose that is used on inputs which are infinite lazy lists, such that the function returns values indefinitely. For instance:
  ;; Danger: infinite loop!!!
  (mappend* (fun identity) (repeat '(nil))) The function is caught in a loop trying to consume and squash an infinite stream of and so doesn't return.
  ;; multiply every element by two
  (mapcar (lambda (item) (* 2 item)) '(1 2 3)) -> (4 6 8)
  ;; "zipper" two lists together
  (mapcar (lambda (le ri) (list le ri)) '(1 2 3) '(a b c))
  -> '((1 a) (2 b) (3 c)))
  ;; like append, mappend allows a lone atom or a trailing atom:
  (mappend (fun identity) 3) -> (3)
  (mappend (fun identity) '((1) 2)) -> (1 . 2)
  ;; take just the even numbers
  (mappend (lambda (item) (if (evenp x) (list x))) '(1 2 3 4 5))
  -> (2 4) The and functions resemble and respectively. When given no arguments or exactly one argument, they behave exactly like those three functions. When two or more arguments are present, differs from in the following way, as do the remaining functions from their aforementioned counterparts. Whereas iterates over the values in parallel, taking successive tuples of element values and passing them to the function iterates over all combinations of elements from the sequences: the Cartesian product. The suffix stands for "product". If one or more arguments specify an empty sequence, then the Cartesian product is empty. In this situation, is not called. The result of the function is then converted to the same kind of sequence as the leftmost The function collects the values into a list just as does. Just like it converts the resulting list into the same kind of sequence as the leftmost argument, if possible. For instance, if the resulting list is a list or vector of characters, and the leftmost is a character string, then the list or vector of characters is converted to a character string and returned. The function ("map product through function and append") iterates the element combinations exactly like passing them as arguments to The values returned by are then treated exactly as by the function. The return values are expected to be sequences which are appended together as if by and the final result is converted to the same kind of sequence as the leftmost if possible. The function, like ignores the result of and returns The combination iteration gives priority to the rightmost which means that the rightmost element of each generated tuple varies fastest: the tuples are traversed in "rightmost major" order. This is made clear in the examples.
  [maprod list '(0 1 2) '(a b) '(i ii iii)]
  ->
  ((0 a i) (0 a ii) (0 a iii) (0 b i) (0 b ii) (0 b iii)
   (1 a i) (1 a ii) (1 a iii) (1 b i) (1 b ii) (1 b iii)
   (2 a i) (2 a ii) (2 a iii) (2 b i) (2 b ii) (2 b iii))
  ;; Vectors #(#\a #\x) #(#\a #\y) ... are appended
  ;; together resulting in #(#\a #\x #\a #\y ...)
  ;; which is converted to a string:
  [maprend vec "ab" "xy"] -> "axaybxby"
  ;; One of the sequences is empty, so the product is an
  ;; empty sequence of the same kind as the leftmost
  ;; sequence argument, thus an empty string:
  [maprend vec "ab" ""] -> "" The function is similar to but always returns It is useful when performs some kind of side effect, hence the "do" in the name, which is a mnemonic for the execution of imperative actions. When only the argument is given, is never called, and is returned. If a single argument is given, then iterates over invoking on each element. If two or more arguments are given, then iterates over the sequences in parallel, extracting parallel tuples of items. These tuples are passed as arguments to which must accept as many arguments as there are sequences. The function performs a transposition on This means that the elements of must be iterable. These iterables are understood to be columns; transpose exchanges rows and columns, returning a sequence of the rows which make up the columns. The returned sequence is of the same kind as and the rows are also the same kind of sequence as the first column of the original sequence. The number of rows returned is limited by the shortest column among the sequences. All of the input sequences (the elements of must have elements which are compatible with the first sequence. This means that if the first element of is a string, then the remaining sequences must be strings, or else sequences of characters, or of strings. The function takes variable arguments, and is equivalent to calling on a list of the arguments. The following equivalences hold:
   (zip . x) <--> (transpose x)
   [apply zip x] <--> (transpose x)
  ;; transpose list of lists
  (transpose '((a b c) (c d e))) -> ((a c) (b d) (c e))
  ;; transpose vector of strings:
  ;; - string columns become string rows
  ;; - vector input becomes vector output
  (transpose #("abc" "def" "ghij")) -> #("adg" "beh" "cfi")
  ;; error: transpose wants to make a list of strings
  ;; but 1 is not a character
  (transpose #("abc" "def" '(1 2 3))) ;; error!
  ;; String elements are catenated:
  (transpose #("abc" "def" ("UV" "XY" "WZ")))
  -> #("adUV" "beXY" "cfWZ")
  ;; Transpose list of ranges
  (transpose (list 1..4 4..8 8..12))
  -> ((1 4 8) (2 5 9) (3 6 10))
  (zip '(a b c) '(c d e)) -> ((a c) (b d) (c e)) The and functions process the elements of by passing arguments derived from each successive element to Both functions return, if possible, a sequence of the same kind as otherwise a list. Under values returned by are accumulated into a sequence of the same type as and that sequence is returned. Under the values returned by the calls to are expected to be sequence which are appended together to form the output sequence. These functions are analogous to and Unlike these, they operate only on a single sequence, and over this sequence they perform a sliding window mapping, whose description follows. The function avoids accumulating a sequence, and instead returns it is analogous to The argument to the parameter must be a positive integer, not exceeding 512. This parameter specifies the amount of ahead/behind context on either side of each element which is processed. It indirectly determines the window size for the mapping. The window size is twice plus one. For instance if range is 2, then the window size is 5: the element being processed lies at the center of the window, flanked by two elements on either side, making five. The argument must specify a function which accepts a number of arguments corresponding to the window size. For instance if is 2, making the window size 5, then must accept 5 arguments. These arguments constitute the sliding window being processed. Each time is called, the middle argument is the element being processed, and the arguments surrounding it are its window. When an element is processed from somewhere in the interior of a sequence, where it is flanked on either side by at least elements, then the window is populated by those flanking elements taken from The parameter specifies the window contents which are used for the processing of elements which are closer than to either end of the sequence. Except if it is of list type, must be a sequence containing at least twice number of elements (one less than the window size): if it has additional elements, they are not used. If it is a list, it may be shorter than twice in this case, the value is substituted for the missing elements. The argument may also be one of the two keyword symbols or described below. If is a sequence, it may be regarded as divided into two pieces of length. These two pieces then flank on either end. The left half of is effectively prepended to the sequence, and the right half effectively appended. When the sliding window extends beyond the boundary of near its start or end, the window is populated from these flanking elements obtained from If argument is specified as the keyword then the sequence is imagined to be flanked at either end by an infinite repetition of copies of itself. These flanks are trimmed to the window size to generate the boundary. For instance if the sequence is and the window size is 9 due to the value of being 4, then the behavior of is as if value of were specified. The left flank is being the last four elements of an infinite repetition of and the right flank is similarly being the first four elements of an infinite repetition of If is given as the keyword then the sequence is imagined to be flanked at either end by an infinite repetition of reversed copies of itself. These flanks are trimmed to the window size to generate the boundary. For instance if the sequence is and the window size is 9 due to the value of being 4, then the behavior of is as if value of were specified. The left flank is being the last four elements of an infinite repetition of and the right flank is similarly being the first four elements of an infinite repetition of
  ;; change characters between angle brackets to upper case.
  [window-map 1 nil (lambda (x y z)
                      (if (and (eq x #\<)
                        (eq z #\>))
                        (chr-toupper y)
                        y))
              "ab<c>de<f>g"]
  --> "ab<C>de<F>g"
  ;; collect all numbers which are the centre element of
  ;; a monotonically increasing triplet
  [window-mappend 1 :reflect (lambda (x y z)
                        (if (< x y z)
                        (list y)))
                  '(1 2 1 3 4 2 1 9 7 5 7 8 5)]
  --> (3 7)
  ;; calculate a moving average with a five-element
  ;; window, flanked by zeros at the boundaries:
  [window-map 2 #(0 0 0 0)
              (lambda (. args) (/ (sum args) 5))
              #(4 7 9 13 5 1 6 11 10 3 8)]
  --> #(4.0 6.6 7.6 7.0 6.8 7.2 6.6 6.2 7.6 6.4 4.2)) The function returns a sequence of the same type as in which the elements from appear with the value inserted between them. If is an empty sequence or a sequence of length 1, then a sequence identical to is returned. It may be a copy of or it may be itself. If is a character string, then the value must be a character. It is permissible for or for a suffix of to be a lazy list, in which case interpose returns a lazy list, or a list with a lazy suffix.
  (interpose #\- "xyz") -> "x-y-z"
  (interpose t nil) -> nil
  (interpose t #()) -> #()
  (interpose #\a "") -> ""
  (interpose t (range 0 0)) -> (0)
  (interpose t (range 0 1)) -> (0 t 1)
  (interpose t (range 0 2)) -> (0 t 1 t 2) The and functions reduce lists of operands specified by and to a single value by the repeated application of In the case of the argument is required to be an object which is iterable according to the function. The function treats the argument using list operations. An effective list of operands is formed by combining and If is specified, then the items of are mapped to new values through as if by If is supplied, then in the case of the effective list of operands is formed by prepending to In the case of the effective operand list is produced by appending to The isn't mapped through The production of the effective list can be expressed like this, though this is not to be understood as the actual implementation:
  (append (if init-value-present (list init-value))
          [mapcar (or key-function identity) list])))) In the case, the arguments to are reversed. If the effective list of operands is empty, then is called with no arguments at all, and its value is returned. This is the only case in which is called with no arguments; in all remaining cases, it is called with two arguments. If the effective list contains one item, then that item is returned. Otherwise, the effective list contains two or more items, and is decimated as follows. Note that an specified as is not the same as a missing this means that the initial value is the object Omitting is the same as specifying a value of (the colon keyword symbol). It is possible to specify while omitting an argument. This is achieved by explicitly specifying as the argument. Under the leftmost pair of operands is removed from the list and passed as arguments to in the same order that they appear in the list, and the resulting value initializes an accumulator. Then, for each remaining item in the list, is invoked on two arguments: the current accumulator value, and the next element from the list. After each call, the accumulator is updated with the return value of The final value of the accumulator is returned. Under the list is processed right to left. The rightmost pair of elements in the effective list is removed, and passed as arguments to in the same order that they appear in the list. The resulting value initializes an accumulator. Then, for each remaining item in the list, is invoked on two arguments: the next element from the list, in right to left order, and the current accumulator value. After each call, the accumulator is updated with the return value of The final value of the accumulator is returned.
  ;;; effective list is (1) so 1 is returned
  (reduce-left (fun +) () 1 nil) -> 1
  ;;; computes (- (- (- 0 1) 2) 3)
  (reduce-left (fun -) '(1 2 3) 0 nil) -> -6
  ;;; computes (- 1 (- 2 (- 3 0)))
  (reduce-right (fun -) '(1 2 3) 0 nil) -> 2
  ;;; computes (* 1 2 3)
  (reduce-left (fun *) '((1) (2) (3)) nil (fun first)) -> 6
  ;;; computes 1 because the effective list is empty
  ;;; and so * is called with no arguments, which yields 1.
  (reduce-left (fun *) nil) The and functions apply a predicate-test function over a list of elements. If the argument is specified, then elements of are passed into and is applied to the resulting values. If is omitted, the behavior is as if were the function. If is omitted, the behavior is as if were the function. These functions have short-circuiting semantics and return conventions similar to the and operators. The function applies to successive values produced by retrieving elements of and processing them through If the list is empty, it returns Otherwise it returns the first return value returned by a call to and stops evaluating the elements. If returns for all elements, returns The function applies to successive values produced by retrieving elements of and processing them through If the list is empty, it returns Otherwise, if yields for any value, the function immediately returns without invoking on any more elements. If all the elements are processed, then the function returns the value which yielded for the last element. The function applies to successive values produced by retrieving elements of and processing them through If the list is empty, it returns Otherwise, if yields for any value, the function immediately returns If yields for all values, the function returns
  ;; some of the integers are odd
  [some '(2 4 6 9) oddp] -> t
  ;; none of the integers are even
  [none '(1 3 4 7) evenp] -> t The function distributes an arbitrary list processing function over multiple sequences given by the arguments. The arguments are first transposed into a single list of tuples. Each successive element of this transposed list consists of a tuple of the successive items from the lists. The length of the transposed list is that of the shortest argument. The transposed list is then passed to as an argument. The is expected to produce a list of tuples, which are transposed again to produce a list of lists which is then returned. Conceptually, the input sequences are columns and is invoked on a list of the rows formed from these columns. The output of is a transformed list of rows which is reconstituted into a list of columns.
  ;; Take three lists in parallel, and remove from all of them the
  ;; element at all positions where the third list has an element of 20.
  (multi (op remove-if (op eql 20) @1 third)
         '(1 2 3)
         '(a b c)
         '(10 20 30))
  -> ((1 3) (a c) (10 30))
  ;; The (2 b 20) "row" is gone from the three "columns".
  ;; Note that the (op remove if (op eql 20) @1 third)
  ;; expression can be simplified using the ap operator:
  ;;
  ;; (op remove-if (ap eql @3 20)) The function destructively sorts producing a sequence which is sorted according to the and arguments. The argument specifies a function which is applied to elements of the sequence to obtain the key values which are then compared using the lessfun. If is omitted, the identity function is used by default: the sequence elements themselves are their own sort keys. The argument specifies the comparison function which determines the sorting order. It must be a binary function which can be invoked on pairs of keys as produced by the key function. It must return a value if the left argument is considered to be lesser than the right argument. For instance, if the numeric function is used on numeric keys, it produces an ascending sorted order. If the function is used, then a descending sort is produced. If is omitted, then it defaults to the generic function. The function has the same argument requirements as but is non-destructive: it returns a new object, leaving the input unmodified, as if a copy of the input object were made using the function and then that copy were sorted in-place using The and functions are stable for sequences which are lists. This means that the original order of items which are considered identical is preserved. For strings and vectors, and are not stable. The and functions have the same argument syntax and semantics as, respectively, and These functions provide a stable sort for all sequences, not only lists, at the cost of temporarily allocating memory. All of these functions can be applied to hashes. They produce meaningful behavior for a hash table which contains N keys which are the integers from 0 to N - 1. Such as hash is treated as if it were a vector. The values are sorted and reassigned to sorted order to the integer keys. The behavior is not specified for hashes whose contents do not conform to this convention. Note: was introduced in TXR 238. Prior to that version, behaved like The functions and are caching counterparts of, respectively, and They have exactly the same argument syntax and semantics. Caching refers to eliminating repeated calls to for the same element of in order to reduce the execution time, at the cost of using more storage. The function returns a list of integer indices which indicate the position of the elements of in sorted order. The and arguments behave like those of the function. The object is not modified. The internal sort performed by is not stable. The indices of any elements considered equivalent under may appear in any order in the returned index sequence. Note: the function is inspired by the "grade up" and "grade down" operators in the APL language.
  ;; Order of the 2 3 positions of the "l"
  ;; characters is not specified:
  [grade "Hello"] -> (0 1 2 3 4)
  [grade "Hello" >] -> (4 2 3 1 0) The function pseudorandomly rearranges the elements of This is performed in place: object is modified. The return value is itself. The rearrangement depends on pseudorandom numbers obtained from the function. The argument, if present, is passed to that function. The function supports hash tables in a manner analogous to the way supports hash tables; the same remarks apply as in the description of that function. The function has the same argument requirements and semantics, but differs from in that it avoids in-place modification of a new, shuffled sequence is returned, as if a copy of were made using and then that copy were shuffled in-place and returned. Note: was introduced in TXR 238. Prior to that version, behaved like The and functions rotate the elements of returning a rotated sequence. The function does this destructively; it modifies in-place, whereas returns a new sequence without modifying the original. The function always returns a new sequence. In cases when no rotation is performed, it copies as if using the function. In cases when no rotation is performed, the function returns the original sequence, which is unmodified. The parameter, an integer, has a default value of 1. To rotate elements means to displace their position within the by some amount, that being given by the parameter, while partially preserving their circular order. Circular order means that for the purposes of rotation, the sequence is regarded to be cyclic: the first element of the sequence is considered to be the successor of the last element and vice versa. Thus, when an element is displaced past the first or last position, it wraps to the end or beginning of the sequence. If the sequence is empty, or contains only one element, then and terminate, performing no rotation. The following remarks apply to situations when has two or more elements. The parameter, which may be negative, is first reduced to the smallest positive residue modulo the length of the sequence, resulting in a value ranging from zero to one less than the sequence length. If the resulting value is zero, then no rotation is performed. The has a negative orientation: each element's position is decreased by this amount. Those elements whose position would become negative move to the end of the sequence. The default displacement of 1 causes the first element to become last, the second element to become first, and so forth. The opposite rotation can be obtained using -1 as the displacement. Note: even though operates destructively, the returned object may not be the same object as Only the returned object is required to be the rotated sequence. If this is different from the original input, the contents of that original object are unspecified. Note: the symbol is the name of a place-mutating macro, which is much older than these functions. If is a three-element sequence, then:
  (set S (nrot S)) ;; alternatively: (upd S nrot) has the same effect as:
  (rotate [S 0] [S 1] [S 2])
  (rot "abc") -> "bca"
  (rot #(1 2 3) -1) -> (3 1 2)
  ;; lower-case rot-13
  (mapcar (relate (range #\a #\z)
                  (rot (range #\a #\z) 13))
          "hello, world!")
  -> "uryyb, jbeyq!" The function sorts according to the and arguments, and then breaks the resulting sequence into groups, based on the equivalence of the elements under The differs from in that it is based on the caching rather than The following equivalence holds:
  (sort-group sq lf kf)
  <-->
  (partition-by kf (sort sq kf lf)) Note the reversed order of and arguments between and The function returns a sequence of the same kind as but with duplicates removed. Elements of are considered equal under the function. The first occurrence of each element is retained, and the subsequent duplicates of that element, of any, are suppressed, such that the order of the elements is otherwise preserved. The function is an alias for the one-argument case of That is to say, this equivalence holds:
  (uniq s) <--> (unique s) The function is a generalization of It returns a sequence of the same kind as but with duplicates removed. If neither nor are specified, then elements of sequence are considered equal under the function. The first occurrence of each element is retained, and the subsequent duplicates of that element, of any, are suppressed, such that the order of the elements is otherwise preserved. If is specified, then that function is applied to each element, and the resulting values are compared for equality. In other words, the behavior is as if were the function. If one or more are present, these specify the arguments for the construction of the internal hash table used by The arguments are like those of the function. The function produces a lazy list which represents a reorganization of the elements of into tuples of where must be a positive integer. The length of the sequence might not be evenly divisible by the tuple length. In this case, if a argument is specified, then the last tuple is padded with enough repetitions of to make it have elements. If is not specified, then the last tuple is left shorter than The output of the function is a list, but the tuples themselves are sequences of the same kind as If is any kind of list, they are lists, and not lazy lists.
  (tuples 3 #(1 2 3 4 5 6 7 8) 0) -> (#(1 2 3) #(4 5 6)
                        #(7 8 0))
  (tuples 3 "abc") -> ("abc")
  (tuples 3 "abcd") -> ("abc" "d")
  (tuples 3 "abcd" #\z) -> ("abc" "dzz")
  (tuples 3 (list 1 2) #\z) -> ((1 2 #\z)) The function produces a lazy list of overlapping tuples taken from The length of the tuples is given by the argument. The argument must be a positive integer. Tuples are subsequences of consecutive items from the input beginning with consecutive elements. The first tuple in the returned list begins with the first item of the second tuple begins with the second item, and so forth. The output of the function is a list, but the tuples themselves are sequences of the same kind as If is any kind of list, they are lists, and not lazy lists. If is shorter than then it contains no tuples of that length. In this case, if no argument is specified, then the empty list is returned. In this same situation, if is specified, then a one-element list is returned, consisting of a tuple of the required length, consisting of the elements from followed by repetitions of which must be of a type suitable as an element of the sequence. The is otherwise ignored.
  (tuples* 1 "abc") -> ("a" "b" "c")
  (tuples* 2 "abc") -> ("ab" "bc")
  (tuples* 3 "abc") -> ("abc")
  (tuples* 4 "abc") -> nil
  (tuples* 4 "abc" #) -> ("abcz")
  (tuples* 6 "abc" #) -> ("abczzz")
  (tuples* 6 "abc" 4) -> error
  (tuples* 2 '(a b c)) -> ((a b) (b c))
  (take 3 (tuples* 3 0)) -> ((0 1 2) (1 2 3) (2 3 4)) If is empty, then returns an empty list, and is never called. Otherwise, returns a lazy list of partitions of the sequence Partitions are consecutive, nonempty substrings of of the same kind as The partitioning begins with the first element of being placed into a partition. The subsequent partitioning is done according to which is applied to each element of Whenever, for the next element, the function returns the same value as it returned for the previous element, the element is placed into the same partition. Otherwise, the next element is placed into, and begins, a new partition. The return values of the calls to are compared using the function.
  [partition-by identity '(1 2 3 3 4 4 4 5)] -> ((1) (2) (3 3)
                        (4 4 4) (5))
  (partition-by (op = 3) #(1 2 3 4 5 6 7)) -> (#(1 2) #(3)
                        #(4 5 6 7)) The function separates the sequence into partitions which are identified by the two-argument The principal idea is that successive overlapping pairs from are passed as arguments to and whenever yields true, those elements are identified as belonging to separate partitions: a partitioning division shall take place between them. The detailed semantics is given below, as a procedure. Firstly, if is empty, then returns an empty list, and is never called. Otherwise, returns a lazy list of partitions of Partitions are consecutive, nonempty substrings of of the same kind as The partitioning begins with the first element of being placed into the first partition. The subsequent partitioning is done according to a Boolean which must accept two arguments. Whenever the function yields true, it indicates that a partition is to be terminated and a new partition to begin. The argument, if present, must be a nonnegative integer. It indicates a limit on how many partitions will be delimited; after this limit is reached, the remainder of the sequence is placed into a single partition. After the first element is placed into a partition, the following partition-building process is repeated until the partition is terminated.

    1.  If contains no more elements, then the partition terminates.

    2.  Otherwise, if the is present, and has a value of zero, then the next available element is unconditionally deposited into the current partition, and the process repeats from step 1.

    3.  Otherwise, is invoked on two values: the previous element which has most recently been deposited into the partition, and its successor from

    4.  If returns then the partition continues: the next element is added to the partition, and the process repeats from step 1.

    5.  Otherwise, has returned true and the partition is terminated. In this case, if is present, it is decremented.

    When the current partition is terminated, it is converted to a sequence of the same kind as as if by using the function, and incorporated as the next element of the lazy list of partitions. If, after a partition is thus produced, a next element is available, it is placed into a new partition, and the above partition-building process takes place from step 1. Otherwise, the lazy list terminates.
      ;; Start new partition for unequal characters.
      [partition-if neql "aaaabbcdee"] -> ("aaaa" "bb" "c" "d" "ee")
      ;; As above, but partition only twice
      [partition-if neql "aaaabbcdee" 2] -> ("aaaa" "bb" "cdee")
      ;; Start new partition when non-digit follows digit:
      [partition-if (do and
                      (chr-isdigit @1)
                      (not (chr-isdigit @2)))
                    "a13cd9foo42z"]
      -> ("a13" "cd9" "foo42" "z")
      ;; Place ascending runs of consecutive integers
      ;; into partitions. I.e. start a partition whenever the
      ;; difference from the previous element isn't 1:
      (partition-if (op /= (- @2 @1) 1)
                    '(1 3 4 5 7 8 9 10 9 8 6 5 3 2))
      -> ((1) (3 4 5) (7 8 9 10) (9) (8) (6) (5) (3) (2))
      ;; Place runs of adjacent integers into partitions.
      ;; I.e. start a new partition if the the absolute value of
      ;; the difference from the previous exceeds 1:
      (partition-if (op > (abs (- @2 @1)) 1)
                    '(1 3 4 5 7 8 9 10 9 8 6 5 3 2))
      -> ((1) (3 4 5) (7 8 9 10 9 8) (6 5) (3 2)) Functions in this category perform efficient traversal of sequences. There are two flavors of these functions: functions in the group, and functions in the group. The latter are obsolescent. User-defined iteration is possible via defining special methods on structures. An object supports iteration by defining the special method which is different from the function. This special function returns an iterator object which supports special methods and Two protocols are supported, one of which is more efficient by eliminating the method. Details are specified in the section Special Structure Functions. The function returns an iterator object suitable for traversing the elements of the sequence denoted by the object. If is a list-like sequence, then may return itself as the iterator. Likewise if is a number. If is an iterator produced by then an iterator similar to that iterator is returned, which can continue iterating the same sequence. The iterator may be itself or share state with and thus may not be relied on to produce an independent, parallel iteration. If is a structure which supports the method, then that method is called and its return value is returned. A structure which does not support this method is possibly considered to be a sequence according to the usual criteria, based on whether it supports the or methods. A struct object supporting none of these methods is deemed not iterable. In all other cases, if is iterable, an object of type is returned. Range objects are iterable if they are numeric. A range consisting of two strings may also be iterable, as described below. A range is considered to be a numeric or character range if the element is a number or character. The is then required to be either a value which is comparable with that number or character using the function, or else it must be one of the two objects or either of which indicate that the range is unbounded. In this unbounded range case, the expressions and are equivalent to Numeric ranges are half-open: the value of ascending ranges is excluded, as is the value of descending ranges, so that steps through the values through and steps through the same values in reverse order. A string range consists of two strings of equal length. If the strings are of unequal length, an error exception is thrown. For the range to operate as intended, the strings must meet some additional requirements. If the string is lexicographically lesser than the string, as determined by the function, then the range is ascending, otherwise it is descending. The string range iterates by incrementing (or decrementing, in the case of a descending range) the characters of the string until they are equal to those of the string. The last character has priority. For instance, the range iterates over the strings and The descending range iterates over the same strings, in reverse order. Whenever the incrementing character attains the value of the corresponding character in the string, that character is reset to its starting value, and its left neighbor, if it exists, is incremented instead. If no left neighbor exists, the iteration terminates. Search trees are iterable. Iteration entails an in-order visits of the elements of a tree. A tree iterator created by is also iterable. It is unspecified whether iteration over a object modifies that object to perform the traversal, or whether it uses a copy of the iterator. If is not an iterable object, an error exception is thrown. The function returns if there remain more elements to be traversed. Otherwise it returns The argument must be a valid iterator returned by a call to or The function doesn't change the state of If is the object then is returned. Note: the may return if its argument is or any empty sequence, or an empty range (a range whose and fields are the same number or character). If is a cell, then returns If is a number, then returns This is the case even if calculating the successor of that number isn't possible due to floating-point overflow or insufficient system resources. If is a character, then returns if isn't the highest possible character code, otherwise If was formed from a descending range, meaning that was invoked on a range with a fielding exceeding its value, then returns true while the current iterator value is greater than the the limiting value given by the field. For an ascending range, it returns true if the current iterator value is lower than the limiting value. However, note the peculiar semantics of with regard to descending range iteration. If is a structure, then if it supports an method, then that method is called with no arguments, and its return value is returned. If the structure does not have an method, then is returned. If the function indicates that more items remain to be visited, then the next item can be retrieved using The argument must be a valid iterator returned by a call to or The function doesn't change the state of If is invoked on an iterator which indicates that no more items remain to be visited, the return value is If is a cell, then returns the field of that cell. If is a character or number, then returns that character or number itself. If is based on an ascending numeric or character range, then returns the current iteration value, which is initialized by as a copy of the range's field. Thus, the range traverses the values and excluding the If is based on a descending numeric or character range, then returns the predecessor of the current iteration value, which is initialized as a copy of the range's field. Thus, the range traverses the values and excluding the exactly the same values are visited as for the range only in reverse order. If is a structure which supports the method, then that method is called and its return value is returned. If the function indicates that more items remain to be visited, then the function may be used to consume the next item. The function returns an iterator denoting the traversal of the remaining items in the sequence. The argument must be a valid iterator returned by a call to or The function may return a new object, in which case it avoids changing the state of or else it may change the state of and return it. If the application discontinues the use of and continues the traversal using the returned iterator, it will work correctly in either situation. If is invoked on an iterator which indicates that no more items remain to be visited, the return value is unspecified. If is a cell, then returns the field of that cell. That value must itself be a or else otherwise an error is thrown. This is to prevent iteration from wrongly iterating into the non-null terminators of improper lists. Without this rule, iteration of a list like would reach the cell at which point a subsequent would return the field But that value is a valid iterator which will then continue by stepping through and so on. If is a list-like sequence, then is invoked on it and that value is returned. The value must also be a list-like sequence, or else The reasoning for this is the same as for the similar restriction imposed in the case when is a If is a character or number, then returns its successor, as if using the function. If is a structure which supports the method, then that method is called and its return value is returned. The function returns an iterator object specialized for the task of traversing the sequence If it is possible for to be that object, then the function may adjust the state of and return it. If doesn't use then it behaves exactly like being invoked on If is a structure which supports the method, then that method is called and its return value is returned. Note the reversed arguments. The method is of the object, not of That is to say, the call results in the call. If is a structure which doesn't support then is ignored, is invoked on and the result is returned. The obsolescent function returns an iterator object specialized to the task of traversing the sequence represented by the input If isn't a sequence, an exception is thrown. Note that if is a lazy list, the returned iterator maintains a reference to the head of that list during the traversal; therefore, generic iteration based on iterators from is not suitable for indefinite iteration over infinite lists. The obsolescent function retrieves the next available item from the sequence iterated by which must be an object returned by If the sequence has no more items to be traversed, then is returned instead. Note: to avoid ambiguities, the application should provide an which is guaranteed distinct from any item in the sequence, such as a freshly allocated object. The obsolescent reinitializes the existing iterator object to begin a new traversal over the given which must be a value of a kind that would be a suitable argument for The function returns TXR Lisp provides an a structure type called which encapsulates state and methods for constructing lists procedurally. Among the advantages of using is that lists can be constructed in the left-to-right direction without requiring multiple traversals or reversal. For example, naturally combines with iteration or recursion: items visited in an iterative or recursive process can be collected easily using in the order they are visited. The type provides methods for adding and removing items at either end of the list, making it suitable where a dequeue structure is required. The basic workflow begins with the instantiation of a object. This object may be initialized with a piece of list material which begins the to-be-constructed list, or it may be initialized to begin with an empty list. Methods such as and are invoked on this object to extend the list with new elements. At any point, the list constructed so far is available using the method, which is also how the final version of the list is eventually retrieved. The methods which add material to the list all return the list builder, making chaining possible.
       (new list-builder).(add 1).(add 2).(pend '(3 4 5)).(get)
       -> (1 2 3 4 5) The macro is provided which syntactically streamlines the process. It implicitly creates a instance and binds it to a hidden lexical variable. It then evaluates forms in a lexical scope in which shorthand macros are available for building the list. The structure encapsulates the state for a list building process. Programs should use the function for creating an instance of The and slots should be regarded as internal variables. The function instantiates and returns an object of struct type If no argument is supplied, then the object is implicitly initialized with an empty list. If the argument is supplied, then it is equivalent to calling without an argument to produce an object by invoking the method call on this object. The object produced by the expression is installed (without being copied) into the object as the prefix of the list to be constructed. The argument can be a sequence other than a list.
       ;; build the list (a b) trivially
       (let ((lb (build-list '(a b))))
         lb.(get)
       -> (a b) The and methods extend the list being constructed by a object by adding individual elements to it. The method adds elements at the tail of the list, whereas adds elements at the front. These methods return the object. The precise semantics is as follows. All of the arguments are combined into a list as if by the function, and the resulting list combined with the current contents of the object as if using the function. The resulting list becomes the new contents.
      ;; Build the list (1 2 3 4)
      (let ((lb (build-list)))
        lb.(add 3 4)
        lb.(add* 1 2)
        lb.(get))
      -> (1 2 3 4)
      ;; Add "c" to "abc"
      ;; same semantics as (append "abc" #\c)
      (let ((lb (build-list "ab")))
        lb.(add #\c)
        lb.(get))
      -> "abc" The and methods extend the list being constructed by a object by adding lists to it. The method catenates the arguments together as if by the function, then appends the resulting list to the end of the list being constructed. The method is similar, except it prepends the catenated lists to the front of the list being constructed. The and operations do not mutate the input lists, but may cause the resulting list to share structure with the input lists. These functions may mutate the list already contained in however, they avoid mutating those parts of the current list that are shared with inputs that were given in earlier calls to these functions. These methods return the object.
      ;; Build the list (1 2 3 4)
      (let ((lb (build-list)))
        lb.(pend '(3 4))
        lb.(pend* '(1 2))
        lb.(get))
      -> (1 2 3 4) The and methods extend the list being constructed by a object by adding lists to it. The method destructively catenates the arguments as if by the function. The resulting list is appended to the list being constructed. The method is similar, except it prepends the catenated lists to the front of the list being constructed. These methods may destructively manipulate the list already contained in the object, and likewise may destructively manipulate the input lists. They may cause the list being constructed to share substructure with the input lists. Additionally, these methods may destructively manipulate the list already contained in the object without regard for shared structure between that list and inputs given earlier any of the or functions. The function can be called with a single argument which is an atom. This atom will simply be installed as the terminating atom of the list being constructed, if the current list is an ordinary list. These methods return the object.
      ;; Build the list (1 2 3 4 . 5)
      (let ((lb (build-list)))
        lb.(ncon* (list 1 2))
        lb.(ncon (list 3 4))
        lb.(ncon 5)
        lb.(get))
      -> (1 2 3 4 . 5) The method discards the list constructed so far, optionally replacing it with new material. The method catenates the arguments together as if by the function. The resulting list, which is empty if there are no arguments, then replaces the object's list constructed so far. The method returns the object.
      ;; Build the list (3 4) by first building (1 2),
      ;; then discarding that and adding 3 and 4:
      (let ((lb (build-list)))
        lb.(add 1 2)
        lb.(oust)
        lb.(add 3 4)
        lb.(get))
      -> (3 4)
      ;; Build the list (3 4 5 6) by first building (1 2),
      ;; then replacing with catenation of (3 4) and (5 6):
      (let ((lb (build-list)))
        lb.(pend '(1 2))
        lb.(oust '(3 4) '(5 6))
        lb.(get))
      -> (3 4 5 6) The method retrieves the list constructed so far by a object. It doesn't change the state of the object. The retrieved list may be passed as an argument into the construction methods on the same object.
      ;; Build the circular list (1 1 1 1 ...)
      ;; by appending (1) to itself destructively:
      (let ((lb (build-list '(1))))
        lb.(ncon* lb.(get))
        lb.(get))
      -> (1 1 1 1 ...)
      ;; build the list (1 2 1 2 1 2 1 2)
      ;; by doubling (1 2) twice:
      (let ((lb (build-list)))
        lb.(add 1 2)
        lb.(pend lb.(get))
        lb.(pend lb.(get))
        lb.(get))
      -> (1 2 1 2 1 2 1 2) The and methods each remove an element from the list and return it. If the list is empty, they return The method removes an element from the front of the list, whereas removes an element from the end of the list. Note: this orientation is opposite to and Thus pairs with to produce FIFO queuing behavior. The and macros provide a shorthand notation for constructing lists using the structure. They eliminate the explicit call to the function to construct the object, and eliminate the explicit references to the object. Both of these macros create a lexical environment in which a object is implicitly constructed and bound to a hidden variable. This lexical environment also provides local functions named and which mimic the methods, but operate implicitly on this hidden variable, so that the object need not be mentioned as an argument. With the exception of and the local functions return unlike like the same-named methods, which return the object. In this lexical environment, each is evaluated in order. When the last is evaluated, returns the constructed list, whereas returns the value of the last If no forms are enclosed, both macros return Note: because the local function has the same name as a global macro, it is implemented as a Inside a or if is invoked with no arguments, then it denotes a call to the method. If invoked with an argument, then it resolves to the global macro for deleting a place.
      ;; Build the circular list (1 1 1 1 ...)
      ;; by appending (1) to itself destructively:
      (build
        (add 1)
        (ncon* (get))) -> (1 1 1 1 ...)
      ;; build the list (1 2 1 2 1 2 1 2)
      ;; by doubling (1 2) twice:
      (build
        (add 1 2)
        (pend (get))
        (pend (get))) -> (1 2 1 2 1 2 1 2)
      ;; build a list by mapping over the local
      ;; add function:
      (build [mapdo add (range 1 3)]) -> (1 2 3)
      ;; breadth-first traversal of nested list;
      (defun bf-map (tree visit-fn)
        (buildn
          (add tree)
          (whilet ((item (del)))
            (if (atom item)
              [visit-fn item]
              (each ((el item))
                (add el))))))
      (let (flat)
        (bf-map '(1 (2 (3 4 (5))) ((6 7) 8)) (do push @1 flat))
         (nreverse flat))
      -> (1 2 8 3 4 6 7 5) The function returns a lazy list which consists of all length permutations of formed by items taken from The permutations do not use any element of more than once. Argument if present, must be a positive integer, and must be a sequence. If is not present, then its value defaults to the length of the list of the full permutations of the entire sequence is returned. The permutations in the returned list are sequences of the same kind as If is zero, then a list containing one permutation is returned, and that permutation is of zero length. If exceeds the length of then an empty list is returned, since it is impossible to make a single nonrepeating permutation that requires more items than are available. The permutations are lexicographically ordered. The function returns a lazy list which consists of all the repeating permutations of length formed by items taken from "Repeating" means that the items from can appear more than once in the permutations. The permutations which are returned are sequences of the same kind as Argument must be a nonnegative integer, and must be a sequence. If is zero, then a single permutation is returned, of zero length. This is true regardless of whether is itself empty. If is empty and is greater than zero, then no permutations are returned, since permutations of a positive length require items, and the sequence has no items. Thus there exist no such permutations. The first permutation consists of repetitions of the first element of The next repetition, if there is one, differs from the first repetition in that its last element is the second element of That is to say, the permutations are lexicographically ordered.
      (rperm "01" 3) -> ("000" "001" "010" "011"
                         "100" "101" "110" "111")
      (rperm #(1) 3) -> (#(1 1 1))
      (rperm '(0 1 2) 2) -> ((0 0) (0 1) (0 2) (1 0)
                            (1 1) (1 2) (2 0) (2 1) (2 2)) The function returns a lazy list which consists of all length nonrepeating combinations formed by taking items taken from "Nonrepeating combinations" means that the combinations do not use any element of more than once. If contains no duplicates, then the combinations contain no duplicates. Argument must be a nonnegative integer, and must be a sequence or a hash table. The combinations in the returned list are objects of the same kind as If is zero, then a list containing one combination is returned, and that combination is of zero length. If exceeds the number of elements in then an empty list is returned, since it is impossible to make a single nonrepeating combination that requires more items than are available. If is a sequence, the returned combinations are lexicographically ordered. This requirement is not applicable when is a hash table.
       ;; powerset function, in terms of comb.
       ;; Yields a lazy list of all subsets of s,
       ;; expressed as sequences of the same type as s.
       (defun powerset (s)
         (mappend* (op comb s) (range 0 (length s)))) The function returns a lazy list which consists of all length repeating combinations formed by taking items taken from "Repeating combinations" means that the combinations can use an element of more than once. Argument must be a nonnegative integer, and must be a sequence. The combinations in the returned list are sequences of the same kind as If is zero, then a list containing one combination is returned, and that combination is of zero length. This is true even if is empty. If is empty, and is nonzero, then an empty list is returned. The combinations are lexicographically ordered. Because TXR Lisp supports structural macros, TXR processes TXR Lisp expressions in two separate phases: the expansion phase and the evaluation/compilation phase. During the expansion phase, a top-level expression is recursively traversed, and all macro invocations in it are expanded. The result is a transformed expression which contains only function calls and invocations of special operators. This expanded form is then evaluated or compiled, depending on the situation. Macro invocations are compound forms and whose operator symbol has a macro definition in scope. A macro definition is a kind of function which operates on syntax during macro-expansion, called upon to calculate a transformation of the syntax. The return value of a macro replaces its invocation, and is traversed to look for more opportunities for macro expansion. Macros differ from ordinary functions in three ways: they are called at macro-expansion time, they receive pieces of unevaluated syntax as their arguments, and their parameter lists are macro parameter lists which support destructuring, as well as certain special parameters. TXR Lisp also supports symbol macros. A symbol macro definition associates a symbol with an expansion. When that symbol appears as a form, the macro-expander replaces it with the expansion. TXR source files are treated somewhat differently with regard to macro expansion compared to TXR Lisp. When TXR Lisp forms are read from a file by or or read by the interactive listener, each form is expanded and evaluated or compiled before the subsequent form is processed. In contrast, when a TXR file is loaded, expansion of the Lisp forms are its arguments takes place during the parsing of the entire source file, and is complete for the entire file before any of the code is executed. TXR macros support destructuring, similarly to Common Lisp macros. This means that macro parameter lists are like function argument lists, but support nesting. A macro parameter list can specify a nested parameter list in every place where an argument symbol may appear. For instance, consider this macro parameter list:
      ((a (b c)) : (c frm) ((d e) frm2 de-p) . g) The top-level of this nested form has the structure in which we can identify the major constituent positions as and The constituent at position is the mandatory parameter Position holds the optional parameter (with default init form At is found the optional parameter (with default init form and presence-indicating variable Finally, the parameter in the dot position is which captures trailing arguments. Obviously, some of the parameters are compound expressions rather than symbols: and These compounds express nested macro parameter lists. Starting in TXR 285, the symbol can be used in a macro parameter list in place of a parameter name. This indicates that an object is expected at that position in the corresponding structure, but no variable will be bound. For completeness, the symbol may also be used for a presence-indicating variable. When the name of an optional parameter is specified as and the corresponding structure is missing, the expression, if present, is still evaluated under the same circumstances as it would if a variable were present. Nested macro parameter lists recursively match the corresponding structure in the argument object. For instance if a simple argument would capture the structure then we can replace the argument with the nested argument list which destructures the such that the parameters and will end up bound to and respectively. Nested macro parameter lists have all the features of the top-level macro parameter lists: they can have optional arguments with default values, use the dotted position, and contain the and special parameters, which are described below. In nested parameter lists, the binding strictness is relaxed for optional parameters. If is optional, and the argument is, say, then gets and and receive Macro parameter lists also supports three special keywords, namely and The parameter list will bind parameter to the entire macro parameter list, bind parameter to the macro environment and bind parameter to the entire macro form (the original compound form used to invoke the macro). The and notations can occur anywhere in a macro parameter list, other than to the right of the consing dot. They can be used in nested macro parameter lists also. Note that in a nested macro parameter list, and do not change meaning: they bind the same object as they would in the top-level of the macro parameter list. However the parameter inside has a restricted scope in a nested parameter list: its parameter will capture just that part of the argument material which matches that parameter list, rather than the entire argument list. The processing of macro parameter lists omits the feature that when the (colon) keyword symbol is given as the argument to an optional parameter, that argument is treated as a missing argument. This special logic is implemented only in the function argument passing mechanism, not in the binding of macro parameters to object structure. If the colon symbol appears in the object structure and is matched against an optional parameter, it is an ordinary value. That parameter is considered present, and takes on the colon symbol as its value. In ANSI Common Lisp, the lambda list keyword binds its corresponding variable to the entire macro form, whereas TXR Lisp's binds its variable only to the arguments of the macro form. Note, however, that ANSI CL distinguishes between destructuring and macro lambda lists, and the parameter has a different behavior in each. Under the parameter receives just the arguments, just like the behavior of TXR Lisp's parameter. TXR Lisp does not distinguish between destructuring and macro lambda lists; they are the same and behave the same way. Thus is treated the same way in macros as in and related binding operators: it binds just the arguments to the parameter. TXR Lisp has the special parameter by means of which macros can access their invoking form. This parameter is also supported in and binds to the entire form. ANSI CL doesn't support the convention that the symbol may appear instead of a parameter symbol to suppress the binding of a variable. The following description omits the treatment of top-level forms by and the compiler. This is described, respectively, in the description of and the section Top-Level Forms inside the LISP COMPILATION chapter. Certain other details are also omitted, such as the dynamic evolution of the macro-time environment, the expansion of macrolet forms. Macro expansion is, generally speaking, a recursive process. The expression to be expanded is classified into cases, and as necessary, the constituent expressions are recursively expanded, depending on these cases. Certain aspects of the process may be regarded as iterative. Macro expansion maintains a macro-time lexical environment which is extended and contracted as the expander descends into various nested binding constructs. The expander may encounter a bindable symbol. If such a symbol has a binding as a symbol macro, then it is replaced by its expansion, and the expander iterates on the resulting form. The form may be another object, including a symbol. If it is the same symbol, than macro expansion terminates; the symbol remains unsubstituted. Symbols are treated differently by the expander if they are in the Lisp-1-style context of the operator, or the equivalent square bracket notation. The expander takes into consideration the semantics of the combined function and variable namespace. The expander may encounter a compound form headed by a symbol which has a macro binding. In this situation, the macro expander function is called, and the form is replaced by the resulting form. That form is considered again as a potential macro. In any case, the expander makes a note that it has expanded a macro, If a form isn't a macro, then it's either a function call, special from or an atomic form: a symbol (that has no binding as a symbol macro) or other atom. The interesting cases are special forms and function calls, since the atomic forms are simply returned as-is without expansion. Special forms and function call forms contain other forms, some or all of which require expansion. The expander recognizes the shape of each special form or function call, pulls out the constituent expressions and expands them recursively, combining the results into a new version of the special form or function call form. Because TXR Lisp allows the same symbol to have a macro and function binding, the expander allows for interplay between the two, which produces useful behaviors. Recall from two paragraphs ago that whenever the expander expands a macro, it makes a note that it has done so. Subsequently, suppose that the rounds of macro expansion happen to terminate in such a way that the result is a function call form. The form's constituents are expanded, If the expansion of those constituents produces any change, then the resulting replacement function call form is again examined for the possibility that it may be a macro. This special requirement, not typically implemented by Lisp macro expanders, greatly simplifies the writing of macros which provide algebraic optimizations of function calls. An example follows to illustrate the benefit of the rule. Note that the example involves some simple macros which change the number of times that an argument expression is evaluated. A more careful handling of this issue is omitted in order to keep the examples simple. Suppose a macro is written for the function like this:
      (defmacro sqrt (:match :form f)
        (((* @exp @exp)) exp)
        (@else f)) The macro uses pattern matching to recognize cases like when the argument is a product expression with two identical terms. This pattern implements the arithmetic identity that the positive square root of a real term multiplied by itself is just that term. Now suppose that a similar macro is written to optimize a certain case of the function:
      (defmacro expt (:match :form f)
        ((@exp 2) ^(* ,exp ,exp))
        (@else f)) This macro recognizes when the argument is being squared, turning into a strength reduction from exponentiation to multiplication. What if the following expression is then written:
       (sqrt (expt x 2)) The special provision in the expander algorithm allows the above combination to reduce to just as follows. Firstly, the expression is treated as a macro call. It doesn't match the main case in the macro, only the fallback case which returns the form unexpanded. The expander notes that it has invoked a macro, and then proceeds to treat the form as a function call. The function call's argument expression is expanded as a macro. This produces a transformation: our macro reduces this quadratic term to Here is where the special rule comes into play. The expander sees that the function's arguments have been transformed. It knows that the original function call was the result of expansion. To promote more opportunities for expansion, it tries the transformed function call again as a macro. The form is handed to the macro, which this time has a match for the argument pattern, reducing the entire form to Effectively, the macro has the opportunity to work with both the unexpanded argument syntax as well as its expanded version. It is first offered the one, and when it declines to expand, then the other. The operator is evaluated at expansion time. It defines a macro-expander function under the name effectively creating a new operator. Note that the above syntax synopsis describes only the canonical parameter syntax which remains after parameter list macros are expanded. See the section Parameter List Macros. Note that the parameter list is a macro parameter list, and not a function parameter list. This means that each and can be not only a symbol, but it can itself be a parameter list. The corresponding argument is then treated as a structure which matches that parameter list. This nesting of parameter lists can be carried to an arbitrary depth. A macro is called like any other operator, and resembles a function. Unlike in a function call, the macro receives the argument expressions themselves, rather than their values. Therefore it operates on syntax rather than on values. Also, unlike a function call, a macro call occurs in the expansion phase, rather than the evaluation phase. The return value of the macro is the macro expansion. It is substituted in place of the entire macro call form. That form is then expanded again; it may itself be another macro call, or contain more macro calls. A global macro defined using may decline to expand a macro form. Declining to expand is achieved by returning the original unexpanded form, which may be captured using the parameter. When a global macro declines to expand a form, the form is taken as-is. At evaluation time, it will be treated as a function call. Note: when a local macro defined by declines, more complicated requirements apply; see the description of A macro in the global namespace introduced by may coexist with a function of the same name introduced by This is not permitted in ANSI Common Lisp. ANSI Common Lisp doesn't describe the concept of declining to expand, except in the area of compiler macros. Since TXR Lisp allows global macros and functions of the same name to coexist, ordinary macros can be used to optimize functions in a manner similar to Common Lisp compiler macros. A macro can be written of the same name as a function, and can optimize certain cases of the function call by expanding them to some alternative syntax. Cases which it doesn't optimize are handled by declining to expand, in which case the form remains as the original function call.
      ;; dolist macro similar to Common Lisp's:
      ;;
      ;; The following will print 1, 2 and 3
      ;; on separate lines:
      ;; and return 42.
      ;;
      ;; (dolist (x '(1 2 3) 42)
      ;; (format t "~s\n" x))
      (defmacro dolist ((var list : result) . body)
        (let ((i (gensym)))
          ^(for ((,i ,list)) (,i ,result) ((set ,i (cdr ,i)))
             (let ((,var (car ,i)))
               ,*body)))) The binding operator extends the macro-time lexical environment by making zero or more new local macros visible. The symbol is followed by a list of macro definitions. Each definition is a form which begins with a followed by which is a macro parameter list, and zero or more These macro definitions are similar to those globally defined by the operator, except that they are in a local environment. The macro definitions are followed by optional The macros specified in the definitions are visible to these forms. Forms inside the macro definitions such as the and initializer forms appearing in the are subject to macro-expansion in a scope in which none of the new macros being defined are yet visible. Once the macro definitions are themselves macro-expanded, they are placed into a new macro environment, which is then used for macro expanding the A form is fully processed in the expansion phase of a form, and is effectively replaced by form which contains expanded versions of This expanded structure shows no evidence that any macrolet forms ever existed in it. Therefore, it is impossible for the code evaluated in the bodies and parameter lists of macros to have any visibility to any surrounding lexical variable bindings, which are only instantiated in the evaluation phase, after expansion is done and macros no longer exist. A local macro defined using may decline to expand a macro form. Declining to expand is achieved by returning the original unexpanded form, which may be captured using the parameter. When a local macro declines to expand a form, the macro definition is temporarily hidden, as if it didn't exist in the lexical scope. If another macro of the same name is thereby revealed (a global macro or another local macro at a shallower nesting level), then an expansion is tried with that macro. If no such macro is revealed, or if a lexical function binding of that name is revealed, then no expansion takes place; the original form is taken as-is. When another macro is tried, the process repeats, resulting in a search which proceeds as far as possible through outer lexical scopes and finally the global scope. The function returns if represents the syntax of a form which is a macro form: either a compound macro or a symbol macro. Otherwise it returns A macro form is one that will transform under or an object which isn't a macro form will not undergo expansion. The optional parameter is a macroexpansion environment. A macroexpansion environment is passed down to macros and can be received via their special parameter. is used by to determine whether is a macro in a lexical macro environment. If is not specified or is then only recognizes global macros.
      ;; macro which translates to 'yes if its
      ;; argument is a macro from, or otherwise
      ;; transforms to the form 'no.
      (defmacro ismacro (:env menv form)
        (if (macro-form-p form menv)
         ''yes ''no))
      (macrolet ((local ()))
        (ismacro (local))) ;; yields yes
      (ismacro (local)) ;; yields no
      (ismacro (ismacro foo)) ;; yields yes During macro expansion, the global macro is handed the macro-expansion environment via When the macro is invoked within the macrolet, this environment includes the macro-time lexical scope in which the macro is defined. So when global checks whether the argument form is a macro, the conclusion is yes: the (local) form is a macro call in that environment: yields When is invoked outside of the macrolet, no local macro is visible is there, and so yields If is a macro form (an object for which returns these functions expand the macro form and return the expanded form. Otherwise, they return performs a single expansion, expanding just the macro that is referenced by the symbol in the first position of and returns the expansion. That expansion may itself be a macro form. performs an expansion similar to If the result is a macro form, then it expands that form, and keeps repeating this process until the expansion yields a non-macro-form. That non-macro-form is then returned. The optional parameter is a macroexpansion environment. A macroexpansion environment is passed down to macros and can be received via their special parameter. The environment they receive is their lexically apparent macro-time environment in which local macros may be visible. A macro can use this environment to "manually" expand some form in the context of that environment.
      ;; (rem-num x) expands x, and if x begins with a number,
      ;; it removes the number and returns the resulting
      ;; form. Otherwise, it returns the entire form.
      (defmacro rem-num (:env menv some-form)
        (let ((expanded (macroexpand some-form menv)))
          (if (numberp (car expanded))
            (cdr expanded)
            some-form)))
      (macrolet ((foo () '(1 list 42))
                 (bar () '(list 'a)))
        (list (rem-num (foo)) (rem-num (bar))))
      --> ((42) (a)) The macro is able to expand the and forms it receives as the argument, even though these forms use local macro that are only visible in their local scope. This is thanks to the macro environment passed to It is correctly able to work with the expansions and to produce and which evaluate to and respectively. The and functions closely resemble, respectively, and The argument and return value syntax and semantics is almost identical, except for one difference. These functions consider argument to be syntax in a Lisp-1 evaluation context, such as any argument position of the operator, or the equivalent DWIM Brackets notation. This makes a difference because in a Lisp-1 evaluation context, an inner function binding is able to shadow an outer symbol macro binding of the same name. The requirements about this language area are given in more detail in the description of the operator. Note: the function is useful to the implementor of a macro whose semantics requires one or more argument forms to be treated in a Lisp-1 context, in situations when such a macro needs to itself expand the material, rather than merely insert it as-is into the output code template. The functions and both perform a complete expansion of in the macro-environment and return that expansion. If is omitted, the expansion takes place in the global environment in which only global macros are visible. The returned object is a structure that is devoid of any macro calls. Also, all and blocks in form are removed in the returned structure, replaced by their fully expanded bodies. The difference between and is that suppresses expansion-time deferred warnings (exceptions of type issued for unbound variables or functions. To suppress a warning means to intercept the warning exception with a handler which throws a exception to resume processing. What this requirement means is that if unbound functions or variables occur in the being expanded by expand, the warning is effectively squelched. Rationale: is may be used by macros for expanding fragments which contain references to variables or functions which are not defined in those fragments. The form performs a full expansion of as if by the function and returns a list containing that expansion, plus four additional items which provide information about variable and function references which occur in If both and are provided, then it is expected that is lexically nested within Note: it is not required that be the immediate parent of Note: a common usage situation is that is the environment of the invocation of a "parent" macro which generates a form that contains local macros. The bodies of those local macros use specifying their own environment as and that of their generating "parent" as In detail, the five items of the returned list are whose descriptions are:
    The full expansion of containing no macro invocations, or or forms. A list of the free variables which occur in relative to the environment. That is to say, variables that are not bound inside and are not also bound in If is omitted, then these are the absolutely free variables occurring in Exactly like but informing about function bindings rather than variables. A list of the variables which which occur in which would be free if the environments between and (including the former, excluding the latter) were removed from consideration. A more detailed description of this semantics is given below. If is omitted, then these are the absolutely free variables occurring in ignoring the Exactly like but informing about function bindings rather than variables.

    The semantics of the treatment of and in the calculation of and is as follows. A new environment is calculated from these two environments, and is expanded in this environment. Variables and functions occurring in which are not bound in are listed as and This is calculated as follows. First is initialized as a copy of Then, all environments below down to are examined for bindings which shadow bindings in Those shadows are removed from Therefore, what remains in are those bindings from that are not shadowed by the environments between and Within each of the lists of variables returned by the order of the variables is not specified. Suppose that is a macro which somehow has access to the two indicated lexical environments in the following code snippet:
      (let (a c) ;; <- outer-env
        (let (b)
          (let (c) ;; <- inner-env
            (mac (list a b c d))))) Suppose that invokes the function, passing in the argument form as and two macro-time environment objects corresponding to the indicated environments. Then the following object shall be a correct return value of
      ((list a b c d) (d) nil (d c b) nil) A complete code example of this is given below. Other correct return values are possible due to permitted variations in the order of the variables within the four lists. For instance, instead of the list may appear. The list is because this is the only variable that occurs in which is free with regard to The and variables are not listed because they appear bound inside The reported list is because the form is considered against which is formed by removing the shadowing bindings from The difference between and is and so the form is considered in an environment containing the binding which leaves free. The following is a complete code sample demonstrating the above descriptions:
      ;; Given this macro:
      (defmacro bigmac (:env out-env big-form)
        ^(macrolet ((mac (:env in-env little-form)
                          ^',(expand-with-free-refs
                            little-form in-env ,out-env)))
          ,big-form))
      (let (a c) ;; <- outer-env, surrounding bigmac
        (bigmac
          (let (b)
            (let (c) ;; <- inner-env, surrounding mac
              (mac (list a b c d))))))
      --> ((list a b c d) (d) nil (d c b) nil) Note: this information is useful because a set difference can be calculated between the two reported sets. The set difference between the variables and the variables is That set difference is significant because it precisely informs about the bound variables which occur in which appear bound in but are not bound due to a binding coming from In the above example, these are the variables enclosed in the macro, but external to the inner macro. The variable is not listed in because it is not a bound variable. The variable is not in because though it is bound in that binding comes from The upshot of this logic is that it allows a macro to inspect a form in order to discover the identities of the variables and functions which are used inside that form, whose definitions come from a specific, bounded scope surrounding that form. These four functions are useful to macro writers. They are intended to be called from the bodies of macro expanders, such as the bodies of or forms. The argument is a macro-time environment, which is available to macros via the special parameter. Using these functions, a macro can enquire whether a given is, respectively, a symbol which has a variable binding, a function binding, a symbol macro (defined by or a macro (defined by in the environment of the macro's invocation. This information is known during macro expansion. The macro expander recognizes lexical function and variable bindings, because these bindings can shadow macros. Special variables are not lexical. The function returns if satisfies function, indicating that it is the name of a special variable. The function also returns for global lexical variables. If is a symbol for which only a global lexical variable binding is apparent, returns Testing for the existence for a global variable can be done using if a symbol is but not then it is a global lexical variable. Similarly, returns for global functions, returns for global symbol macros and returns for global macros.
      ;;
      ;; this macro replaces itself with :lexical-var if its
      ;; argument is a lexical variable, :lexical-fun if
      ;; its argument is a lexical function, or with
      ;; :not-lex-fun-var if neither is the case.
      ;;
      (defmacro classify (sym :env e)
        (cond
          ((lexical-var-p e sym) :lexical-var)
          ((lexical-fun-p e sym) :lexical-fun)
          (t :not-lex-fun-var)))
      ;;
      ;; Use classify macro above to report classification
      ;; of the x, y and f symbols in the given scope
      ;;
      (let ((x 1) (y 2))
        (symacrolet ((y x))
          (flet ((f () (+ 2 2)))
            (list (classify x) (classify y) (classify f)))))
      --> (:lexical-var :not-lex-fun-var :lexical-fun)
      ;; Locally bound specials are not lexical
      (let ((*stdout* *stdnull*))
        (classify *stdout*))
      --> :not-lex-fun-var The function inspects the macro-time environment to determine what kind of binding, if any, does have in the the variable namespace of that environment. If the innermost binding for is a variable binding, then either is returned if the variable is lexical, otherwise is returned if the variable is special. If the innermost binding for is a symbol macro, then is returned. In all other cases, is returned. The function does not consider global symbol macros or global lexical variables. The function inspects the macro-time environment to determine what kind of binding, if any, does have in the the function namespace of that environment. If the innermost binding for is a function binding, then is returned. If the innermost binding for is a macro, then is returned. In all other cases, is returned. The function does not consider global macros or functions. The function inspects the macro-time environment to determine what kind of binding, if any, does have in that environment, from a Lisp-1 perspective. That is to say, it considers function bindings, variable bindings and symbol macro bindings to be in a single name space and finds the innermost binding of one of these types for If such a binding is found, then the function returns one of the three keyword symbols or If no such lexical binding is found, then the function returns Note that is never returned for a special variable, but such a variable can be shadowed by a symbol macro, in which case is returned. Note that a return doesn't mean that the symbol doesn't have a lexical binding. It could have an operator macro lexical binding (a macro binding in the function namespace established by Unlike the function, the function never returns because Lisp-1-style evaluation of symbols is blind to the existence of macros, other than symbol macros. A form introduces a symbol macro. A symbol macro consists of a binding between a symbol and and a The binding denotes the form itself, rather than its value. The argument is not subject to macro expansion; it is associated with in its unexpanded state, as it appears in the form. The form must be evaluated for its defining to take place; therefore, the definition is not available in the top-level form which contains the invocation; it becomes available to a subsequent top-level form. Subsequent to the evaluation of the definition, whenever the macro expander encounters sym as a form, it replaces it by After this replacement takes place, itself is then processed for further replacement of macros and symbol macros. Symbol macros are also recognized in contexts where denotes a place which is the target of an assignment operation like and similar. Note: if a symbol macro expands to itself directly, expansion stops. However, if a symbol macro expands to itself through a chain of expansions, runaway expansion-time recursion will occur. If a global variable exists by the name then first removes that variable from the global environment, and if that variable is special, the symbol's special marking is removed. doesn't alter the dynamic binding of a special variable. Any such a binding remains intact. If is evaluated in a scope in which there is any lexical or dynamic binding of in the variable namespace, whether as a variable or macro, the global symbol macro is shadowed by that binding. The operator binds local, lexically scoped macros that are similar to the global symbol macros introduced by Each in the bindings list is bound to its corresponding form, creating a new extension of the expansion-time lexical macro environment. Each is subsequently macro-expanded in this new environment in which the new symbol macros are visible. Note: ordinary lexical bindings such as those introduced by let or by function parameters lists shadow symbol macros. If a symbol is bound by nested instances of and a then the scope enclosed by both constructs will see whichever of the two bindings is more inner, even though the bindings are active in completely separate phases of processing. From the perspective of the arguments of a form, lexical function bindings also shadow symbol macros. This is consistent with the Lisp-1-style name resolution which applies inside a form. Lexical operator macros do not shadow symbol macros under any circumstances. The macro binds lexically scoped symbol macros in such a way that they behave as aliases for places denoted by place forms. Each must be an expression denoting a syntactic place. The corresponding is established as an alias for the storage location which that place denotes, over the scope of the This binding takes place in such a way that each is evaluated exactly once, only in order to determine its storage location. The corresponding then serves as an alias for that location, over the scope of the This means that whenever is evaluated, it stands for the value of the storage location, and whenever a value is apparently stored into it is actually the storage location which receives it. The variant implements an alternative scoping rule, which allows a later form to refer to a bound to an earlier form. In other words, a given binding is visible not only to the but also to forms which occur later. Note: certain kinds of places, notably expressions, must be accessed before they can be stored, and this restriction continues to hold when those places are accessed through aliases. Note: differs from in that the forms themselves are not aliased, but the storage locations which they denote. performs the syntactic substitution of symbol by form wherever appears inside as an evaluated form, and is not shadowed by any inner binding. Whereas generates code which arranges for to be evaluated to a storage location, and syntactically replaces occurrences of with a form which directly denotes that storage location, wherever appears inside as an evaluated form, and is not shadowed by any inner binding. Also, is not necessarily substituted by a single, fixed form, as in the case of Rather it may be substituted by one kind of form when it is treated as a pure value, and another kind of form when it is treated as a place. Note: multiple accesses to an alias created by denote multiple accesses to the aliased storage location. That can mean multiple function calls or array indexing operations and such. If the target of the alias is instead of then a single access occurs to fetch the prior value of and stored into a hidden variable. All of the multiple occurrences of the alias then simply retrieve this cached prior value from the hidden variable, rather than accessing the place. The macro is independent of and separately documented. Implementation of using
      (defmacro inc (place : (delta 1))
        (with-gensyms (p)
          ^(placelet ((,p ,place))
             (set ,p (+ ,p ,delta))))) The gensym is used to avoid accidental capture of references emanating from the form. The operator strongly resembles but has different semantics, relevant to expansion. It also has a stricter syntax in that variables may not be symbols without a only variable binding specifications of the form are allowed. Symbols bound using are expected to be special variables. For every the expression should be true. The behavior is unspecified for any which doesn't name a special variable. The macro establishes a new dynamic environment which each given has the value of the specified which is evaluated in the top-level environment. Then, the are turned into the arguments of a form, and that form is then expanded in the new environment in which the dynamic bindings are visible. Thus may be used to bind special variables which are visible to expansion-time computations occurring within A macro may generate an form in order to communicate values to macros contained in that form. The macro evaluates its arguments immediately during macro expansion. The arguments are processed from left to right. Each is fully expanded and evaluated in the top-level environment before the next form is considered. The value of the last or else if there aren't any arguments, is converted into a literal expression which denotes that value, and the resulting literal is produced as the expansion of Note: supports techniques that require a calculation to be performed in the environment where the program is being compiled, and inserting the result of that calculation as a literal into the program source. Possibly, the calculation can have some useful effect in that environment, or use as an input information that is available in that environment. The operator also inserts a calculated value as a de facto literal into the program, but it performs that calculation in the environment where the compiled file is being loaded. The two operators may be considered complementary in this sense. Consider the source file:
      (defun host-name-c () (macro-time (uname).nodename))
      (defun host-name-l () (load-time (uname).nodename)) If this is compiled via the call in takes place when it is macro-expanded. Thereafter, the compiled version of the function returns the name of the machine where the compilation took place, no matter in what environment it is subsequently loaded and called. In contrast, the compilation of arranges for that function's call to take place just one time, whenever the compiled file is loaded. Each time the function is subsequently called, it will return the name of the machine where it was loaded, without making any additional calls to Note: can be understood in terms of the following implementation. Note that this implementation always produces a expression, which is not required to do if is self-evaluating:
      (defmacro macro-time (. forms)
        (let (val)
          (each ((f forms))
            (set val (eval f)))
          ^(quote ,val))) Because treats a top-level specially, this implementation is also possible:
      (defmacro macro-time (. forms)
        ^(quote ,(eval ^(progn ,*forms))))
      ;; The (1 2 3) object is produced at macro-expansion time, becoming
      ;; a quoted literal which evaluates to (1 2 3).
      (macro-time (list 1 2 3)) -> (1 2 3)
      ;; The above fact is revealed by macroexpand: the list form was
      ;; evaluated, and then quote was inserted to produce (quote (1 2 3))
      ;; which is notated '(1 2 3):
      (macroexpand '(macro-time (list 1 2 3))) -> '(1 2 3)
      ;; Quote isn't required on a self-evaluating object; it serves
      ;; as a literal expression denoting itself:
      (macroexpand '(macro-time (join-with "-" "a" "b"))) -> "a-b" The macro ("expand and quote") performs a full expansion of in the surrounding macro environment. Then it constructs a form whose argument is the expansion. This quote form is then returned as the macro replacement for the original form.
      (symacrolet ((a (+ 2 2)))
        (list (quote a) (equot a) a))
      --> (a (+ 2 2) 4) Above, the expansion of is Thus the macro call expands to When that is evaluated, it yields If is quoted, then the result is no expansion or evaluation takes place. Whereas if is presented for evaluation, then not only is it expanded to but that expansion is reduced to 4. The operator is a an intermediate point between these two semantics: it permits expansion to proceed, but then suppresses evaluation of the result. The operator evaluates and then uses the resulting value as a counterpart to a macro-style parameter list. If the value has a tree structure which matches the parameters, then those parameters are established as bindings, and the if any, are evaluated in the scope of those bindings. The value of the last is returned. If there are no forms, is returned. Under the value of the available to is the form itself. The operator is similar to except that it takes an extra argument, This argument is an expression which is evaluated. It is expected to evaluate to a compound form. If an error occurs during binding, the error diagnostic message is based on information obtained from this form. By contrast, the operator's error diagnostic refers to the form, which is cryptic if the binding is used for the implementation of some other construct, hidden from the user of that construct. In addition, specifies the value for the parameter that may refer to. The is an extension of which takes one more argument, before the macro parameters. This expression is evaluated, and becomes the value of the parameter that may refer to. Under and the parameter takes on the value Under all three operators, the parameter takes on the value of These operators throw an exception if there is a structural mismatch between the parameters and the value of One way to avoid this exception is to use which is based on the conventions of There exists no analog for or The operator evaluates and matches it against a succession of zero or more cases. Each case defines a pattern match, expressed as a macro style parameter list If the object produced by matches then the parameters are bound, becoming local variables, and the if any, are evaluated in order in the environment in which those variables are visible. If there are forms, the value of the last becomes the result value of the case, otherwise the result value of the case is nil. If the result value of a case is the object (the colon symbol), then processing continues with the next case. Otherwise the evaluation of terminates, returning the result value. If the value of does not match the parameter list of a case, processing continues with the next case. If no cases match, then terminates, returning
      ;; reverse function implemented using tree-case
      (defun tb-reverse (obj)
        (tree-case obj
          (() ()) ;; the empty list is just returned
          ((a) obj) ;; one-element list returned
          ((a . b) ^(,*(tb-reverse b) ,a)) ;; car/cdr recursion
          (a a))) ;; atom is just returned Note that in this example, the atom case is placed last, because an argument list which consists of a symbol is a "catch all" match that matches any object. We know that it matches an atom, because the previous case matches conses. In general, the order of the cases in is important: even more so than the order of cases in a or The one-element list case is unnecessary; it can be removed. The macro is similar to the operator but its argument binding is based on a macro-style parameter list. The name is an abbreviation of A form evaluates to a function which takes a variable number of arguments. When that function is called, those arguments are taken as a list object which is matched against as if by If the match is successful, then the parameters are bound to the corresponding elements from the argument structure and each successive is evaluated in an environment in which those bindings are visible. The value of the last is the return value of the function. If there are no forms, the function's return value is The following equivalence holds, where should be understood to be a globally unique symbol:
      (tb pattern body ...) <--> (lambda (. args)
                            (tree-bind pattern args body ...)) The macro produces an anonymous function whose behavior is closely based on the operator. Its name is an abbreviation of The anonymous function takes a variable number of arguments. Its argument list is taken to be the value macro is tested against the multiple pattern clauses of an implicit The return value of the function is that of the implied The following equivalence holds, where should be understood to be a globally unique symbol:
      (tc clause1 clause2 ...) <--> (lambda (. args)
                            (tree-case args
                            clause1 clause2 ...)) The evaluates the in an environment in which each variable name symbol is bound to a new uninterned symbol ("gensym"). The code:
      (let ((x (gensym))
            (y (gensym))
            (z (gensym)))
        ^(,x ,y ,z)) may be expressed more conveniently using the shorthand:
      (with-gensyms (x y z)
        ^(,x ,y ,z)) Parameter list macros, also more briefly called parameter macros are an original feature of TXR Lisp. If the first element of a function or macro parameter list is a keyword symbol other than or (the colon symbol), it denotes a parameter macro. This keyword symbol is expected to have a binding in the parameter macro namespace: a global namespace which associates keyword symbols with parameter list expander functions. Parameter list macros are recognized in both function parameter lists and macro parameter lists. A macro parameter list can, via nesting, contain multiple nested parameter lists. Each such nested list may contain parameter macro invocations; those are all traversed and processed. Expansion of a parameter list macro occurs at macro-expansion time, when a function's or macro's parameter list is traversed by the macro expander. It takes place as follows. First, the keyword is removed from the parameter list. The keyword's binding in the parameter macro namespace is retrieved. If it doesn't exist, an exception is thrown. Otherwise, the remaining parameter list is first recursively processed for more occurrences of parameter macros. This expansion produces a transformed parameter list, along with a transformed function body. These two artifacts are then passed to the transformer function retrieved from the keyword symbol's binding. The function returns a further transformed version of the parameter list and body. These are processed for more parameter macros. The process terminates when no more expansion is possible, because a parameter list has been produced which does not begin with a parameter macro. This final parameter list and its accompanying body are then taken in place of the original parameter list and body. TXR Lisp provides a two built-in parameter list macros. The parameter macro endows a function keyword parameters. The parameter macro allows a function to be expressed using pattern matching, which requires the body to consist of pattern-matching clauses. The implementation of both of these macros is written entirely using this parameter list macro mechanism, by means of the public macro. The variable holds a hash table which associates keyword symbols with parameter list expander functions. The functions are expected to conform to the following syntax: The parameter receives the parameter list of the function which is undergoing parameter expansion. All other parameter macros have already been expanded. The parameter receives the list of body forms. The function is expected to return a cell whose contains the transformed parameter list, and whose contains the transformed list of body forms. Parameter expansion takes place at macro expansion time. The parameter receives the macro-expansion-time environment which surrounds the function being expanded. Note that this environment doesn't take into account the parameters themselves; therefore, it is not the correct environment for expanding macros among the forms. For that purpose, it must be extended with shadowing entries, the manner of doing which is undocumented. However may be used directly for expanding init forms for optional parameters occurring in The parameter receives the overall function-defining form that is being processes, such as a or form. This is intended for error reporting. A parameter transformer returns the transformed parameter list and body as a single object: a list whose first element is the parameter list, and whose remaining elements are the forms of the body. Thus, the following is a correct null transformer:
      (lambda (params body env form)
        (cons params body)) The macro provides syntax for defining parameter macros. Invocations of this macro expand to code which constructs an anonymous function and installs it into the hash table, under the key given by The parameter's argument should be a keyword symbol that is valid for use as a parameter macro name. The and arguments must be symbols suitable for variable binding. These symbols define the parameters of the expander function which shall, respectively, receive the parameter list, body forms, macro environment and function form. If is omitted, a symbol generated by the function is used. Likewise if is omitted. The arguments constitute the body of the expander. The form returns The parameter macro returns the transformed parameter list and body as a single object: a list whose first element is the parameter list, and whose remaining elements are the forms of the body. The following example shows the implementation of a parameter macro which provides rudimentary memoization. Using the macro is extremely easy. It is a matter of simply inserting the keyword at the front of a function's parameter list. The function is then memoized.
      (defvarl %memo% (hash :weak-keys))
      (defun ensure-memo (sym)
        (or (gethash %memo% sym)
            (sethash %memo% sym (hash))))
      (define-param-expander :memo (param body)
        (let* ((memo-parm [param 0..(posq : param)])
               (hash (gensym))
               (key (gensym)))
          ^(,param (let ((,hash (ensure-memo ',hash))
                          (,key (list ,*memo-parm)))
                     (or (gethash ,hash ,key)
                         (sethash ,hash ,key (progn ,*body))))))) The above macro may be used to define a memoized Fibonacci function as follows:
      (defun fib (:memo n)
        (if (< n 2)
          (clamp 0 1 n)
          (+ (fib (pred n)) (fib (ppred n))))) All that is required is the insertion of the keyword. Parameter list macro injects keyword parameter support into functions and macros. When appears as the first item in a function parameter list, a special syntax is recognized in the parameter list. After any required and optional parameters, the symbol (two dashes) may appear. Parameters after this symbol are interpreted as keyword parameters. After the keyword parameters, a rest parameter may appear in the usual way as a symbol in the dotted position. Keyword parameters use the same syntax as optional parameters, except that if used in a macro parameter list, they do not support destructuring whereas optional parameters do. That is to say, regardless whether is used in a function or macro, keyword parameters are symbols. A keyword parameter takes three possible forms:
    A keyword parameter may be specified as a simple symbol If the argument for such a keyword parameter is missing, it takes on the value If the keyword parameter symbol is enclosed in a list, then the second element of that list specifies a default value, similarly to the default value for an optional argument. If the function is called in such a way that the argument for the parameter is missing, the is evaluated and the resulting value is bound to the keyword parameter. The evaluation takes place in a lexical scope in which the required and optional parameters are are already visible, and their values are bound. If there is a it is also visible in this scope, even though in the parameter list it appears to the left. The three-element form of the keyword parameter specifies an additional symbol which names an argument that implicitly receives a Boolean argument indicating the presence of the keyword argument. If an argument is not passed for the keyword parameter then parameter takes on the value If an argument is given for then the argument takes on the value This mechanism also closely resembles the analogous one supported in optional arguments. See the previous paragraph regarding the evaluation scope of

    In a call to a function, keyword arguments begin after those arguments which satisfy all of the required and optional parameters. Keyword arguments consist of interleaved indicators and values, which are separate arguments. Thus passing a keyword argument actually requires the passing of two function arguments: an indicator keyword symbol, followed by the associated value. The indicator keywords are expected to have the same symbol name as the defined keyword parameters. For instance, the indicator-value pair passes the value to a keyword parameter that may be named in any package: it may be or and so forth. Arguments specifying unrecognized keywords are ignored. If the function has a then that parameter receives the keyword arguments as a list. Since that list contains indicators and values, it is a de facto property list. In detail, the mechanism generates a regular variadic function which receives the keyword arguments as the trailing argument list. That function parses the recognized keyword arguments out of the trailing list, and binds them to the keyword parameter symbols as local variables. If a parameter is defined, then the entire keyword argument list is available through that parameter, and the keyword argument parsing logic also refers to the value of that parameter to gain access to the keyword arguments. If there is no specified, then the macro adds a using a machine-generated symbol. The argument parsing logic then refers to the value of that symbol. Define a function with two required arguments one optional argument two keyword arguments and and a rest parameter
      (defun fun (:key a b : c -- foo bar . klist)
        (list a b c foo bar klist))
      (fun 1 2 3 :bar 4) -> (1 2 3 nil 4 (:bar 4)) Define a function with only keyword arguments, with default expressions and Boolean indicator params:
      (defun keyfun (:key -- (a 10 a-p) (b 20 b-p))
        (list a a-p b b-p))
      (keyfun :a 3) -> (3 t 20 nil)
      (keyfun :b 4) -> (10 nil 4 t)
      (keyfun :c 4) -> (10 nil 20 nil)
      (keyfun) -> (10 nil 20 nil) The function expands all of the parameter list macros expressed in the prototype form returning an expanded version of the form. The is a compound form which has a shape very similar to a lambda expression, and may be a lambda expression. The first element of is a name, which is an arbitrary object, though the use of a symbol is strongly recommended. This object plays no role in other than for composing diagnostic messages if errors occur. The second element of is the parameter list. The remaining elements of are zero or more body forms. If contains no parameter macro invocations, then it is returned. The optional parameter specifies the macro environment which is passed to the parameter macro expanders, which they can receive via the parameter. The default value specifies the top-level environment.
      ;; No expansion: argument is returned
      (macroexpand-params '(foo (arg) body)) -> (foo (arg) body)
      ;; Expand :key macro
      (macroexpand-params '(bar (:key a b c -- d (e 1234 f-p)) body))
      --> (bar (a b c . #:g0014)
            (let (d e f-p)
              (let ((#:g0015 (memp :d #:g0014)))
                (when #:g0015
                  (set d (cadr #:g0015))))
              (let ((#:g0015 (memp :e #:g0014)))
                (cond
                  (#:g0015 (set e (cadr #:g0015))
                           (set f-p t))
                  (t (set e 1234))))
              body)) The operator stores the values of expressions in places. It must be given an even number of arguments. If there are no arguments, then does nothing and returns If there are two arguments, and then is evaluated to determine its storage location, then is evaluated to determine the value to be stored there, and then the value is stored in that location. Finally, the value is also returned as the result value. If there are more than two arguments, then performs multiple assignments in left-to-right order. Effectively, is precisely equivalent to The syntax of is similar to that of and the semantics is similar also in that zero or more places are assigned zero or more values. In fact, if there are no arguments, or if there is exactly one pair of arguments, is equivalent to If there are two or more argument pairs, then all of the arguments are evaluated first, in left-to-right order. No store takes place until after every is determined, and every is calculated. During the calculation, the values to be stored are retained in hidden, temporary locations. Finally, these values are moved into the determined places. The rightmost value is returned as the form's value. The assignments thus appear to take place in parallel, and is capable of exchanging the values of a pair of places, or rotating the values among three or more places. (However, there are more convenient operators for this, namely and
      ;; exchange x and y
      (pset x y y x)
      ;; exchange elements 0 and 1; and 2 and 3 of vector v:
      (let ((v (vec 0 10 20 30))
            (i -1))
        (pset [vec (inc i)] [vec (inc i)]
              [vec (inc i)] [vec (inc i)])
         vec)
      -> #(10 0 30 20) The macro assigns to and returns the previous value of If is missing, then is used. In more detail, first is evaluated to determine the storage location. Then, the location is accessed to retrieve the previous value. Then, the expression is evaluated, and that value is placed into the storage location. Finally, the previously retrieved value is returned. The macro toggles the Boolean value stored in If previously held it is set to and if it previously held a value other than it is set to The macro examines the value of If it is then it stores into the place, and returns Otherwise it leaves unchanged and returns The macro examines the value of If it is Boolean true (any value except then it stores into the place, and returns Otherwise it leaves unchanged and returns The macro examines the value of and compares it to using the comparison function given by the function name This comparison takes places as if by evaluating the expression where denotes the current value of If the comparison is false, is not modified, the expression is not evaluated, and the macro returns If the comparison is true, then evaluates the expression, stores the resulting value into and returns The macro examines the value of If the current value is then is evaluated. The value is stored in and becomes the result of the form. If the value of is other than then the form yields that value. In this case, isn't evaluated, and isn't modified. The expression is evaluated only once to determine the place. The macro increments by adding to its value. If is missing, the value used in its place the integer 1. First the argument is evaluated as a syntactic place to determine the location. Then, the value currently stored in that location is retrieved. Next, the expression is evaluated. Its value is added to the previously retrieved value as if by the function. The resulting value is stored in the place, and returned. The macro works exactly like except that addition is replaced by subtraction. The similarly defaulted value is subtracted from the previous value of the place. The macros and are similar to and The only difference is that they return the previous value of rather than the incremented value. The and macros provide combined operations which change the value of a place and provide a test whether, respectively, a certain previous value was overwritten, or a certain new value was attained. By default, this tested value is zero. The macro notes the prior value of and then updates it with that value, plus which defaults to 1. If the prior value is to then it returns otherwise The default value of is zero. The macro produces a new value by subtracting from the value of The argument defaults to 1. The new value is stored into If the new value is to then is returned, otherwise The macro exchanges the values of and and returns the value which is thereby transferred to First, and are evaluated, in that order, to determine their locations. Then the prior values are retrieved, exchanged and stored back. The value stored in is also returned. If and are ranges of the same sequence, the behavior is not specified if the ranges overlap or are of unequal length. Note: the macro's behavior is somewhat more specified in this regard. Thus, although any correct expression can be expressed using but the reverse isn't true. The macro places at the head of the list stored in and returns the updated list which is stored back in First, the expression is evaluated to produce the push value. Then, is evaluated to determine its storage location. Next, the storage location is accessed to retrieve the list value which is stored there. A new object is produced as if by invoking function on the push value and list value. This object is stored into the location, and returned. The macro removes an element from the list stored in and returns it. First, is evaluated to determine the place. The place is accessed to retrieve the original value. Then a new value is calculated, as if by applying the function to the old value. This new value is stored. Finally, a return value is calculated and returned, as if by applying the function to the original value. The macro inspects the list stored in If the list already contains the item, then it returns the list. Otherwise it creates a new list with the item at the front and stores it back into and returns it. First, the expression is evaluated to produce the push value. Then, is evaluated to determine its storage location. Next, the storage location is accessed to retrieve the list value which is stored there. The list is inspected to check whether it already contains the push value, as if using the function. If that is the case, the list is returned and the operation finishes. Otherwise, a new object is produced as if by invoking function on the push value and list value. This object is stored into the location and returned. The macro treats one or more places as a "multi-place shift register". The values of the places are shifted one place to the left. The first (leftmost) place receives the value of the second place, the second receives that of the third, and so on. The last (rightmost) place receives (which is not treated as a place, even if it is a syntactic place form). The previous value of the first place is returned. More precisely, all of the argument forms are evaluated left to right, in the process of which the storage locations of the places are determined, is reduced to its value. The values stored in the places are sampled and saved. Note that it is not specified whether the places are sampled in a separate pass after the evaluation of the argument forms, or whether the sampling is interleaved into the argument evaluation. This affects the behavior in situations in which the evaluation of any of the forms, or of has the side effect of modifying later places. Next, the places are updated by storing the saved value of the second place into the first place, the third place into the second and so forth, and the value of into the last place. Finally, the saved original value of the first place is returned. If any of the places are ranges which index into the same sequence, and the behavior is not otherwise unspecified due to the issue noted in an earlier paragraph, the effect upon the multiply-stored sequence can be inferred from the above-described storage order. Note that even if stores take place which change the length of the sequence and move some elements, not-yet-processed stores whose ranges to refer to these elements are not adjusted. With regard to the foregoing paragraph, a recommended practice is that if subranges of the same sequence object are shifted, they be given to the macro in ascending order of starting index. Furthermore, the semantics is simpler if the ranges do not overlap. Treats zero or more places as a "multi-place rotate register". If there are no arguments, there is no effect and is returned. Otherwise, the last (rightmost) place receives the value of the first (leftmost) place. The leftmost place receives the value of the second place, and so on. If there are two arguments, this equivalent to The prior value of the first place, which is the value rotated into the last place, is returned. More precisely, the arguments are evaluated left to right, and the storage locations are thereby determined. The storage locations are sampled, and then the sampled values are stored back into the locations, but rotated by one place as described above. The saved original value of the leftmost is returned. It is not specified whether the sampling of the original values is a separate pass which takes place after the arguments are evaluated, or whether this sampling it is interleaved into argument evaluation. This affects the behavior in situations in which the evaluation of any of the forms has the side effect of modifying the value stored in a later form. If any of the places are ranges which index into the same sequence, and the behavior is not otherwise unspecified due to the issue noted in the preceding paragraph, the effect upon the multiply-stored sequence can be inferred from the above-described storage order. Note that even if stores take place which change the length of the sequence and move some elements, not-yet-processed stores whose ranges to refer to these elements are not adjusted. With regard to the foregoing paragraph, a recommended practice is that if subranges of the same sequence object are shifted, they be given to the macro in ascending order of starting index. Furthermore, the semantics is simpler if the ranges do not overlap. The macro requests the deletion of If doesn't support deletion, an exception is thrown. First is evaluated, thereby determining its location. Then the place is accessed to retrieve its value. The place is then subject to deletion. Finally, the previously retrieved value is returned. Precisely what deletion means depends on the kind of place. The built-in places in TXR Lisp have deletion semantics which are intended to be unsurprising to the programmer familiar with the data structure which holds the place. Generally, if a place denotes the element of a sequence, then deletion of the place implies deletion of the element, and deletion of the element implies that the gap produced by the element is closed. The deleted element is effectively replaced by its successor, that successor by its successor and so on. If a place denotes a value stored in a dynamic data set such as a hash table, then deletion of that place implies deletion of the entry which holds that value. If the entry is identified by a key, that key is also removed. If is a DWIM bracket expression indexing into a structure, the structure is expected to implement the and methods. Moreover, the place form must have only two arguments: the object and an index argument. In other words, the form must have this syntax: The method will be invoked with the unmodified and arguments to determine the prior value to be returned. Then the method will be invoked with three arguments: a possibly modified value and the argument representing an empty replacement sequence. If is a sequence or range, it is passed to the method unmodified. Otherwise it is expected to be an integer, and converted into a one-element range spanning the indicated element. For instance, if the value is it is converted to the range In effect, the method is thereby asked to replace the one-element subsequence starting at index with the empty sequence The operator's parameter list consists of one or more places followed by an expression The macro evaluates which is expected to produce a sequence. Successive elements of the resulting list are then assigned to each successive If there are fewer elements in the sequence than places, the unmatched places receive the value Excess elements in the sequence are ignored. An error exception occurs if the sequence is an improper list with fewer elements than places. A form produces the value of as its result value. The macro evaluates and passes the value as an argument to the operational pipeline function formed, as if by the macro, from the arguments. The result of this function is then stored back into The following equivalence holds, except that place is evaluated only once:
      (upd p x y z ...) <--> (set p (call (opip x y z ...) p)) TXR Lisp provides a number of place-modifying operators such as and It also provides a variety of kinds of syntactic places which may be used with these operators. Both of these categories are open-ended: TXR Lisp programs may extend the set of place-modifying operators, as well as the vocabulary of forms which are recognized as syntactic places. Regarding place operators, it might seem obvious that new place operators can be developed, since they are macros, and macros can expand to uses of existing place operators. As an example, it may seem that operator could be written as a macro which uses
      (defmacro new-inc (place : (delta 1))
        ^(set ,place (+ ,place ,delta))) However, the above macro has a problem: the argument form is inserted into two places in the expansion, which leads to two evaluations. This is visibly incorrect if the place form contains any side effects. It is also potentially inefficient. TXR Lisp provides a framework for writing place update macros which evaluate their argument forms once, even if they have to access and update the same places. The framework also supports the development of new kinds of place forms as capsules of code which introduce the right kind of material into the lexical environment of the body of an update macro, to enable this special evaluation. The central design concept in TXR Lisp syntactic places are place-expander functions. Each compound place is defined by up to three place-expander functions, which are associated with the place via the leftmost operator symbol of the place form. One place-expander, the update expander, is mandatory. Optionally, a place may also provide a clobber expander as well as a delete expander. An update expander provides the expertise for evaluating a place form once in its proper run-time context to determine its actual run-time storage location, and to access and modify the storage location. A clobber expander provides an optimized mechanism for uses that perform a one-time store to a place without requiring its prior value. If a place definition does not supply a clobber expander, then the syntactic places framework uses the update expander to achieve the functionality. A delete expander provides the expertise for determining the actual run-time storage location corresponding to a place, and obliterating it, returning its prior value. If a place does not supply a delete expander, then the place does not support deletion. Operators which require deletion, such as will raise an error when applied to that place. The expanders operate independently, and it is expected that place-modifying operators choose one of the three, and use only that expander. For example, accessing a place with an update expander and then overwriting its value with a clobber expander may result in incorrect code which contains multiple evaluations of the place form. The programmer who implements a new place does not write expanders directly, but rather defines them via the or macro. The programmer who implements a new place update macro likewise does not call the expanders directly. Usually, they are invoked via the macros and These are sufficient for most kind of macros. In certain complicated cases, expanders may be invoked using the wrapper functions and These convenience macros and functions perform certain common chores, like macro-expanding the place in the correct environment, and choosing the appropriate function. The expanders are described in the following sections. The update expander is a code-writer. It takes a argument, representing code, and returns a larger form which surrounds this code with additional code. This larger form returned by the update expander can be regarded as having two abstract actions, when it is substituted and evaluated in the context where occurs. The first abstract action is to evaluate exactly one time, in order to determine the actual run-time location to which that form refers. The second abstract action is to evaluate the caller's in a lexical environment in which bindings exist for some lexical functions or (more usually) lexical macros. These lexical macros are explicitly referenced by the the update expander just provides their definition, under the names it is given via the and arguments. The update expander writes local functions or macros under these names: a getter function and a setter function. Usually, update expanders write macros rather than functions, possibly in combination with some lexical anonymous variables which hold temporary objects. Therefore the getter and setter are henceforth referred to as macros. The code being generated is with regard to some concrete instance of This argument is the actual form which occurs in a program. For instance, the update expander for the place might be called with an arbitrary variant of the which might look like In the abstract semantics, upfront code wrapped around the by the update expander provides the logic to evaluate this place to a location, which is retained in some hidden local context. The getter local macro named by must provide the logic for retrieving the value of this place. The getter macro takes no arguments. The makes free use of the getter function; they may call it multiple times, which must not trigger multiple evaluations of the original place form. The setter local macro named by must generate the logic for storing a new value into the once-evaluated version of The setter function takes exactly one argument, whose value specifies the value to be stored into the place. It is the caller's responsibility to ensure that the argument form which produces the value to be stored via the setter is evaluated only once, and in the correct order. The setter does not concern itself with this form. Multiple calls to the setter can be expected to result in multiple evaluations of its argument. Thus, if necessary, the caller must supply the code to evaluate the new value form to a temporary variable, and then pass the temporary variable to the setter. This code can be embedded in the or can be added to the code returned by a call to the update expander. The setter local macro or function must return the new value which is stored. That is to say, when invokes this local macro or function, it may rely on it yielding the new value which was stored, as part of achieving its own semantics. The update expander does not macro-expand It is assumed that the expander is invoked in such a way that the place has been expanded in the correct environment. In other words, the form matches the type of place which the expander handles. If the expander had to macro-expand the place form, it would sometimes have to come to the conclusion that the place form must be handled by a different expander. No such consideration is the case: when an expander is called on a form, that is final; it is certain that it is the correct expander, which matches the symbol in the position of the form, which is not a macro in the context where it occurs. An update expander is free to assume that any place which is stored (the setter local macro is invoked on it) is accessed at least once by an invocation of the getter. A place update macro which relies on an update expander, but uses only the store macro, might not work properly. An example of an update expander which relies on this assumption is the expander for the place type. If has not yet been forced, and only the setter is used, then might remain unforced as its internal value location is updated. A subsequent access to the place will incorrectly trigger a force, which will overwrite the value. The expected behavior is that storing a value in an unforced place changes the place to forced state, preempting the evaluation of the delayed form. Afterward, the promise exhibits the value which was thus assigned. The update expander is not responsible for all issues of evaluation order. A place update macro may consist of numerous places, as well as numerous value-producing forms which are not places. Each of the places can provide its registered update expander which provides code for evaluating just that place, and a means of accessing and storing the values. The place update macro must call the place expanders in the correct order, and generate any additional code in the correct order, so that the macro achieves its required documented evaluation order.
      ;; First, capture the update expander
      ;; function for (car ...) places
      ;; in a variable, for clarity.
      (defvar car-update-expander [*place-update-expander* 'car])
      ;; Next, call it for the place (car [a 0]).
      ;; The body form specifies logic for
      ;; incrementing the place by one and
      ;; returning the new value.
      (call car-update-expander 'getit 'setit '(car [a 0])
        '(setit (+ (getit) 1)))
      ;; --> Resulting code:
      (rlet ((#:g0032 [a 0]))
        (macrolet ((getit nil
                     (append (list 'car) (list '#:g0032)))
                   (setit (val)
                     (append (list 'sys:rplaca)
                            (list '#:g0032) (list val))))
          (setit (+ (getit) 1))))
      ;; Same expander call as above, with a call to expand added
      ;; to show the fully expanded version of the returned code,
      ;; in which the ;; setit and getit calls have disappeared,
      ;; replaced by their macro-expansions.
      (expand
        (call car-update-expander 'getit 'setit '(car [a 0])
          '(setit (+ (getit) 1))))
      ;; --> Resulting code:
      (let ((#:g0032 [a 0]))
        (sys:rplaca #:g0032 (+ (car #:g0032) 1))) The main noteworthy points about the generated code are:

    -   the place is evaluated by evaluating the embedded form and storing storing the resulting object into a hidden local variable. That's as close a reference as we can make to the field.

    -   the getter macro expands to code which simply calls the function on the cell.

    -   the setter uses a system function called which differs from in that it returns the stored value, rather than the cell.
The clobber expander is a code-writer similar to the update expander. It takes a argument, and returns a larger form which surrounds this form with additional program code. The returned block of code has one main abstract action. It must arrange for the evaluation of in a lexical environment in which a lexical macro or lexical function exists which has the name requested by the argument. The simple setter local macro written by the clobber expander is similar to the local setter written by the update expander. It has exactly the same interface, performs the same action of storing a value into the place, and returns the new value. The difference is that its logic may be considerably simplified by the assumption that the place is being subject to exactly one store, and no access. A place update macro which uses a clobber expander, and calls it more than once, break the assumption; doing so may result in multiple evaluations of the The delete expander is a code-writer similar to clobber expander. It takes a arguments, and returns a larger form which surrounds this form with additional program code. The returned block of code has one main abstract action. It must arrange for the evaluation of in a lexical environment in which a lexical macro or lexical function exists which has the name requested by the argument. The deleter macro written by the clobber expander takes no arguments. It may be called at most once. It returns the previous value of the place, and arranges for its obliteration, whatever that means for that particular kind of place. The macro evaluates the argument, whose result is expected to be a Lisp form. The macro adds additional code around this code, and the result is returned. This additional code is called the place-access code. The and arguments must be symbols. Over the evaluation of the these symbols are bound to the names of local functions which are provided in the place-access code. The argument is a form which evaluates to a syntactic place. The generated place-access code is based on this place. The argument is a form which evaluates to a macro-expansion-time environment. The macro uses this environment to perform macro-expansion on the value of the form, to obtain the correct update expander function for the fully macro-expanded place. The place-access code is generated by calling the update expander for the expanded version of The following is an implementation of the macro, which exchanges the contents of two places. Two places are involved, and, correspondingly, the macro is used twice, to add two instances of place-update code to the macro's body.
  (defmacro swap (place-0 place-1 :env env)
    (with-gensyms (tmp)
      (with-update-expander (getter-0 setter-0) place-0 env
        (with-update-expander (getter-1 setter-1) place-1 env
          ^(let ((,tmp (,getter-0)))
             (,setter-0 (,getter-1))
             (,setter-1 ,tmp)))))) The basic logic for swapping two places is contained in the code template:
  ^(let ((,tmp (,getter-0)))
     (,setter-0 (,getter-1))
     (,setter-1 ,tmp)) The temporary variable named by the symbol is initialized by calling the getter function for Then the setter function of is called in order to store the value of into Finally, the setter for is invoked to store the previously saved temporary value into that place. The name for the temporary variable is provided by the macro, but establishing the variable is the caller's responsibility; this is seen as an explicit binding in the code template. The names of the getter and setter functions are similarly provided by the macros. However, binding those functions is the responsibility of that macro. To achieve this, it adds the place-access code to the code generated by the backquote template. In the following example macro-expansion, the additional code added around the template is seen. It takes the form of two binding blocks, each added by an invocation of
  (macroexpand '(swap a b))
  -->
  (macrolet ((#:g0036 () 'a) ;; getter macro for a
             (#:g0037 (val-expr) ;; setter macro for a
               (append (list 'sys:setq) (list 'a)
                       (list val-expr))))
    (macrolet ((#:g0038 () 'b) ;; getter macro for b
               (#:g0039 (val-expr) ;; setter macro for b
                 (append (list 'sys:setq) (list 'b)
                        (list val-expr))))
      (let ((#:g0035 (#:g0036))) ;; temp <- a
        (#:g0037 (#:g0038)) ;; a <- b
        (#:g0039 #:g0035)))) ;; b <- temp In this expansion, for example is the generated symbol which forms the value of the variable in the macro. The getter is a macro which simply expands to a straightforward access to the variable a. The symbol is the value of the variable. Thus the swap macro's ^(let ((,tmp (,getter-0))) ...) has turned into ^(let ((#:g0035 (#:g0036))) ...) A full expansion, with the local macros expanded out:
  (expand '(swap a b))
  -->
  (let ((#:g0035 a))
    (sys:setq a b)
    (sys:setq b #:g0035)) In other words, the original syntax (,getter-0) became (#:g0036) and finally just Similarly, (,setter-0 (,getter-1)) became the invocations (#:g0037 (#:g0038)) which finally turned into: The macro evaluates whose result is expected to be a Lisp form. The macro adds additional code around this form, and the result is returned. This additional code is called the place-access code. The argument must be a symbol. Over the evaluation of the this symbol is bound to the name of a functions which are provided in the place-access code. The argument is a form which evaluates to a syntactic place. The generated place-access code is based on this place. The argument is a form which evaluates to a macro-expansion-time environment. The macro uses this environment to perform macro-expansion on the value of the form, to obtain the correct update expander function for the fully macro-expanded place. The place-access code is generated by calling the update expander for the expanded version of The following implements a simple assignment statement, similar to except that it only handles exactly two arguments:
  (defmacro assign (place new-value :env env)
    (with-clobber-expander (setter) place env
      ^(,setter ,new-value))) Note that the correct evaluation order of and is taken care of, because generates the code which performs all the necessary evaluations of This evaluation occurs before the code which is generated by ^(,setter ,new-value) part is evaluated, and that code is what evaluates Suppose that a macro were desired which allows assignment to be notated in a right to left style, as in:
   (assign 42 a) ;; store 42 in variable a Now, the new value must be evaluated prior to the place, if left-to-right evaluation order is to be maintained. The standard macro has this property: the push value is on the left, and the place is on the right. Now, the code has to explicitly take care of the order, like this:
  ;; WRONG! We can't just swap the parameters;
  ;; place is still evaluated first, then new-value:
  (defmacro assign (new-value place :env env)
    (with-clobber-expander (setter) place env
      ^(,setter ,new-value)))
  ;; Correct: arrange for evaluation of new-value first,
  ;; then place:
  (defmacro assign (new-value place :env env)
    (with-gensym (tmp)
      ^(let ((,tmp ,new-value))
         ,(with-clobber-expander (setter) place env
           ^(,setter ,tmp))))) The macro evaluates whose result is expected to be a Lisp form. The macro adds additional code around this code, and the resulting code is returned. This additional code is called the place-access code. The argument must be a symbol. Over the evaluation of the this symbol is bound to the name of a functions which are provided in the place-access code. The argument is a form which evaluates to a syntactic place. The generated place-access code is based on this place. The argument is a form which evaluates to a macro-expansion-time environment. The macro uses this environment to perform macro-expansion on the value of the form, to obtain the correct update expander function for the fully macro-expanded place. The place-access code is generated by calling the update expander for the expanded version of The following implements the macro:
  (defmacro del (place :env env)
    (with-delete-expander (deleter) place env
      ^(,deleter))) The function provides an alternative interface for making use of an update expander, complementary to Arguments and are symbols, provided by the caller. These are passed to the update expander function, and are used for naming local functions in the generated code which the update expander adds to The argument is a place which has not been subject to macro-expansion. The function takes on the responsibility for macro-expanding the place. The parameter is the macro-expansion environment object required to correctly expand in its original environment. The argument represents the source code of a place update operation. This code makes references to the local functions whose names are given by and Those arguments allow the update expander to write these functions with the matching names expected by The return value is an object representing source code which incorporates the augmenting it with additional code which evaluates to determine its location, and provides place accessor local functions expected by the The following shows how to implement a macro using
  (defmacro with-update-expander ((getter setter)
                        unex-place env body)
    ^(with-gensyms (,getter ,setter)
       (call-update-expander ,getter ,setter
                        ,unex-place ,env ,body))) Essentially, all that does is to choose the names for the local functions, and bind them to the local variable names it is given as arguments. Then it calls Implement the swap macro using
  (defmacro swap (place-0 place-1 :env env)
    (with-gensyms (tmp getter-0 setter-0 getter-1 setter-1)
      (call-update-expander getter-0 setter-0 place-0 env
        (call-update-expander getter-1 setter-1 place-1 env
          ^(let ((,tmp (,getter-0)))
             (,setter-0 (,getter-1))
             (,setter-1 ,tmp)))))) The function provides an alternative interface for making use of a clobber expander, complementary to Argument is a symbol, provided by the caller. It is passed to the clobber expander function, and is used for naming a local function in the generated code which the update expander adds to The argument is a place which has not been subject to macro-expansion. The function takes on the responsibility for macro-expanding the place. The parameter is the macro-expansion environment object required to correctly expand in its original environment. The argument represents the source code of a place update operation. This code makes references to the local function whose name is given by That argument allows the update expander to write this function with the matching name expected by The return value is an object representing source code which incorporates the augmenting it with additional code which evaluates to determine its location, and provides the clobber local function to the The function provides an alternative interface for making use of a delete expander, complementary to Argument is a symbol, provided by the caller. It is passed to the delete expander function, and is used for naming a local function in the generated code which the update expander adds to The argument is a place which has not been subject to macro-expansion. The function takes on the responsibility for macro-expanding the place. The parameter is the macro-expansion environment object required to correctly expand in its original environment. The argument represents the source code of a place delete operation. This code makes references to the local function whose name is given by That argument allows the update expander to write this function with the matching name expected by The return value is an object representing source code which incorporates the augmenting it with additional code which evaluates to determine its location, and provides the delete local function to the The macro provides a simplified way to write certain kinds of place update macros. Specifically, it provides a way to write place update macros which modify a place by retrieving the previous value, pass it through a function (perhaps together with some additional arguments), and then store the resulting value back into the place and return it. The parameter specifies the name for the place update macro to be written. The parameter must specify a symbol: the name of the update function. The update macro and update function both take at least one parameter: the place to be updated, and its value, respectively. The specifies the additional parameters for the update function, which will also become additional parameters of the macro. Because it is a function parameter list, it cannot use the special destructuring features of macro parameter lists, or the or special parameters. It can use optional parameters, and may be empty. The macro writes a macro called The leftmost parameter of this macro is a place, followed by the additional arguments specified by The macro will arrange for the evaluation of the place argument to determine the place location. It will then retrieve and save the prior value of the place, and evaluate the remaining arguments. The prior value of the place, and the values of the additional arguments, are all passed to and the resulting value is then stored back into the location previously determined for Some standard place update macros are implementable using such as The macro reads the old value of the place, then passes it through the (plus) function, along with an extra argument: the delta value, which defaults to one. The macro could be written using as follows:
  (define-modify-macro inc (: (delta 1)) +) Note that the argument list doesn't specify the place, because the place is the implicit leftmost argument of the macro which isn't given a name. With the above definition in place, when is invoked, then is first reduced to a location, and that location's value is retrieved and saved. Then the parameter s evaluated to its value, which has defaulted to 1, since the argument was omitted. Then these two values are passed to the function, and so 1 is added to the value previously retrieved from The resulting sum is then stored back without evaluating again. The macro is used to introduce a new kind of syntactic place. It writes the update expander, and optionally clobber and delete expander functions, from a simpler, more compact specification, and automatically registers the resulting functions. The compact specification of a call contains only code fragments for the expander functions. The name and syntax of the place is determined by the argument, which is macro-style parameter list whose structure mimics that of the place. In particular, its leftmost symbol gives the name under which the place is registered. The macro provides automatic destructuring of the syntactic place, so that the expander code fragments can refer to the components of a place by name. The parameter must be be a symbol. This symbol will capture the parameter which is passed to the update expander, clobber expander or delete expander. The code fragments then have access to the body forms via this name. The and parenthesized triplet specify the update expander fragment. The macro will bind and to symbols. The must then specify a template of code which evaluates the syntactic place to determine its storage location, and provides a pair of local functions, using these two symbols as their name. The template must also insert the forms into the scope of these local functions, and the place determining code. The and arguments similarly specify an optional clobber expander fragment, as a single optional argument. If specified, the must generate a local function named using wrapped around forms. The and likewise specify a delete expander fragment. If this is omitted, then the place shall not support deletion. Implementation of the place denoting the field of cells:
  (defplace (car cell) body
    ;; the update expander fragment
    (getter setter
      (with-gensyms (cell-sym) ;; temporary symbol for cell
        ^(let ((,cell-sym ,cell)) ;; evaluate place to cell
           ;; getter and setter access cell via temp var
           (macrolet ((,getter ()
                        ^(car ,',cell-sym))
                      (,setter (val)
                        ^(sys:rplaca ,',cell-sym ,val)))
             ;; insert body form from place update macro
             ,body))))
    ;; clobber expander fragment: simpler: no need
    ;; to evaluate cell to temporary variable.
    (ssetter
      ^(macrolet ((,ssetter (val)
                     ^(sys:rplaca ,',cell ,val)))
        ,body))
    ;; deleter: delegate to pop semantics:
    ;; (del (car a)) == (pop a).
    (deleter
      ^(macrolet ((,deleter () ^(pop ,',cell)))
         ,body))) The macro provides a mechanism for introducing a new kind of syntactic place. It is simpler to use than and more concise, but not as general. The macro is designed for situations in which a function or macro which evaluates all of its arguments is required to serve as a syntactic place. It provides two flavors of syntax: the long form, indicated by giving five arguments, and a short form, which uses two arguments. In the long form of the syntactic place is described by and The form expresses the request that a call to the function or operator named be treated as a syntactic place, which has arguments described by the parameter list The argument specifies an expression which generates the code for storing a new value to the place. The macro makes the necessary arrangements such that when an operator form named by is treated as a syntactic place, then at macro-expansion time, code is generated to evaluate all of its argument expressions into machine-generated variables. The names of those variables are automatically bound to the corresponding symbols given in the argument list of the syntax. Code is also generated to evaluate the expression which gives the new value to be stored, and that is bound to a generated variable whose name is bound to the symbol. Then arrangements are made to invoke the operator named by and to evaluate the in an environment in which these symbol bindings are visible. The operator named is invoked using an altered argument list which uses temporary symbols in place of the original expressions. The task of is to insert the values of the symbols from and into a suitable code templates that will perform the store actions. The code generated by must also take on the responsibility of yielding the new value as its result. If list contains optional parameters, the default value expressions of those parameters shall be evaluated in the scope of the definition. The list may specify a rest parameter. In the expansion, this parameter will capture a list of temporary symbols, corresponding to the list of variadic argument expressions. For instance if the parameter list for a place is featuring the rest parameter and its is and the place is invoked as then parameter will be bound to a list of gensyms such as so that the evaluation of will yield syntax resembling Here, gensyms and are understood to be bound to the values of the expressions and the two trailing parameters corresponding to the rest parameter Syntactic places defined by that have a rest parameter may be invoked with improper syntax such as In this situation, that rest parameter will be bound to the name of a temporary variable which holds the value of rather than to a list of temporary variable names holding the values of trailing expressions. The must be prepared for this situation. In particular, the rest parameter's value is an atom, then it cannot be spliced in the backquote syntax, except at the last position of a list. Although syntactic places defined by perform macro-parameter-like destructuring of the place form, binding unevaluated argument expressions to the parameter symbols, nested macro parameter lists are not supported: specifies a function parameter list. The parameter list may use parameter macros, keeping in mind that the parameter expansion is applied at the time the form is processed, specifying an expanded parameter list which receives unevaluated expressions. The may refer to all symbols produced by parameter list expansion, other than generated symbols. For instance, if a parameter list macro exists which adds the parameter symbol to the parameter list, and this is invoked in the list of a then will be visible to the The short, two-argument form of simply specifies the names of two functions or operators: names the operator which accesses the place, and names the operator which stores a new value into the place. It is expected that all arguments of these operators are evaluated expressions, and that the store operator takes one argument more than the access operator. The operators are otherwise assumed to be variadic: each instance of a place based on individually determines how many arguments are passed to that operator and to the one named by The definition means that will generate code which ensures that and are evaluated exactly once, and then those two values are passed as arguments to which returns the current value of the place. That value is then incremented by one, and stored into the place by calling the function/operator with three arguments: the two values that were passed to and the new value. The exact number of arguments is determined by each individual use of as a place; the form doesn't specify the arity of and only that must accept one more argument relative to The following equivalence holds between the short and long forms:
   (defset g s) <--> (defset g (. r) n ^(g ,*r) ^(s ,*r ,n)) Note: the short form of is similar to the macro. Implementation of as a syntactic place using a long form
  (defset car (cell) new
    (let ((n (gensym)))
      ^(rlet ((,n ,new))
         (progn (rplaca ,cell ,n) ,n)))) Given such a definition, the expression expands to code closely resembling:
  (let ((#:g0048 (abc)))
    (let ((#:g0050 (succ (car #:g0048))))
      (rplaca #:g0048 #:g0050)
      #:g0050)) The macro has arranged for the argument expression of to be evaluated to a temporary variable a This, then, holds the cell being operated on. At macro-expansion time, the variable from the parameter list specified by the is bound to this symbol. The access expression to retrieve the prior value is automatically generated by combining the name of the place with the gensym to which its argument has been evaluated. The variable was bound to the expression giving the new value, namely The is careful to evaluate this only one time, storing its value into the temporary variable referenced by the variable The fragment thus turned into where references the cons cell being operated on, and the calculated new value to be stored into its field. The is careful to arrange for the new value to be returned. Those place-mutating operators which yield the new value, such as and rely on this behavior. In some situations, an equivalence exists between two forms, only one of which is recognized as a place. The macro can be used to establish a form as a place in terms of a translation to an equivalent form which is already a place. The has the same syntax as It specifies a macro transformation for a compound form which has the symbol in its leftmost position. Place macro expansion doesn't use an environment; place macros are in a single global namespace, special to place macros. There are no lexically scoped place macros. Such an effect can be achieved by having a place macro expand to an a form which is the target of a global or local macro, as necessary. To support place macros, forms which are used as syntactic places are subject to a modified macro-expansion algorithm:

    1.  If a place macro exists for a form that is being used as a place, then the that place macro is invoked to expand the form, and the expansion is taken in place of the original form. This process repeats until the form can no longer be expanded as a place macro, or the place macro declines to expand the form by returning the unexpanded input.

    2.  A form that has been fully expanded as a place macro is then subject to a single-round of macro-expansion, as if by which takes place in the original form's lexical environment. If the form doesn't expand, or the result of expansion is or a non-symbolic atom, then the process terminates. Otherwise, the process is repeated from step 1.

    The macro does not cause to become There can exist both an ordinary macro and a place macro of the same name. In this situation, when the macro call appears as a place form, it is expanded as a place macro, according to the above steps. When the macro call appears as an evaluated form, not being used as a place, the form is expanded using the ordinary macro. Implementation of in terms of
      (define-place-macro first (obj)
        ^(car ,obj)) If is a place macro form (a form whose operator symbol has been defined as a place macro using these functions expand the place macro form and return the expanded form. Otherwise, they return performs a single expansion, expanding only the place the macro that is referenced by the symbol in the first position of and returns the expansion. Note that if is an ordinary macro form, this function will not expand it, even if such an expansion would reveal a place macro form. performs a full place expansion of by the following process. If is a place macro call, it is expanded, and the result is checked again to see whether it is a place macro, and expanded. This is repeated as many times as necessary until the result is no longer a place macro call. Then, if the resulting form is an ordinary macro invocation, it is expanded once as if by This process is iterated until a fixed point is reached. The optional parameter is a macro environment. Note: the function ignores the parameter, which could change in the future. Given this ordinary macro definition
      (defmacro leftmost (x) ^(first ,x)) the following results are obtained:
      ;; ordinary macro leftmost expands to first,
      ;; then first place macro expands to car:
      (macroexpand-place '(leftmost x)) -> (car x)
      ;; macroexpand-1-place won't expand ordinary macro:
      (macroexpand-1-place '(leftmost x)) -> (leftmost x)
      ;; macroexpand-1-place expands place macro
      (macroexpand-1-place '(first x)) -> (car x) The macro is similar to the operator. It establishes bindings for one or more which are initialized using the values of Note that the simplified syntax for a variable which initializes to by default is not supported by that is to say, the syntax cannot be used in place of the syntax when is to be initialized to The macro differs from in that assumes that those whose after macro expansion, are constant expressions (according to the function) may be safely implemented as a symbol macro rather than a lexical variable. Therefore is suitable in situations in which simpler code is desired from the output of certain kinds of machine-generated code, which binds local symbols: code with fewer temporary variables. On the other hand, is not suitable in some situations when true variables are required, which are assignable, and provide temporary storage.
      ;; WRONG! Real storage location needed.
      (rlet ((flag nil))
        (flip flag)) ;; error: flag expands to nil
      ;; Demonstration of constant-propagation
      (let ((a 42))
        (rlet ((x 1)
               (y a))
          (+ x y))) --> 43
      (expand
        '(let ((a 42))
          (rlet ((x 1)
                 (y a))
            (+ x y)))) --> (let ((a 42))
                            (let ((y a))
                            (+ 1 y))) The last example shows that the variable has disappeared in the expansion. The macro turned it into into a denoting the constant 1, which then propagated to the use site, turning the expression into The macro is a stronger form of the macro. Just like reduces bindings initialized by constant expressions to symbol macros. In addition, unlike also reduces to symbol macros those bindings whose initializing expressions are simple references to lexical variables.
       ;; reduces to let
       (slet ((a (list x y)))
         a)
       ;; b is a free variable, so this is also let
       (slet ((a b))
         a)
       ;; b is lexical, so a becomes a symbol macro
       ;; the (slet ...) form becomes b.
       (let (b)
         (slet ((a b))
           a))
       ;; a becomes symbol macro; form transforms to 1.
       (slet ((a 1))
         a) The macro ("atomic" or "all") is a stronger form of the macro. All bindings initialized by constant expressions are turned to symbol macros. Then, if all of the remaining bindings are all initialized by lexical variables, they are also turned to symbol macros. Otherwise, none of the remaining bindings are turned to symbol macros. The macro can be used even in situations when it is possible that the initializing forms the variables may have side effects through which they affect each others' evaluations. In this situation still propagates constants via symbol macros, and can eliminate the remaining temporaries if they can all be made symbol macros for existing lexicals: i.e. there doesn't exist any initialization form with interfering side effects. The macro is used for turning a function into an accessor, such that forms which call the function can be treated as places. Arguments to are two symbols, which must name functions. When the call is evaluated, the symbol is registered as a syntactic place. Stores to the place are handled via calls to If names a function which takes N arguments, must name a function which takes N+1 arguments. Moreover, in order for the accessor semantics to be correct must treat its rightmost argument as the value being stored, and must also return that value. When a function call form targeting is treated as a place which is subject to an update operation (for instance an increment via the macro), the accessor definition created by ensures that the arguments of are evaluated only once, even though the update involves a call to and with the same arguments. The argument forms are evaluated to temporary variables, and these temporaries are used as the arguments in the calls. No other assurances are provided by In particular, if and internally each perform some redundant calculation over their arguments, this cannot be optimized. Moreover, if that calculation has a visible effect, that effect is observed multiple times in an update operation. If further optimization or suppression of multiple effects is required, the more general macro must be used to define the accessor. It may also be possible to treat the situation in a satisfactory way using a definition, which effectively then supplies inline code whenever a certain form is used as a place, and that code itself is treated as a place. Note: is similar to the short form of When the accessor is invoked as a function, it behaves like simply returning the value of which is not required to be a syntactic place. If a form is used as a syntactic place then its argument must also be a The syntactic place denotes the same place as the enclosed form, but with somewhat altered semantics, which is most useful in conjunction with and in writing place-mutating macros which make multiple accesses to a place. Firstly, if the place is evaluated, it accesses the existing value of exactly once, even if it occurs in a place-mutating form which normally doesn't use the prior value, such as the macro. When accesses it stores the value in a hidden variable. Then, within the same place-mutating form, multiple references to the same form all access the value of this hidden variable. Whenever the form is assigned, both the the hidden variable and the underlying receive the new value. Multiple references to the same form can be produced using the or macros, or by making multiple calls to the getter function obtained using in the implementation of a user-defined place-mutating operator, or user-defined place. In both of the following two examples, there is no question that the and expressions are themselves evaluated only once; the issue is the access to the array itself; under the plain placelet, the array referencing takes place more times.
      ;; without read-once, array element [array i] is
      ;; accessed twice to fetch its current value: once
      ;; in the plusp expression, and then once again in
      ;; the dec expression.
      (placelet ((cell [array i]))
        (if (plusp cell)
          (dec cell)))
      ;; with read-once, it is accessed once. plusp refers
      ;; to a hidden lexical variable to obtain the prior
      ;; value, and so does dec. dec stores the new value
      ;; through to [array i] and the hidden variable.
      (placelet ((cell (read-once [array i])))
        (if (plusp cell)
          (dec cell))) The following is not an example of multiple references to the same form:
      (defmacro inc-positive (place)
        ^(if (plusp (read-once ,place))
           (inc (read-once ,place)))) Here, even though the forms may be structurally identical, they are separate instances. The first instance isn't even a syntactic place, but a call to the function. Multiple references to the same place can only be generated using or else by multiple explicit calls to the same getter function or macro generated for a place by an update expander. The following is a corrected version of
      (defmacro inc-positive (place :env env)
        (with-update-expander (getter setter) ^(read-once ,place) env
          ^(if (plusp (,getter))
             (,setter (succ (,getter)))))) To write the macro without requires that it handles the job of providing a temporary variable for the value:
      (defmacro inc-positive (place :env e)
        (with-update-expander (getter setter) place env
          (with-gensym (value)
            ^(slet ((,value (,getter)))
              ^(if (plusp ,value)
                 (,setter (succ ,value))))))) The accessor wrapped around allows to simply make multiple references to which will cache the value; the macro doesn't have to introduce its own hidden caching variable. These variables hold hash tables, by means of which update expanders, clobber expanders and delete expanders are registered, as associations between symbols and functions. If yields a function, then symbol is the basis for a syntactic place. If the expression yields then forms beginning with are not syntactic places. (The situation of a clobber accessor or delete accessor being defined without an update expander is improper). The special variable holds the hash table of associations between symbols and place macro expanders. If the expression yields a function, then symbol has a binding as a place macro. If that expression yields then there is no such binding: compound forms beginning with do not undergo place macro expansion. TXR Lisp provides a structural pattern-matching system. Structural pattern matching is a syntax which allows for the succinct expression of code which classifies objects according to their shape and content, and which accesses the elements within objects, or both. The central concept in structural pattern matching is the resolution of a pattern against an object. The pattern is specified as syntax which is part of the program code. The object is a run-time value of unknown type, shape and other properties. The primary pattern-matching decision is Boolean: does the object match the pattern? If the object matches the pattern, then it is possible to execute an associated body of code in a scope in which variables occurring in the pattern take on values from the corresponding parts of the object. Structural pattern matching is available via several different macro operators, which are: and Function and macro argument lists may also be augmented with pattern matching using the parameter macro. The macro is the simplest. It tests an object against a pattern, and if there is a match, evaluates zero or more forms in an environment in which the pattern variables have bindings to the corresponding elements of the object. The macro evaluates a single form if there is a match, in the scope of the bindings established by the pattern, otherwise an alternative form evaluated in a scope in which those bindings are absent. The macro tests and object against a pattern, expecting a match. If the match fails, an exception is thrown. Otherwise, it evaluates zero or more forms in the scope of the bindings established by the pattern. The macro evaluates the same object against multiple clauses, each consisting of a pattern and zero or more forms. At most one matching clause is identified and evaluated. The macro is similar to except that if no matching case is identified, an exception is thrown. The macro evaluates multiple clauses, each of which specifies a pattern and an object expression. If the object produced by the expression matches the pattern, the forms in the clause are evaluated in scope of the variables bound by the clause's pattern. The macro provides a way to express an anonymous function whose argument list is matched against multiple clauses similarly to and provides a way to define a top-level function using the same concept. Additionally, there exist and macro families. TXR Lisp's structural pattern-matching notation is template-based. With the exception of structures and hash tables, objects are matched using patterns which are based on their printed notation. For instance, the pattern is a pattern matching the list binding to The notation supports lists, vectors, ranges and atoms. Atoms are compared using the function. Thus, in the above pattern, the 1 and 2 in the pattern match the corresponding 1 and 2 atoms in the object using All parts of a pattern are static material which matches literally, except those parts introduced by the meta prefix This prefix denotes variables like as well as useful pattern-matching operators like which matches a list or sublist whose elements all match The quasiquote syntax is specially supported for expressing matching, in an alternative style. For instance the quasiquote is a pattern equivalent to the Structure objects are matched using a dedicated operator, or else in the quasiquote style using syntax. The non-quasiquoted literal syntax cannot be used for matching. Similarly, hash objects are matched using a operator, or else syntax in the quasiquote style. cannot be used. Note: the non-quasiquoted and literals are not and cannot be used for matching because they produce structure and hash objects which lose important information about how they were specified in the syntax, and carry restrictions which are unacceptable for pattern matching. The order of sub-patterns is important in pattern syntax, but struct and hash objects do not preserve the order in which their elements were specified. A struct literal is required to specify the name of an existing struct type, and slot names which are valid for that type, otherwise it is erroneous. This is not acceptable for pattern matching, because patterns may appear in place of those elements. The pattern match for a hash may specify the same key pattern more than once, which means that the key pattern cannot be an actual key in an actual hash, which requires every key to be unique. Structure and hash quasiquotes do not have these issues; they are not actually literal structure and hash objects, but list-based syntax. Patterns use meta-symbols for denoting variables. Variables must be either bindable symbols, or else which has a special meaning: the pattern variable matches any object, and binds no variable. Pattern variables are ordinary Lisp variables. Whereas in ordinary non-pattern matching Lisp code, it is always unambiguous whether a variable is being bound or referenced, this is deliberately not the case in patterns. A variable occurring in a pattern may be a fresh variable, or a reference to an existing one. The difference between these situations is not apparent from the syntax of the pattern; it depends on the context established by the scope. With one exception, if a pattern contains a variable which is already bound in the surrounding scope, then it refers to that binding. Otherwise, it freshly binds the variable. The exception is that pattern operator always binds a fresh variable. A variable being already bound includes as a lexical or global symbol macro or When a pattern variable refers to an existing variable, then each occurrence of that variable must match an object which is to the value of that variable. For instance, the following function returns the third element of a list, if the first two elements are repetitions of the argument, otherwise
      (defun x-x-y (list x)
        (when-match (@x @x @y) list y))
      (x-x-y '(1 1 2) 1) -> 2
      (x-x-y '(1 2 3) 1) -> nil ;; no @x @x match
      (x-x-y '(1 1 2 r2) 1) -> nil ;; list too long If the variable does not exist in the scope surrounding the pattern, then the leftmost occurrence of the variable establishes a binding, taking the value from is corresponding object being matched by that occurrence of the variable. The remaining occurrences of the variable, if any, must correspond to objects which are to that value, or else there is no match. For instance, the pattern matches the list like as follows. First binds to the leftmost and then the second matches the existing value of that An input such as fails to match because the second occurrence of retrieves an object that is not to that variable's existing value. A pattern can contain multiple occurrences of the same symbol as a variable. These may or may not refer to the same variable. Two occurrences of the same symbol refer to distinct variables if:

    1.  they are freshly bound in separate branches of the operator; or

    2.  one of the two variables is freshly bound by the operator and the other variable occurs outside of that or

    3.  or both of the variables are freshly bound using
Any other two or more occurrences same symbol occurring in the same pattern refer to the same variable. TXR Lisp's macro-style parameter lists, appearing in and related macros, also provide a form of structural pattern matching. Macro-style parameter list pattern matching is limited to objects of one kind: tree structures made of cells. It is only useful for matching on shape, not content. For example, cannot express the idea of matching a list whose first element is the symbol and whose third element is Moreover, every position in the tree pattern much specify a variable which captures the corresponding element of the structure. For instance, a pattern which matches a three-element list must specify three variables, one for each list position. This is because macro-style parameter lists are oriented toward writing macros, and macros usually make use of every parameter position. User-defined pattern operators are possible. When the symbol in the syntax doesn't match any built-in operator, a search takes place to determine whether is a pattern macro. If so, the pattern macro is expanded, and its result of the expansion treated as a pattern to process recursively, unless it is the original macro form, in which case it is treated as a predicate pattern. User-defined pattern macros are defined using the macro. The pattern-matching notation is documented in the following sections; a section describing the pattern-matching macros follows. A pattern consisting of an atom other than a vector matches a similar object. The similarity is determined using the function. The atom is not subject to evaluation, which means that a symbolic atom stands for itself, and not the value of a variable.
  ;; the pattern 1 matches the object 1
  (if-match 1 1 'yes 'no) --> yes
  ;; the object 0 does not match
  (if-match 1 0 'yes 'no) --> no
  ;; a matches a, does not match b
  (let ((sym 'a))
    (list (if-match a sym 'yes 'no)
          (if-match b sym 'yes 'no)))
  --> (yes no) A meta-symbol can be used as a pattern expression. This pattern unconditionally matches an object of any kind. The is required to be a either a bindable symbol according to the function, or else the symbol If is a bindable symbol, which has not binding in scope, then a variable by that name is freshly bound, and takes on the corresponding object as its value. If is a bindable symbol with an existing binding, then the corresponding object must be to that variable's existing value, or else the match fails. If is then the match succeeds unconditionally, without binding a variable.
  (when-match @a 42 (list a)) -> (42)
  (when-match (@a @b @c) '(1 2 3) (list c b a)) -> (3 2 1)
  ;; No match: list is longer than pattern
  (when-match (@a @b) '(1 2 3) (list a b)) -> nil
  ;; Use of nil in dot position to match longer list
  (when-match (@a @b . @nil) '(1 2 3) (list a b)) -> (1 2) Pattern syntax consisting of a nonempty, possibly improper list matches list structure. A pattern expression may be specified in the dotted position. If it is omitted, then there is an implicit terminating which constitutes an atom expression matching A list pattern matches a list of the same shape. For each expressions, there must exist an item in the list. A match occurs when every matches the corresponding element of the list, including the in the dotted position. Because the dotted position matches a list, it is possible for a short pattern to match a longer list. The syntax is indicated as requiring at least one because otherwise the list is empty, which corresponds to the atom pattern The syntax is valid, but indistinguishable from and therefore is not a list pattern.
  (if-match (@a @b @c . @d) '(1 2 3 . 4) (list d c b a))
  --> (4 3 2 1)
  ;; 2 doesn't satisfy oddp
  (if-match (@(oddp @a) @b @c . @d) '(2 x y z)
    (list a b c d)
    :no-match)
  --> :no-match
  ;; 1 and 2 match, a takes (3 4)
  (if-match (1 2 . @a) '(1 2 3 4) a) --> (3 4)
  ;; nesting
  (if-match ((1 2 @a) @b) '((1 2 3) 4) (list a b)) -> (3 4) A pattern match for a vector is expressed using vector notation enclosing pattern expressions. This pattern matches a vector object which contains exactly as many elements as there are patterns. Each pattern is applied against the corresponding vector element.
  ;; empty vector pattern matches empty vector
  (if-match #() #() :yes :no) -> :yes
  ;; empty vector pattern fails to match nonempty vector
  (if-match #() #(1) :yes :no) -> :no
  ;; match with nested list and vector
  (if-match #((1 @a) #(3 @b)) #((1 2) #(3 4)) (list a b))
  --> (2 4) A pattern match for a range can be expressed by embedding pattern expressions in the notation. The resulting pattern requires the corresponding object to be a range, otherwise the match fails. If the corresponding object is a range, then the is matched against its and the is matched against its part. Note that if the range expression notation is used as a pattern, that is actually a list pattern, due to that being a syntactic sugar for
  (if-match #R(10 20) 10..20 :yes :no) -> :yes
  (if-match #R(10 20) #R(10 20) :yes :no) -> :yes
  (if-match #R(10 20) #R(1 2) :yes :no) -> :no
  (when-match #R(@a @b) 1..2 (list a b)) -> (1 2)
  ;; not a range match! rcons syntax match
  (when-match @a..@b '1..2 (list a b)) -> (1 2)
  ;; above, de-sugared:
  (when-match (rcons @a @b) '(rcons 1 2) (list a b)) -> (1 2) The quasiliteral syntax is supported as a pattern-matching operator. The corresponding object is required to be a character string, which is analyzed according to the structure of the quasiliteral pattern, and portions of the string are captured by variables. If the corresponding object isn't a string according to then the match fails. The quasiliteral pattern must match the entire input string. In order that the quasiliteral's syntactic structure is not misinterpreted as a predicate pattern, and in order to make certain situations work in quasiquoted pattern matching, a quasiliteral pattern may be specified as either or The latter form, which is structurally is specially recognized and treated as equivalent to the unadorned quasiliteral pattern. A quasiliteral pattern matches in a linear fashion, from left to right. Variables bound earlier in the pattern can be referenced later in the pattern as bound variables. With one exception, bound variables denote character strings in accordance with the usual quasiliteral conversion and formatting rules. All of the modifier notations may be used. For instance, if is a bound variable, then denotes the value of converted to a string, and right-aligned in a forty-character-wide field. Consequently, the notation matches exactly such a forty-character text. The exception is that if a bound variable has a regular expression modifier, as in then it has a special meaning as a pattern. Moreover, this syntax has no meaning in a quasiliteral. In the following description of the quasiliteral pattern-matching rules, the symbols and represent to unbound variables: variables which have no apparent lexical binding and are not defined as global variables. Unless indicated otherwise, refers to a plain variable syntax such as or else to braced syntax without modifiers, such as The same remarks apply to and The symbol represents a bound variable: a variable which has an existing binding, which can occur in the form of the ordinary notation, or the braced notation with or without modifiers. The notation denotes a substring of the pattern, possibly empty.
    The empty quasiliteral pattern matches an empty string. A quasiliteral pattern which begins with a portion of text matches a string which begins with the same text. The remaining portion of the pattern is then matched against a suffix of the input string which excludes the matched text. A simple unbound variable occurring as the last element of the pattern matches and binds the entire rest of the input string. A simple unbound variable followed by a text element matches the input string if occurs in that string as a substring. In that case, is bound to the possibly empty prefix of the input string consisting of the characters before the leftmost match for The rest of the pattern is then matched against that suffix of the input string which begins after the last character of the leftmost match for The bound variable is converted to text in the manner of an ordinary quasiliteral substitution. The situation then reduces to the pattern, where denotes the character string produced by substitution of An unbound variable which uses the brace notation to specify a literal modifier denotes a match for that many characters. It is an error if the value is zero or negative. The match succeeds if the input string has at least that many characters, in which case the variable takes on those characters, and the rest of the pattern is matched against a suffix of the string with those characters removed. An unbound variable which carries a regular-expression modifier specifies a regular-expression match. If a prefix of the input string matches then the match is successful and captures that prefix. The rest of the pattern is then matched against the rest of the string after the prefix. A bound variable which carries a regular expression modifier specifies a regular expression match exactly like an unbound variable. This syntax produces a successful match if two conditions are met: a prefix of the input string matches and the matched prefix is to the value of The rest of the pattern is then matched against the rest of the string after the prefix. The bound variable is converted to text the manner of an ordinary quasiliteral substitution. The situation then reduces to the pattern, where denotes the character string produced by substitution of Two consecutive unbound variables, where is a plain variable with no modifiers, constitutes an invalid pattern. This situation is diagnosed as an error. If is braced, carrying an integer or regular-expression modifier then the situation is treated as the pattern where refers to the portion.

    No other quasiliteral syntax, or combination of variable modifiers, is supported in quasiliteral patterns.
      (when-match `@a-@b` "foo-bar" (list a b)) -> ("foo" "bar")
      (when-match `@{a #/\d+/}@b` "123xy" (list a b)) -> ("123" "xy")
      (let ((a 42))
        (when-match `[@{a -8}] @b` "[ 42] packets` b))
      -> "packets" Quasiquoting provides an alternative pattern-matching syntax. It uses a subset of the quasiquoting notation. Only specific kinds of quasiquoted objects listed in this description are supported. Within a quasiquote used for pattern-matching, unquotes indicate operators and variables instead of the prefix. Splicing unquote syntax plays no role; its presence produces unspecified behavior. The quasiquote matching notation is described, understood and implementing in terms of a translation to the standard pattern-matching syntax, according to the following rules. The notation used here indicates that the element enclosed in brackets is subject to a recursive translation according to the rules:
    An unquoted expression occurring in the quasiquote is translated to the pattern-matching syntax. If is a symbol, then this is a meta-variable: otherwise it is translated to the syntax. An unquoted quasiliteral is treated uniformly as and is therefore translated into Since that is equivalent to quasiliteral matching is supported within quasiquote notation in a straightforward way. In JSON syntax, unquotes are given the same above treatment as (comma) unquotes in ordinary syntax. Similarly, quasiliterals are supported in JSON syntax. Hash quasiliteral syntax is translated according to the pattern, with each key and value recursively translated. The syntax must specify for the hash construction arguments part, otherwise an error is diagnosed. That is to say, it must be of the form where the first element is Structure quasiliteral syntax is translated according to the pattern. Vector quasiliteral syntax is translated according to the pattern: it becomes a vector object containing embedded patterns. A JSON array quasiquote is translated into exactly like a vector. Here, the transformation recognizes JSON (tilde) unquotes, and recursively recognizes and transform JSON syntax not prefixed by A JSON hash quasiquote is translated into exactly like a hash. Tree structure is translated according to the pattern: it is recursively examined for translations. A nested quasiquote pattern is diagnosed as an error. Splicing syntax is diagnosed as an error. Splicing JSON syntax is diagnosed as an error inside a JSON quasiliteral. Any other quasiquoted object is left untranslated.

      ;; basic unquote: variables embedded via unquote,
      ;; not requiring @ prefix.
      (when-match ^(,a ,b) '(1 2) (list a b))
      --> (1 2)
      ;; operators embedded via unquote; interior of operators
      ;; is regular non-quasiquoting pattern syntax.
      (when-match ^(,(oddp @a) ,(evenp @b)) '(1 2) (list a b))
      --> (1 2)
      (when-match ^#(,a ,b) #(1 2) (list a b))
      --> (1 2)
      (when-match ^#S(,type year ,y) #S(time year 2021)
        (list (struct-type-name type) y))
      --> (time 2021)
      (when-match ^#H(() (x ,y) (,(symbolp @y) ,datum))
                  #H(() (x k) (k 42))
                  datum)
      --> (42)
      ;; JSON syntax
      (when-match ^#J~a 42.0 a) --> 42.0
      (when-match ^#J[~a, ~b] #J[true, false] (list a b)) --> (t nil)
      (when-match ^#J{"x" : ~y, ~(symbolp @y) : ~datum}
                  #J{"x" : true, true : 42}
                  datum)
      --> (42.0)
      (when-match ^#J{"foo" : {"x" : ~val}}
                  #J{"foo" : {"x" : "y"}} val)
      --> "y" The pattern operator matches a structure object. The operator supports two modes of matching, the choice of which depends on whether the first argument is a or a The first argument is considered a if it is a bindable symbol according to the function. In this situation, the operator operates in strict mode. Otherwise, the operator is in loose mode. The or argument is followed by zero or more pairs, which are not enclosed in lists, similarly to the way slots are presented in the struct syntax and in the argument conventions of the macro. In strict mode, is assumed to be the name of an existing struct type. The object being matched is tested whether it is a subtype of this type, as if using the function. If it isn't, the match fails. In loose mode, the object being matched is tested whether it is a structure object of any structure type. If it isn't, the match fails. In strict mode, each pair requires that the object's slot of that name contain a value which matches The operator assumes that all the are slots of the struct type indicated by In loose mode, no assumption is made that the object actually has the slots specified by the arguments. The object's structure type is inquired to determine whether it has each of those slots. If it doesn't, the match fails. If the object has the required slots, then the values of those slots are matched against the patterns. In loose mode, the given in the first argument position of the syntax is matched against the object's structure type: the type itself, rather than its symbolic name.
      ;; extract the month from a time structure
      ;; that is required to have a year of 2021.
      (when-match @(struct time year 2021 month @m)
                  #S(time year 2021 month 1)
        m) -> 1
      ;; match any structure with name and value slots,
      ;; whose name is foo, and extract the value.
      (defstruct widget ()
        name
        value)
      (defstruct grommet ()
        name
        value)
      (append-each ((obj (list (new grommet name "foo" value :grom)
                            (new widget name "foo" value :widg))))
        (when-match @(struct @type name "foo" value @v) obj
          (list (list type v))))
      --> ((#<struct-type grommet> :grom)
           (#<struct-type widget> :widg)) The pattern operator matches a hash-table object by means of patterns which target keys, values or both. An important concept in the requirements governing the operation of the operator is that of a trivial pattern. A pattern is nontrivial if it is a variable or operator pattern. A pattern is also nontrivial if it is a list, vector or range pattern containing at least one nontrivial pattern. Otherwise, it is trivial. The operator requires the corresponding object to be a hash table, otherwise the match fails. If the corresponding object is a hash table, then matches each and pair against that object as described below. Each of the pairs must successfully match, otherwise the overall match fails. The following requirements apply to key-value pattern pairs in which the value pattern is specified. If is a trivial pattern, then the semantics of the match is that is taken as a literal object representing a hash key. The hash table is searched for that key. If the key is not found, the match fails. Otherwise, the value corresponding to that key is matched against the which may be trivial or nontrivial. If is a simple variable pattern and if has an existing binding, then the value of is looked up in the hash table. If it is not found, then the match fails, otherwise the corresponding value is matched against which may be trivial or nontrivial. If is a nontrivial pattern other than a variable pattern for a variable which has an existing binding, and if is trivial, then is taken as a literal object, which is used for searching the hash table for one or more keys, as if it were the argument in a call to the function, to find all keys which have a value to that value. If no keys are found, then the match fails. Otherwise, the is then matched against the retrieved list of hash keys. Finally, if both and are nontrivial, then an exhaustive search is performed of the hash table. Every key in the hash table is matched against and if it matches, the value is matched against If both match, then the values from the matches are collected into lists. At least one matching key-value pair must be found, otherwise the overall match fails. Note: this situation can be understood as if the hash table were an association list of cells of the form and as if the two patterns were combined into a operator against this list in the following way: such that the semantics can then be understood in terms of the operator matching against an association list. The following requirements apply when the is omitted. If is a nontrivial pattern other than a variable pattern for a variable which has an existing binding, then the pattern is applied against the list of keys from the hash table, which are retrieved as if using the function. If is a variable pattern referring to an existing binding, then that pattern is taken as a literal object. The match is successful if that object occurs as a key in the hash table.
      ;; First, (x @y) has a trivial key pattern so the x
      ;; entry from the hash table is retrieved, the
      ;; value being the symbol k. This k is bound to @y.
      ;; Because y now a bound variable the pattern (@y @datum)
      ;; is interpreted as search of the hash table for
      ;; a single entry matching the value of @y. This
      ;; is the k entry, whose value is 42. The @datum
      ;; value match takes this 42.
      (when-match @(hash (x @y) (@y @datum))
                  #H(() (x k) (k 42)) datum)
      --> 42
      ;; Again, (x @y) has a trivial key pattern so the x
      ;; entry from the hash table is retrieved, the
      ;; value being the symbol k. This k is bound to @y.
      ;; This time the second pattern has a @(symbolp)
      ;; predicate operator. This is not a variable, and
      ;; so the pattern searches the entire
      ;; hash table. The @y variable has a binding to k,
      ;; so only the (k 42) entry is matched. The 42
      ;; value matches @datum, and is collected into a list.
      (when-match @(hash (x @y) (@(symbolp @y) @datum))
                  #H(() (x k) (k 42)) datum)
      --> (42) The pattern operator binds the corresponding object to a fresh variable given by similarly to the Lisp operator. If another variable called exists, it is shadowed; thus, no back-referencing is performed. The argument must be a bindable symbol, or else If is then no name is bound. Thus is equivalent to Otherwise, processed in a scope in which the new binding is already visible. The operator succeeds if matches. Note: in a situation when it is necessary to bind a variable to an object in parallel with one or more patterns, such that the variable can back-reference to an existing occurrence, the pattern operator can be used.
      ;; w captures the entire (1 2 3) list:
      (when-match @(as w (@a @b @c)) '(1 2 3) (list w a b c))
      --> ((1 2 3) 1 2 3)
      ;; match a list which has itself as the third element
      (when-match @(as a (1 2 @a 4)) '#1=(1 2 #1# 4) :yes)
      --> :yes The pattern operator matches the optional against a corresponding object, while matching a or against the value of the expression which is embedded in the syntax. First, if is present in the syntax, it is matched against its corresponding object. This match must succeed, or else the operator fails to match, in which case is not evaluated. Next, if successfully matched, or is absent, is evaluated in the scope of earlier pattern variables, including any which that emanate from It is unspecified whether later pattern variables are visible. Finally, is matched against the value of If that succeeds, then the operator has successfully matched. If a is specified instead of a it must be a bindable symbol or else
      (when-match (@(with @a x 42) @b @c) '(1 2 3) (list a b c x))
      --> (1 2 3 42)
      (let ((o 3))
        (when-match (@(evenp @x) @(with @z @(oddp y) o)) '(4 6)
          (list x y z)))
      --> (4 3 6) The pattern operator applies the specified to the corresponding object. If the matches, the operator then imposes the additional constraints specified by zero or more forms. Each is evaluated in a scope in which the variables from have already been established. For the operator to be a successful match, every must evaluate true, otherwise the match fails. The forms behave as if they were the arguments of an implicit operator, which implies left-to-right evaluation behavior, stopping evaluation on the first which produces and defaulting to a result of when no forms are specified.
      ;; Match a (+ a b) expression where a and b are similar:
      (when-match @(require (+ @a @b) (equal a b)) '(+ z z) (list a b))
      --> (z z)
      ;; Mismatched case
      (if-match @(require (+ @a @b) (equal a b)) '(+ y z)
         (list a b)
         :no-match)
      --> :no-match The and pattern operators require the corresponding object to be a sequence. The specified is applied against every element of the sequence. The match is successful if matches every element. Furthermore, in the case of a successful match, each variable that is freshly bound by is converted into a list of all of the objects which that variable encounters from all elements of the sequence. Those variables which already have a binding from another are not converted to lists. Their existing values are merely required to match each corresponding object they encounter. The difference between and is as follows. The operator respects the vacuous truth of the match when the sequence is empty. In that case, the match is successful, and the variables are all bound to the empty list In contrast, the alternative operator behaves like a failed match when the sequence is empty.
      ;; all elements of list match the pattern (x @a @b)
      ;; a is bound to (1 2 3); b to (a b c)
      (when-match @(all (x @a @b))
                 '((x 1 a) (x 2 b) (x 3 c))
        (list a b))
      --> ((1 2 3) (a b c))
      ;; Match a two element list whose second element
      ;; consists of nothing but zero or more repetitions
      ;; of the first element. x is not turned into a list
      ;; because it has a binding due to @x.
      (when-match @(@x @(all x)) '(1 (1 1 1 1)) x) -> 1
      ;; no match because of the 2
      (when-match @(@x @(all x)) '(1 (1 1 1 2)) x) -> nil The pattern operator requires the corresponding object to be a sequence. The specified is applied against every element of the sequence. The match is successful if matches at least one element. Variables are extracted from the first matching which is found.
      ;; the second (x 2 b) element is the leftmost one
      ;; which matches the (x @a @b) pattern
      (when-match @(some (x @a @b))
                 '((y 1 a) (x 2 b) (z 3 c))
        (list a b))
      -> (2 b) The pattern operator requires the corresponding object to be a sequence. The specified is applied against every element of the sequence. The match is successful if matches at least one element. Each variable that is freshly bound by the is converted into a list of all of the objects which that variable encounters from the matching elements of the sequence. Those variables which already have a binding from another are not converted to lists. Their existing values are merely required to match each corresponding object they encounter. Variables are extracted from all matching elements, and collected into parallel lists, just like with the operator.
      (when-match @(coll (x @a @b))
                 '((y 1 a) (x 2 b) (z 3 c) (x 4 d))
        (list a b))
      -> ((2 4) (b d)) The operator matches against the corresponding object. If the match fails, and the object is a cell, the match is tried on the of the cons cell. The traversal repeats until a successful match is found, or a match failure occurs against against an atom. Thus, a list object, possibly improper, matches under if any suffix of that object matches.
      ;; mismatch: 1 doesn't match 2
      (when-match @(scan 2) 1 t) -> t
      ;; simple atom match: 42 matches 42
      (when-match @(scan 42) 42 t) -> t
      ;; (2 3) is a sublist of (1 2 3 4)
      (when-match @(scan (2 3 . @nil)) '(1 2 3 4) t) -> t
      ;; (2 @x 4 . @nil) matches (2 3 4), binding x to 3:
      (when-match @(scan (2 @x 4 . @nil)) '(1 2 3 4 5) x) -> 3
      ;; The entire matching suffix can be captured.
      (when-match @(scan @(as sfx (2 @x 4 . @nil)))
                   '(1 2 3 4 5)
        sfx)
      -> (2 3 4 5)
      ;; Missing . @nil in pattern anchors search to end:
      (when-match @(scan (@x 2))
                   '(1 2 3 2 4 2)
        x)
      ;; Terminating atom anchors to improper end:
      (when-match @(scan (@x . 4))
                   '(1 2 3 . 4)
        x)
      -> 3
      ;; Atom pattern matches only terminating atom
      (when-match @(scan #(@x @y))
                   '(1 2 3 . #(4 5))
        (list x y))
      -> (4 5) The and operators match multiple patterns in parallel, against the same object. The operator matches if every matches the object, otherwise there is no match. The operator requires one to match. It tries the patterns in left-to-right order, and stops at the first matching one, declaring failure if none match. The and operators have different scoping rules. Under later patterns are processed in the scopes of earlier patterns, just like with other pattern operators. Duplicate variables back-reference. Under the patterns are processed in separate, parallel scopes. No back-referencing takes place among same-named variables introduced in separate patterns of the same When the matches, the variables from all of the patterns are bound. When the operator matches, the variables from all of the patterns are also bound. However, only the variables from the matching take on the values implied by that pattern. The variables from the nonmatching patterns that do not have the same names as variables in the matching and that have been newly introduced in the operator, take on values.
      (if-match @(and (@x 2 3) (1 @y 3) (1 2 @z)) '(1 2 3)
        (list x y z)) -> (1 2 3)
      (if-match @(or (@x 3 3) (1 @x 3) (1 2 @x)) '(1 2 3)
        x) -> 2 The pattern operator provides logical inverse semantics. It matches if and only if the does not match. Whether or not the operator matches, no variables are bound. If the embedded matches, the variables which it binds are suppressed by the operator.
       ;; @a matches unconditionally, so @(not @a) always fails:
       (if-match @(not @a) 1 :yes :no) -> :no
       ;; error: a is not bound
       (if-match @(not @a) 1 :yes a) -> error
       (match-case '(1 2 3)
         ((@(not 1) @b @c) (list :case1 b c))
         ((@(not 0) @b @c) (list :case2 c b)))
       --> (:case2 3 2) Whenever the operator position of a pattern consists of a symbol which is neither the name of a pattern operator, nor the name of a macro, the expression denotes a predicate pattern. An expression is also a predicate pattern if it is handled by a pattern macro which declines to expand it by yielding the original expression. An operator pattern is expected to conform to one of the first three syntactic variations above. Together, these three variations constitute the first form of the pattern predicate operator. Whenever the operator position of a pattern consists of a meta-symbol, it is also a predicate pattern, expected to conform to one of the second three syntax variations above. These three variations constitute the second form of the operator. The first form of the predicate pattern consists of a compound form consisting of an operator and arguments. Exactly one of the arguments may be a pattern variable ("argument variable") which must be a bindable symbol or else The pattern variable may also appear in the dot position, rather than as an argument. The role of and the consequences of omitting it are described below. The second form of the predicate pattern consists of a meta-symbol ("result variable") which must be a bindable symbol or else This is followed by a compound form which consists of an operator symbol, followed by arguments, one of which may be a pattern as in the simple form. If is then the predicate pattern is equivalent to the first form. That is to say, the following are equivalent:
      @(@nil (f ...)) <--> @(f ...) The matching of the predicate pattern is processed as follows. If the variable is present, then the predicate pattern first binds the corresponding object to the variable, performing an ordinary variable match with the potential back-referencing which that implies. If that succeeds, then the object is inserted into the compound form, substituted in the position indicated by the variable, either an ordinary argument position or the dot position. This form is then evaluated. If it yields true, then the match is successful, otherwise the match fails. If the variable is absent, then no initial variable matching takes place. The corresponding object is added as an extra rightmost argument into the compound form, which is evaluated. Its truth value then determines the success of the match, just like in the case with If the second form is being processed, and specifies a that is not and if the predicate has succeeded, then then an extra processing step takes place. A variable match is performed to bind the variable to the result of the predicate, with potential back-referencing. If that match succeeds, then the predicate pattern succeeds. The compound form may be headed by the operator, and therefore the DWIM bracket notation may be used. For instance is equivalent to and is processed accordingly. Similarly, is equivalent to The dot position of in the predicate syntax denotes function application. So that is to say, the pattern predicate form where is in the dotted position invokes the function as if by evaluation of the form where is hidden temporary variable holding the object corresponding to the pattern. The form is a standard TXR Lisp notation with the same meaning as If is the symbol, then no variable is bound. The matched object is substituted into the predicate expression at the position indicated by
      (when-match (@(evenp) @(oddp @x)) '(2 3) x) -> 3
      (when-match @(<= 1 @x 10) 4 x) -> 4
      (when-match @(@d (chr-digit @c)) #\5 (list d c)) -> (5 #\5)
      (when-match @(<= 1 @x 10) 11 x) -> nil
      ;; use hash table as predicate:
      (let ((h #H(() (a 1) (b 2))))
        (when-match @[h @x] 'a x))
      -> a
      ;; as above, also capture hash value
      (let ((h #H(() (a 1) (b 2))))
        (when-match @(@y [h @x]) 'a (list x y)))
      -> (a 1)
      ;; apply (1 2 3) to < using dot position
      (when-match @(@x (< . @sym)) '(1 2 3) (list x sym))
      -> (t (1 2 3))
      ;; Match three-element list whose middle element
      ;; is a number in the range 10 20, without
      ;; binding any variables:
      (when-match (@nil @(<= 10 @nil 20) @nil) obj
        (prinl "obj matches")) The pattern macro (start, middle, end) is a notation defined using the macro. The macro generates a complex pattern which matches three non-overlapping parts of a list object using three patterns. The pattern is required to match a prefix of the input list. If that match is successful, then the remainder of the list is searched for a match for using the operator. If that match, in turn, is successful, then the suffix of the remainder of the list is required to match The optional and arguments must be bindable symbols, if they are specified. These symbols specify lexical variables which are bound to, respectively, the object matched by and using the fresh binding semantics of the pattern operator. The first two patterns, and must be possibly dotted list patterns. The last pattern, may be any pattern: it may be an atom match for the terminating atom, or a possibly dotted list pattern matching the list suffix. Important to the semantics of is the concept of the length of a list pattern. The length of a pattern with a pattern variable or operator in the dotted position is the number of items before that variable or operator. The length of is 2; likewise the length of is also 2. The length of a pattern which does not have a variable or operator in the dotted position is simply its list length. For instance, the pattern has length 3, and so does the pattern The length is determined by the list object structure of the pattern, and not the printed syntax used to express it. Thus, is still a length 3 pattern, because it denotes the same object, using the dot notation unnecessarily. The non-overlapping semantics of evolves as follows. In the following description, it is understood that a match is required at every step. If that match fails, then the entire operator fails:

    1.  First, is required to match a a prefix of the input list. If the match succeeds, then a middle suffix of the input is calculated by dropping from it leading elements. The number of elements dropped is equal to the length of

    2.  The middle suffix is then searched for an occurrence of the middle pattern as if using the pattern operator. All elements skipped by the search are dropped, until a match is found.

    3.  At that point, if has been specified, it is bound to the remaining input, which still includes the part which just matched

    4.  Next, a number of elements equal to the length of are dropped from the middle suffix, leaving a residue comprising the final suffix.

    5.  The end pattern must then match a suffix of the final suffix.

    6.  If the variable has been specified, it is bound to the entire suffix that was matched by
  (when-match @(sme (1 2) (3 4) (5 . 6) m e)
              '(1 2 3 4 5 . 6)
    (list m e))
  -> ((3 4 5 . 6) (5 . 6))
  (when-match @(sme (1 2) (3 4) (5 . 6) m e)
              '(1 2 abc 3 4 def 5 . 6)
    (list m e))
  ((3 4 def 5 . 6) (5 . 6))
  ;; backreferencing
  (when-match @(sme (1 @y) (@z @x @y @z) (@x @y)) '(1 2 3 1 2 3 1 2)
    (list x y z))
  -> (1 2 3))
  ;; collect odd items starting at 3, before 7
  (when-match @(and @(sme (1 @x) (3) (7) m e)
                    @(with @(coll @(oddp @y)) (ldiff m e)))
              '(1 2 3 4 5 6 7)
    (list x y))
  -> (2 (3 5)))
  ;; no overlap
  (when-match @(sme (1 2) (2 3) (3 4)) '(1 2 3 4) t) -> nil
  ;; The atom 5 is like a "zero-length improper list".
  (when-match @(sme () () 5) 5 t) -> t The pattern macro is a notation defined using the macro, which matches against the suffix of a corresponding list object, which may be an improper list or atom. The optional argument specifies the name of a variable which captures the matched portion of the object. The macro is related to the macro according to the following equivalence:
  @(end pat var) <--> @(sme () () pat : var) All of the requirements given for apply accordingly.
  ;; atom match
  (when-match @(end 3 x) 3 x) -> 3
  ;; y captures (2 3)
  (when-match @(end (2 @x) y)
             '(1 2 3)
    (list x y))
  -> (3 (2 3))
  ;; variable in dot position
  (when-match @(end (2 . @x) y)
              '(1 2 . 3)
    (list x y))
  -> (3 (2 . 3))
  ;; z captures entire object
  (when-match @(as z @(end (2 @x) y))
             '(1 2 3)
    (list x y z))
  -> (3 (2 3) (1 2 3))) The and macros conditionally evaluate code based on whether the value of matches The macro arranges for every to be evaluated in the scope of the variables established by when it matches the object produced by The value of the last is returned, or else if there are no forms. If the match fails, the forms are not evaluated, and is produced. The macro behaves exactly like when the match is successful. When the match fails, throws an exception of type The macro evaluates in the scope of the variables established by if the match is successful, and yields the value of that form. Otherwise, it evaluates which defaults to if it is not specified. The macro matches the value of against zero or more patterns. Normally, the patterns are considered in left-to-right order. If the value matches more than one the leftmost is selected and that clause is evaluated. Under certain conditions, detailed below, it is possible for and to be transformed into a form. In that case, if there are multiple clauses with equivalent patterns, it is not specified which one is evaluated. The syntax of consists of an expression followed by zero or more clauses. Each clause is a compound expression whose first element is which is followed by zero or more forms. First, is evaluated. Then, the value is matched against each in succession, stopping at the first pattern which provides a successful match. If no pattern provides a successful match, then terminates and returns If a matches successfully, then each associated with the pattern is evaluated in the scope of the variable bindings established by that Then terminates, returning the value of the last or else if there are no forms. The macro differs from as follows. When none of the clauses match under then that form terminates with a value of In the same situation, the form throws an exception of type An form may be transformed to a form if all the are trivial. A trivial pattern is either an atom, or else a vector or list expression containing no variables. A form may be transformed to a form under the same conditions as Additionally, may also be transformed if it contains exactly one clause which matches any object by means of the key or else a variable match such as if that clause appears last. That clause is transformed into an of the form.
  ;; classify sequence of objects by pattern matching,
  ;; returning a list of the results
  (collect-each ((obj (list '(1 2 3)
                        '(4 5)
                        '(3 5)
                        #S(time year 2021 month 1 day 1)
                        #(vec tor))))
    (match-case obj
      (@(struct time year @y) y)
      (#(@x @y) (list x y))
      ((@nil @nil @x) x)
      ((4 @x) x)
      ((@x 5) x)))
   --> (3 5 3 2021 (vec tor))
   ;; default case can be represented by a guaranteed match
   (match-case 1
     (2 :two)
     (@x :default)) --> :default The macro's arguments are zero or more clauses, each of which specifies a an expression and zero or more The clauses are processed in order. Successive are evaluated, and matched against their corresponding pattern. If there is no match, processing continues with the next clause. If no match is found in any clause, the form terminates, returning If an value matches the corresponding then every is evaluated in scope of the variables established by the pattern. The then terminates, yielding the value of the last or else the value of if there are no Note: the pattern is recommended for specifying an unconditionally matching clause.
  (let ((x 42))
    (match-cond
       (`@x-73` "73-73" :a)
       (`@x-@y` "42-24" y)))
  --> "24" The is conceptually similar to The arguments of are zero or more clauses similar to those of each consisting of a compound expression headed by a followed by zero or more The macro generates a expression which evaluates to an anonymous function in the usual way. When the anonymous function is called, each clause's is matched against the function's actual arguments. When a match occurs, each associated with the is evaluated, and the value of the last becomes the return value of the function. If none of the clauses match, then is returned. Whenever is a list-like pattern, it is not matched against a list object, as is the usual case with a list-like pattern, but against the actual arguments. For instance, the pattern expects that the function was called with exactly three arguments. If that is the case, the patterns are then matched to the arguments. The pattern takes the first argument, binding it to variable and so forth. If is a dotted list-like pattern, then the dot position is matched against the remaining arguments. For instance, the pattern requires at least two arguments. The first two are bound to and respectively. The list of remaining arguments, if any, is bound to which will be if there are no remaining arguments. Any non-list-like is analyzed as an equivalent list-like dotted pattern due to syntax being equivalent to syntax. Such a pattern matches the list of all arguments. Thus, the following are all equivalent:
  (lambda-match (@a a))
  (lambda-match ((. @a) a))
  (lambda a a)
  (lambda (. a) a) The characteristics of the resulting anonymous function are determined as follows. If at least one specified in a is a dotted pattern, the function is variadic. The arity of the resulting anonymous function is determined as follows, from the lengths of the patterns. The length of a pattern is the number of elements, not including the dotted element. The length of the longest pattern determines the number of fixed arguments. Unless the function is variadic, it may not be called with more arguments than can be matched by the longest pattern. The length of the shortest pattern determines the number of required arguments. The function may not be called with fewer arguments than can be matched by the shortest pattern. If these two lengths are unequal, then the function has a number of optional arguments, equal to the difference. Note: an anonymous function which takes one argument and matches that object against clauses using can be obtained with the operator, using the pattern: Note: the parameter macro can also define a with pattern matching. Any form can be written as The parameter macro offers the additional ability of defining named arguments which are inserted before the implicit arguments generated from the clauses, and combining with other parameter macros.
  (let ((f (lambda-match
             (() (list 0 :args))
             ((@a) (list 1 :arg a))
             ((@a @b) (list 2 :args a b))
             ((@a @b . @c) (list* '> 2 :args a b c)))))
    (list [f] [f 1] [f 1 2] [f 1 2 3]))
  -->
  ((0 :args) (1 :arg 1) (2 :args 1 2) (> 2 :args 1 2 3))
  [(lambda-match
     ((0 1) :zero-one)
     ((1 0) :one-zero)
     ((@x @y) :no-match)) 1 0] --> :one-zero
  [(lambda-match
     ((0 1) :zero-one)
     ((1 0) :one-zero)
     ((@x @y) :no-match)) 1 1] --> :no-match
  [(lambda-match
     ((0 1) :zero-one)
     ((1 0) :one-zero)
     ((@x @y) :no-match)) 1 2 3] --> ;; error The macro can be used to define a top-level function in the style of It produces a form which has all of the properties of such as a block of the same being established around the implicit so that is possible. The clauses of have exactly the same syntax and semantics as those of Note: instead of the parameter macro may be used. The following equivalence holds:
  (defun name (:match) ...) <--> (defun-match ...) The parameter macro offers the additional ability of defining named arguments which are inserted before the implicit arguments generated from the clauses, and combining with other parameter macros.
  ;; Fibonacci
  (defun-match fib
    ((0) 1)
    ((1) 1)
    ((@x) (+ (fib (pred x)) (fib (ppred x)))))
  (fib 0) -> 1
  (fib 1) -> 1
  (fib 2) -> 2
  (fib 3) -> 3
  (fib 4) -> 5
  (fib 5) -> 8
  ;; Ackermann
  (defun-match ack
    ((0 @n) (+ n 1))
    ((@m 0) (ack (- m 1) 1))
    ((@m @n) (ack (- m 1) (ack m (- n 1)))))
  (ack 3 7) -> 1021
  (ack 1 1) -> 3
  (ack 2 2) -> 7 Parameter list macro allows any function to be expressed in the style of with extra features. The macro expects the body of the function to consist of clauses, which are semantically treated in exactly the same manner as under The following restrictions apply. The parameter list may not include optional parameters delimited by (the colon keyword symbol). The parameter list may not be dotted. The macro produces a function which the parameters, if any, are inserted to the left of the implicit parameters generated by the transformation. Furthermore, the parameter macro supports integration with the parameter macro, or any other macro which uses a compatible convention for delimiting special arguments. If the parameter list includes the symbol then that portion of the parameter list is set aside and not included in the transformation. Then, that list is integrated into the resulting lambda. A complete transformation can be described by the following diagram:
  (lambda (:match a b c ... -- s t u ...) clauses ...)
  -->
  (lambda (a b c ... m n p ... -- s t u ... . z) body ...) In this diagram, denote the parameters. The symbols denote the fixed parameters generated by the transformation from the semantic analysis of The symbols denote the original parameters. Finally, denotes the dotted parameter generated by the transform. If the transform produces no dotted parameter, then this is The dotted parameter is thus separated from the group to which it belongs. When no and are present, the transformation reduces to:
  (lambda (:match a b c ...) clauses ...)
  -->
  (lambda (a b c ... m n p ... . z) body ...) Note: these requirements harmonize with the parameter macro. If that is present to the left of it removes the and the keyword parameters, reuniting the parameter with the group. Furthermore, the macro generates code which refers to the existing dotted parameter as the source for the keyword parameters, unless is in which case it inserts its own generated symbol.
  ;; Match-style cond-like macro with unreachability diagnosis.
  ;; Demonstrates usefulness of :match, which allows the :form
  ;; parameter to be promoted through to the macro definition.
  (defmacro my-cond (:match :form f)
    (() nil)
    (((@(and @(constantp @test) @(eval))) . @rest)
      (when rest
        (compile-error f "unreachable code after ~s" test))
      test)
    (((@(and @(constantp @test) @(eval)) . @forms) . @rest)
      (when (and rest)
        (compile-error f "unreachable code after ~s" test))
      ^(progn ,*forms))
    (((@test) . @rest)
      ^(or ,test (my-cond ,*rest)))
    (((@test . @forms) . @rest)
      ^(if ,test (progn ,*forms)
         (my-cond ,*rest)))
    ((@else . @rest) (compile-error f "bad syntax")))
  (my-cond (3)) --> 3
  (my-cond (3 4)) --> 4
  (my-cond (3 4) (5)) --> ;; my-cond: unreachable code after 3
  (my-cond 42) --> ;; my-cond: bad syntax
  ;; Keyword parameter example.
  (defstruct simple-widget ()
    name)
  (defstruct widget (simple-widget)
    frobosity
    luminance)
  (defstruct simple-point-widget (simple-widget)
    (:static width 0)
    (:static height 0))
  (defstruct point-widget (widget)
    (:static width 0)
    (:static height 0))
  (defstruct general-widget (widget)
    width
    height)
  ;; Note that in clauses with no . @rest parameter, there
  ;; is a mismatch if keyword arguments are present. The (0 0)
  ;; clause exploits this to match only when keywords are absent.
  (defun make-widget (:key :match name -- frob lum)
    ((0 0) (new simple-point-widget name name))
    ((0 0 . @rest) (new point-widget name name
                        frobosity frob
                        luminance lum))
    ((@x @y . @rest) (new general-widget name name
                        width x
                        height x
                        frobosity frob
                        luminance lum)))
  (make-widget "abc" 0 0) --> #S(simple-point-widget name "abc")
  (make-widget "abc" 0 0 :frob 42)
  --> #S(point-widget name "abc" frobosity 42 luminance nil)
  (make-widget "abc" 0 0 :lum 9)
  --> #S(point-widget name "abc" frobosity nil luminance 9)
  (make-widget "abc" 0 1 :lum 9)
  --> #S(general-widget name "abc" frobosity nil luminance 9
                        width 0 height 0) The macro allows for the definition of pattern macros: user-defined pattern operators which are implemented via expansion into existing operator syntax. The macro has the same syntax as It specifies a macro transformation for a compound form which has the symbol in its leftmost position. This macro transformation is performed when is used as a pattern operator: an expression of the form occurring in pattern-matching syntax. The behavior is unspecified if is the name a built-in pattern operator, or a predefined pattern macro. The pattern macro bindings are stored in a hash table held by the variable whose keys are symbols, and whose values are expander functions. There are no lexically scoped pattern macros. Pattern macros defined with may specify the special macro parameters and in their parameter lists. The values of these parameters are determined in a manner particular to The parameter captures the pattern-matching form, or a constituent thereof, in which the macro is being invoked. For instance, if the operator is being used inside a pattern given to a macro invocation, then the form will be that entire form. The parameter captures a specially constructed macro-time environment object in which all of the variables to the left of the pattern appear as lexical variables. The parent of this environment is the surrounding macro environment. If the pattern macro needs to treat a variable which already has a binding differently from an unbound variable, it can look up the variable in this environment.
  ;; Create an alias called let for the @(as var pattern) operator:
  ;; Note that the macro produces @(as ...) and not just (as ...)
  (defmatch let (var pattern)
    ^@(as ,var ,pattern))
  ;; use the macro in matching:
  (when-match @(let x @(or foo bar)) 'foo x)
  ;; Error reporting example using :form
  (defmatch foo (sym)
    (unless (bindable sym)
      (compile-error *match-form*
                     "~s: bindable symbol expected, not ~s"
                     'foo sym))
    ...)
  ;; Pattern macro which uses = equality to backreference
  ;; an existing lexical binding, or else binds the variable
  ;; if it has no existing lexical binding.
  (defmatch var= (sym :env e)
    (if (lexical-var-p e sym)
      (with-gensyms (obj)
        ^@(require (sys:var ,obj)
            (= ,sym ,obj)))
      ^(sys:var ,sym)))
  ;; example use:
  (when-match (@(var= a) @(var= a)) '(1 1.0) a)
  -> 1
  ;; no match: (equal 1 1.0) is false
  (when-match (@a @a) '(1 1.0) a)
  -> nil If is a compound form whose operator symbol has been defined as a macro pattern using then will expand that pattern and return the expansion. Otherwise it returns the argument. In order to be recognized by the argument must not include the prefix that would normally be used to invoke it. The expansion, however, will include that syntax. The parameter specifies the macro-time environment for the expander. Note: pattern expanders, like built-in patterns, may use the macro environment for deciding whether a variable is an existing lexical variable, or a free variable, based on which a pattern may be expanded differently. Given:
  (defmatch point (x y)
    ^@(struct point x @,x y @,y)) a result similar to the following may be obtained:
  (macroexpand-match '(point a b)) -> @(struct point x @a y @b) Note that the pattern is specified plainly as rather than yet the expansion is The special variable holds the hash table of associations between symbols and pattern macro expanders. If the expression yields a function, then symbol has a binding as a pattern macro. If that expression yields then there is no such binding: pattern operator forms based on do not undergo place macro expansion. The macro expanders in are two-parameter functions. The first argument passes the operator syntax to be expanded. The second argument is used for passing the environment object which the expander can capture using in its macro parameter list. The macro arranges for elements from multiple sequences to be visited in parallel, and each to be matched against respective patterns. For each matching tuple of parallel elements, a body of forms is evaluated in the scope of the variables bound in the patterns. The first argument of specifies a list of alternating and expressions. Each is associated with the sequence which results from evaluating the immediately following Items coming from that sequence correspond with that pattern. The remaining arguments are to evaluated for successful matches. The are surrounded by an implicit anonymous block. If any of the forms invoke a return out of this block, then the iteration terminates, and the result value of the block becomes the result value of the loop. The processing takes place as follows:

    1.  Every is evaluated in left-to-right order and is expected to produce an iterable sequence or object that would be a suitable argument to or This evaluation takes place in the scope surrounding the macro form, in which none of the variables that are bound in the expressions are yet visible.

    2.  The next available item is taken from each of the sequences. If any of the sequences has no more items available, then terminates and returns

    3.  Each item taken in step 2 is matched against the which is corresponds with its sequence. Each successive pattern can refer to the variables bound in the previous patterns in the same iteration. If any pattern match fails, then the process continues with step 2.

    4.  If all the matches are successful, then if any, are executed in the scope of variables bound in the Processing then continues at step 2.

    The differs from in that instead of taking parallel tuples of items from the sequences, it iterates over the tuples of the Cartesian product of the sequences similarly to the function. The product tuples are ordered in such a way that the rightmost element, which always coming coming from sequence produced by the last varies the fastest. If there are two sequences and then iterates over the tuples and whereas iterates over and
      ;; Number all the .JPG files in the current directory.
      ;; For instance foo.jpg becomes foo-0001.jpg, if it is
      ;; the first file.
      (each-match (@(as name `@base.jpg`) (glob "*.jpg")
                   @(@num (fmt "~,04a")) 1)
        (rename-path name `@base-@num.jpg`))
      ;; Iterate over combinations of matching phone
      ;; numbers and odd integers from the (1 2 3) list
      (build
        (each-match-product (`(@a) @b-@c` '("x"
                            ""
                            "(311) 555-5353"
                            "(604) 923-2323"
                            "133"
                            "4-5-6-7")
                            @(oddp @x) '(1 2 3))
         (add (list x a b c))))
      -->
      ((1 "311" "555" "5353") (3 "311" "555" "5353")
       (1 "604" "923" "2323") (3 "604" "923" "2323"))) The macro is subject to all of the requirements specified for in regard to the argument conventions and semantics, and the presence of the implicit anonymous block around the Whereas returns the macro requires, in each iteration which produces a match for each that the last evaluated must produce a list. These lists are catenated together as if by the function and returned. It is unspecified whether the nonmatching iterations produce empty lists which are included in the append operation. If the last tuple of items which produces a match is absolutely the the last tuple, the corresponding evaluation may yield an atom which then becomes the terminator for the returned list, in keeping with the semantics of an atom. The macro differs from in that it iterates over the Cartesian product tuples of the sequences, rather than parallel tuples. The difference is exactly like that between and
      (append-matches
        ((:foo @y) '((:foo a) (:bar b) (:foo c) (:foo d))
         (@x :bar) '((1 :bar) (2 :bar) (3 :bar) (4 :foo)))
        (list x y))
      --> (1 a 3 c)
      (append-matches (@x '((1) (2) (3) 4)) x)
      --> (1 2 3 . 4)
      (append-match-products (@(oddp @x) (range 1 5)
                            @(evenp @y) (range 1 5))
        (list x y))
      --> (1 2 1 4 3 2 3 4 5 2 5 4) The macro is subject to all of the requirements specified for in regard to the argument conventions and semantics, and the presence of the implicit anonymous block around the Whereas returns the macro returns a list of the values produced by all matching iterations which led to the execution of the The macro differs from in that it iterates over the Cartesian product tuples of the sequences, rather than parallel tuples. The difference is exactly like that between and
      (keep-matches ((:foo @y) '((:foo a) (:bar b) (:foo c) (:foo d))
                     (@x :bar) '((1 :bar) (2 :bar) (3 :bar) (4 :foo)))
        (list x y))
      --> ((1 a) (3 c))
      (keep-match-products (@(oddp @x) (range 1 5)
                            @(evenp @y) (range 1 5))
        (list x y))
      --> ((1 2) (1 4) (3 2) (3 4) (5 2) (5 4)) The macro evaluates and matches it against similarly to If the match is successful, every is evaluated in an environment in which new bindings from are visible. In this case, the process repeats: is evaluated again, and tested against If the match fails, terminates and produces as its result value. Each iteration produces fresh bindings for any variables that are implicated for binding in The and expressions are surrounded by an anonymous block. The macros and combine iteration with the semantics of The evaluates and matches it against zero or more clauses in the manner of If there is a match, this process is repeated. If there is no match, terminates, and returns In each iteration, the matching clause produces fresh bindings for any variables implicated for binding in its respective The and expressions are surrounded by an anonymous block. The macro is identical in almost every respect to except that it terminates the loop if evaluates to without attempting to match that value against the clauses. Note: the semantics of can be obtained in by inserting a clause. That is to say, a construct of the form
      (while-true-match-case expr
        ...) may be rewritten into
      (while-match-case expr
        (nil (return)) ;; match nil and return
        ...) except that isn't required to rely on performing a block return. The (quasi-quote) macro operator implements a notation for convenient list construction. If is an atom, or a list structure which does not contain any or operators, then is equivalent to If however, is a list structure which contains or operators, then the substitutions implied by those operators are performed on and the operator returns the resulting structure. Note: how the qquote operator actually works is that it is compiled into code. It becomes a Lisp expression which, when evaluated, computes the resulting structure. A can contain another If an or operator occurs within a nested it belongs to that and not to the outer one. However, an unquote operator which occurs inside another one belongs one level higher. For instance in
      (qquote (qquote (unquote (unquote x)))) the leftmost belongs with the rightmost unquote, and the inner and belong together. When the outer is evaluated, it will insert the value of resulting in the object If this resulting qquote value is evaluated again as Lisp syntax, then it will yield the value of when treated as a Lisp expression and evaluated.
      (qquote a) -> a
      (qquote (a b c)) -> (a b c)
      (qquote (1 2 3 (unquote (+ 2 2)) (+ 2 3))) -> (1 2 3 4 (+ 2 3))
      (qquote (unquote (+ 2 2))) -> 4 In the second-to-last example, the and the are quoted verbatim. Whereas the operator caused the evaluation of and the substitution of the resulting value. The last example shows that can itself (the entire argument of can be an unquote operator. However, note: is not valid. Note: a way to understand the nesting behavior is a via a possible model of quasi-quote expansion which recursively compiles any nested quasi quotes first, and then treats the result of their expansion. For instance, in the processing of
      (qquote (qquote (unquote (unquote x)))) the operator first encounters the embedded and compiles it to code. During that recursive compilation, the syntax is encountered. The inner quote processes the outer unquote which belongs to it, and the inner becomes material that is embedded verbatim in the compilation, which will then be found when the recursion pops back to the outer quasiquote, which will then traverse the result of the inner compilation and find the In Lisp dialects which have a published quasiquoting operator syntax, there is the expectation that the quasiquote read syntax corresponds to it. That is to say, the read syntax is expected to translate to In TXR Lisp, this is not true! Although is translated to a quasiquoting macro, it is an internal one, not based on the public and symbols being documented here. This idea exists for hygiene. The quasiquote read syntax is not confused by the presence of the symbols or in the template, since it doesn't treat them specially. This also allows programmers to use the quasiquote read syntax to construct quasiquote macros. For instance
      ^(qquote (unquote ,x)) ;; does not mean ^^,,x ! To the quasiquote reader, the and symbols mean nothing special, and so this syntax simply means that if the value of is the result of evaluating this expression will be The form's expansion is actually this:
      (sys:qquote (qquote (unquote (sys:unquote x)))) the macro recognizes embedded in the form, and the other symbols not in the package are just static template material. The macro and its associated and operators work exactly like their ordinary counterparts. So in effect, TXR has two nearly identical, independent quasi-quote implementations, one of which is tied to the read syntax, and one of which isn't. This is useful for writing quasiquotes which write quasiquotes. The operator is not an operator per se. The symbol has no binding in the global environment. It is a special syntax that is recognized within a form, to indicate forms within the quasiquote which are to be evaluated and inserted into the resulting structure. The syntax is equivalent to the and "cancel out". The operator is not an operator per se. The symbol has no binding in the global environment. It is a special syntax that is recognized within a form, to indicate forms within the quasiquote which are to be evaluated and inserted into the resulting structure. The syntax is not permitted and raises an exception if evaluated. The syntax must occur within a list, and not in the dotted position. The form differs from unquote in that requires that must evaluate to a list. That list is integrated into the surrounding list. The following documentation describes the behavior of the Math Library functions as they apply to the native numeric and character types. The functions also support application-defined structure types. That feature is not described here but in the section User-Defined Arithmetic Types. When one or more operands of a Math Library function is a user-defined arithmetic structure, no conversions are performed on the operands, and the stated restrictions do not apply. The operands are passed to the methods as described in the User-Defined Arithmetic Types section. The operands need not be numeric. User-defined arithmetic structures can work with operands which are not numbers. If is such a type, it is possible for an expression such as to be meaningful and correct. Similarly, it is possible for an apparent division by zero such as to be meaningful and correct, since the method of the object decides how to handle zero. The and functions perform addition, subtraction and multiplication, respectively. Additionally, the function performs additive inverse. The function requires zero or more arguments. When called with no arguments, it produces 0 (the identity element for addition), otherwise it produces the sum over all of the arguments. Similarly, the function requires zero or more arguments. When called with no arguments, it produces 1 (the identity element for multiplication). Otherwise it produces the product of all the arguments. The semantics of changes from subtraction to additive inverse when there is only one argument. The argument is treated as a subtrahend, against an implicit minuend of zero. When there are two or more argument, the first one is the minuend, and the remaining are subtrahends. When there are three or more operands, these operations are performed as if by binary operations, in a left-associative way. That is to say, means The sum of and is computed first, and then this is added to Similarly means First, is subtracted from and then is subtracted from that result. The arithmetic inverse is performed as if it were subtraction from integer 0. That is, means the same thing as The operands of and can be characters, integers (fixnum and bignum), and floats, in nearly any combination. If two operands have different types, then one of them is converted to the type of the one with the higher rank, according to this ranking: character < integer < float. For instance if one operand is integer, and the other float, the integer is converted to a float. Characters are not considered numbers, and participate in these operations in limited ways. Subtraction can be used to computed the displacement between the Unicode values of characters, and an integer displacement can be added to a character, or subtracted from a character. For instance The Unicode value of a character can be found using the displacement from the NUL character. The rules can be stated as a set of restrictions:

    1.  Two characters may not be added together.

    2.  A character may not be subtracted from an integer (which also rules out the possibility of computing the additive inverse of a character).

    3.  A character operand may not be opposite to a floating point operand in any operation.

    4.  A character may not be an operand of multiplication.
The function performs floating-point division. Each operands is first converted to floating-point type, if necessary. In the one-argument form, the argument is omitted. An implicit dividend is present, whose value is such that the one-argument form is equivalent to the two-argument form If there are two or more arguments, explicitly or by the above equivalence, then a cumulative division is performed. The value is taken into consideration, and divided by the first If another follows, then that value is divided by that subsequent divisor. This process repeats until all divisors are exhausted, and the value of the last division is returned. A division by zero throws an exception of type The and functions operate on an effective sequence of numbers derived from which is an object suitable for iteration according to If the argument is omitted, then the effective sequence is the argument itself. Otherwise, the effective sequence is understood to be a projection mapping of the elements of through as would be calculated by the expression. The function returns the left-associative sum of the elements of the effective sequence calculated as if using the function. Similarly, the function calculates the left-associative product of the elements of the sequence as if using the function. If is empty then returns and returns If the effective sequence contains one number, then both functions return that number. The and functions reduce into the range specified by and Under the range is inclusive of the value, whereas under it is exclusive. The following equivalence holds
  (wrap a b c) <--> (wrap* a (succ b) c) The expression performs the following calculation: In other words, first is subtracted from Then the result is reduced modulo the displacement between and Finally, is added back to that result, which is returned.
  ;; perform ROT13 on the string "nop"
  [mapcar (opip (+ 13) (wrap #\a #\z)) "nop"] -> "abc" The function computes the greatest common divisor: the largest positive integer which divides each The function computes the lowest common multiple: the smallest positive integer which is a multiple of each Each must be an integer. Negative integers are replaced by their absolute values, so is and yields The value of is and that of is 1 . The value of and is Any arguments of which are zero are effectively ignored so that and are both the same as and is the same as If has any argument which is zero, it yields zero. The function tests whether integer divides integer If this is true, is returned, otherwise The integers 1 and -1 divide every other integer and themselves. By established convention, every integer, except zero, divides zero. For other values, divides if division of by leaves no remainder. The function computes the absolute value of If is positive, it is returned. If is negative, its additive inverse is returned: a positive number of the same type with exactly the same magnitude. The function calculates a representation of the sign of as a numeric value. If is an integer, then returns -1 if the integer is negative, 1 if the integer is positive, or else 0. If is a floating-point value then returns -1.0 if the value is negative, 1.0 if the value is positive or else 0.0. The and functions perform division of the by the returning an integer quotient. If the is omitted, it defaults to 1. A zero results in an exception of type If both inputs are integers, the result is of type integer. If all inputs are numbers and at least one of them is floating-point, the others are converted to floating-point and the result is floating-point. The input may be a range. In this situation, the operation is recursively distributed over the and fields of the range, individually matched against the and the result is a range composed of these two individual quotients. When the quotient is a scalar value, returns the closest integer, in the zero direction, from the value of the quotient. The function returns the highest integer which does not exceed the value of the quotient. That is to say, the division is truncated to an integer value toward negative infinity. The function the lowest integer which is not below the value of the quotient. does not exceed the value of That is to say, the division is truncated to an integer value toward positive infinity. The function returns the nearest integer to the quotient. Exact halfway cases are rounded to the integer away from zero so that yields and yields 1. Note that for large floating point values, due to the limited precision, the integer value corresponding to the mathematical floor or ceiling may not be available. In ANSI Common Lisp, the function chooses the nearest even integer, rather than rounding halfway cases away from zero. TXR's choice harmonizes with the semantics of the function in the C language. The function performs a modulus operation. Firstly, the absolute value of is taken to be a modulus. Then a residue of with respect to is calculated. The residue's sign follows that of the sign of That is, it is the smallest magnitude (closest to zero) residue of with respect to the absolute value of having the same sign as If the operands are integer, the result is an integer. If either operand is of type float, then the result is a float. The modulus operation is then generalized into the floating point domain. For instance the expression yields a residue of 0.25 because 0.5 "goes into" 0.75 only once, with a "remainder" of 0.25. If is zero, throws an exception of type These functions, respectively, perform the same division operation as and referred to here as the respective target functions. If the is missing, it defaults to 1. Each function returns a list of two values: a and a The is exactly the same value as what would be returned by the respective target function for the same inputs. The value obeys the following identity: If is zero, these functions throw an exception of type These trigonometric functions convert their argument to floating point and return a float result. The and functions compute the sine and cosine and tangent of the argument which represents an angle expressed in radians. The and are their respective inverse functions. The argument to and must be in the range -1.0 to 1.0. The function converts the rectilinear coordinates and to an angle in polar coordinates in the range [0, 2π). These functions are the hyperbolic analogs of the trigonometric functions and so forth. They convert their argument to floating point and return a float result. The function calculates the value of the transcendental number e raised to the exponent The function calculates the base e logarithm of which must be a positive value. The function calculates the base 10 logarithm of which must be a positive value. The function calculates the base 2 logarithm of which must be a positive value. The function raises to zero or more exponents given by the arguments. is equivalent to and yields for all For three or more arguments, the operation is right-associative. That is to say, is equivalent to similarly to the way nested exponents work in standard algebraic notation. Exponentiation is done pairwise using a binary operation. If both operands to this binary operation are nonnegative integers, then the result is an integer. If the exponent is negative, and the base is zero, the situation is treated as a division by zero: an exception of type is thrown. Otherwise, a negative exponent is converted to floating-point, if it already isn't, and a floating-point exponentiation is performed. If either operand is a float, then the other operand is converted to a float, and a floating point exponentiation is performed. Exponentiation that would produce a complex number is not supported. If the exponent is zero, then the return value is 1.0 if at least one operand is floating-point, otherwise 1. The function produces a floating-point square root of which is converted from integer to floating-point if necessary. Negative operands are not supported. The function computes the integer square root of which must be an integer. The integer square root is a value which is the greatest integer that is no greater than the real square root of The input value must be an integer. The function performs modular exponentiation and accepts only integer arguments. Furthermore, must be a nonnegative and must be positive. The return value is raised to and reduced to the least positive residue modulo The function returns the product of with itself. The following equivalence holds, except that is evaluated only once in the expression:
  (square x) <--> (* x x) The function calculates an approximation to the cumulative normal distribution function: the integral, of the normal distribution function, from negative infinity to the The function calculates an approximate to the inverse of the cumulative normal distribution function. The argument, a value expected to lie in the range [0, 1], represents the integral of the normal distribution function from negative infinity to some domain point p. The function calculates the approximate value of p. The minimum value returned is -10, and the maximum value returned is 10, regardless of how closely the argument approaches, respectively, the 0 or 1 integral endpoints. For values less than zero, or exceeding 1, the values returned, respectively, are -10 and 10. The function computes the binomial coefficient nCk which expresses the number of combinations of items that can be chosen from a set of where combinations are subsets. The function computes nPk: the number of permutations of size that can be drawn from a set of where permutations are sequences, whose order is significant. The calculations only make sense when and are nonnegative integers, and does not exceed The behavior is not specified if these conditions are not met. These functions test the type of returning if it is an object of the implied type, otherwise. The and functions return if the object is of the basic type or The function returns true of is either a or a The function returns if is either a or The function returns true if is a character, integer, floating-point number, range or a user-defined arithmetic object. For a range, is returned without examining the values of the and fields. A user-defined arithmetic object is identified as a struct type which implements the method as a static slot. The function tests for equivalence to zero. The argument must be a number or character. It returns for the integer value and for the floating-point value For other numbers, it returns It returns for the null character and for all other characters. If is a range, then returns if both of the range endpoints individually satisfy The function is the logical inverse of it returns for those arguments for which returns and vice versa. These functions test whether a number is positive or negative, returning or as the case may be. The argument may also be a character. All characters other than the null character are positive. No character is negative. The and functions require integer arguments. returns if is even (divisible by two), otherwise it returns returns if is not divisible by two (odd), otherwise it returns The function adds 1 to its argument and returns the resulting value. If the argument is an integer, then the return value is the successor of that integer, and if it is a character, then the return value is the successor of that character according to Unicode. The function subtracts 1 from its argument, and under similar considerations as above, the result represents the predecessor. The and functions add 2 and 3, respectively. Similarly, and subtract 2 and 3 from their argument. These relational functions compare characters, numbers, ranges and sequences of characters or numbers for numeric equality or inequality. The arguments must be one or more numbers, characters, ranges, or sequences of these objects, or, recursively, of sequences. If just one argument is given, then these functions all return If two arguments are given then, they are compared as follows. First, if the numbers do not have the same type, then the one which has the lower ranking type is converted to the type of the other, according to this ranking: character < integer < float. For instance if a character and integer are compared, the character is converted to its integer character code. Then a numeric comparison is applied. Three or more arguments may be given, in which case the comparison proceeds pairwise from left to right. For instance in the comparison is performed in isolation. If the comparison is false, then is returned, otherwise the comparison is performed in isolation, and if that is false, is returned, otherwise is returned. Note that it is possible for to undergo two different conversions. For instance in the comparison, will first convert to a floating-point representation of its Unicode value so that it can be compared to and if that comparison succeeds, then in the second comparison, will be converted to integer so that it can be compared to Ranges may only be compared with ranges. Corresponding fields of ranges are compared for equality by such that and are reported as equal. The inequality comparisons are lexicographic, such that the field of the range is considered more major than the field. For example the inequalities and hold. Sequences may only be compared with sequences, but mixtures of any kinds of sequences may be compared: lists with vectors, vectors with strings, and so on. The function considers a pair of sequences of unequal length to be unequal, reporting Sequences are equal if they have the same length and their corresponding elements are recursively equal under the function. The inequality functions treat sequences lexicographically. A pair of sequences is compared by comparing corresponding elements. The function tests each successive pair of corresponding elements recursively using the function. If this recursive comparison reports then the function immediately returns without considering any more pairs of elements. Otherwise the same pair of elements is compared again using the function. If that reports false, then the function reports false without considering any more pairs of elements. Otherwise processing continues with the next pair, if any. If all corresponding elements are equal, but the right sequence is longer, returns otherwise the function reports The function tests each successive pair of corresponding elements recursively using the function. If this returns then the function returns without considering any more pairs. Otherwise processing continues with the next pair, if any. If all corresponding elements satisfy the test, but the left sequence is longer, then is returned. Otherwise is returned. The inequality relations exhibit symmetry, which means that the functions and functions are equivalent, respectively, to and with the order of the argument values reversed. For instance, the expression is equivalent to except for the difference in evaluation order of the and operands themselves. Any semantic description of or applies, respectively, also to or with the appropriate adjustment for argument order reversal. The arguments to may be numbers or characters. The function returns if no two of its arguments are numerically equal. That is to say, if there exist some and which are distinct arguments such that is true, then the function returns Otherwise it returns The and functions determine and return the highest or lowest value from among their arguments. If only is given, that value is returned. These functions are type generic, since they compare arguments using the same semantics as the function. If two or more arguments are given, then is equivalent to and is equivalent to If the operands do not have the same type, then one of them is converted to the type of the other; however, the original unconverted values are returned. For instance yields the integer not If three or more arguments are given, and reduce the arguments in a left-associative manner. Thus means The function clamps value into the range to The function returns if is less than If is greater than or equal to but less than then it returns Otherwise it returns More precisely, is equivalent to The function's arguments consist of one required followed by n arguments. The arguments are optional; in other words, n may be zero. The function calculates the bracket of the argument: a zero-based positional index of the value, in relation to the arguments. Each of the arguments, of which there may be none, is associated with an integer index, starting at zero, in left-to-right order. The arguments are examined in that order. When a argument is encountered which exceeds that argument's index is returned. If exceeds all of the arguments, then n is returned. Determining whether exceeds a is performed using the function.
  (bracket 42) -> 0
  (bracket 5 10) -> 0
  (bracket 15 10) -> 1
  (bracket 15 10 20) -> 1
  (bracket 15 10 20 30) -> 1
  (bracket 20 10 20 30) -> 2
  (bracket 35 10 20 30) -> 3
  (bracket "a" "aardvark" "zebra") -> 0
  (bracket "ant" "aardvark" "zebra") -> 1
  (bracket "zebu" "aardvark" "zebra") -> 2 These functions extract numeric values from character string Leading whitespace in if any, is skipped. If no digits can be successfully extracted, then is returned. Trailing material which does not contribute to the number is ignored. The function converts a string of digits in the specified to an integer value. If isn't specified, it defaults to 10. Otherwise it must be an integer in the range 2 to 36, or else the character For radices above 10, letters of the alphabet are used for digits: represent a digit whose value is 10, represents 11 and so forth until Uppercase and lowercase letters are recognized. Any character which is not a digit of the specified radix is regarded as the start of trailing junk at which the extraction of the digits stops. When is specified as the character object this indicates that a C-language-style integer constant should be recognized. If, after any optional sign, the remainder of begins with the character pair then that pair is considered removed from the string, and it is treated as base 16 (hexadecimal). If, after any optional sign, the remainder of begins with a leading zero not followed by then the radix is taken to be 8 (octal). In scanning these formats, function is not otherwise constrained by C language representational limitations. Specifically, the input values are taken to be the printed representation of arbitrary-precision integers and treated accordingly. The function converts a floating-point decimal notation to a nearby floating point value. The material which contributes to the value is the longest match for optional leading space, followed by a mantissa which consists of an optional sign followed by a mixture of at least one digit, and at most one decimal point, optionally followed by an exponent part denoted by the letter or an optional sign and one or more optional exponent digits. If the value specified by is out of range of the floating-point representation, then is returned. The function converts a decimal notation to either an integer as if by a radix 10 application of or to a floating point value as if by The floating point interpretation is chosen if the possibly empty initial sequence of digits (following any whitespace and optional sign) is followed by a period, or by or These functions perform numeric conversion between integer and floating point type. The function returns an integer by truncating toward zero. The function returns an exact floating point value corresponding to if possible, otherwise an approximation using a nearby floating point value. These functions convert to floating-point or integer, respectively. The can be of several types, including string. If a floating-point value is passed into tofloat, or an integer value into toint, then that value is simply returned. If is a character, then it is treated as a string of length one containing that character. If is a string, then it is converted by as if by the function and by as if by the function If is an integer, then it is converted by as if by the function If is a floating-point number, then it is converted by as if by the function If is a structure, then it is expected to implement the or method. This method is invoked by the same-named function, and the value is returned. These variables hold, respectively, the most negative value of the integer type, and its most positive value. Integer values from to are all of type Integers outside of this range are integers. These functions are closely related to, respectively, and They differ in that these functions return a floating-point or integer zero, respectively, in some situations in which those functions would return or throw an error. Whereas those functions reject a argument of for that same argument function returns 0.0 and returns 0. Likewise, in cases when contains a string or character which cannot be converted to a number, and and would return these functions return 0.0 and 0, respectively. In other situations, these functions behave exactly like and These variables hold, respectively: the smallest positive floating-point value; the largest positive floating-point value; and the difference between 1.0 and the smallest representable value greater than 1.0. and define the floating-point range, which consists of three regions: values from to the value 0.0, and values from to This variable holds an integer representing the number of decimal digits in a decimal floating-point number such that this number can be converted to a TXR floating-point number, and back to decimal, without a change in any of the digits. This holds regardless of the value of the number, provided that it does not exceed the floating-point range. This variable holds an integer representing the maximum number of decimal digits required to capture the value of a floating-point number such that the resulting decimal form will convert back to the same floating-point number. See also the variable. These variables hold an approximation of the mathematical constants π and e. To four digits of precision, π is 3.142 and e is 2.718. The and approximations are accurate to decimal digits. The function returns a list of the digits of represented in the base given by The argument must be a nonnegative integer, and must be an integer greater than one. If is omitted, it defaults to 10. The return value is a list of the digits in descending order of significance: most significant to least significant. The digits are integers. For instance, if is 42, then the digits are integer values in the range 0 to 41. The returned list always contains at least one element, and includes no leading zeros, except when is zero. In that case, a one-element list containing zero is returned.
  (digits 1234) -> (1 2 3 4)
  (digits 1234567 1000) -> (1 234 567)
  (digits 30 2) -> (1 1 1 1 0)
  (digits 0) -> (0) The function decomposes the argument into a power series whose terms add up to The argument must be a nonnegative integer, and must be an integer greater than one. The returned power series consists of a list of nonnegative integers. It is formed from the digits of in the given which serve as coefficients which multiply successive powers of the starting at the zeroth power (one). The terms are given in decreasing order of significance: the term corresponding to the most significant digit of multiplying the highest power of is listed first. The returned list always contains at least one element, and includes no leading zeros, except when is zero. In that case, a one-element list containing zero is returned.
  (digpow 1234) -> (1000 200 30 4)
  (digpow 1234567 1000) -> (1000000 234000 567)
  (digpow 30 2) -> (16 8 4 2 0)
  (digpow 0) -> (0) The and functions evaluate a polynomial, for the given numeric argument value and the coefficients given by a sequence of numbers. If is an empty sequence, it denotes the zero polynomial, whose value is zero everywhere; the functions return zero in this case. Otherwise, the function considers to hold the coefficients in the conventional order, namely in order of decreasing degree of polynomial term. The first element of is the leading coefficient, and the constant term appears as the last element. The function takes the coefficients in opposite order: the first element of gives the constant term coefficient, and the last element gives the leading coefficient. Note: except in the case of operating on a list or list-like sequence of coefficients, Horner's method of evaluation is used: a single result accumulator is initialized with zero, and then for each successive coefficient, in order of decreasing term degree, the accumulator is multiplied by the argument, and the coefficient is added. When operates on a list or list-like sequence, it makes a single pass through the coefficients in order, thus taking them in increasing term degree. It maintains two accumulators: one for successive powers of and one for the resulting value. For each coefficient, the power accumulator is updated by a multiplication by and then this value is multiplied by the coefficient, and that value is then added to the result accumulator.
  ;; 2
  ;; evaluate x + 2x + 3 for x = 10.
  (poly 10 '(1 2 3)) -> 123
  ;; 2
  ;; evaluate 3x + 2x + 1 for x = 10.
  (rpoly 10 '(1 2 3)) -> 321 The function reports the machine-specific bignum order of the integer or character argument If is a character or integer, the function returns zero. Otherwise is expected to be a integer, and the function returns the number of "limbs" used for its representation, a positive integer. Note: the function is intended to be of use in algorithms whose performance benefits from ordering the operations on multiple integer operands according to the magnitudes of those operands. The function provides an estimate of magnitude which trades accuracy for efficiency. The function returns a function which estimates a specific quantile of a set of real-valued samples. The desired quantile is indicated by the parameter, which is a number in the range 0 to 1.0. If is specified as 0.5, then the median is estimated. The value of 0.9 leads to the estimation of the 90th percentile: a value such that approximately 90% of the samples are below that value. If the parameter is specified, it must be a positive integer. The returned function then operates in grouped mode. The parameter is relevant only to grouped mode. Grouped mode is described below. The function returned by maintains internal state in relation to calculating the quantile. The function may be called with any number of arguments, including none. It expects every argument to be either a number, or a sequence of numbers. These numbers are accumulated into the quantile calculation, and a revised estimate of the quantile is then returned. Note: the algorithm used is the P-Squared algorithm invented in 1985 by Raj Jain and Imrich Chlamtac, which avoids accumulating and sorting the entire data set, while still obtaining good quality estimates of the quantile. The algorithm requires an initial seed of five samples. Then additional samples input into the algorithm produce quantile estimates. To eliminate this special case from the abstract interface, the TXR implementation is capable of producing an estimate when five or fewer samples have been presented, including none. In this low situation, the value is ignored in reporting the estimate. When no samples have been given, the estimate is zero. When one sample has been given, the estimate is that sample itself. When between two and five samples have been given, the estimate is their median. Using the median as the estimate ensures a smooth transition from these early estimates into the estimates produced by the P-Squared algorithm. This is because the P-Squared algorithm always reports the value of the middle height accumulator as the estimate, and that accumulator's initial value is the median of the first five samples. The function returned by though not accumulating all of the samples passed to it, nevertheless has a limited sample capacity, because the registers it uses for tracking the sample group positions are fixed-width integers. The sample capacity is approximately 4 times the value of
   (defparm q (quantile 0.9)) ;; create 90-th percentile accumulator
   [q] -> 0.0 ;; no samples given: estimate is 0.
   [q 3.14] -> 3.14 ;; one sample: estimate is that sample
   [q 13.3 7.9 5.2 6.3] -> 7.9 ;; five samples: estimate is median.
   [q 6.8 7.3 9.1 4.0] ;; more than five samples; estimate now
   -> 8.44651234567901 ;; from P-Square algorithm
   [q #(13.1 5 2.5)] ;; vector argument
   -> 9.68660493827161
   [q] -> 9.68660493827161 ;; no arguments: repeat current estimate If the argument is specified, then the quantile accumulator operates in grouped mode. Grouped mode allows infinite sample operation without overflow: an unlimited number of samples can be accepted. However, old samples lose their influence over the estimated value: newer samples are considered more significant than old samples. In grouped mode, the quantile accumulator is reset to its initial state whenever samples have been accumulated, and begins freshly calculating the quantile. Prior to the reset, an estimate is obtained and retained in an internal register. Going forward, this remembered previous estimate is blended in with the newly calculated estimate values, as described below. The cycle repeats itself whenever samples accumulate: the state is reset, and the current estimate is loaded into the previous estimate register, which is then blended with newly computed values. The parameter, whose default value is 0.999, controls the estimate blending. It should be a value between 0 and 1. Upon each reset, a blend value register is initialized to 1.0. Each time a new sample is accumulated, the blend register is multiplied by the rate parameter, and the product is stored back into the blend register. Thus if the rate is between 0 and 1, exclusive, then the blend register exponentially decreases as the number of samples grows. The blend register indicates the fraction of the estimate which comes from the remembered previous estimate. For instance, if the current blend value is 0.8, then the returned estimate value is 0.8 times the remembered previous estimate, plus 0.2 times the newly computed estimate for the current sample in the new group: the previous and current estimate are blended 80:20. The default value of 0.999 is chosen for a slow transition to the new estimates, which helps to conceal inaccuracies in the algorithm associated with having accumulated a small number of samples. At this rate, it requires about 290 samples before the blend value drops to 75% of the old estimate. If is specified as 0, then no blending of the previous estimate value takes place, since the blend factor will drop to zero upon the first sample being received after the group reset, causing the newly calculated estimates to be returned without blending. The previous sample groups therefore have no influence over newer estimates. If is specified as 1, then the blend factor will stay at 1, and so the estimate will forever remain at the previous value, ignoring the calculations driven by the new samples. Note: it is recommended that if is specified, the value should be at least several hundred. Too small a group size will prevent the estimation algorithm from settling on good results. The parameter should not much smaller than 1. A rate too low will cause the previous estimate's contribution to the quantile value to diminish, too quickly, before the new estimation settles. These variables hold integer values suitable as arguments to the function, which controls the rounding mode for the results of floating-point operations. These variables are only defined on platforms which support rounding control. Their values have the following meanings:
    Round to nearest: the result of an operation is rounded to the nearest representable value. Round down: the result of an operation is rounded to the nearest representable value that lies in the direction of negative infinity. Round up: the result of an operation is rounded to the nearest representable value that lies in the direction of positive infinity. Round to zero: the result of an operation is rounded to the nearest representable value that lies in the direction of zero.

    Sometimes floating-point operations produce a result which requires more bits of precision than the floating point representation can provide. A representable floating-point value must be substituted for the true result and yielded by the operation. On platforms which support rounding control, these functions are provided for selecting the decision procedure by which the floating-point representation is taken. The returns the current rounding mode. The rounding mode is represented by an integer value which is either equal to one of the four variables and or else some other value specific to the host environment. Initially, the value is that of Otherwise, the value returned is that which was stored by the most recent successful call to The function changes the rounding mode. The argument to its parameter may be the value of one of the above four variables, or else some other value supported by the host environment's C library function. The function returns if it is successful, otherwise the return value is and the rounding mode is not changed. If a value is passed to which is not the value of one of the above four rounding mode variables, and the function succeeds anyway, then the rounding behavior of floating-point operations depends on the host environment's interpretation of that value. The following functions are defined, if they are available from the host platform. They corresponds to same-named functions in the ISO C language standard, which appeared in the 1999 revision ("C99"). Even if some of these functions happen not to be defined, it is nevertheless possible to define them as methods in a user-defined arithmetic structure. See the section User-Defined Arithmetic Types below These are one-argument functions, which take a numeric argument, and return a floating-point result. These are two-argument functions, which take numeric arguments, and return a floating-point result. In TXR Lisp, similarly to Common Lisp, bit operations on integers are based on a concept that might be called "infinite two's complement". Under infinite two's complement, a positive number is regarded as having a binary representation prefixed by an infinite stream of zero digits (for example is A negative number in infinite two's complement is the bitwise negation of its positive counterpart, plus one: it carries an infinite prefix of 1 digits. So for instance the number is represented by an infinite sequence of 1 bits. There is no specific sign bit; any operation which produces such an infinite sequence of 1 digits on the left gives rise to a negative number. For instance, consider the operation of computing the bitwise complement of the number Since the number is represented as its complement is Each one of the digits in the infinite sequence is replaced by And this leading sequence means that the number is negative, in fact corresponding to the two's complement representation of the value Hence, the infinite digit concept corresponds to an arithmetic interpretation. In fact TXR Lisp's bignum integers do not use a two's complement representation internally. Numbers are represented as an array which holds a pure binary number. A separate field indicates the sign: negative, or nonnegative. That negative numbers appear as two's complement under the bit operations is merely a carefully maintained illusion (which makes bit operations on negative numbers more expensive). The function, as well as a feature of the function, allow bit manipulation code to be written which works with positive numbers only, even if complements are required. The trade off is that the application has to manage a limit on the number of bits. These operations perform the familiar bitwise and, inclusive or, and exclusive or operations, respectively. Positive values inputs are treated as pure binary numbers. Negative inputs are treated as infinite-bit two's complement. For example produces This is because is in infinite-bit two's complement. And-ing this value with (or produces The and functions are variadic, and may be called with zero, one, two, or more input values. If is called with no arguments, it produces the value -1 (all bits 1). If is called with no arguments it produces zero. In the one-argument case, the functions just return their argument value. In the two-argument case, one of the operands may be a character, if the other operand is a fixnum integer. The character operand is taken to be an integer corresponding to the character value's Unicode code point value. The resulting value is regarded as a Unicode code point and converted to a character value accordingly. When three or more arguments are specified, the operation's semantics is that of a left-associative reduction through two-argument invocations, so that the three-argument case is equivalent to the expression which features two two-argument cases. The function returns true if and have bits in common. The following equivalence holds:
      (logtest a b) <--> (not (zerop (logand a b))) The function performs a bitwise complement of When the one-argument form of lognot is used, then if is nonnegative, then the result is negative, and vice versa, according to the infinite-bit two's complement representation. For instance is and is The two-argument form of produces a truncated complement. Conceptually, a bitwise complement is first calculated, and then the resulting number is truncated to the number of bits given by which must be a nonnegative integer. The following equivalence holds:
      (lognot a b) <--> (logtrunc (lognot a) b) The function truncates the integer to the specified number of bits. If is negative, then the two's complement representation is truncated. The return value of is always a nonnegative integer. The function first truncates the infinite-bit two's complement representation of the integer to the specified number of bits, similarly to the function. Then, this truncated value is regarded as a two's complement integer. The value of this integer is calculated and returned.
      (sign-extend 127 8) -> 127
      (sign-extend 128 8) -> -128
      (sign-extend 129 8) -> -127
      (sign-extend 255 8) -> -1
      (sign-extend 256 8) -> 0
      (sign-extend -1 8) -> -1
      (sign-extend -255 8) -> 0 The function shifts by the specified number of producing a new value. If is positive, then a left shift takes place. If is negative, then a right shift takes place. If is zero, then is returned unaltered. For positive numbers, a left shift by n bits is equivalent to a multiplication by two to the power of n, or A right shift by n bits of a positive integer is equivalent to integer division by with truncation toward zero. For negative numbers, the bit shift is performed as if on the two's complement representation. Under the infinite two's complement representation, a right shift does not exhaust the infinite sequence of digits which extends to the left. Thus if is shifted right it becomes because the bitwise representations of these values are and The function tests whether the integer or character has a 1 in bit position The argument must be a nonnegative integer. A value of of zero indicates the least-significant-bit position of The function has a Boolean result, returning the symbol if bit of is set, otherwise If is negative, it is treated as if it had an infinite-bit two's complement representation. For instance, if is then the function returns for a value of zero, and for all other values, since the infinite bit two's complement representation of is The function takes zero or more integer arguments, and produces an integer value which corresponds to a bitmask made up of the bit positions specified by the integer arguments. If is called with no arguments, then the return value is zero. If is called with a single argument then the return value is the same as that of the expression the value 1 shifted left by bit positions. If is zero, then the result is if is the result is and so forth. If is negative, then the result is zero. If is called with two or more arguments, then the result is a bitwise or of the masks individually computed for each of the values. In other words, the following equivalences hold:
      (mask) <--> 0
      (mask a) <--> (ash 1 a)
      (mask a b c ...) <--> (logior (mask a) (mask b) (mask c) ...) The function returns a list of the positions of bits which have a value of 1 in a positive argument, or the positions of bits which have a value of zero in a negative argument. The positions are ordered from least to greatest. The least significant bit has position zero. If is zero, the empty list is returned. A negative integer is treated as an infinite-bit two's complement representation. The argument may be a character. If is nonnegative, the following equivalence holds:
      x <--> [apply mask (bitset x)] That is to say, the value of may be reconstituted by applying the bit positions returned by as arguments to the function. The value of a negative may be reconstituted from its as follows:
      x <--> (pred (- [apply mask (bitset x)])) also, more trivially, thus:
      x <--> (- [apply mask (bitset (- x))]) A two's complement representation of an integer consists of a sign bit and a mantissa field. The function computes the minimum number of bits required for the mantissa portion of the two's complement representation of the argument. For a nonnegative argument, the width also corresponds to the number of bits required for a natural binary representation of that value. Two integer values have a width of zero, namely 0 and -1. This means that these two values can be represented in a one-bit two's complement, consisting of only a sign bit: the one-bit two's complement bitfield 1 denotes -1, and 0 denotes 0. Similarly, two integer values have a width of 1: 1 and -2. The two-bit two's complement bitfield 01 denotes 1, and 10 denotes -2. The argument may be a character. The function considers to have a two's complement representation. If the integer is positive, it returns the count of bits in that representation whose value is 1. If is negative, it returns the count of zero bits instead. If is zero, the value returned is zero. The argument may be a character. The and macros set to 1 and 0, respectively, the bits in corresponding to bits that are equal to 1 in the mask resulting from applying the inclusive or operation to the arguments. The following equivalences hold:
      (set-mask place integer ...)
      <--> (set place (logior place integer ...)
      (clear-mask place integer ...)
      <--> (set place (logand place (lognot (logior integer ...)))) TXR Lisp makes it possible for the user application program to define structure types which can participate in arithmetic operations as if they were numbers. Under most arithmetic functions, a structure object may be used instead of a number, if that structure object implements a specific method which is required by that arithmetic function. The following paragraphs give general remarks about the method conventions. Not all arithmetic and bit manipulation functions have a corresponding method, and a small number of functions do not follow these conventions. In the simplest case of arithmetic functions which are unary, the method takes no argument other than the object itself. Most unary arithmetic functions expect a structure argument to have a method which has the same name as that function. For instance, if is a structure, then will invoke If has no method, then an exception is thrown. A few unary methods are not named after the corresponding function. The unary case of the function expects an object to have a method named thus, invokes Unary division requires a method called thus, invokes When a structure object is used as an argument in a two-argument (binary) arithmetic function, there are several cases to consider. If the left argument to a binary function is an object, then that object is expected to support a binary method. That method is called with two arguments: the object itself, of course, and the right argument of the arithmetic operation. In this case, the method is named after the function. For instance, if is an object, then invokes If the right argument, and only the right argument, of a binary operation is an object, then the situation falls into two cases depending on whether the operation is commutative. If the operation is commutative, then the same method is used as in the case when the object is the left argument. The arguments are merely reversed. Thus also invokes If the operation is not commutative, then the object must supply an alternative method. For most functions, that method is named by a symbol whose name begins with a prefix. For instance invokes whereas invokes Note: the "r" may be remembered as indicating that the object is the right argument of the binary operation or that the arguments are reversed. Two functions do not follow the convention. These are and For these, the methods used for the object as a right argument, respectively, are and Thus invokes and invokes Several binary functions do not support an object as the right argument. These are and Variadic arithmetic functions, when given three or more arguments, are regarded as performing a left-associative decimation of the arguments through a binary function. Thus for instance is understood as where is evaluated first. If that method yields an object then is invoked. Certain variadic arithmetic functions, if invoked with one argument, just return that argument: for instance, and are in this category. A special concession exists in these functions: if their one and only argument is a structure, then that structure is returned without any error checking, even if it implements no methods related to arithmetic. The following sections describe each of the methods that must be implemented by an object for the associated arithmetic function to work with that object, either at all, or in a specific argument position, as the case may be. These methods are not provided by TXR Lisp; the application is required to provide them. The method should return a floating-point value. It is also permissible for the method to return in which case if it is invoked via that function will replace the return with value of 0.0. The method should return an integer value. It is permissible for the method to return in which case if it is invoked via that function will replace the return with value of 0. Note: the function doesn't support structure objects in the second and third argument positions. The and arguments must be integers. Note: the function doesn't support a structure as the right argument, which must be an integer. Note: the function doesn't support a structure as the right argument, which must be an integer. Note: the function doesn't support a structure as the right argument, which must be an integer. An exception in TXR is a special event in the execution of the program which potentially results in a transfer of control. An exception is identified by a symbol, known as the exception type, and it carries zero or more arguments, called the exception arguments. When an exception is initiated, it is said to be thrown. This action is initiated by the following functions: and and possibly other functions which invoke these. When an exception is thrown, TXR enters into exception processing mode. Exception processing mode terminates in one of several ways:

-   A catch is found which matches the exception, and control is transferred to the catch by a nonlocal transfer which performs unwinding. Catches are defined by the macro.

-   A handler is found which matches the exception, and control is transferred to the handler by invoking its function. The handler function accepts the exception by performing a nonlocal transfer to a destination of its choice, or else declines to accept the exception by returning. Handlers are defined by the operator or macro.

-   If no catch or accepting handler is found for an exception derived from and is then a built-in strategy for handling the exception is invoked, consisting of unwinding, and then printing some informational messages and terminating. If the variable contains a value that isn't then control is transferred to the function stored in the that variable first; only if that function returns is the above built-in strategy invoked.

-   If no catch or accepting handler is found for an exception derived from then a warning diagnostic is issued on the stream and a exception is thrown with no arguments. If no catch or handler is found for that exception, then control returns normally to the site which threw the warning exception.

-   If no catch or accepting handler is found for an exception that is neither derived from nor from then no control transfer takes place; control returns to the or function which returns normally, with a return value of

There are two ways by which exceptions are handled: catches and handlers. Catches and handlers are similar, but different. A catch is an exit point associated with an active scope. When an exception is handled by a catch, the form which threw the exception is abandoned, and unwinding takes place to the catch site, which receives the exception type and arguments. A handler is also associated with an active scope. However, it is a function, and not a dynamic exit point. When an exception is passed to handler, unwinding does not take place; rather, the function is called. The function then either completes the exception handling by performing a nonlocal transfer, or else declines the exception by performing an ordinary return. Catches and handlers are identified by exception type symbols. A catch or handler is eligible to process an exception if it handles a type which is a supertype of the exception which is being processed. Handles and catches are found by means of a combined search which proceeds from the innermost nesting of dynamic scope to the outermost, without performing any unwinding. When an eligible handler is encountered, its registered function is called, thereby suspending the search. If the handler function returns, the search continues from that scope to yet unvisited outer scopes. When an eligible catch is encountered rather than a handler, the search terminates and a control transfer takes place to the catch site. That control transfer then performs unwinding, which requires it to make a second pass through the same nestings of dynamic scope that had just been traversed in order to find that catch. Because handlers execute in the dynamic context of the exception origin, without any unwinding having taken place, they expose a potential route of sandbox escape via the package system, unless special steps are taken. The threat is that code at the handler site could take advantage of the current value of the and variables established at the exception throw site to gain inappropriate access to symbols. For this reason, when a handler is established, the current values of and are recorded into the handler frame. When that handler is later invoked, it executes in a dynamic environment in which those variables are bound to the previously noted values. The catch mechanism doesn't do any such thing because the unwinding which is performed prior to the invocation of a catch implicitly restores the values of all special variables to the values they had at the time the frame was established. Exception type symbols are arranged in an inheritance hierarchy, at whose top the symbol is the supertype of every exception type, and the symbol is at the bottom, the subtype of every exception type. Keyword symbols may be used as exception types. Every symbol is its own supertype and subtype. Thus whenever X is known to be a subtype of Y, it is possible that X is exactly Y. The macro registers exception supertype/subtype relationships among symbols. The following tree diagram shows the relationships among TXR Lisp's built-in exception symbols. Not shown is the exception symbol subtype of every exception type:
  t ----+--- warning
        |
        +--- restart ---+--- continue
        | |
        | +--- retry
        | |
        | +--- skip
        |
        +--- error ---+--- type-error
                      |
                      +--- internal-error
                      |
                      +--- panic
                      |
                      +--- numeric-error
                      |
                      +--- range-error
                      |
                      +--- query-error
                      |
                      +--- file-error -------+--- path-not-found
                      | |
                      | +--- path-exists
                      | |
                      | +--- path-permission
                      |
                      +--- process-error
                      |
                      +--- socket-error
                      |
                      +--- system-error
                      |
                      +--- alloc-error
                      |
                      +--- timeout-error
                      |
                      +--- assert
                      |
                      +--- syntax-error
                      |
                      +--- eval-error
                      |
                      +--- match-error
                      |
                      +--- case-error
                      |
                      +--- opt-error Program designers are encouraged to derive new error exceptions from the type. The type is intended to be the root of a hierarchy of exception types used for denoting restart points: designers are encouraged to derive restarts from this type. A catch for the exception should be established around constructs which can throw an error from which it is possible to recover. That exception provides the entry point into the recovery which resumes execution. A catch for should be provided in situations when it is possible and makes sense for a failed operation to be tried again. A catch for should be provided in situations when it is possible and sensible to continue with subsequent operations even though an operation has failed. Exception handling in TXR Lisp provides capabilities similar to the condition system in ANSI Common Lisp. The implementation and terminology differ. Most obviously, ANSI CL uses the "condition" term, whereas TXR Lisp uses "exception". In ANSI CL, a condition is "raised", whereas a TXR Lisp exception is "thrown". In ANSI CL, when a condition is raised, a condition object is created. Condition object are similar to class objects, but are not required to be in the Common Lisp Object System. They are related by inheritance and can have properties. TXR Lisp exceptions are unencapsulated: they consist of a symbol, plus zero or more arguments. The symbols are related by inheritance. When a condition is raised in ANSI CL, the dynamic scope is searched for a handler, which is an ordinary function which receives the condition. No unwinding or nonlocal transfer takes place. The handler can return, in which case the search continues. Matching the condition to the handler is by inheritance. Handler functions are bound to exception type names. If a handler chooses to actually handle a condition (thereby terminating the search) it must itself perform some kind of dynamic control transfer, rather than return normally. ANSI CL provides a dynamic control mechanism known as restarts which is usually used for this purpose. A condition handler may invoke a particular restart handler. Restart handlers are similar to exception handlers: they are functions associated with symbols in the dynamic environment. In TXR Lisp, the special behavior which occurs for exceptions derived from and those from is built into the exception handling system, and tied to those types. When an error or warning exception is unhandled, the exception handling system itself reacts, so the special behaviors occur no matter how these exceptions are raised. In ANSI CL, the special behavior for unhandled conditions (of invoking the debugger) is implemented only in the function; conditions signalled other than via that function are not subject to any special behavior. There is a parallel situation with regard to warnings: the ANSI CL function implements a special behavior for unhandled warnings (of emitting a diagnostic) but warnings not signalled via that function are not treated that way. Thus in TXR Lisp, there is no way to raise an error or warning that is simply ignored due to being unhandled. In TXR Lisp exceptions are a unification of conditions and restarts. From an ANSI CL perspective, TXR Lisp exceptions are a lot like CL restarts, except that the symbols are arranged in an inheritance hierarchy. TXR Lisp exceptions are used both as the equivalent of ANSI CL conditions and as restarts. In TXR Lisp the terminology "catch" and "handle" is used in a specific way. To handle an exception means to receive it without unwinding, with the possibility of declining to handle it, so that the search continues for another handler. To catch an exception means to match an exception to a catch handler, terminate the search, unwind and pass control to the handler. TXR Lisp provides an operator called for specifying handlers. It has a different syntax from ANSI CL's TXR Lisp provides a macro called which simplifies the use of This macro superficially resembles ANSI CL's but is semantically different. The most notable difference is that the bodies of handlers established by execute without any unwinding taking place and may return normally, thereby declining to take the exception. In other words, has the same semantics as providing only convenient syntax. TXR Lisp provides a macro called which has the same syntax as but specifies a catch point for exceptions. If, during an exception search, a clause matches an exception, a dynamic control transfer takes place from the throw site to the catch site. Then the clause body is executed. The macro resembles ANSI CL's or possibly depending on point of view. TXR Lisp provides unified introspection over handler and catch frames. A program can programmatically discover what handler and catches are available in a given dynamic scope. ANSI CL provides introspection over restarts only; the standard doesn't specify any mechanism for inquiring what condition handlers are bound at a given point in the execution. The following two examples express a similar approach implemented using ANSI Common Lisp conditions and restarts, and then using TXR Lisp exceptions.
  ;; Common Lisp
  (define-condition foo-error (error)
    ((arg :initarg :arg :reader foo-error-arg)))
  (defun raise-foo-error (arg)
    (restart-case
      (let ((c (make-condition 'foo-error :arg arg)))
        (error c))
      (recover (recover-arg)
        (format t "recover, arg: ~s~%" recover-arg))))
  (handler-bind ((foo-error
                   (lambda (cond)
                     (format t "handling foo-error, arg: ~s~%"
                        (foo-error-arg cond))
                     (invoke-restart 'recover 100))))
    (raise-foo-error 200)) The output of the above is:
  handling foo-error, arg: 200
  recover, arg: 100 The following is possible TXR Lisp equivalent for the above Common Lisp example. It produces identical output.
  (defex foo-error error)
  (defex recover restart) ;; recommended practice
  (defun raise-foo-error (arg)
    (catch
      (throw 'foo-error arg)
      (recover (recover-arg)
        (format t "recover, arg: ~s\n" recover-arg))))
  (handle
    (raise-foo-error 200)
    (foo-error (arg)
      (format t "handling foo-error, arg: ~s\n" arg)
      (throw 'recover 100))) To summarize the differences: exceptions serve as both conditions and restarts in TXR. The same function is used to initiate exception handling for and then to transfer control out of the handler to the recovery code. The handler accepts one exception by raising another. When an exception symbol is used for restarting, it is a recommended practice to insert it into the inheritance hierarchy rooted at the symbol, either by inheriting directly from or from an exception subtype of that symbol. Some TXR Lisp library functions generate exceptions in response to conditions arising in the operating system, and those conditions are associated with a numeric code in the POSIX/ISO C variable This code isn't represented as an exception argument. Rather, in many of these situations, the value is attached to the error message string which is passed as the first and only exception argument. The value can be retrieved by using the function on the error message string. If this function returns then no such code is available in connection with the given error.
  (catch
    (open-file "AsDf")
    (error (msg)
      ;; the value 2 is retrieved from msg
      ;; 2 is the common value of ENOENT
      (list (string-get-code msg) msg)))
  -> (2 "error opening \"AsDf\": 2/\"No such file or directory\"") These functions generate an exception. The and functions generate an exception identified by whereas throws an exception of type The call can be regarded as a shorthand for The function takes zero or more additional arguments. These arguments become the arguments of a handler which takes the exception. The handler will have to be capable of accepting that number of arguments. The and functions generate an exception which has a single argument: a character string created by a formatted print to a string stream using the string and additional arguments. Because throws an error exception, it does not return. If an error exception is not handled, TXR will issue diagnostic messages and terminate. Likewise, or are used to generate an error exception, they do not return. If the and functions are used to generate an exception not derived from and no handler is found which accepts the exception, they return normally, with a value of The macro establishes an exception catching block around the The is followed by zero or more catch clauses. Each catch clause consists of a symbol which denotes an exception type, an argument list, and zero or more body forms. If terminates normally, then the catch clauses are ignored. The catch itself terminates, and its return value is that of the If throws an exception which is a subtype of one or more of the type symbols given in the exception clauses, then the first (leftmost) such clause becomes the exit point where the exception is handled. The exception is converted into arguments for the clause, and the clause body is executed. When the clause body terminates, the catch terminates, and the return value of the catch is that of the clause body. If throws an exception which is not a subtype of any of the symbols given in the clauses, then the search for an exit point for the exception continues through the enclosing forms. The catch clauses are not involved in the handling of that exception. When a clause catches an exception, the number of arguments in the catch must match the number of elements in the exception. A catch argument list resembles a function or lambda argument list, and may be dotted. For instance the clause catches an exception subtyped from with one or more elements. The first element binds to parameter and the rest, if any, bind to parameter If there is only one element, takes on the value The macro is a variant of with the following difference: when invokes a clause, it passes the exception symbol as the leftmost argument Then the exception arguments follow. In contrast, only the exception arguments are passed to the clauses of The macro is a further variant, which differs from by requiring each catch clause to provide a description an expression which evaluates to a character string. The expressions are evaluated in left-to-right order prior to the evaluation of Also see: the operator, and the functions and as well as the operator and macro. The operator evaluates in such a way that no matter how the execution of terminates, the will be executed. The however, are not protected. If a terminates via some nonlocal jump, the subsequent are not evaluated. themselves can "hijack" a nonlocal control transfer such as an exception. If a is evaluated during the processing of a dynamic control transfer such as an exception, and that initiates its own dynamic control transfer, the original control transfer is aborted and replaced with the new one. The exit points for dynamic control transfers are removed as unwinding takes place. That is to say, at the start of a dynamic control transfer, a search takes place for the target exit point. That search might skip other exit points which aren't targets of the control transfer. Those skipped exit points are left undisturbed and are still visible during unwinding until their individual binding forms are abandoned. Thus at the time of execution of an all of the exit points of dynamically surrounding forms are still visible, even ones which are nearer than the targeted exit point.
  (block foo
    (unwind-protect
      (progn (return-from foo 42)
             (format t "not reached!\n"))
      (format t "cleanup!\n"))) In this example, the protected form terminates by returning from block Therefore the form does not complete and so the output is not produced. However, the cleanup form executes, producing the output The macro operator evaluates each similarly to the operator. If no forms are present, it returns Otherwise it evaluates each in turn, yielding the value of the last one. If the evaluation of any is abandoned due to an exception of type the code generated by the macro catches this exception. In this situation, the execution of the form terminates without evaluating the remaining forms, and yields The macro resembles It arranges for the evaluation of each in left-to-right order. If all the forms are evaluated, then the value of the last one is returned. If no forms are present, then is returned. If any throws an exception of type then this exception is intercepted by a handler established by This handler reacts by throwing an exception of type The effect is that the warning is ignored, since the handler doesn't issue any diagnostic, and passes control to the warning's continue point. Note: all sites within TXR which throw a also provide a nearby catch for a exception, for resuming evaluation at the point where the warning was issued. The operator establishes a handler for one or more exception types, and evaluates zero or more in a dynamic scope in which that handler is visible. When the form terminates normally, the handler is removed. The value of the last is returned, or else if there are no forms. The argument is an expression which must evaluate to a function. The function must be capable of accepting the exception arguments. All exceptions functions require at least one argument, since the leftmost argument in an exception handler call is the exception type symbol. The argument is a list of symbols, not evaluated. If it is empty, then the handler isn't eligible for any exceptions. Otherwise it is eligible for any exception whose exception type is a subtype of any of the symbols. If the evaluation of any throws an exception which is not handled within that form, and the handler is eligible for that exception, then the function is invoked. It receives the exception's type symbol as the leftmost argument. If the exception has arguments, they appear as additional arguments in the function call. If the function returns normally, then the exception search continues. The handler remains established until the exception is handled in such a way that a dynamic control transfer abandons the form. Note: while a handler's function is executing, the handler is disabled. If the function throws an exception for which the handler is eligible, the handler will not receive that exception; it will be skipped by the exception search as if it didn't exist. When the handler function terminates, either via a normal return or a nonlocal control transfer, then the handler is reenabled. The macro is a syntactic sugar for the operator. Its syntax is exactly like that of The difference between and is that the clauses in are invoked without unwinding. That is to say, does not establish an exit point for an exception. When control passes to a clause, it is by means of an ordinary function call and not a dynamic control transfer. No evaluation frames are yet unwound when this takes place. The macro establishes a handler, by whose consists of every gathered from every clause. The handler function established in the generated is synthesized from all of the clauses, together with dispatch logic which which passes the exception and its arguments to the first eligible clause. The is evaluated in the context of this handler. The clause of the syntax can return normally, like a function, in which case the handler is understood to have declined the exception, and exception processing continues. To handle an exception, the clause of the macro must perform a dynamic control transfer, such returning from a block via or throwing an exception. The macro is a variant of with the following difference: when invokes a clause, it passes the exception symbol as the leftmost argument Then the exception arguments follow. In contrast, only the exception arguments are passed to the clauses of The macro provides a sequential binding construct similar to Every is established as a variable which is visible to the of subsequent variables, to all subsequent including that of the same variable, and to the If no is supplied, then is bound to the value If an is supplied, but no then is bound to the value of the If one or more are supplied in addition to they specify forms to be executed upon the termination of the construct. When an instance of terminates, either normally or by a nonlocal control transfer, then for each whose had executed, thus causing that to be bound to a value, the corresponding to are evaluated in the usual left-to-right order. The are cleaned up in reverse (right-to-left) order. The of the most recently bound are processed first; those of the least recently bound are processed last. When the form terminates normally, the value of the last is returned, or else if no are present. From its inception, until TXR 265, featured an undocumented behavior. Details are given in the COMPATIBILITY section's Compatibility Version Values subsection, in the notes for compatibility value 265. The following expression opens a text file and reads a line from it, returning that line, while ensuring that the stream is closed immediately:
  (with-resources ((f (open-file "/etc/motd") (close-stream f)))
    (whilet ((l (get-line f)))
      (put-line l))) Note that a better way to initialize exactly one stream resource is with the macro, which implicitly closes the stream when it terminates. The variable is initialized with by default. It may instead be assigned a function which is capable of taking three arguments. When an exception occurs which has no handler, this function is called, with the following arguments: the exception type symbol, the exception object, and a third value which is either or else the form which was being evaluated when the exception was thrown. The call occurs before any unwinding takes place. If the variable is or isn't a function, or the function returns after being called, then unwinding takes place, after which some informational messages are printed about the exception, and the process exits with a failed termination status. In the case when the variable contains a object other than which isn't a function, a diagnostic message is printed on the stream prior to unwinding. Prior to the function being called, the variable is reset to Note: the functions or may be applied to the third argument of the function to obtain more information about the form. The macro records hierarchical relationships among symbols, for the purposes of the use of those symbols as exceptions. It is closely related to the directive in the TXR pattern language, performing the same function. All symbols are considered to be exception subtypes, and every symbol is implicitly its own exception subtype. This macro does not introduce symbols as exception types; it only introduces subtype-supertype relationships. If is invoked with no arguments, it has no effect. If arguments are present, they must be symbols. If is invoked with only one symbol as its argument, it has no effect. At least two symbols must be specified for a useful effect to take place. If exactly two symbols are specified, then, subject to error checks, makes the left symbol an exception subtype of the right symbol. This behavior generalizes to three or more arguments: if three or more symbols are specified, then each symbol other than the last is registered as a subtype of the symbol which follows. If a has three or more arguments, they are processed from left to right. If errors are encountered during the processing, the correct registrations already made for prior arguments remain in place. Every symbol is implicitly considered to be its own exception subtype, therefore it is erroneous to explicitly register a symbol as its own subtype. The symbol is implicitly a subtype of every exception type. Therefore, it is erroneous to attempt to specify it as a supertype in a registration. Using as a subtype in a registration is silently permitted, but has no effect. No explicit registration is recorded between and its successor in the argument list. The symbol is implicitly the supertype of every exception type. Therefore, it is erroneous to attempt to register it as an exception subtype. Using as a supertype in a registration is also erroneous. A symbol may not be registered as a subtype of a symbol if the reverse relationship already exists between those two symbols. The foregoing rules allow redefinitions to take place, while forbidding cycles from being created in the exception subtype inheritance graph. Keyword symbols may be used as exception types. The function constitutes the underlying implementation for the macro. The following equivalence applies:
  (defex a b ...) <--> (register-exception-subtypes 'a 'b ...) That is, the macro works as if by generating a call to the function, with the arguments quoted. The semantics of the function is precisely that of the macro. The function tests whether two symbols are in a relationship as exception types, such that is a direct or indirect exception subtype of If that is the case, then is returned, otherwise The function returns a tree structure which captures information about all registered exception types. The map appears as an association list which contains an entry for every exception symbol, paired with that type's supertype path. The first element in the supertype path is the exception's immediate supertype. The next element is that type's supertype and so on. The last element in every path is the grand supertype For instance, if only the types and existed in the system, and were linked according to this inheritance graph:
  t ----+--- b --- a
        |
        +--- c such that the supertype of and is and has as supertype, then the function might return:
  ((a b t) (b t) (c t) (t)) or any other equivalent permutation. The returned list may share substructure, so that the sublist is shared among all four entries, and between the first two. If the program alters the tree structure returned by the consequences are unspecified; this structure may be the actual object which represents the type hierarchy. The structure types and are used by the and functions to represent information about the currently established exception catches (see the macro) and handlers (see and The type serves as the common base for and Modifying any of the slots of these structures has no effect on the actual frame from which they are derived; the frame structures are only representation which provides information about frames. They are not the actual frames themselves. Both and have a slot. This holds the list of exception type symbols which are matched by the catch or handler. The slot of a holds a list of the descriptions produced by the macro. If there are no descriptions, then this member is otherwise it is a list whose elements are in correspondence with the list in the slot. The slot of a is an opaque ("C pointer") object which is related to the stack address of the catch frame. If it is altered, the catch frame object becomes invalid for the purposes of The slot of a is the registered handler function. Note that all the clauses of a macro are compiled to a single function, which is established via so an instance of the macro corresponds to a single The function inquires the current dynamic environment in order to retrieve information about established exception catch and handler frames. The function returns a list, ordered from the innermost nesting level to the outermost nesting, of structure objects derived from the structure type. The list contains two kinds of objects: structures of type and of type These objects are not the frames themselves, but only provide information about frames. Modifying the slots in these structures has no effect on the original frames. Also, these structures have their own lifetime and can endure after the original frames have disappeared. This has implications for the use of the function. The structures have a slot, which holds a function. It may be invoked directly. A structure may be passed as an argument to the function. The function locates the first (innermost) instance of a specific kind of exception frame (a catch frame or a handler frame) which is eligible for processing an exception of a specific type. If such a frame is found, it is returned. The returned frame object is of the same kind as the objects which comprise the list returned by the function If such a frame is not found, is returned. The argument specifies a match by exception type: the candidate frame must specify in its list of matches at least one type which is an exception supertype of If this argument is omitted, it defaults to which finds any handler that matches at least one type. There is no way to search for handlers which match an empty set of types; the function skips such frames. The argument specifies which frame type to find. Useful values for this argument are the structure type names and or the actual structure type objects which these type names denote. If any other value is specified, the function returns If the argument is omitted, it defaults to the type of the structure. That is to say, by default, the function looks for catch frames. Thus, if is called with no arguments at all it finds the innermost catch frame, if any exists, or else returns The function is similar to except that it returns all matching frames, ordered from the innermost nesting level to the outermost nesting. If called with no arguments, it returns a list of the catch frames. The function abandons the current evaluation context to perform a nonlocal control transfer directly to the catch described by the argument, which must be a structure of type obtained using any of the functions or The control transfer is possible only if the catch frame represented by structure is still established, and if the structure hasn't been tampered with. If a given structure is usable with then a copy of that structure made with is also usable, denoting the same catch frame. The argument should be an exception symbol. It is passed to the exception frame, as if it had appeared as the first argument of the function. Similarly, the are passed to the catch frame as if they were the trailing arguments of a The difference between and is that targets a specific catch frame as its exit point, rather than searching for a matching catch or handler frame. That specific frame receives the control. The frame receives control even if it it is not otherwise eligible for catching the exception type denoted by The macro evaluates If yields any true value, then terminates normally, and that value is returned. If instead yields then throws an exception of type The exception carries an informative character string that contains a diagnostic detailing the expression which yielded and the source location of that expression, if available. If the and possibly additional format arguments are given to then those arguments are used to format additional text which is appended to the diagnostic message after a separating character such as a colon. This section describes a number of features related to the diagnosis of errors during the static processing of program code prior to evaluation. The material is of interest to developers of macros intended for broad reuse. TXR Lisp uses exceptions of type to identify erroneous situations during both transformation of code and its evaluation. These exceptions have one argument, which is a character string. If not handled by program code, exceptions are specially recognized and treated by the built-in handling logic. The message is incorporated into diagnostic output which includes more information which is deduced. TXR Lisp uses exceptions of type to identify certain situations of interest. Ordinary non-deferrable warnings have a structure identical to errors, except for the exception symbol. TXR's provides built-in "auto continue" handling for warnings. If a warning exception is not intercepted by a catch or an accepting handler, then a diagnostic is issued on the stream, after which a exception is thrown with no arguments. If that exception is not handled, then control returns normally to the point that exception to resume the computation which generated the warning. Callers which invoke code that may generate warning exceptions are therefore not required to handle them. However, callers which do handle warning exceptions expect to be able to throw a exception in order to resume the computation that triggered the warning, without allowing other handlers to see the exception. The generation of a warning should thus conform to the following pattern:
  (catch
    (throw 'warning "message")
    (continue ())) TXR supports a form of diagnostic known as a deferrable warning. A deferrable warning is distinguished in two ways. Firstly, it is either of the type or subtyped from that type. The type itself is a direct subtype of Secondly, a deferrable warning carries an additional tag argument after the exception message. A deferrable exception is thrown according to this pattern:
  (catch
    (throw 'defr-warning "message" . tag)
    (continue ())) TXR's built-in exception handling logic reacts specially to the presence of the tag material in the exception. First, the global tentative definition list is searched for the presence of the tag, using equality. If the tag is found, then the warning is discarded. If the tag is not found, then the exception argument list is added to the global deferred warning list. In either case, the exception is thrown to resume the computation which threw the warning, as in the case of an ordinary non-deferrable warning. The purpose of this mechanism is to suppress warnings which become superfluous when more of the program code is examined. For instance, a warning about a call to an undefined function is superfluous if a definition of that function is supplied later, yet before that function call is executed. Deferred warnings accumulate in the deferred warning list from which they can be removed. The list is purged at various times such as when a top-level load completes, and the deferred warnings are released, as if by a call to the function. The functions and provide a convenient and uniform way for code transforming functions such as macro-expanders to generate diagnostics. The function throws an exception of type The function throws an exception of type and internally provides a for the exception which allow a warning handler to resume execution after the warning. If a handler throws a exception which is caught by then returns Because throws a non-error exception, it returns in the event that no catch is found for the exception, and no handler which accepts it. The argument conventions are the same for both functions. The is typically a compound form to which the diagnostic applies. The functions produce a diagnostic message which incorporates the location information and symbol obtained from and the arguments and its The function throws an exception of type and internally provides a for the exception needed to resume after the warning. The function produces a diagnostic message which incorporates the location information and symbol obtained from and the arguments and its This diagnostic message constitutes the first argument of the exception. The argument is taken as the second argument. If the exception isn't intercepted by a catch or by an accepting handler, returns In also returns if it catches a exception. The removes all warnings marked with from the deferred list. It also removes all tags matching from the tentative definition list. Tags are compared using the function. The function adds to the list of tentative definitions which are used to suppress deferrable warnings. The idea is that a definition of some construct has been seen, but not yet executed. Thus the construct is not defined, but it can reasonably be expected that it will be defined; hence, warnings about its nonexistence can be suppressed. For example, in the following code, when the expression is being expanded and transformed, the function does not exist:
  (progn (defun foo ()) (foo)) The function won't be defined until the is evaluated. Thus a warning is generated that refers to an undefined function. However, this warning is discarded, because the expander for registers a tentative definition tag for When the definition of takes place, the operator will call which will remove not only all accumulated warnings related to the undefinedness of but also remove the tentative definition. Note: this mechanism isn't perfect because it will still suppresses the warning in situations like
  (progn (if nil (defun foo ())) (foo)) The function checks whether has been registered via and not yet purged by The function attempts to register a deferred warning. The argument corresponds to the arguments which are passed to the function in order to generate a warning exception, not including the exception symbol. Args is expected to have at least two elements, the second of which is a deferred warning tag. The function returns Note: this function is intended for use in exception handlers. The following example shows a handler which intercepts warnings. It defers deferrable warnings, and prints ordinary warnings:
  (handle
    (some-form ..) ;; some code which might generate warnings
    (defr-warning (msg tag) ;; catch deferrable and defer
      (defer-warning (cons msg tag))
      (throw 'continue)) ;; warning processed: resume execution
    (warning (msg)
      (put-line `warning: @msg`) ;; print non-deferrable
      (throw 'continue))) ;; warning processed: resume execution The removes all warnings from the deferred list. Then, it issues each deferred warning as an ordinary warning. Note: there is normally no need for user programs to use this function since deferred warnings are issued automatically. The empties the list of deferred warnings, and converts each one into a diagnostic message sent to sent to After the diagnostics are printed, the list of pending warnings is cleared. Note: there is normally no need for user programs to use this function since deferred warnings are issued automatically. TXR Lisp supports delimited continuations, which are integrated with the feature. Any named or anonymous block, including the implicit blocks created around function bodies, can be used as the delimiting prompt for the capture of a continuation. A delimited continuation is section of a possible future of the computation, up to a delimiting prompt, reified as a first class function.
  (defun receive (cont)
    (format t "cont returned ~a\n" (call cont 3)))
  (defun function ()
    (sys:capture-cont 'abcd (fun receive)))
  (block abcd
    (format t "function returned ~a\n" (function))
    4)
  Output:
  function returned 3
  cont returned 4
  function returned t

Evaluation begins with the form. This form calls which uses to capture a continuation up to the prompt. The continuation is passed to the function as an argument. This captured object represents the continuation of computation up to that prompt. It appears as a one-argument function which, when called, resumes the captured computation. Its argument emerges out of the call as a return value. When the computation eventually returns all the way to the delimiting prompt, the return value of that prompt will then appear as the return value of the continuation function. In this example, the function immediately invokes the continuation function which it receives, passing it the argument value And so, evaluation now continues in the resumed future represented by the continuation. Inside the continuation, appears to return, yielding the value This bubbles up through up to the where a message is printed: The terminates, yielding the value 4. Thereby, the continuation ends, since it is delimited up to that block. Control now returns to the function which invoked the continuation, where the function call form terminates, yielding the value that was returned by the continuation's delimiting form. The message is printed. The function returns normally, returning the value which emerged from the call. Control is now back in where the form terminates and returns the This bubbles up to which prints In summary, a continuation represents, as a function, the subsequent computation that is to take place starting at some point, up to some recently established, dynamically enclosing delimiting prompt. When the continuation is captured, that future doesn't have to take place; an alternative future can carry out in which that continuation is available as a function. That alternative future can invoke the continuation at will. Invocations (resumptions) of the continuation appear as additional returns from the capture operator. A resumption of a continuation terminates when the delimiting prompt terminates, and the continuation yields the value which emerges from the prompt. Delimited continuations are implemented by capturing a segment of the evaluation stack between the prompt and the capture point. When a continuation is resumed, this saved copy of a stack segment is inserted on top of the current stack and the procedure context is resumed such that evaluation appears to emerge from the capture operator. As the continuation runs to completion, it simply pops these inserted stack frames naturally. Eventually it pops out of the delimiting prompt, at which point control ends up at the point which invoked the continuation function. The low-level operator for capturing a continuation is More expressive and convenient programming with continuations is provided by the macros and which create an abstraction which models the continuation as a suspended procedure supporting two-way communication of data. A operator is provided, which is more general. It is identical to the operator described in various computer science literature about delimited continuations, except that it refers to a specific delimiting prompt by name. Continuations raise the issue of what to do about unwinding. The language Scheme provides the much criticized operator which can execute initialization and clean-up code as a continuation is entered and abandoned. TXR takes a simpler, albeit risky approach. It provides a non-unwinding escape operator for use with continuations. Code which has captured a continuation can use this operator to escape from the delimiting block without triggering any unwinding among the frames between the capture point and the delimiter. When the continuation is restarted, it will then do so with all of the resources associated with it frames intact. When the continuation executes normal returns within its context, the unwinding takes place then. Thus tidy, "thread-like" use of continuations is possible with a small measure of coding discipline. Unfortunately, the absconding operator is dangerous: its use breaks the language guarantee that clean-up associated with a form is done no matter how a form terminates. Delimited continuations resemble lexical closures in some ways. Both constructs provide a way to return to some context whose evaluation has already been abandoned, and to access some aspects of that context. However, lexical closures are statically scoped. Closures capture the lexically apparent scope at a given point, and produce a function whose body has access to that scope, as well as to some arbitrary arguments. Thus, a lexical scope is reified as a first-class function. By contrast, a delimited continuation is dynamic. It captures an an entire segment of a program activation chain, up to the delimiting prompt. This segment includes scopes which are not lexically visible at the capture point: the scopes of parent functions. Moreover, the segment includes not only scopes, but also other aspects of the evaluation context, such as the possibility of returning to callers, and the (captured portion of) the original dynamic environment, such as exception handlers. That is to say, a lexical closure's body cannot return to the surrounding code or see any of its original dynamic environment; it can only inspect the environment, and then return to its own caller. Whereas a restarted delimited continuation can continue evaluation of the surrounding code, return to surrounding forms and parent functions, and access the dynamic environment. The continuation function returns to its caller when that entire restarted context terminates, whereas a closure returns to its caller as soon as the closure body terminates. Delimited continuations in TXR expose a behavioral difference between compiled and interpreted code which mutates the values of lexical variables. When a continuation is captured in compiled code, it captures not only the bindings of lexical variables, but also potentially their current values at the time of capture. What this means is that whenever the continuation is resumed, those variables will appear to have the captured values, regardless of any mutations that have taken place since. In other words, the captured future includes those specific values. This is because in compiled code, variables are allocated on the stack, which is copied as part of creating a continuation. Those variables are effectively newly instantiated in each resumption of the continuation, when the captured stack segment is reinstated into the stack, and take on those original values. In contrast, interpretation of code only maintains an environment pointer on the stack; the lexical environment is a dynamically allocated object whose contents aren't included in the continuation's stack segment capture. If the captured variables are modified after the capture, the continuation will see the updated values: all resumptions of the continuation share the same instance of the captured environment among themselves, and with the original context where the capture took place. An additional complication is that when compiled code captures lexical closures, captured variables are moved into dynamic storage and then they become shared: the semantics of the mutation of those variables is then similar to the situation in interpreted code. Therefore, the above described non-sharing capture behavior of compiled code is not required to hold. In continuation-based code which relies on mutation of lexical variables created with or the macros and can be used instead. These macros create variable bindings whose storage is always outside of the stack, and therefore the variables will exhibit consistent interpreted and compiled semantics under continuations. All contexts which capture the same lexical binding of a given variable share a single instance. The most recent assignment to the variable taking place in any context establishes its value, as seen by any other context. The resumption of a continuation will not restore such a variable to a previous value. If the affected variables are other kinds of bindings such as function parameters or variables created with specialized binding constructs such as additional coding changes may be required to get interpreted code working under compilation. The function captures a continuation, and also serves as the resume point for the resulting continuation. Which of these two situations is the case (capture or resumption) is distinguished by the use of the argument, which must be a function capable of being called with one argument. A block named must be visible; the continuation is delimited by the closest enclosing block of this name. The optional argument should be a compound form. If reports an error, it reports it against this form, and uses the form's operator symbol as the name of the function which encountered the error. If the argument is omitted, uses its own name. The function captures a continuation, represented as a function. It immediately calls passing it it the continuation function as an argument. If returns normally, then returns whatever value returns. Resuming a continuation is done by invoking the continuation function. When this happens, the entire continuation context is restored by recreating its captured evaluation frames on top of the current stack. Inside the continuation, the function call which captured the continuation now appears to return, and yields a value. That value is precisely the value which was just passed to the continuation function moments ago. The resumed continuation can terminate in one of three ways. Firstly, it can simply keep executing until it discards all of its evaluation frames below the delimiting block, and then allows that block to terminate naturally by evaluating the last form contained in the block. Secondly, can use against its delimiting block to explicitly abandon all evaluations in between and terminate that block. Or it may perform a nonlocal control transfer past the delimited block somewhere into the evaluation frames of the caller. In the first two cases, the termination of the block turns into an ordinary return from the continuation function, and the result value of the terminated block becomes the return value of that function call. In the last case, the call of the continuation function is abandoned and unwinding continues through the caller. If the symbol is passed to the continuation function, the continuation will be resumed in a different manner: its context will be restored as in the ordinary resume case, whereupon it will be immediately abandoned by a nonlocal exit, causing unwinding to take place across all of the continuation's evaluation frames. The function then returns If the symbol is passed to the continuation function, the continuation isn't be resumed at all; rather, the buffer which holds the saved context of the continuation is released. Thereafter, an attempt to resume the continuation results in an error exception being thrown. After releasing the buffer, the function returns The continuation function may be used any time after it is produced, and may be called more than once, regardless of whether the originally captured dynamic context is still executing. The continuation object may be communicated into the resumed continuation, which can then use it to call itself, resulting in multiple nested resumptions of the same continuation. A delimited continuation is effectively a first class function. The underlying continuation object produced by stores a copy of the captured dynamic context. Whenever the continuation function is invoked, a copy of the captured is reinstated as if it were a new context. Thus each apparent return from the inside a resumed continuation is not actually made in the original context, but in a copy of that context. That context can be resumed multiple times sequentially or recursively. Just like lexical closures, continuations do not copy lexical environments; they capture lexical environments by reference. If a continuation modifies the values of captured lexical variables, those modifications are visible to other resumptions of the same continuation, to other continuations which capture the same environment, to lexical closures which capture the same environment and to the original context which created that environment, if it is still active. Unlike lexical closures, continuations do capture the local bindings of special variables. That is to say, if is a special variable, then a lexical closure created inside a form will not capture the local rebinding of which holds 42. When the closure is invoked and accesses it accesses whatever value of is dynamically current, as dictated by the environment which calls the closure, rather than the capturing environment. With continuations, the behavior is different. If a continuation is captured inside a form then it does capture the local binding. This is regardless whether the delimited prompt of the capture is enclosed in this form, or outside of the form. The special variable has a binding in a dynamic environment. There is always a reference to a current dynamic environment associated with every evaluation context, and a continuation captures that reference. Because it is a reference, it means that the binding is shared. That is to say, all invocations of all continuations which capture the same dynamic environment in which that binding was made share the same binding; if is modified by assignment, the modification is visible to all those views. Inside a resumed continuation, a form which binds a special variable such as may terminate. As expected, this causes the binding to be removed, revealing either another local binding of or the global binding. However, this unbinding only affects only that that executing continuation; it has no effect inside other instances of the same continuation or other continuations which capture the same variable. Unbinding isn't a mutation of the dynamic environment, but may be understood as merely the restoration of an earlier dynamic environment reference. The following example shows an implementation of the operator.
  (defmacro suspend (:form form name var . body)
    ^(sys:capture-cont ',name (lambda (,var)
                        (sys:abscond-from ,name ,*body))
                       ',form)) The operator closely resembles and performs the same function: it causes an enclosing block to terminate with which defaults to However, unlike does not perform any unwinding. This operator should never be used for any purpose other than implementing primitives for the use of delimited continuations. It is used by the and operators to escape out of a block in which a continuation has been captured. Neglecting to unwind is valid due to the expectation that control will return into a restarted copy of that context. The function is similar to the operator, except that is an ordinary function parameter, and so when is used, an argument expression must be specified which evaluates to a symbol. Thus allows the target block of a return to be dynamically computed. The following equivalence holds between the operator and function:
  (sys:abscond-from a b) <--> (sys:abscond* 'a b) Expressions used as arguments to which do not simply quote a symbol have no equivalent in The and macros closely interoperate. The macro treats zero or more as a suspendable execution context called the obtain block. It is expected that establish a block named and return its result value to Without evaluating any of the forms in the obtain block, returns a function, which takes one optional argument. This argument, called the resume value, defaults to if it is omitted. The function represents the suspended execution context. The context is resumed whenever the function is called, and executes until the next statement which references the block named The function's reply argument is noted. If the specifies a argument, then the execution context suspends, and the resume function terminates and returns the value of that form. When the function is called again to resume the context, the returns the previously noted resume value (and the new resume value just passed is noted in its place). If the specifies no argument, then it briefly suspends the execution context only to retrieve the resume value, without producing an item. Since no item is produced, the resume function does not return. The execution context implicitly resumes. When execution reaches the last form in the obtain block, the resume value is discarded. The execution context terminates, and the most recent call to the resume function returns the value of that last form. The macro registers a finalizer against the returned resume function. The finalizer invokes the function, passing it the symbol thereby triggering unwinding in the most recently captured continuation. Thus, abandoned blocks are subject to unwinding when they become garbage. The macro works by capturing a continuation and performing a nonlocal exit to the nearest block called It passes a special yield object to that block. The macro generates code which knows what to do with this special yield object. The following example shows a function which recursively traverses a cell structure, yielding all the atoms it encounters. Finally, it returns the object The function is invoked on a list, and the invocation is wrapped in an block to convert it to a generating function. The generating function is then called six times to retrieve the five atoms from the list, and the final value. These are collected into a list. This example demonstrates the power of delimited continuations to suspend and resume a recursive procedure.
  (defun yflatten (obj)
    (labels ((flatten-rec (obj)
               (cond
                 ((null obj))
                 ((atom obj) (yield-from yflatten obj))
                 (t (flatten-rec (car obj))
                    (flatten-rec (cdr obj))))))
      (flatten-rec obj)
      nil))
  (let ((f (obtain (yflatten '(a (b (c . d)) e)))))
    (list [f] [f] [f] [f] [f] [f]))
  --> (a b c d e nil) The following interactive session log exemplifies two-way communication between the main code and a suspending function. Here, is invoked on a list of symbols representing fruit and vegetable names. The objective is to return a list containing only fruits. The function suspends execution and yields a question out of the block. It then classifies the item as a fruit or not according to the reply it receives. The reply emerges as the result value of the call. The macro converts the block to a generating function. The first call to the function is made with no argument, because the argument would be ignored anyway. The function returns a question, asking whether the first item in the list, the potato, is a fruit. To answer positively or negatively, the user calls the function again, passing in or respectively. The function returns the next question, which is answered in the same manner. When the question for the last item is answered, the function call yields the final item: the ordinary result of the block, which is the list of fruit names.
  1> (obtain
       (block map
         (mappend (lambda (item)
                    (if (yield-from map `is @item a fruit?`)
                      (list item)))
                  '(potato apple banana lettuce orange carrot))))
  #<interpreted fun: lambda (: reply)>
  2> (call *1)
  "is potato a fruit?"
  3> (call *1 nil)
  "is apple a fruit?"
  4> (call *1 t)
  "is banana a fruit?"
  5> (call *1 t)
  "is lettuce a fruit?"
  6> (call *1 nil)
  "is orange a fruit?"
  7> (call *1 t)
  "is carrot a fruit?"
  8> (call *1 nil)
  (apple banana orange) The following example demonstrates an accumulator. Values passed to the resume function are added to a counter which is initially zero. Each call to the function returns the updated value of the accumulator. Note the use of with no arguments to receive the value passed to the first call to the resume function, without yielding an item. The first return value is produced by the form, not by The latter only obtains the initial value and uses it to establish the seed value of the accumulator. Without causing the resume function to terminate and return, control passes into the loop, which yields the first item, causing the resume function call to return
  1> (obtain
       (block acc
         (let ((sum (yield-from acc)))
            (while t (inc sum (yield-from acc sum))))))
  #<interpreted fun: lambda (: resume-val)>
  2> (call *1 1)
  1
  3> (call *1 2)
  3
  4> (call *1 3)
  6
  5> (call *1 4)
  10 The macro combines and into a single expression. The are evaluated in a block named That is to say, the following equivalence holds:
  (obtain-block n f ...) <--> (obtain (block n f ...)) The macro is to as is to it yields from an anonymous block. It is equivalent to calling using as the block name. In other words, the following equivalence holds:
  (yield x) <--> (yield-from nil x)
  ;; Yield the integers 0 to 4 from a for loop, taking
  ;; advantage of its implicit anonymous block:
  (defvarl f (obtain (for ((i 0)) ((< i 5)) ((inc i))
                       (yield i))))
  [f] -> 0
  [f] -> 1
  [f] -> 2
  [f] -> 3
  [f] -> 4
  [f] -> nil
  [f] -> nil The and macros implement a useful variation of and The macro differs from in exactly one regard: prior to returning the function, it invokes it one time, with the argument value Thus, the following equivalence holds
  (obtain* forms ...) <--> (let ((f (obtain forms ...)))
                        (call f)
                        f) In other words, the suspended block is immediately resumed, so that it executes either to completion (in which case its value is discarded), or to its first or call (in which case the yielded value is discarded). Note: the macro is useful in creating suspensions which accept data rather than produce data. The macro combines and in the same manner that combines and
  ;; Pass three values into suspended block,
  ;; which get accumulated into list.
  (let ((f (obtain*-block nil
             (list (yield nil) (yield nil) (yield nil)))))
    (call f 1)
    (call f 2)
    (call f 3)) -> (1 2 3)
  ;; Under obtain, extra call is required:
  (let ((f (obtain-block nil
             (list (yield nil) (yield nil) (yield nil)))))
    (call f nil) ;; execute block to first yield
    (call f 1) ;; resume first yield with 1
    (call f 2)
    (call f 3)) -> (1 2 3) The operator captures a continuation up to the prompt given by the symbol and binds it to the variable name given by which must be a symbol suitable for binding variables with Each is then evaluated in the scope of the variable When the last is evaluated, a nonlocal exit takes place to the block named by (using the operator, so that unwinding isn't performed). When the continuation bound to is invoked, a copy of the entire block is restarted, and in that copy, the call appears to return normally, yielding the value which had been passed to the continuation. Define John McCarthy's function using and
  (defmacro amb-scope (. forms)
    ^(block amb-scope ,*forms))
  (defun amb (. args)
    (suspend amb-scope cont
      (each ((a args))
        (if a
          (iflet ((r (call cont a)))
            (return-from amb-scope r)))))) Use to bind the and which satisfy the predicate nondeterministically:
  (amb-scope
    (let ((x (amb 1 2 3))
          (y (amb 4 5 6)))
      (amb (eql (* x y) 8))
      (list x y)))
  -> (2 4) The and macros behave exactly like and respectively except that they guarantee that the variable bindings are allocated in storage which isn't captured by delimited continuations. The in the names stands for "heap", serving as a mnemonic based on the implementation concept of these bindings being "heap-allocated". TXR provides a "pure" regular-expression implementation based on automata theory, which equates regular expressions, finite automata and sets of strings. A regular expression determines whether or not a string of input characters belongs to a set. TXR regular expressions do not support features such as "anchoring" a match to the start or end of a string, or capturing parenthesized subexpression matches into registers. Parenthesis syntax denotes only grouping, with no additional meaning. The semantics of whether a regular expression is used for a substring search, prefix match, suffix match, string splitting and so forth comes from the functions which use regular expressions to perform these operations. A regular expression is callable as a function in TXR Lisp. When used this way, it requires a string argument. It searches the string for the leftmost match for itself, and returns the matching substring, which could be empty. If no match is found, it returns A regex takes one, two, or three arguments. The required is always the rightmost argument. This allows for convenient partial application over optional arguments using macros in the family, and macros in which the syntax is implicit. The optional arguments and are treated exactly as their like-named counterparts in the function. Keep those elements from a list of strings which match the regular expression
  (keep-if #/a.*b/ '#"abracadabra zebra hat adlib adobe deer")
  --> ("abracadabra" "adlib" "adobe") The function searches through starting at position for a match for If is omitted, the search starts at position 0. If is specified and has a value, the search proceeds in reverse, from the position just beyond the last character of toward If exceeds the length of the string, then returns If is negative then it indicates positions from the end of the string, such that -1 is the last character, -2 the second last and so forth. If the value is so negative that it refers beyond the start of the string, then the starting position is deemed to be zero. If is equal to the length of and thus refers to the position one character past its length, then a match occurs at that position if admits such a match. The function returns if no match is found, otherwise it returns a cons, whose indicates the position of the match, and whose indicates the length of the match. If is capable of matching empty strings, and no other kind of match is found within then search regex reports a zero length match. If is false, then this match is reported at otherwise it is reported at the position one character beyond the end of the string. The function is similar to except that when a match is found, it returns a position range, rather than a position and length. A range object is returned whose field indicates the position of the match, and whose indicates the position one element past the last character of the match. If the match is empty, the two integers are equal. Also see the function, which provides an alternative argument syntax for the semantics of The differs from in the representation of the return value in the matching case. Rather than returning the position and length of the match, it returns the matching substring of The function tests whether matches at in If is not specified, it is taken to be zero. Negative values of index from the right end of the string such that -1 refers to the last character. Excessively negative values which index before the first character cause to be returned. If the regex matches, then the length of the match is returned. If it does not match, then is returned. The differs from in the representation of the return value in the matching case. Rather than returning the length of the match, it returns matching substring of The function tests whether some substring of which terminates at the character position just before matches If is not specified, it defaults to the length of the string, and the function performs a right-anchored regex match. The argument can be a negative integer, in which case it denotes positions from the end of the string, such that -1 refers to the last character. If the value is excessively negative such that the position immediately before it is before the start of the string, then is returned. If is a positive value beyond the length of then, likewise, is returned. If a match is found, then the length of the match is returned. A more precise way of articulating the role of is that for the purposes of matching, is considered to terminate just before in other words, that is the length of the string. The match is then anchored to the end of this effective string. The differs from in the representation of the return value in the matching case. Rather than returning the length of the match, it returns the matching substring of
  ;; Return matching portion rather than length thereof.
  (defun match-regex-right-substring (str reg : end-pos)
    (set end-pos (or end-pos (length str)))
    (let ((len (match-regex-right str reg end-pos)))
      (if len
        [str (- end-pos len)..end-pos]
        nil)))
  (match-regex-right-substring "abc" #/c/) -> ""
  (match-regex-right-substring "acc" #/c*/) -> "cc"
  ;; Regex matches starting at multiple positions, but all
  ;; the matches extend past the limit.
  (match-regex-right-substring "acc" #/c*/ 2) -> nil
  ;; If the above behavior is not wanted, then
  ;; we can extract the string up to the limiting
  ;; position and do the match on that.
  (match-regex-right-substring ["acc" 0..2] #/c*/) -> "c"
  ;; Equivalent of above call
  (match-regex-right-substring "ac" #/c*/) -> "c" The determines whether the input string might be the prefix of a string which matches regular expression The result is true if the input string matches exactly. However, it is also true in situations in which the input string doesn't match yet can be extended with one or more additional characters beyond the end such that the extended string does match. The argument must be a character string. The function takes the input string to be the suffix of which starts at the character position indicated by the argument. If that argument is omitted, then is taken as the input in its entirety. Negative values index backwards from the end of according to the usual conventions elsewhere in the library. Note: this function is not to be confused for the semantics of a regex matching a prefix of a string: that capability is provided by the functions and
  ;; The empty string is not a viable prefix match for
  ;; a regex that matches no strings at all:
  (regex-prefix-match #/~.*/ "") -> nil
  (regex-prefix-match #/[]/ "") -> nil
  ;; The empty string is a viable prefix of any regex
  ;; which matches at least one string:
  (regex-prefix-match #// "") -> t
  (regex-prefix-match #/abc/ "") -> t
  ;; This string doesn't match the regex because
  ;; it doesn't end in b, but is a viable prefix:
  (regex-prefix-match #/a*b/ "aa") -> t
  (regex-prefix-match #/a*b/ "ab") -> t
  (regex-prefix-match #/a*b/ "ac") -> nil
  (regex-prefix-match #/a*b/ "abc") -> nil The function operates in two modes, depending on whether the first argument is a regular expression, or function. If the first argument is a regular expression or string, then searches for multiple occurrences of non-overlapping matches for that or A new string is constructed similar to but in which each matching region is replaced with using as follows. The object may be a character or a string, in which case it is simply taken to be the replacement for each match of the regular expression. The object may be a function of one argument, in which case for every match which is found, this function is invoked, with the matching piece of text as an argument. The function's return value is then taken to be the replacement text. If the first argument is a function, then it is called, with as its argument. The return value must be either a range object (see the function) which indicates the extent of to be replaced, or else which indicates that no replacement is to take place.
  ;; match every lowercase e or o, and replace by filtering
  ;; through the upcase-str function:
  [regsub #/[eo]/ upcase-str "Hello world!"] -> "HEllO wOrld!"
  ;; Replace Hello with Goodbye:
  (regsub #/Hello/ "Goodbye" "Hello world!") -> "Goodbye world!"
  ;; Same, as a simple substring match, rather than regex:
  (regsub "Hello" "Goodbye" "Hello world!") -> "Goodbye world!"
  ;; Left-anchored replacement with r^ function:
  (regsub (fr^ #/H/) "J" "Hello, hello!") -> "Jello, hello!" The function returns if is a compiled regular-expression object. For any other object type, it returns The and functions return a new string, equivalent to with a leading or trailing portion removed. If the first argument is a regular expression then, respectively, and find a prefix or suffix of which matches the regular expression. If there is no match, or if the match is empty, then is returned. Otherwise, a copy of is returned in which the matching characters are removed. If matches all of then the empty string is returned. If the first argument is a character string, then it is treated as an exact match for that sequence of characters. Thus, interprets that string as a to be removed, and as a If starts with then returns a copy of with removed. Otherwise, is returned. Likewise, if ends with then returns a copy of with removed. Otherwise, is returned. The function takes the source code of a regular expression, expressed as a Lisp data structure representing an abstract syntax tree, or else a regular expression specified as a character string, and compiles it to a regular-expression object. If is a character string, it is parsed to an abstract syntax tree first, if by the function. If the parse is successful (the result is not then the resulting tree structure is compiled by a recursive call to The optional argument is passed down to as well as in the recursive call to if that call takes place. If is specified, it must be a stream. Any error diagnostics are sent to that stream.
  ;; the equivalent of #/[a-zA-Z0-9_]/
  (regex-compile '(set (#\a . #\z) (#\A . #\Z)
                       (#\0 . #\9) #\_))
  ;; the equivalent of #/.*/ and #/.+/
  (regex-compile '(0+ wild))
  (regex-compile '(1+ wild))
  ;; #/a|b|c/
  (regex-compile '(or (or #\a #\b) #\c))
  ;; string
  (regex-compile "a|b|c") The function returns the source code of compiled regular expression The source code isn't the textual notation, but the Lisp data structure representing the abstract syntax tree: the same representation as what is returned by The function parses a character string which contains a regular expression and turns it into a Lisp data structure (the abstract syntax tree representation of the regular expression). The regular-expression syntax produces the same structure, but as a literal which is processed at the time TXR source code is read; the function performs this parsing at run time. If there are parse errors, the function returns The optional argument specifies a stream to which error messages are sent from the parser. By default, diagnostic output goes to the stream, which discards it. If is specified as then the diagnostic output goes to the stream. If returns a value, that structure is then something which is suitable as input to There is a small difference in the syntax accepted by and the syntax of regular-expression literals. Any (slash) characters occurring in any position within are treated as ordinary characters, not as regular-expression delimiters. The call (regex-parse "/a/") matches three characters: a slash, followed by the letter "a", followed by another slash. Note that the slashes are not escaped. Note: if a call is written using a string literal as the argument, then note that any backslashes which are to be processed by the regular expression must be doubled up, otherwise they belong to the string literal:
  (regex-parse "\*") ;; error, invalid string literal escape
  (regex-parse "\\*") ;; correct: the \* literal match for * The double backslash in the string literal produces a single backslash in the resulting string object that is processed by The function accepts the source code of a regular expression, expressed as a Lisp data structure representing an abstract syntax tree, and calculates an equivalent structure in which certain simplifications have been performed, or in some cases substitutions which eliminate the dependence on derivative-based processing. The argument is assumed to be correct, as if it were produced by the or functions. Incorrect syntax produces unspecified results: an exception may be thrown, or some object may appear to be successfully returned. Note: it is unnecessary to call this function to prepare the input for because that function optimizes internally. However, the source code attached to a compiled regular-expression object is the original unoptimized syntax tree, and that is used for rendering the notation when the object is printed. If the syntax is passed through before the resulting object will have the optimized code attached to it, and subsequently render that way in printed form.
  ;; a|b|c -> [abc]
  (regex-optimize '(or #\a (or #\b #\c))) -> (set #\a #\b #\c)
  ;; (a|) -> a?
  (regex-optimize '(or #\a nil)) -> (? #\a) The function reads characters from accumulating them into a string, which is returned. If an argument is not specified for then the stream is used. The argument is Boolean, indicating whether the delimiting text matched by is included in the returned string. It defaults to The accumulation of characters is terminated by a match on the end of the stream, or an error. This means that characters are read from the stream and accumulated while the stream has more characters available, and while its prefix does not match If matches the stream before any characters are accumulated, then an empty string is returned. If the stream ends or a non-exception-throwing error occurs before any characters are accumulated, the function returns When the accumulation of characters terminates by a match on the longest possible matching sequence of characters is removed from the stream. If is true, that matching text is included in the returned string. Otherwise, it is discarded. The next available character in the stream is the first nonmatching character following the matched text. However, the next available character, as well as some number of subsequent characters, may originate from the stream's push-back buffer, rather than from the underlying operating system object, due to this function's internal use of the function. Therefore, the stream position, as would be reported by is unspecified. The functions and read characters from until a match occurs in the stream for regular expression the stream runs out of characters, or an error occurs. If the stream runs out of characters, or a non-exception-throwing error occurs, before a match for is identified, these functions return If a match for occurs in then returns the number of characters that were read and discarded prior to encountering the first matching character. In the same situation, the function returns a cell whose holds the count of discarded characters, that being the same value as what would be returned by and whose holds a character string that comprises the text matched by The text matched by is as long as possible, and is removed from the stream. The next available character in the stream is the first nonmatching character following the matched text. However, the next available character, as well as some number of subsequent characters, may originate from the stream's push-back buffer, rather than from the underlying operating system object, due to these functions' internal use of the function. Therefore, the stream position, as would be reported by is unspecified. These functions provide functionality similar to the and functions, but under alternative interfaces which are more convenient. The and notation used in their names are an allusion to the regular-expression search-anchoring operators found in familiar POSIX utilities such as The argument, if omitted, defaults to zero, so that the entire is operated upon. The argument defaults to the length of so that the end position coincides with the end of the string. If the or arguments are negative, they index backwards from the length of so that -1 denotes the last character. A value in either parameter which is excessively negative or positive, such that it indexes before the start of the string or exceeds its length results in a failed match and consequently being returned. The function tests whether the entire portion of starting at through to the end of the string is in the set of strings matched by If this is true, then that portion of the string is returned. Otherwise is returned. The function tests whether the portion of the starting at has a prefix which matches If so, then this matching prefix is returned. Otherwise is returned. The function tests whether the portion of ending just before has a suffix which matches If so, then this matching suffix is returned. Otherwise is returned. The first three of these functions perform the same operations as, respectively, and with the same argument conventions. They differ in return value. When a match is found, they return a range value indicating the extent of the matching substring within rather than the matching substring itself. The function performs the same operation as with different conventions with regard to argument order, harmonizing with those of the other three functions above. The argument, if omitted, defaults to zero, so that the entire is operated upon. The argument defaults to the length of so that the end position coincides with the end of the string. With one exception, a value in either parameter which is excessively negative or positive, such that it indexes before the start of the string or exceeds its length results in a failed match and consequently being returned. The exception is that the function permits a negative value which refers before the start of the string; this is effectively treated as zero. The argument defaults to The function tests whether the entire portion of starting at through to the end of the string is in the set of strings matched by If this is true, then the matching range is returned, as a range object. The function tests whether the portion of the starting at has a prefix which matches If so, then the matching range is returned, as a range object. Otherwise is returned. The function tests whether the portion of ending just before has a suffix which matches If so, then the matching range is returned. Otherwise is returned. The function searches starting at for a match for If is specified and true, the rightmost match is reported. If a match is found, it is reported as a range. A regular expression which matches empty strings matches at the start position, and every other position, including the position just after the last character, coinciding with the length of Except for the different argument order such that is always the rightmost argument, the function is equivalent to the function, such that correspondingly named arguments have the same semantics. The function searches between the and position for matches for the regular expression The matches are returned as a list of range objects. The argument defaults to zero, and defaults to the length of the string (the position one past the last character). Negative values of and indicate positions from the end of the string, such that -1 denotes the last character, -2 the second-to-last and so forth. If is so negative that it refers before the start of it is treated as zero. If this situation is true of the argument, then the function returns If refers to a character position beyond the length of (two characters or more beyond the end of the string), then the function returns If this situation is true of then is curtailed to the string length. The function returns all non-overlapping matches, including zero length matches. Zero length matches may occur before the first character of the string, or after the last character. If so, these are included. These regular-expression functions do not directly perform regex operations. Rather, they each return a function of one argument which performs a regex operation. The returned functions perform the same operations as, respectively, and The following equivalences nearly hold, except that the functions on the right side produced by can accept two arguments when only is curried, whereas the functions on the left take only one argument:
  [f^$ r] <--> (op m^$ r)
  [f^$ r p] <--> (op m^$ r p)
  [f^ r] <--> (op m^ r)
  [f^ r p] <--> (op m^ r p)
  [f$ r] <--> (op m$ r)
  [f$ r p] <--> (op m$ r p) That is to say, returns a function which binds and possibly the optional When this function is invoked, it must be given an argument which is a string. It performs the same operation as being called on and possibly The same holds between and and between and
  ;; produce list which contains only strings
  ;; beginning with "cat":
  (keep-if (f^ #/cat/) '#"dog catalog cat fox catapult")
  --> ("catalog" "cat" "catapult")
  ;; map all strings in a list to just their trailing
  ;; digits.
  (mapcar (f$ #/\d*/) '#"a123 4 z bc465")
  --> ("123" "4" "" "465")
  ;; check that all strings consist of digits after
  ;; the third position.
  (all '#"ABC123 DFE45 12379" (f^$ #/\d*/ 3))
  --> "79" ; i.e. true
  (all '#"ABC123 DFE45 12379A" (f^$ #/\d*/ 3))
  --> nil These regular-expression functions do not directly perform regex operations. Rather, they each return a function of one argument which performs a regex operation. The returned functions perform the same operations as, respectively, and The following equivalences nearly hold, except that some of the functions on the right side produced by op can accept additional arguments after the input string, whereas the functions on the left produced by et al. accept only one parameter: the input string.
  [fr^$ r] <--> (op m^$ r)
  [fr^$ r p] <--> (op m^$ r p)
  [fr^ r] <--> (op m^ r)
  [fr^ r p] <--> (op m^ r p)
  [fr$ r] <--> (op m$ r)
  [fr$ r p] <--> (op m$ r p)
  [frr r] <--> (op m$ r)
  [frr r s] <--> (op m$ r s)
  [frr r s fe] <--> (op m$ r s fe) That is to say, returns a function which binds and possibly the optional When this function is invoked, it must be given an argument which is a string. It performs the same operation as being called on and possibly and the string. The same holds between and between and and between and
  ;; Remove leading digits from "123A456",
  ;; other than first digit:
  (regsub (fr^ #/\d+/ 1) "" "123A456")
  --> "1A456" A hash table is an object which retains an association between pairs of objects. Each pair consists of a key and a value. Given an object which is similar to a key in the hash table, it is possible to retrieve the corresponding value. Entries in a hash table are not ordered in any way, and lookup is facilitated by hashing: quickly mapping a key object to a numeric value which is then used to index into one of many buckets where the matching key will be found (if such a key is present in the hash table). In addition to keys and values, a hash table contains a storage location which allows it to be associated with user data. Important to the operation of a hash table is the criterion by which keys are considered same. By default, this similarity follows the function. A hash table will search for a stored key which is to the given search key. A hash table constructed with the property compares keys using the function instead. In addition to storing key-value pairs, a hash table can have a piece of information associated with it, called the user data. TXR hash tables contain a seed value which permutes the hashing operation, at least for keys of certain types. This feature, if the seed is randomized, helps to prevent software from being susceptible to hash collision denial-of-service attacks. However, by default, the seed is not randomized. Newly created hash tables for which a seed value is not specified take their seed value from the special variable, which is initialized to zero. That includes hash tables created by parsing hash literal syntax. Security-sensitive programs requiring protection against collision attacks may use to create a randomized hash seed, and, depending on their specific need, either store that value in or pass the value to hash-table constructors like or both. Note: randomization of hash seeding isn't a default behavior because it affects program reproducibility. The seed value affects the order in which keys are traversed, which can change the output of programs whose inputs have not changed, and whose logic is is otherwise deterministic. A hash table can be traversed to visit all of the keys and data. The order of traversal bears no relation to the order of insertion, or to any properties of the key type. During an open traversal, new keys can be inserted into a hash table or deleted from it while a a traversal is in progress. Insertion of a new key during traversal will not cause any existing key to be visited twice or to be skipped; however, it is not specified whether the new key will be traversed. Similarly, if a key is deleted during traversal, and that key has not yet been visited, it is not specified whether it will be visited during the remainder of the traversal. These remarks apply not only to deletion via or the operator, but also to wholesale deletion of all keys via The garbage collection of hash tables supports weak keys and weak values. If a hash table has weak keys, this means that from the point of view of garbage collection, that table holds only weak references to the keys stored in it. Similarly, if a hash table has weak values, it means that it holds a weak reference to each value stored. A weak reference is one which does not prevent the reclamation of an object by the garbage collector. That is to say, when the garbage collector discovers that the only references to some object are weak references, then that object is considered garbage, just as if it had no references to it. The object is reclaimed, and the weak references "lapse" in some way, which depends on what kind they are. Hash-table weak references lapse by entry removal. When an object used as a key in one or more weak-key hash tables becomes unreachable, those hash entries disappear. This happens even if the values are themselves reachable. Vice versa, when an object appearing as a value in one or more weak-value hash tables becomes unreachable, those entries disappear, even if the keys are reachable. When a hash table has both weak keys and weak values, then an the behavior is one of two possible semantics. Under the the hash table entry is removed if either the key or the value is unreachable. Under the the entry is removed only if both the key and value are unreachable. If the keys of a weak-key hash table are reachable from the values, or if the values of a weak-key hash table are reachable from the keys, then the weak semantics is defeated for the affected entries: the hash table retains those entries as if it were an ordinary table. A hash table with both weak keys and values does not have this issue, regardless of its semantics. An open traversal of a hash table is performed by the function and the operator. The traversal is open because code supplied by the program is evaluated for each entry. The functions and also perform an open traversal, because they return lazy lists. The traversal isn't complete until the returned lazy list is fully instantiated. In the meanwhile, the TXR program can mutate the hash table from which the lazy list is being generated. Certain hash operations expose access to the internal key-value association entries of a hash table, which are represented as ordinary cells. Modifying the field of such a cell potentially violates the integrity of the hash table; the behavior of subsequent lookup and insertion operations becomes unspecified. Similarly, if an object is used as a key in an hash table, and that object is mutated in such a way that its equality to other objects under the function is affected or its hash value under is altered, the behavior of subsequent lookup and insertion operations on the becomes unspecified. These functions construct a new hash table. takes three mandatory Boolean arguments. The Boolean argument specifies whether the hash table shall have weak keys. The argument specifies whether it shall have weak values, and specifies whether it is If the argument is one of the keywords or then the hash table shall have both weak keys and weak values, with the semantics implied by the keyword: specifies and specifies The argument is then ignored. If both and are true, and is not one of the keywords or then the hash table has The function defaults all three of these properties to false, and allows them to be overridden to true by the presence of keyword arguments. The optional parameter must be an integer, if specified. Its value perturbs the hashing function of the hash table, which affects hash tables, when character strings and buffers are used as keys. If is omitted, then the value of the variable is used as the seed. It is an error to attempt to construct an hash table which has weak keys. The function provides an alternative interface. It accepts optional keyword arguments. The supported keyword symbols are: and which can be specified in any order to turn on the corresponding properties in the newly constructed hash table. Only one of and may be specified. If specified, then the hash table uses or equality, respectively, for considering two keys to be the same key. If none of these is specified, the function produces an hash table by default. If or is specified, then may not be specified. At most one of or may be specified. If either of these is specified, then the and keywords are redundant and unnecessary. If and are both specified, and isn't specified, the situation is equivalent to If is present, it must be followed by an argument value; that value specifies the user data for the hash table, which can be retrieved using the function. Note: there doesn't exist a keyword for specifying the seed. This omission is deliberate. These hash construction keywords may appear in the hash literal syntax. A seed keyword would allow literals to specify their own seed, which would allow malicious hash literals to be crafted that perpetrate a hash collision attack against the parser. The function constructs a populated hash in one step. The argument specifies a list suitable as an argument list in a call to the function. The is a sequence of pairs, which are two-element lists representing key-value pairs. A hash is constructed as if by a call to then populated with the specified pairs, and returned. The function is an alternative interface to the same semantics. The argument is first, and the are passed as trailing variadic arguments, rather than a single list argument. The function is similar to except that the argument specifies they keys and values as an association list. The elements of the list are cells, each of whose is a key, and whose is the value. The function constructs a hash as if by a call to where is a list of the individual variadic arguments. The hash is then populated with keys taken from and returned. The value associated with each key is that key itself. The function constructs a hash as if by a call to where is a list of the individual variadic arguments. The hash is then populated with keys taken from which are paired with values taken from from and returned. If is longer than then the excess keys are ignored, and vice versa. The function constructs a populated hash table without requiring the caller to construct a list of entries. The hash table contents are specified as direct arguments. The function requires an even number of arguments, which are interleaved key-value pairs. The returned hash table is and no parameters are available for customizing any of its properties, such as weakness. The function constructs a a hash table from a of keys and a which maps them to values. The argument must be a function that can be called with one argument. The elements of become the keys of the returned hash table. The value associated with each key is determined by passing that value to function and taking the returned value. The remaining arguments determine what kind of hash table is created, as if the function were applied to them. If the sequence contains duplicate elements (according to the hash table equality in effect for the hash table being constructed), duplicate elements later in the sequence replace earlier elements. The function replaces each value in with the value of applied to that value. The return value is The function operates on a single entry in the hash table. If exists in the hash table, then its corresponding value is passed into and the return value of is then installed in place of the key's value. The value is then returned. If does not exist in the hash table, and no argument is given, then does nothing and returns If does not exist in the hash table, and an argument is given, then is applied to and then is inserted into with the value returned by as the datum. This value is also returned. The function produces a hash table from Entries of the hash table are not elements of but lists of elements of The function is applied to each element of to compute a key. That key is used to determine which list the item is added to in the hash table. The trailing arguments if any, consist of the same keywords that are understood by the function, and determine the properties of the hash. The fun extends the semantics of with a filtering step. It groups the elements of in exactly the same manner, using These lists of elements are then passed to whose return values become the values associated with the hash table keys. The effect of may be obtained by a combination of and according to the following equivalence:
  (group-map bf ff seq) <--> (let ((h (group-by bf seq)))
                        (hash-update h ff)) Group the integers from 0 to 10 into three buckets keyed on 0, 1 and 2 according to the modulo 3 congruence:
  (group-by (op mod @1 3) 0..11)
  -> #H(() (0 (0 3 6 9)) (1 (1 4 7 10)) (2 (2 5 8))) Same as above, but associate the keys with the sums of the buckets:
  [group-map (op mod @1 3) sum 0..11]
  -> #H(() (0 18) (1 22) (2 15)) The updates hash table by grouping and reducing sequence The function regards the hash table as being populated with keys denoting accumulator values. Missing accumulators which need to be created in the hash table are initialized with which defaults to The function iterates over and treats each element according to the following steps:

    1.  Each element is mapped to a hash key through

    2.  The value associated with the hash key (the accumulator for that key) is retrieved. If it doesn't exist, is used.

    3.  The function is invoked with two arguments: the accumulator from step 2, and the original element from

    4.  The resulting value from step 3 is stored back into the hash table under the key from step 2.

    After the above processing, one more step is performed if the argument is present. In this case, the hash table is destructively mapped through before being returned. That is to say, every value in the hash table is projected through and stored back in the table under the same key, as if by an invocation the expression.

    If is invoked on an empty hash table, its net result closely resembles a operation followed by separately performing a on each value in the hash. Frequency histogram:
      [group-reduce (hash) identity (do inc @1)
        "fourscoreandsevenyearsago" 0]
      --> #H(() (#\a 3) (#\c 1) (#\d 1) (#\e 4) (#\f 1)
            (#\g 1) (#\n 2) (#\o 3) (#\r 3) (#\s 3)
            (#\u 1) (#\v 1) (#\y 1)) Separate the integers 1–10 into even and odd, and sum these groups:
      [group-reduce (hash) evenp + 1..11 0]
      -> #H(() (t 30) (nil 25)) The function produces a histogram in the form of an association list, which is sorted in descending order of frequency. The keys in the association list are elements of and the values are the frequency values: positive integers indicating how many times the keys occur in Note: for a description of association lists, see the function, and the section Association Lists in which its description is contained. The function works by internally constructing a hash table, which is not returned. Elements of serve as keys in that hash. The trailing arguments if any, consist of the same keywords that are understood by the function, and determine the properties of that hash. The function differs from in that it requires an additional argument with the following semantics: every element of is passed to such that the resulting value is used as the hash key in the resulting histogram. Thus, an invocation of is equivalent to an invocation of where the argument is specified as the function. (hist-sort nil) -> nil (hist-sort '(3 4 5)) -> ((3 . 1) (4 . 1) (5 . 1)) (hist-sort '("a" "b" "c" "a" "b" "a" "b" "a")) -> (("a" . 4) ("b" . 3) ("c" . 1)) The and functions create a new hash object based on the existing object. produces an empty hash table which inherits all of the attributes of It uses the same kind of key equality, the same configuration of weak keys and values, and has the same user data (see the function). The also produces a hash table similar to in the same way as However, rather than producing producing an empty hash table, it returns a duplicate table which has all the same elements as it contains the same key and value objects. The function searches hash table for If is found, then it return the hash table's cons cell which represents the association between and Otherwise, it returns If argument is specified, then the function will create an entry for in whose value is that of The cons cell representing that association is returned. Note: for as long as the continues to exist inside modifying the field of the returned cons has ramifications for the logical integrity of the hash; doing so results in unspecified behavior for subsequent insertion and lookup operations. Modifying the field has the effect of updating the association with a new value. The function searches hash table for key If the key is found then the associated value is returned. Otherwise, if the argument was specified, it is returned. If the argument was not specified, is returned. A valid form serves as a place. It denotes either an existing value in a hash table or a value that would be created by the evaluation of the form. The argument is meaningful when is used as a place, and, if present, is always evaluated whenever the place is evaluated. In place update operations, it provides the initial value, which defaults to if the argument is not specified. For example will increment the value stored under key in hash table by one. If the key does not exist in the hash table, then the value is inserted into the table under that key. The expression is always evaluated, whether or not its value is needed. If a place is subject to a deletion, but doesn't exist, it is not an error. The operation does nothing, and is considered the prior value of the place yielded by the deletion. The function places a value into table under the given If a similar key already exists in the hash table, then that key's value is replaced by Otherwise, the and pair is newly inserted into The function returns the argument. The function is useful when the values stored in a hash table are lists. If the given does not already exist in then a list of length one is made which contains and stored in table under If the already exists in the hash table, then the corresponding value must be a list. The value is added to the front of that list, and the extended list then becomes the new value under The return value is Boolean. If true, indicates that the hash-table entry was newly created. If false, it indicates that the push took place on an existing entry. The function searches for a key similar to the If that key is found, then that key and its corresponding value are removed from the hash table. If the key is found and removal takes place, then the associated value is returned. Otherwise is returned. The function removes all key-value pairs from causing it to be empty. If is already empty prior to the operation, then is returned. Otherwise an integer is returned indicating the number of entries that were purged from The function returns an integer representing the number of key-value pairs stored in The function retrieves the user data object associated with A hash table can be created with user data using the keyword in a hash-table literal or in a call to the function, directly, or via other hash-constructing functions which take the hash construction keywords, such as If a hash table is created without user data, its user data is initialized to Because is an accessor, a form can be used as a place. Assigning a value to this place causes the user data of to be replaced with that value. The function is a deprecated synonym for The replaces, with the the user data object associated with The function returns if the is a hash table, otherwise it returns The function successively invokes for each entry stored in Each entry's key and value are passed as arguments to The function returns The function performs a reverse lookup on It searches through the entries stored in for an entry whose value matches If such an entry is found, that entry's key is returned. Otherwise is returned. If multiple matching entries exist, it is not specified which entry's key is returned. The function has exactly the same argument conventions, and likewise searches the However, it returns a list of all keys whose values match The function is applied to each value in and the resulting value is compared with The default is the function. The comparison is performed using The default is the function. The function calculates and returns an inversion of hash table The values in become keys in the returned hash table. Conversely, the values in the returned hash table are derived from the keys. The optional and arguments must be functions, if they are given. These functions determine the behavior of with regard to duplicate values in which turn into duplicate keys. The function must be callable with two arguments, and must accept one argument. If is omitted, it defaults to the function; defaults to The function constructs a hash table as if by a call to the function, passing the arguments which determine the properties of the newly created hash. The new hash table is then populated by iterating over the key-value pairs of and inserting them as follows: The key from is turned into a value by invoking the function on it, and taking the return value. The value from is used as a key to perform a lookup in the new hash table. If no entry exists, then a new entry is created, whose value is Otherwise if the entry already exists, then the value of that entry is combined with by calling the on the arguments and The entry is updated with the resulting value. The new hash table is then returned.
      ;; Invert simple 1 to 1 table:
      (hash-invert #H(() (a 1) (b 2) (c 3)))
      --> #H(() (1 a) (2 b) (3 c))
      ;; Invert table such that the keys of duplicate values
      ;; are accumulated into lists:
      [hash-invert #H(() (1 a) (2 a) (3 c) (5 c) (7 d)) append list]
      --> #H(() (d (7)) (c (3 5)) (a (1 2)))
      ;; Invert table such that keys of duplicate values are summed:
      [hash-invert #H(() (1 a) (2 a) (3 c) (5 c) (7 d)) +]
      --> #H(() (d 7) (c 8) (a 3)) These functions each compute an integer hash value from the internal representation of which satisfies the following properties. If two objects and are the same under the function, then and produce the same integer hash value. Similarly, if two objects and are the same under the function, then and each produce the same integer hash value. In all other circumstances, the hash values of two distinct objects are unrelated, and may or may not be the same. Object of struct type may support custom hashing by way of defining an equality substitution via an method. See the Equality Substitution section under Structures. The optional value perturbs the hashing function used by for strings and buffer objects. This seed value must be a nonnegative integer no wider than 64 bits: that is, in the range 0 to 18446744073709551615. If the value isn't specified, it defaults to zero. On systems with 32-bit addresses, only the low 32 bits of this value may be significant. Effectively, each possible value of the significant part of the seed specifies a different hashing function. If two objects and are the same under the function, then and each produce the same integer hash value for any valid seed value The value returned is a value, and may be negative. It may be any value in the range to These functions retrieve the bulk key-value data of hash table in various ways. retrieves a list of the keys. retrieves a list of the values. retrieves a list of pairs, which are two-element lists consisting of the key, followed by the value. Finally, retrieves the key-value pairs as a Lisp association list: a list of cons cells whose fields are keys, and whose fields are the values. Note that returns the actual entries from the hash table, which are conses. Modifying the fields of these conses constitutes modifying the hash values in the original hash table. Modifying the fields interferes with the integrity of the hash table, resulting in unspecified behavior for subsequent hash insertion and lookup operations. These functions all retrieve the keys and values in the same order. For example, if the keys are retrieved with and the values with then the corresponding entries from each list pairwise correspond to the pairs in The list returned by each of these functions is lazy, and hence constitutes an open traversal of the hash table. The operator iterates over a hash table. The expression must evaluate to an object of hash-table type. The and arguments must be symbols suitable for use as variable names. Bindings are established for these variables over the scope of the and the optional For each element in the hash table, the and variables are set to the key and value of that entry, respectively, and each if there are any, is evaluated. When all of the entries of the table are thus processed, the is evaluated, and its return value becomes the return value of the dohash form. If there is no the return value is The and are in the scope of an implicit anonymous block, which means that it is possible to terminate the execution of dohash early using or These functions perform basic set operations on hash tables in a nondestructive way, returning a new hash table without altering the inputs. The arguments and must be compatible hash tables. This means that their keys must use the same kind of equality. The resulting hash table inherits attributes from as if created by the function. If has userdata, the resulting hash table has the same userdata. If has weak keys, the resulting table has weak keys, and so forth. The function performs a set union. The resulting hash contains all of the keys from and all of the keys from and their corresponding values. If a key occurs both in and then it occurs only once in the resulting hash. In this case, if the argument is not given, the value associated with this key is the one from If is specified then it is called with two arguments: the respective data items from and The return value of this function is used as the value in the union hash. If is specified it must be a function that can be called with one argument. All values from are projected through this function: the function is applied to each value, and the function's return value is used in place of the original value. Similarly, if is present, specifies a function through which values from are projected. These two functions are independent of they are applied to values without regard for whether their keys exist in both hashes or just one. The function performs a union operation similar to, but usefully different from The argument is mandatory in and is applied to all items, regardless of whether they are present in just one hash or both hashes. The arguments and specify default values used in invocations of for keys that are present only in one hash. These values default to For every key that is present only in is invoked with that key's value as its left argument, and the value as the right argument. Conversely, for every key that is present only in is invoked with the value as the left argument, and that key's value as its right argument. For every key that is present in both hashes, is invoked with the values, respectively, from and The returned hash contains all the keys from both hashes, associated with the values returned by The function performs a set difference. First, a copy of is made as if by the function. Then from this copy, all keys which occur in are deleted. The function performs a symmetric difference. A new hash is returned which contains all of the keys from that are not in and vice versa: all of the keys from that are not in The keys carry their corresponding values from and respectively. The function performs a set intersection. The resulting hash contains only those keys which occur both in and If is not specified, the values selected for these common keys are those from If is specified, then for each key which occurs in both and it is called with two arguments: the respective data items. The return value is then used as the data item in the intersection hash. The function returns if the keys in are a subset of the keys in The function returns if the keys in are a proper subset of the keys in This means that has all the keys which are in and at least one which isn't. Note: the return value may not be mathematically meaningful if and use different equality. In any case, the actual behavior may be understood as follows. The implementation of tests whether each of the keys in occurs in using their respective equalities. The implementation of applies first, as above. If that is true, and the two hashes have the same number of elements, the result is falsified. The function returns a an iterator object capable of retrieving the entries in stored in one by one. The function changes the state of an existing iterator, such that it becomes prepared to retrieve the entries stored in the newly given which may be the same one as the previously associated hash. In addition, may be given a argument of which dissociates it from its hash table. The function's argument is a hash iterator returned by If unvisited entries remain in then returns the next one as a cons cell whose holds the key and whose holds the value. That entry is then considered visited by the iterator. If no more entries remain to be visited, returns The function also returns if the iterator has been dissociated from a hash table by The function returns the same value that a subsequent call to will return for the same without changing the state of That is to say, if a cell representing a hash entry is returned, that entry remains unvisited by the iterator. The macro evaluates in an environment in which a lexically scoped function is visible. The function is named by which must be a symbol suitable for naming functions with The argument must be a form which evaluates to a hash-table object. Invocations of the function retrieve successive entries of the hash table as cons-cell pairs of keys and values. The function returns to indicate no more entries remain. If either of the or arguments are present, they must be symbols suitable as variable names. They are bound as variables visible to initialized to the value If is specified, then whenever the function macro is invoked and retrieves a hash-table entry, the variable is set to the key. If the function returns then the value of is set to Similarly, if is specified, then the function stores the retrieved hash value in that variable, or else sets the variable to if there is no next value. The special variable is initialized with a value of zero. Whenever a new hash table is explicitly or implicitly created, it takes its seed from the value of the variable in the current dynamic environment. The only situation in which is not used when creating a new hash table is when is called with an argument given for the optional argument. Only hash tables make use of their seed, and only for keys which are strings and buffers. The purpose of the seed is to scramble the hashing function, to make a hash table resistant to a type of denial-of-service attack, whereby a malicious input causes a hash table to be populated with a large number of keys which all map to the same hash-table chain, causing the performance to severely degrade. The value of must be a nonnegative integer, no wider than 64 bits. On systems with 32-bit addresses, only the least significant 32 bits of this value may be significant. The function returns an integer value suitable for the variable, or as the argument of the and functions. The value is derived from the host environment, from information such as the process ID and time of day. TXR Lisp provides binary search trees, which are objects of type Trees have a printed notation denoted by the prefix. A tree may be constructed by invoking the function. Binary search trees differ from hashes in that they maintain items in order. They also differ from hashes in that they store only elements, not key-value pairs. Every tree is associated with three key abstraction functions: It has a key function which is applied to the elements to map each one to a key. It also has a less function and equal function for comparing keys. If these three functions are not specified, they respectively default to and which means that the tree uses its elements as keys directly, and that they are compared using and Note: these default functions work for simple elements such as character strings or numbers, and also structures implementing equality substitution. The elements are stored inside a tree using tree nodes, which are objects of type whose printed notation is introduced by the prefix. Several tree-related functions take objects as arguments or return objects. Trees may store duplicate elements. The literal syntax may freely specify duplicate elements. The constructor function specifies an initial sequence of elements to be populated into the newly constructed tree. If this initial sequence contains duplicate elements, they are preserved if the optional argument is true, otherwise only the rightmost member of any duplicate group appears in the tree. The insertion functions and also overwrite duplicates by default, but optionally allow them. Duplicates are ordered by insertion: most recently inserted duplicate is rightmost. However, tree lookup chooses an unspecified duplicate. The function allocates, initializes and returns a single tree node. A tree node has three fields and which are accessed using the functions and The function returns if is a tree node. Otherwise, it returns The and functions retrieve the corresponding fields of the object, which must be of type Forms based on the and symbol are defined as syntactic places. Assigning a value to using the operator, as in is equivalent to except that the value of the expression is rather than Similar statements hold true for and in relation to and The and functions replace the corresponding fields of with new values. The argument must be of type These functions all return The function creates a new object, whose and fields are copied from The function constructs and returns a new tree object. All arguments are optional. The argument specifies a sequence of the elements to be stored in the tree. If the argument is absent or the sequence is empty, then an empty tree is created. The argument specifies the function which is applied to every element to produce a key. If omitted, the tree object shall behave as if the function were used, taking the elements themselves to be keys. The argument specifies the function by which two keys are compared for inequality. If omitted, the function is used. A function used as should take two arguments, produce a Boolean result, and have ordering properties similar to the function. The argument specifies the function by which two keys are compared for equality. The default value is the function. A function used as should take two arguments, produce a Boolean result, and have the properties of an equivalence relation. These three functions are collectively referred to as the tree's key abstraction functions. The argument, which defaults to is relevant if an sequence is specified containing some elements which which appear to be duplicates, according to the tree object's function. If is true then duplicates are preserved: the tree will have as many nodes as there are elements in the sequence. Moreover, the duplicates appear in the same relative order in the tree as they appear in the original sequence. If is false, then duplicates are suppressed: if any element appears more than once in then only the last occurrence of that element appears in the tree. Note: the and functions also has an optional argument indicating whether a duplicate insertion replaces an existing element. Note: although the order of duplicate elements is preserved, when the function is used look up a key which is duplicated, the element which is retrieved is unspecified, and can change when the tree is reorganized due to insertions and deletions. The function returns if is a tree. Otherwise, it returns The function returns an integer indicating the number of nodes currently inserted into which must be a search tree object. The function inserts an existing object into a search tree. The object must be of type and must be of type The field of the object holds the element that is being inserted. The actual search key which is associated with this element is determined by applying to the value. The object must not currently be inserted into any existing tree. The values stored in the and fields of are overwritten as required by the semantics of the insertion operation. Their original values are ignored. The argument, defaulting to is concerned with what happens if the tree already contains one or more nodes having a key equal to the key. If is false, then replaces an unspecified one of those existing nodes: that replaced node is deleted from the tree. Key equivalence is determined using tree's equality function (see the argument of the function). If is true, then the new node is inserted without replacing any node, and appears together with the existing duplicate or duplicates. Among the duplicates, the newly inserted node is the rightmost node in the tree order. The function returns the argument. The function inserts into The argument must be an object of type The value may be of any type which is semantically compatible with key abstraction functions. The function allocates a new as if by invoking function, and inserts that as if by using the function. If one or more elements equal to already exist in the tree, then the behavior is determined by the argument, which defaults to The semantics of is as given in the description of The function returns the newly inserted object. The searches for an element which matches The argument must be an object of type The argument may be a value of any type. An element inside matches if the tree's applied to that element produces a key value which is equal to under the tree's function. If such an element is found, then returns the tree node which contains that element as its field. If no such element is found, then returns If multiple nodes exist in the tree which have a matching key, it is unspecified which one of those nodes is retrieved. The function finds an element inside which matches the given If the element is found, it is returned. Otherwise, is returned. Note: the semantics of the function can be understood in terms of A possible implementation is this:
      (defun tree-lookup (tree key)
        (iflet ((node (tree-lookup-node tree key)))
          (key node))) If the tree contains multiple elements which match it is unspecified which element is retrieved. The function searches for an element which matches The argument must be an object of type The argument may be a value of any type which is semantically compatible with key abstraction functions. If the matching element is found, then its node is removed from the tree, and returned. Otherwise, if a matching element is not found, then is returned. If more than one element exists inside which matches it is unspecified which node is deleted and returned. The function tries to removes from the element which matches If successful, it returns that element, otherwise it returns If more than one element exists inside which matches it is unspecified which one is deleted. Note: the semantics of the function can be understood in terms of A possible implementation is this:
      (defun tree-delete (tree key)
        (iflet ((node (tree-delete-node tree key)))
          (key node))) The function searches to find the specific node given by the argument. If is inserted into the tree, then it is deleted, and returned. If is not found in the tree, then the tree is unchanged, and is returned. Note: the search for is informed by key, for efficiency. However, if the tree contains duplicates of that key, then a linear search takes place among the duplicates. The function returns the node in which holds the lowest element. If the tree is empty, it returns The function returns the lowest element, or else if the tree is empty. The function returns the node in which has the lowest key, and removes that node from the tree. If the tree is empty, it returns The function returns the lowest element and removes it from the tree, or else if the tree is empty. The following equivalence holds:
      (tree-del-min tr) <--> (iflet ((node (tree-del-min-node tr)))
                            (key node)) Note: together with provide the basis for using a tree as a priority queue. Elements are inserted into the queue using and then removed in priority order using The function returns the root node of which must be a object. If is empty, then is returned. The function deletes all elements from which must be a object. If is already empty, then the function returns otherwise it returns an integer which gives the count of the number of deleted nodes. The returns a new tree object which is a copy of The argument must be an object of type The returned object has the same key abstraction functions as and contains the same elements. The nodes held inside the new tree are freshly allocated, but their key objects are shared with the original tree. The returns a new, empty search tree object. The argument must be an object of type The returned object has the same key abstraction functions as The function returns a new object of type which provides in-order traversal of nodes stored in The argument must be an object of type If the argument is specified, then nodes with keys lesser than are omitted from the traversal. If the argument is specified, then nodes with keys equal to or greater than are omitted from the traversal. The nodes are traversed by applying the function to the returned object. A object is iterable.
      (collect-each ((el (tree-begin #T(() 1 2 3 4 5)
                            2 5)))
        (* 10 el))
      --> (20 30 40) The functions is closely analogous to The argument must be an existing object, previously returned by a call to Regardless of its current state, the object is re-initialized to traverse the specified with the specified parameters, and is then returned. The function prepares to traverse in the same manner as would new iterator returned by for the specified and arguments. The and function returns the next node in sequence from the tree iterator The iterator must be an object of type returned by the function. If there are no more nodes to be visited, these functions If, during the traversal of a tree, nodes are inserted or deleted, the behavior of and on objects that were obtained prior to the insertion or deletion is not specified. An attempt to complete the iteration may not successfully visit all keys that should be visited. The function changes the state of the iterator. If is invoked repeatedly on the same iterator, it returns successive nodes of the tree. If is invoked more than once on the same iterator without any intervening calls to it returns the same node; it does not appear to change the state of the iterator and therefore does not advance through successive nodes. The function selects elements from which must be a search tree. If is specified, then elements lesser than are omitted from the selection. If is specified, the elements greater than or equal to are omitted from the selection. A list of the selected elements is returned, in which the elements appear in the same order as they do in The function creates and returns a duplicate of the object, which must be a tree iterator returned by The returned object has the same state as the original; it references the same traversal position in the same tree. However, it is independent of the original. Calls to on the original have no effect on the duplicate and vice versa. The function causes the tree iterator to be in the same state as Both and must be tree iterator objects returned by The contents of are updated such that it now references the same tree as at the same position. The argument is returned. The variable holds a list of function names that may be used in the tree literal syntax as the or operations of a tree. The initial value of this variable is a list which holds at least the following three symbols: and The application may change the value of this variable, or dynamically bind it, in order to allow literals to be processed which specify functions other than these three. Like the operator, the macro denotes an anonymous function. Unlike the arguments of the function are implicit, or optionally specified within the expression, rather than as a formal parameter list which precedes a body. The arguments of are implicitly turned into a DWIM expression, which means that argument evaluation follows Lisp-1 rules. (See the operator). The argument forms of are arbitrary expressions, within which special conventions are permitted regarding the use of certain implicit variables:
    A number preceded by a is, syntactically, a metanumber. If it appears inside as an expression, it behaves as a positional argument, whose existence it implies. For instance means that the function shall have at least two arguments, the second argument of which is be substituted in place of the generates a function which has a number of required arguments equal to the highest value of appearing in a construct in the body. For instance generates a three-argument function (which passes its third argument to returning the result, and ignores its first two arguments). There is no way to use to generate functions which have optional arguments. The positional arguments are mutable; they may be assigned. If the meta-symbol appears in the syntax as an expression, it explicitly denotes and evaluates to the list of trailing arguments. Like the metanumber positional arguments, it may be assigned. If the meta-symbol appears in the syntax as an expression, it denotes a mutable variable which is bound to the function itself which is generated by that expression. If this syntax appears inside it specifies a recursive call to the function.

    Functions generated by are always variadic; they always take additional arguments after any required ones, whether or not the syntax is used. If the body does not contain any or syntax, then is implicitly inserted. What this means is that, for example, since the form does not contain any implicit positional arguments like and does not contain it is actually a shorthand for a function which applies to all of its arguments. If the body does contain at least one or then isn't implicitly inserted. The notation denotes a function which takes any number of arguments, and ignores all but the first one, which is passed to The operator is similar to with the following three differences:

    1.  The first argument of namely is an operator. This argument is not processed for the presence of implicit variables. Thus for instance is invalid. By contrast, is possible, and makes sense under the right circumstances. The argument may be the name of a macro or special operator, whereas doesn't support the invocation of macros or special operators. For instance is possible.

    2.  The arguments of are not implicitly treated as DWIM expressions, but as ordinary expressions.

    3.  When syntax doesn't contain any references to implicit variables (metanumbers or then a variadic function is generated which requires one argument. That argument is added to the form. Thus for instance effectively serves as a shorthand for The corresponding defaulting behavior in is that a variadic function is generated which requires no arguments. All of the available arguments are applied. Thus is effectively a shorthand for

    Because it accepts operators, can be used with imperative constructs which are not functions, like For example, produces an anonymous function which, if called with one argument, stores that argument into The actions of and can be understood by the following examples, which convey how the syntax is rewritten to lambda. However, note that the real translator uses generated symbols for the arguments, which are not equal to any symbols in the program.
      (op) -> invalid
      (op +) -> (lambda rest [+ . rest])
      (op + foo) -> (lambda rest [+ foo . rest])
      (op @1 . @rest) -> (lambda (arg1 . rest) [arg1 . @rest])
      (op @1 @rest) -> (lambda (arg1 . rest) [arg1 @rest])
      (op @1 @2) -> (lambda (arg1 arg2 . rest) [arg1 arg2])
      (op foo @1 (@2) (bar @3)) -> (lambda (arg1 arg2 arg3 . rest)
                            [foo arg1 (arg2) (bar arg3)])
      (op foo @rest @1) -> (lambda (arg1 . rest) [foo rest arg1])
      (do + foo) -> (lambda (arg1 . rest) (+ foo arg1))
      (do @1 @2) -> (lambda (arg1 arg2 . rest) (@1 arg2)) ;; invalid!
      (do foo @rest @1) -> (lambda (arg1 . rest) (foo rest arg1)) Note that if argument appears in the syntax, it is not necessary for arguments through to appear. The function will have arguments:
      (op @3) -> (lambda (arg1 arg2 arg3 . rest) [arg3]) The and operators can be nested, in any combination. This raises the question: if an expression like or occurs in an that is nested within an what is the meaning? An expression with a single always belongs with the innermost or operator. So for instance means that an expression is nested within an outer expression that contains no references to its implicit variables. The belongs to the inner There is a way for an inner to refer to the implicit variables of an outer one. This is expressed by adding an extra prefix for every level of escape. For example in the belongs to the outer it is the same as appearing in the outer That is to say, in the expression the and are the same thing: both are parameter 1 of the lambda function generated by the outer By contrast, in the expression there are two different parameters: the first is argument of the outer function, and the second is the first argument of the inner function. If there are three levels of nesting, then three meta-prefixes are needed to insert a parameter from the outermost into the innermost Note that the implicit variables belonging to an can be used in the dot position of a function call, such as:
      [(op list 1 . @1) 2] -> (1 . 2) This is a consequence of the special transformations described in the paragraph Dot Position in Function Calls in the subsection Additional Syntax of the TXR Lisp section. The syntax works in conjunction with quasiliterals which are nested within it. The metanumber notation as well as are recognized without requiring an additional escape, which is effectively optional:
      (apply (op list `@1-@rest`) '(1 2 3)) -> "1-2 3"
      (apply (op list `@@1-@@rest`) '(1 2 3)) -> "1-2 3" Though they produce the same result, the above two examples differ in that embeds a metasymbol into the quasiliteral structure, whereas embeds the Lisp expression into the quasiliteral. Either way, in the scope of undergoes the macro-expansion which renames it to the machine-generated function argument symbol of the implicit function denoted by the macro form. This convenient omission of the character isn't supported for reaching the arguments of an outer from a quasiliteral within a nested
      ;; To reach @@1, @@@1 must be written.
      ;; @@1 Lisp expression introduced by @.
      (op ... (op ... `@@@1`)) Because the macro may be applied to operators, it is possible to apply it to itself, as well as to as in the following example:
      [[[[(do do do op list) 1] 2] 3] 4] -> (1 2 3 4) The chained application associates right-to-left: the rightmost is applied to the second rightmost is applied to the rightmost one and so on. The effect is that partial application has been achieved. The value is passed to the resulting function, which returns another function which takes the next argument. Finally, all these chained argument values are passed to Each level is processed independently. The following examples show how the list may be permuted into several different orders by referring to an implicit argument at various levels of nesting, making it the first argument of The unmentioned arguments implicitly follow, in order. This works because mentioning the argument explicitly means that its corresponding operator no longer inserts its argument implicitly into body of the function which it generates:
      [[[[(do do do op list @1) 1] 2] 3] 4] -> (4 1 2 3)
      [[[[(do do do op list @@1) 1] 2] 3] 4] ->(3 1 2 4)
      [[[[(do do do op list @@@1) 1] 2] 3] 4] -> (2 1 3 4)
      [[[[(do do do op list @@@@1) 1] 2] 3] 4] -> (1 2 3 4)) The following example mentions all arguments at every nesting level, thereby explicitly establishing the order in which they are passed to
      [[[[(do do do op list @1 @@1 @@@1 @@@@1) 1] 2] 3] 4] -> (4 3 2 1)
      (let ((c 0))
        (mapcar (op cons (inc c)) '(a b c)))
      --> ((1 . a) (2 . b) (3 . c))
      (reduce-left (op + (* 10 @1) @2) '(1 2 3)) --> 123 The macro is variant of with special semantics. The arguments support the same notation as those of the operator. If only one is given then is equivalent to If two or more arguments are present, then generates a variadic function which inserts all of its trailing arguments between the first and second That is to say, trailing arguments coming into the anonymous function become the left arguments of the function or function-like object denoted by the first and the remaining give additional arguments. Hence the name which stands for "left-inserting This left insertion of the trailing arguments takes place regardless of whether occurs in any The syntax determines the number of required arguments of the generated function, according to the highest-valued meta-number. The trailing arguments which are inserted into the left position are any arguments in excess of the required arguments. The macro's expansion can be understood via the following equivalences, except that in the real implementation, the symbols and through are replaced with hygienic, unique symbols.
      (lop f) <--> (op f) <--> (lambda (. rest) [f . rest])
      (lop f x y) <--> (lambda (. rest)
                           [apply f (append rest [list x y])])
      (lop f x @3 y) <--> (lambda (arg1 arg2 arg3 . rest)
                            [apply f
                            (append rest
                            [list x arg3 y])])
      (mapcar (lop list 3) '(a b c)) --> ((a 3) (b 3) (c 3))
      (mapcar (lop list @1) '(a b c)) --> ((a) (b) (c))
      (mapcar (lop list @1) '(a b c) '(d e f))
      --> ((d a) (e b) (f c)) The macro provides a shorthand notation for uses of the macro which inserts the first argument of the anonymous function as the leftmost argument of the specified operator. The syntax can be understood in terms of these equivalences:
      (ldo f) <--> (do f @1)
      (ldo f x) <--> (do f @1 x)
      (ldo f x y) <--> (do f @1 x y)
      (ldo f x @2 y) <--> (do f @1 x @2 y) The implicit argument is always inserted as the leftmost argument of the operator specified by the first form.
      ;; push elements of l1 onto l2.
      (let ((l1 '(a b c)) l2)
        (mapdo (ldo push l2) l1)
        l2)
      --> (c b a) The macro is based on the macro and has identical argument conventions. The macro analyzes its arguments and produces a function in exactly the same same way as the macro. However, instead of returning directly, it returns a different function which is a one-argument function that accepts a list. The list specifies arguments to which applies and then returns the resulting value. In other words, the following equivalence holds:
      (ap form ...) <--> (apf (op form ...)) The macro nests properly with and in any combination, in regard to the notation. The macro is similar to the macro, except that it is based on the semantics of the function rather than according to the following equivalence:
      (ip form ...) <--> (ipf (op form ...)) The and macros are related to do macro in the same way that and are related to They produce a one-argument function which works as if by applying the function generated by do to its its own arguments, according to the following equivalence:
      (ado form ...) <--> (apf (do form ...))
      (ido form ...) <--> (ipf (do form ...)) See also: the and functions.
      ;; Take a list of pairs and produce a list in which those pairs
      ;; are reversed.
      (mapcar (ap list @2 @1) '((1 2) (a b))) -> ((2 1) (b a)) The and operators make it possible to chain together functions which are expressed using the syntax. (See the operator for more information). Both macros perform the same transformation except that translates its arguments to a call to the function, whereas translates its arguments in the same way to a call to the function. More precisely, these macros perform the following rewrites:
      (opip arg1 arg2 ... argn) -> [chain {arg1} {arg2} ... {argn}]
      (oand arg1 arg2 ... argn) -> [chand {arg1} {arg2} ... {argn}] where the above notation denotes the following transformation applied to each argument:
      ;; these specific form patterns are left untransformed:
      (dwim ...) -> (dwim ...)
      [...] -> [...]
      (qref ...) -> (qref ...)
      (uref ...) -> (uref ...)
      (op ...) -> (op ...)
      (do ...) -> (do ...)
      (lop ...) -> (lop ...)
      (ldo ...) -> (ldo ...)
      (ap ...) -> (ap ...)
      (ip ...) -> (ip ...)
      (ado ...) -> (ado ...)
      (ido ...) -> (ido ...)
      (ret ...) -> (ret ...)
      (aret ...) -> (aret ...)
      .slot -> .slot
      .(method ...) -> .(method ...)
      atom -> atom
      ;; forms headed by let are treated specially
      (let sym) -> ;; described below
      (let (s0 i0)
           (s1 i1)
           ....) -> ;; described below
      (let ((s0 i0)
            (s1 i1)) -> ;; described below
        body)
      ;; other compound forms are transformed like this:
      (function ...) -> (op function ...)
      (operator ...) -> (do operator ...)
      (macro ...) -> (do macro ...) In other words, compound forms whose leftmost symbol is a macro or operator are translated to the notation. Compound forms denoting function calls are translated to the notation. Compound forms which are invocations, either explicit or via the DWIM brackets notation, are used without transformation. Used without transformation also are forms denoting struct slot access, either explicitly using or or the respective dot notations, forms which invoke any of the family of operators, as well as any atom forms. The and operators are similar to, respectively, and except that they insert the implicit argument as the leftmost argument. For these macros, the above specification of what transformations are applied to the arguments is modified as follows:
      ;; other compound forms are transformed like this:
      (function ...) -> (lop function ...)
      (operator ...) -> (ldo operator ...)
      (macro ...) -> (ldo macro ...) When a or expression occurs in syntax, it denotes a special syntax which is treated as follows.

    1.  The simple form where is a symbol is transformed into the syntax, which is then handled via the following case (2).

    2.  The form specifies an implicit function which binds the specified variables. The variables are bound sequentially as if by even though the operator is Note also that the bindings are not enclosed in a list. An example of the syntax is which specifies a function of two arguments, inside of which will be bound to the first argument, and will be bound to the value of plus the second argument. The remaining elements of the are incorporated into the body of this function. The value of the first argument, is injected into the remaining opip chain, and that chain is processed in a scope in which the variables bound by are visible.

    3.  All other forms not matching the above syntax are treated like all special operators. They become a element of the pipeline. For instance denotes a one-argument function which binds to its first argument, then produces the value which is passed to the next stage of the chain. The remaining chain is not evaluated in the scope of the variable.

    Note: the and macros use their macro environment in determining whether a form is a macro call, thereby respecting lexical scoping. Note: an form with no arguments specifies a function which returns which follows from a documented property of the function. Take each element from the list and multiply it by three, then add 1. If the result is odd, collect that into the resulting list: (mappend (opip (* 3)
                   (+ 1)
                   [iff oddp list])
             (range 1 4)) The above is equivalent to: (mappend (chain (op * 3)
                    (op + 1)
                    [iff oddp list])
             (range 1 4)) The and terms are rewritten to and respectively, whereas is passed through untransformed. The following demonstrates the single variable
      (let ((pipe (opip (+ 1) (let x)
                        (+ 2) (let y)
                        (+ 3)
                        (list x y))))
        [pipe 1])
      -> (2 4 7) The variable intercepts the value coming from and binds to that value. When the function is invoked with the argument that value is That value also continues to the element which yields which is similarly captured by variable The final expression lists the values of and as well as, implicitly, the value coming from the previous element, The and function make available the functional arguments in conjunction with an arbitrary The arguments of and are processed exactly like those of and The syntax
      (opf f c1 c2 c3 ...) is converted into a function call of the form:
      [f {c1} {c2} {c3} ...] where every argument is converted to a form denoting a function, in exactly the same manner as the arguments of The same remarks apply to code in relation to Thus, it is possible to express using by choosing as the argument, according to this equivalence:
      (opip c1 c2 c3 ...) <--> (opf chain c1 c2 c3 ...)
      ;; Remove values greater than 10 or less than five from list
      (remove-if (lopf orf (> 10) (< 5)) (range 0 20)) -> (5 6 7 8 9 10))
      ;; Note: could be expressed as
      (remove-if (orf (lop > 10) (lop < 5)) (range 0 20)) As the example shows, the and macros provide a way to avoid repeating the and syntax in every argument of a functional combinator of a function. The macro passes the value of through the processing stages described by the arguments, yielding the resulting value. The arguments follow the semantics of the macro. The same requirements apply to except that it is related to the macro which inserts the implicit argument into the leftmost position. The following equivalences hold:
      (flow x ...) <--> [(opip ...) x]
      (lflow x ...) <--> [(lopip ...) x] That is to say, is equivalent to the application of an function to the value of and likewise is equivalent to the application of a function. Note: if there are no or arguments, then evaluates the argument and returns which follows from the behavior of the and macros, when those are invoked with no arguments.
      (flow 1 (+ 2) (* 3) (cons 0)) -> (0 . 9)
      (flow "abc" (upcase-str) (regsub #/B/ "ZTE")) -> "AZTEC"
      (flow 1 (- 10)) -> 9
      (lflow 10 (- 1)) -> 9 The macro's argument is treated similarly to the second and subsequent arguments of the operator. The macro produces a function which takes any number of arguments, and returns the value specified by can contain meta syntax like and The following equivalence holds:
      (ret x) <--> (op identity* x)) Thus the expression returns a function similar to and the expression returns a function similar to The macro's argument is treated similarly to the second and subsequent arguments of the operator. The macro produces a function which takes any number of arguments, and returns the value specified by can contain meta syntax like and The following equivalence holds:
      (aret x) <--> (ap identity* x)) Thus the expression returns a function similar to and the expression returns a function similar to The macro is intended for use in conjunction with and other macros in that family. It is a short-hand for writing a pipeline element which performs a side-effecting operation, but unconditionally returns the original input value. The exact expansion of is unspecified, but the following equivalence indicates a possible expansion strategy:
      (tap ...) <--> (prog1 @1 (...)) Assuming that expansion strategy, the expression would expand to Note: in addition to being useful for inserting necessary side effects into pipelines, is also useful for inserting temporary debug print forms. For that purpose, inserting the `prinl` function is often enough:
      (flow 10 (+ 2) print (* 4)) Here, the pipeline will calculate with the side effect of the value of being printed. With the output can be customized, allowing multiple output points to be distinguished.
      (flow 10
        (tap put-line `input: @1`)
        (+ 2)
        (tap put-line `+ 2: @1`)
        (* 4)
        (tap put-line `* 4: @1`))
      -> 48 Output produced:
      input: 10
      + 2: 12
      * 4: 48 The function returns a one-argument function which calls the two-argument function by duplicating its argument.
      ;; square the elements of a list
      (mapcar [dup *] '(1 2 3)) -> (1 4 9) The function returns a two-argument function which calls the two-argument function with reversed arguments. The function accepts zero or more functions as arguments, and returns a single function, called the chained function, which represents the chained application of those functions, in left-to-right order. If is given no arguments, then it returns a variadic function which ignores all of its arguments and returns Otherwise, the first function may accept any number of arguments. The second and subsequent functions, if any, must accept one argument. The chained function can be called with an argument list which is acceptable to the first function. Those arguments are in fact passed to the first function. The return value of that call is then passed to the second function, and the return value of that call is passed to the third function and so on. The final return value is returned to the caller. The function is similar, except that it combines the functionality of into chaining. The difference between and is that immediately terminates and returns whenever any of the functions returns without calling the remaining functions.
      (call [chain + (op * 2)] 3 4) -> 14 In this example, a two-element chain is formed from the function and the function produced by which is a one-argument function that returns the value of its argument multiplied by two. (See the definition of the operator). The chained function is invoked using the function, with the arguments and The chained evaluation begins by passing and to which yields This is then passed to the doubling function, resulting in A way to write the above example without the use of the DWIM brackets and the op operator is this:
      (call (chain (fun +) (lambda (x) (* 2 x))) 3 4) The function accepts a variable number of arguments which are functions. It combines these into a single function which, when invoked, passes its arguments to each of these functions, and collects the results into a list. Note: the function can be understood in terms of the following reference implementation:
      (defun juxt (funcs)
        (lambda (. args)
          (mapcar (lambda (fun)
                    (apply fun args))
                  funcs))) The function generalizes by allowing the combining function to be specified.
       ;; separate list (1 2 3 4 5 6) into lists of evens and odds,
       ;; which end up juxtaposed in the output list:
       [(op [juxt keep-if remove-if] evenp)
        '(1 2 3 4 5 6)] -> ((2 4 6) (1 3 5))
       ;; call several functions on 1, collecting their results:
       [[juxt (op + 1) (op - 1) evenp sin cos] 1]'
       -> (2 0 nil 0.841470984807897 0.54030230586814) The and functions are the functional equivalent of the and operators. These functions accept multiple functions and return a new function which represents the logical combination of those functions. The input functions should have the same arity. Failing that, there should exist some common argument arity with which each of these can be invoked. The resulting combined function is then callable with that many arguments. The function returns a function which combines the input functions with a short-circuiting logical conjunction. The resulting function passes its arguments to the input functions successively, in left-to-right order. As soon as any of the functions returns then is returned and the remaining functions are not called. If none of the functions return then the value returned by the last function is returned. If the list of functions is empty, then is returned. That is, returns a function which accepts any arguments and returns The function returns a function which combines the input functions with a short-circuiting logical disjunction. The resulting function passes its arguments to the input functions successively, in left-to-right order. As soon as any of the functions returns a value, that value is returned and the remaining functions are not called. If all of the functions return then is returned. If the list of functions is empty, then is returned. That is, returns a function which accepts any arguments and returns The function returns a function which is the Boolean negation of The returned function takes a variable number of arguments. When invoked, it passes all of these arguments to and then inverts the result as if by application of The and functions are the logical negation of the and functions. They are related according to the following equivalences:
      [nandf f0 f1 f2 ...] <--> (notf [andf f0 f1 f2 ...])
      [norf f0 f1 f2 ...] <--> (notf [orf f0 f1 f2 ...]) The function is the functional equivalent of the operator. It accepts functional arguments and returns a function. The resulting function passes its arguments to If yields true, then the arguments are passed to and the resulting value is returned. Otherwise the arguments are passed to and the resulting value is returned. If is omitted then is used as default. This omission is not permitted by only If needs to be called, but is omitted, then is returned. The function differs from only in the defaulting behavior with respect to the argument. If is omitted in a call to then the default function is This is useful in situations when one value is to be replaced with another one when the condition is true, otherwise preserved. The following equivalences hold between and
      (iffi a b c) <--> (iff a b c)
      (iffi a b) <--> (iff a b identity)
      [iffi a b nilf] <--> [iff a b]
      [iffi a identity nilf] <--> [iff a] The following equivalences illustrate with both optional arguments omitted:
      [iff a] <---> [iff a identity nilf] <---> a The and functions take zero or more arguments, and ignore them. The function returns and the function returns The function is a synonym of Note: the following equivalences hold between these functions and the operator, and function.
      (fun tf) <--> (ret t) <--> (retf t)
      (fun nilf) <--> (ret nil) <--> (ret) <--> (retf nil) In Lisp-1-style code, and behave like constants which can replace uses of and
      [mapcar (ret nil) list] <--> [mapcar nilf list] Note: the function can be used for suppressing unused variable warnings.
      ;; tf and nilf are useful when functions are chained together.
      ;; test whether (trunc n 2) is odd.
      (defun trunc-n-2-odd (n)
        [[chain (op trunc @1 2) [iff oddp tf nilf]] n]) In this example, two functions are chained together, and is passed through the chain such that it is first divided by two via the function denoted by and then the result is passed into the function denoted by The function passes its argument into and if yields true, it passes the same argument to Here proves its utility by ignoring that value and returning If the argument (the divided value) passed into is even, then iff passes it into the function, which ignores the value and returns The following example shows how may be used to suppress compiler warnings about unused parameters or other variables:
      (defun (x y)
        (ignore x y)) The function returns a function. That function can take zero or more arguments. When called, it ignores its arguments and returns See also: the macro.
      ;; the function returned by (retf 42)
      ;; ignores 1 2 3 and returns 42.
      (call (retf 42) 1 2 3) -> 42 The function returns a one-argument function whose argument conventions are similar to those of the function: it accepts one or more arguments, the last of which should be a list. When that function is called, it applies to these arguments to as if by It then returns whatever returns. If one or more additional are passed to then these are stored in the function which is returned. When that function is invoked, it prepends all of the stored arguments to the passed arguments, and applies the to the resulting combined argument list. Thus the become the leftmost arguments of The function is similar to except that the argument conventions and application semantics of the function returned by are based on rather than See also: the macro.
      ;; Function returned by [apf +] accepts the
      ;; (1 2 3) list and applies + to it, as
      ;; if (+ 1 2 3) were called.
      (call [apf +] '(1 2 3)) -> 6 The function returns a function which applies each to its arguments, juxtaposing the return values of these calls to form arguments to which is then applied. The return value of is returned. The following equivalence holds, except for the order of evaluation of arguments:
      (callf fm f0 f1 f2 ...) <--> (chain (juxt f0 f1 f2 ...)
                            (apf fm))
      ;; Keep those pairs which are two of a kind
      (keep-if [callf eql first second] '((1 1) (2 3) (4 4) (5 6)))
      -> ((1 1) (4 4)) The following equivalence holds between and
      [juxt f0 f1 f2 ...] <--> [callf list f0 f1 f2 ...]:w Thus, may be regarded as a specialization of in which the main function is implicitly The function returns a function which distributes its arguments into the That is to say, each successive argument of the returned function is associated with a successive Each is called, passed the corresponding argument. The return values of these functions are then passed as arguments to and the resulting value is returned. If the returned function is called with fewer arguments than there are then only that many functions are used. Conversely, if the function is called with more arguments than there are then those arguments are ignored. The following equivalence holds:
      (mapf fm f0 f1 ...) <--> (lambda (. rest)
                            [apply fm [mapcar call
                            (list f0 f1 ...)
                            rest]])
      ;; Add the squares of 2 and 3
      [[mapf + [dup *] [dup *]] 2 3] -> 13 TXR Lisp supports input and output streams of various kinds, with generic operations that work across the stream types. In general, I/O errors are usually turned into exceptions. When the description of error reporting is omitted from the description of a function, it can be assumed that it throws an error. These variables hold predefined stream objects. The and streams closely correspond to the underlying operating system streams. Various I/O functions require stream objects as arguments. The stream goes to the same destination as but is a separate object which can be redirected independently, allowing debugging output to be separated from normal output. The stream is a special kind of stream called a null stream. To read operations, the stream appears empty, like a stream open on an empty file. To write operations, it appears as a data sink of infinite capacity which consumes data and discards it. This stream is similar to the device on Unix, and in fact has a relationship to it. If an attempt is made to obtain the underlying file descriptor of using the function, then the device is open, if the host platform supports it. The resulting file descriptor number is returned, and also retained in the device. When is invoked on that descriptor is closed. This feature of allows it to be useful for establishing redirections around the execution of external utilities.
      ;; redirect output of ls *.txt command to /dev/null
      (let ((*stderr* *stdnull*))
        (sh "ls *.txt")) The variable determines the conversion format which is applied when a floating-point value is converted to decimal text by the functions and The default value is The related variable similarly determines the conversion format applied to floating-point values by the functions and The default value is The format string in either variable must specify the consumption of exactly one argument. The conversion string may use embedded width and precision values: for instance, is a valid value for or The special variable specifies the default floating-point printing precision which is used when the or conversion specifier of the function is used for printing a floating-point value, and no precision is specified. Note that since the default value of the variable is the string the variable, by default, also determines the precision which applies when floating-point values are converted to decimal text by the functions and The default value of is that of the variable. Note: to print floating-point values in such a way that their values can be precisely recovered from the printed representation, it is recommended to override to the value of the variable. The special variable specifies the default floating-point printing precision which is used when the or conversion specifier of the function is used for printing a floating-point value, and no precision is specified. Its default value is The variable controls the base (radix) used for printing integer values. It applies when the functions and process an integer value. It also applies when the and conversion specifiers of the function are used for printing an integer value. The default value of the variable is Meaningful values are: and When base 16 is selected, hexadecimal digits are printed as uppercase characters. The variable is a Boolean which controls whether the circle notation is in effect for printing aggregate objects: conses, ranges, vectors, hash tables and structs. The initial value of this variable is circle notation printing is disabled. The circle notation works for structs also, including structs which have user-defined methods. When a method calls functions which print objects, such as or on the same stream, the detection of circularity and substructure sharing continues in these recursive invocations. However, there are limitations in the degree of support for circle notation printing across methods. Namely, a method of a struct must not procure and submit for printing objects which are not part of the ordinary structure that is reachable from the (static or instance) slots of if those objects have already been printed prior to invoking the method, and have been printed without a circle notation label. The "ordinary structure that is reachable from the slots" denotes structure that is directly reachable by traversing conses, ranges, vectors, hashes and struct slots: all printable aggregate objects. The variable controls the behavior of the parser upon encountering structure literal syntax which specifies an unknown structure type. If this variable's value is then such a literal is erroneous; an exception is thrown. Otherwise, such a structure is converted not into a structure object, which is impossible, but into a list object whose first element is the symbol The remaining elements are taken from the syntax. The function performs output to a stream given by by interpreting the actions implicit in a incorporating material pulled from additional arguments given by Though the function is simple to invoke, there is complexity in format string language, which is documented below. The argument can be a stream object, or one of the values or The value serves as a shorthand for The value means that the function will send output into a newly instantiated string output stream, and then return the resulting string. Within most characters represent themselves. Those characters are simply output. The character (tilde) introduces formatting directives, which are denoted by a single character, usually a letter. The special sequence (tilde-tilde) encodes a single tilde. Nothing is permitted between the two tildes. The syntax of a directive is generally as follows: In other words, the (tilde) character, followed by a specifier, a specifier introduced by a comma, and a such that and are independently optional: either or both may be omitted. No whitespace is allowed between these elements. The is a single alphabetic character which determines the general action of the directive. The optional width and precision are specified as follows:
    The width specifier consists of an optional (left angle bracket) character or (caret) character followed by an optional width specification. If the leading character is present, then the printing will be left-adjusted within this field. If the character is present, the printing will be centered within the field. Otherwise it will be right-adjusted by default. The width can be specified as a decimal integer with an optional leading minus sign, or as the character The notation means that instead of digits, the value of the next argument is consumed, and expected to be an integer which specifies the width. If the width, specified either way, is negative, then the field will be left-adjusted. If the value is positive, but either the or prefix character is present in the width specifier, then the field is adjusted according to that character. The padding calculations for alignment and centering take into account character display width, as defined by the function. For instance, a character string containing four Chinese characters (kanji) has a display width of 8, not 4. The width specification does not restrict the printed portion of a datum. Rather, for some kinds of conversions, it is the precision specification that performs such truncation. A datum's display width (or that of its printed portion, after such truncation is applied) can equal or exceed the specified field width. In this situation it overflows the field: the printed portion is rendered in its entirety without any padding applied on either side for alignment or centering. The precision specifier is introduced by a leading comma. If this comma appears immediately after the directive's character, then it means that is being omitted; there is only a precision field. The precision specifier may begin with these optional characters, whose effect
        the "leading zero option": pad with leading zeros; print a sign for positive values; print a single leading zero in place of a positive sign; and

        space
            print a space in place of a positive sign.
    The precision value influences the printing of values of all types. The precision options apply only when the value being printed is a number; otherwise they are ignored. If the or space are multiply specified, the rightmost one takes precedence. The precision specifier itself follows: it must be either a decimal integer or the character indicating that the precision value comes from an integer argument. The leading zero option is only active if accompanied by a precision value, either coming from additional digits in the formatting directive, or from an argument indicated by If no precision specifier is present, then the leading zero option is interpreted as a specifier indicating a precision value of zero, rather than requesting leading zeros. To request zero padding together with zero precision, either two or more zero digits are required, or else the leading zero indicator must be given together with the specifier. For non-numeric values, the precision specifies the maximum number of print positions to occupy, taking into account the display width of each character of the printed representation of the object, as according to the function. The object's printed representation is truncated, if necessary, to the maximum number of characters which will not exceed the specified number of print positions. A numeric argument is formatted into the field in two distinct steps, both of which involve the precision value in a different role. The details of the first of these steps, and the role payed by precision, depend on which conversion directive is used, as well as whether the argument is integer or floating-point. That first step prepares the printed representation of a number which is then fitted into the field by the second step, and also calculates the effective precision value, which is based on the original width and precision. The second step works with the effective precision rather than the original precision. Its description follows. First, the length of the printed representation of the number, not including its sign, is calculated. If this part of the number is shorter than the effective precision, then it is padded on the left with spaces or leading zeros so that the resulting string is equal to the precision. Next, if the number is negative, or else if adding a positive sign has been requested, then the sign is added. It is added to the left of the padding zeros, or else to the right of padding spaces, whichever the case may be. At this stage, if the number is not yet adorned with a sign, and either the or space precision option had been given, then the appropriate character, the digit or a space, is added in the place where the sign would go. This is done only if the result will not overflow the field width, but without regard for whether the character will overflow the effective precision. Finally, the resulting number is rendered into the field, using the requested left, right or center adjustment, as if it were a character string. If it overflows the field, it is reproduced in its entirety without any adjustment being performed.
    Format directives are case sensitive, so that for example and have a different effect, and doesn't exist whereas does. They are: Prints any object in an aesthetic way, as if by the function. The aesthetic notation violates read-print consistency: this notation is not necessarily readable if it is implanted in TXR source code. The field width specifier is honored, including the left-right adjustment semantics. When the specifier is used for numbers, the formatting is performed in two distinct steps: the printed representation of the number is calculated first, and then that representation is set into the field. At the same time, an effective precision is calculated, based on the precision and width, and that effective precision is used in the second step. In the first step, the rendering of a floating-point number to its printed representation, the precision specifies the maximum number of total significant figures, which do not include any digits in the exponent, if one is printed. Numbers are printed in E notation if their magnitude is small, or else if their exponent exceeds their precision. If the precision is not specified, then it is obtained from the special variable, whose default value is the same as that of the variable. The effective precision for the second step is then taken from the original precision, or one less than the width, whichever of the these two values is smaller, but no lower than zero. If the width is unspecified, it is taken as zero. Floating point values which are integers are printed without a trailing (point zero). The flag in the precision is honored for rendering an explicit sign on nonnegative values. If a leading zero is specified in the precision, and a nonzero width is specified, then the printed value's integer part will be padded with leading zeros up to one less than the field width. These zeros are placed before the sign. A precision value of zero imposed on floating-point values is equivalent to a value of one; it is not possible to request zero significant figures. Integers are not affected by the precision value in the conversion to text; all of the digits of the integer are taken into the second step. In the case of integers, The effective precision for the second step is then taken from the original precision, or one less than the width, whichever of the these two values is smaller. However, if the width is not specified, or given as zero, then the unmodified precision value is taken as the effective precision. Thus, in the zero width or missing width case, integers are always padded with spaces or leading zeros due to the precision value, even if such padding overflows the field width. Rationale: the purpose of the elaborate rules for calculating the effective precision is to both obtain consistency in the printing of integers and floating-point values that are integers, as well as to break that consistency when the width is omitted or zero. This break in consistency has two benefits. The common situation of adding leading spaces or zeros to integers can be specified without specifying the width. For instance will format an integer right-justified in an eight-character extent, without width having to be used in order to specify a field to accommodate that padding. The effective padding amount going into the second step is 8, exceeding the zero width, and thus allowing the padding to overflow the field. In the case of floating-point, precision alone can express the common requirement for limiting the number of digits can be expressed by the precision, without causing unwanted padding when there are fewer digits. If the above is used to format a floating-point value, it will be limited to 8 digits of precision, regardless of its magnitude and the position of its decimal point, or whether or not exponential notation is used. The effective precision for field placement shall then be zero in the second step, so that no padding is generated. However, if a nonzero width is used, then formatting becomes consistent between floating-point and integer so that, for instance, the format directive produces the same output for the argument values 42 and 42.0, namely an eight-character-wide field in which the digits appear right-aligned. Prints any object in a standard way, as if by the function. Objects for which read-print consistency is possible are printed in a way such that if their notation is implanted in TXR source, they are readable. The field width specifier is honored, including the left-right adjustment semantics. The precision field is treated similarly to the format directive, except that non-exponentiated floating point numbers that would be mistaken for integers include a trailing for the sake of read-print consistency. Objects truncated by precision may not have read-print consistency. For instance, if a string object is truncated, it loses its trailing closing quote, so that the resulting representation is no longer a properly formed string object. For integer objects, the variable is honored. Effectively, an integer is printed by the directive as if by the or directive, depending on the value of the variable. Requires an argument of integer or character type type. The integer value or character code is printed in decimal. Width and precision semantics are as described for the format directive, for integers. Requires an argument of character, integer or buffer type. The integer value, character code, or buffer contents are printed in hexadecimal, using lowercase letters for the digits through Width and precision semantics are as described for the format directive, for integers. Like the directive, but the hexadecimal digits through are rendered in uppercase. Like the directive, but octal is used instead of hexadecimal. Like the directive, but binary is used instead of hexadecimal. The directive prints numbers in a fixed point decimal notation, with a fixed number of digits after the decimal point. It requires a numeric argument. (Unlike and it does not allow an argument of character type). The formatting performed by is performed in two distinct steps: the printed representation of the number is calculated first, and then that representation is set into the field. The precision parameter coming from the directive is only involved in the first step. In the first step, the precision specifier gives the number of digits past the decimal point. The number is rounded off to the specified precision, if necessary. Furthermore, that many digits are always printed, regardless of the actual precision of the number or its type. If it is omitted, then the value is obtained from the special variable whose default value is three: three digits past the decimal point. A precision of zero means no digits past the decimal point, and in this case the decimal point is suppressed (regardless of whether the numeric argument is floating-point or integer). No limit is placed on the number of significant figures in the number by either the precision or width value. When the resulting textual number passes to the second formatting step, the precision value, for the purposes of that step, is calculated by taking one less than the field width, or else zero if the field width is zero. This value is not related to the precision that had been used to determine the number of places past the decimal point. The directive prints numbers in E notation. It requires a numeric argument. (Unlike and it does not allow an argument of character type). The formatting performed by is performed in two distinct steps: the printed representation of the number is calculated first, and then that representation is set into the field. The precision parameter coming from the directive is only involved in the first step. In the first step, the precision specifier gives the number of digits past the decimal point printed in the E notation, not counting the digits in the exponent. Exactly that many digits are printed, regardless of the precision of the number. If the precision is omitted, then the number of digits after the decimal point is obtained from the value of the special variable whose default value is three. If the precision is zero, then a decimal portion is truncated off entirely, including the decimal point. When the resulting textual number passes to the second formatting step, the precision value, for the purposes of that step, is calculated by taking one less than the field width, or else zero if the field width is zero. This value is not related to the precision that had been used to determine the number of places past the decimal point. The directive prints a numeric representation in hexadecimal of the bit pattern of the object, which is meaningful to someone familiar with the internals of TXR. If the object is a pointer to heaped data, that value has a correspondence to its address. The directive establishes hanging indentation, and turns on the stream's indentation mode. Subsequent lines printed within the execution of the same call will be automatically indented. If no width is specified, then the directive sets the hanging indentation to the current printing column position. If a width is specified, then it represents an offset (positive or negative). If the prefix character is present, the hanging indentation is set to the specified offset relative to the current printing column. If the prefix is present on the width field, then the offset is applied relative to the indentation which was saved on entry into the function. The indentation mode and indentation column are automatically restored to their previous values when function terminates, naturally or via an exception or nonlocal jump. The effect of a precision field (even if zero) combined with the directive is currently not specified, and reserved for future extension. The precision field is processed syntactically, and no error occurs, however.
The function provides a shorthand for formatting to a string, according to the following equivalence which holds between and
  (fmt s arg ...) <--> (format nil s arg ...) The macro ("picture based formatting") provides a notation for constructing a character string under the control of which indicates the insertion of zero or more argument values. Like the function or quasiliteral syntax, the macro returns a character string. The macro's notation is different from quasiliterals or from The argument isn't an evaluated expression, but syntax. It must be either a string literal or else a string quasiliteral. No other syntax is permitted. If is a string, is scanned left to right in search of pic patterns. Any characters not belonging to a pic pattern are copied into the output string verbatim. When a pic pattern is found, it is removed from and applied to the next successive to perform a conversion and formatting of that value to text. The resulting text is appended to the output string, and the process continues in search of the next pic pattern. When the is exhausted, the constructed string is returned. If is a quasiliteral, then all of the text strings embedded within the quasiliteral are examined in the same way, in left to right order. Each such string is transformed into an expression which produces a character string according to the semantics of the pic patterns it contains, and the resulting expressions are substituted into the original quasiliteral to produce a transformed quasiliteral. There must be exactly as many arguments as there are pic patterns in The macro arranges for the left-to-right evaluation of the expressions. If is a quasiliteral, the evaluation of these expressions is interleaved into the quasiliterals expressions and variables, in the order implied by the placement of the corresponding pic patterns relative to the quasiliteral elements. For instance, if is then the function is called first, then the is evaluated which produces a value for the pic pattern, after which the function is called. There are two kinds of pic patterns: alignment patterns, numeric patterns and escape patterns. Escape patterns consist of a two-character sequence introduced by the (tilde) character, which is followed by one of the characters that are special in pic pattern syntax:
  < > | + - 0 # . ! ~ , ( ) An escape pattern produces the second character as its output. For instance encoded a single character, and encodes a literal character that is not part of any pattern. Alignment patterns are described next.
    A sequence of one or more (less than) characters specifies that the corresponding argument is rendered left-aligned in a field whose width is given by the number of characters. If the argument's textual representation doesn't fit into the field, it overflows. A sequence of one or more (greater than) characters specifies that the corresponding argument is rendered right-aligned in a field whose width is given by the number of characters. If the argument's textual representation doesn't fit into the field, it overflows. A sequence of one or more (pipe) characters specifies that the corresponding argument is centered in a field whose width is given by the number of characters. If the argument's textual representation doesn't fit into the field, it overflows. If the argument cannot be precisely centered, because the even-odd parity of its character count is different from the parity of the field width, it is centered slightly to the left: one less space appears on its left side in respect to its right side.

    The numeric patterns, by means of their visual pattern and several optional prefix codes, specify the parameters for the conversion of a numeric argument, which is rendered right-aligned in a fixed-width field. Numeric patterns that do not contain any commas conform this simple rule: or else if they contain commas, the placement of these commas is governed by the more complicated rule: Commas may be placed anywhere within the pattern of hash characters, except at the beginning or end, or adjacent to the decimal point. If the leading zero is present, a comma may appear immediately after it, before the first hash. A second form of both of the above patterns is supported, for specifying that negative numbers be shown in parentheses. Instead of the sign, an opening parenthesis may appear, which must be matched by a closing parenthesis which follows a valid pattern interior: With embedded commas: The pattern consists of an optional which is one of the characters (plus) or (minus), or else it may optionally begin with an opening parenthesis, indicating one of the two alternative forms. This is followed by an optional leading zero. After this comes a sequence of one or more (hash) characters, which may contain exactly one element, which is defined as one of the characters (period) or (exclamation mark). This element may appear at most once, and must not be the first or last character, unless it is the exclamation mark, in which case it may appear last. Except if ending in the exclamation mark, a numeric pattern specifies a field width which is equal to the number of characters occurring in the pattern itself. For instance, the patterns and all specify a field width of four. If the numeric pattern ends in an exclamation mark, that character is not counted toward the field width that it specifies. Thus the pattern specifies a field width of three. If the leading sign is present, it has the following meanings:
    If the corresponding numeric argument is nonnegative, the character shall appear before first digit. Otherwise the minus character will appear. Like except that when the numeric argument is nonnegative, instead of a character, a space appears before the first digit. This space counts toward the field width and therefore contributes to overflow.

    If a leading sign is not present, then no extra character appears before the first digit of a positive value, which means that an extra character of field width is available for representing nonnegative values. If the leading zero is present, it specifies that the number is padded with zeros on the left. In combination with the sign, this shall not cause the leading space before a positive value to be overwritten with a zero; leading zeros, if any, begin after that space. The remainder of the pattern specifies the number of digits of the fractional part which is indicated by number of characters after the The number is rounded to that many fractional digits, which are all rendered, even if there are trailing zeros. If no is not specified, then the number of fractional digits is zero. The same is true if is specified as as the last character. In both cases, the numeric argument is rounded to integer, and rendered without any decimal point or fractional part. There is a difference between being specified using the ordinary decimal point character versus the character. The character specifies that if the conversion of the numeric argument overflows the field, then instead of showing any digits, the field is filled with a pattern consisting of (hash) characters, and possibly an embedded decimal point. In contrast, the character permits the field's width to increase to accommodate overflowing output. If overflow takes place and the character appears other than as the rightmost character of the pattern, then the decimal point character character appears at the position indicated by that character. If the character is the rightmost character of the pattern, then, just as in the case of normal, non-overflowing output, it doesn't contribute to the width of the hash fill, and only hash characters appear. If commas appear in the numeric pattern according to the more complex syntactic rule, they count toward the field width and specify the insertion of digit-separating commas at the indicated locations. Digit separators may be specified on either side of the decimal point, but not adjacent to it. In the output, a digit separating comma shall not appear if it would be immediately preceded by a or sign or space. In this situations, the sign character or space appears in place of the digit separator. A digit separator that appears in a position occupied by a space is also suppressed in favor of the space. Digit separators are included among leading zeros. It is not logically possible for a digit separator to appear as the first character of a pattern's output, because it may not be the first character of a pattern. However, if a numeric pattern is preceded or followed by a comma, those commas are ordinary characters which are copied to the output. When, due to the presence of an overflowing field is handled by the generation of a the hash character fill, the hash characters are treated as digits for the purpose of digit separation. When the pattern uses parentheses to specify that negative numbers are to be shown with parentheses, the parentheses count toward the field width. The field portion between the parentheses is called the inner field. The parentheses appear in the output when the number is negative, and are placed immediately outside of the inner field, so that if leading zeros are not requested, there may be one or more spaces between the opening parenthesis and the first digit. If the number is nonnegative, then each parenthesis is replaced by one space, flanking the inner field in the same manner as parentheses.
      ;; numeric formatting
      (pic "######" 1234.1) -> " 1234"
      (pic "######.#" 1234.1) -> " 1234.1"
      (pic "#######.##" 1234.1) -> " 1234.10"
      (pic "#######.##" -1234.1) -> " -1234.10"
      (pic "0######.##" 1234.1) -> "0001234.10"
      (pic "+######.##" 1234.1) -> " +1234.10"
      (pic "-######.##" 1234.1) -> " 1234.10"
      (pic "+0#####.##" 1234.1) -> "+001234.10"
      (pic "-0#####.##" 1234.1) -> " 001234.10"
      (pic "#######.##" -1234.1) -> " -1234.10"
      ;; digit separation
      (pic "0,###,###.##" 1234.1) -> "0,000,123.10"
      (pic "#,###,###.##" 1234.1) -> " 123.10"
      ;; overflow with !
      (pic "#!#" 1234) -> "###"
      (pic "#!#" 123) -> "###"
      (pic "-##!#" -123) -> "#####"
      (pic "+##!#" 123) -> "#####"
      (pic "###!" 1234) -> "###"
      ;; negative parentheses
      (pic "(#,###.##) 1234.56) -> " 1,234.56 "
      (pic "(#,###.##) -234.56) -> "( 234.56)"
      ;; alignment, multiple arguments
      (pic "<<<<<< 0#.# >>>>>>>" "foo" (+ 2 2) "bar")
      --> "foo 04.0 bar"
      ;; quasiliteral
      (let ((a 2) (b "###") (c 13.5))
        (pic `abc@(+ a a)###.##@b>>>>` c "x"))
      --> "abc4 13.50### x"
      ;; filename generation
      (mapcar (do pic "foo~-0##.jpg") (rlist 0..5 8 12))
      --> ("foo-000.jpg" "foo-001.jpg" "foo-002.jpg" "foo-003.jpg"
           "foo-004.jpg" "foo-005.jpg" "foo-008.jpg" "foo-012.jpg") The and functions render a printed character representation of the argument into If the argument is not supplied, then the destination is the stream currently stored in the variable. If Boolean argument is not supplied or is explicitly specified as then the function renders in a way which strives for read-print consistency: an object is printed in a notation which is recognized as a similar object of the same kind when it appears in TXR source code. Floating-point objects are printed as if using the function, with formatting controlled by the variable. If is true, then does not strive for read-print consistency. Strings are printed by sending their characters to the output stream, as if by the function, rather than being rendered in the string literal notation consisting of double quotes, and escape sequences for control characters. Likewise, character objects are printed via rather than the notation. Buffer objects are printed by sending their bytes to the output stream using rather than being rendered in the notation. Symbols are printed without their package prefix, except that symbols from the keyword package are still printed with the leading colon. Floating-point objects are printed as if using the function, with formatting controlled by the variable. When aggregate objects like conses, ranges and vectors are printed, the notations of these objects themselves are unaffected by the flag; however, that flag is distributed to the elements. The function returns The ("pretty print") function is equivalent to with the argument hardcoded true. The function ("print and new line") behaves like a call to with defaulting to followed by issuing a newline characters to the stream. The function ("pretty print and new line") behaves like followed by issuing a newline to the stream. The and functions are like and but they do not accept a stream argument. Instead they print to a freshly instantiated string stream, and return the resulting string. The following equivalences hold between calls to the function and calls to the above functions:
      (format stream "~s" obj) <--> (print obj stream)
      (format t "~s" obj) <--> (print obj)
      (format t "~s\n" obj) <--> (prinl obj)
      (format nil "~s" obj) <--> (tostring obj) For and the equivalence is produced by using in format rather than For floating-point numbers, the above description of the behavior in terms of the format specifiers and only applies with respect to the default values of the variables and For characters, the print function behaves as follows: most control characters in the Unicode and range are rendered using the notation, using two hex digits. Codes in the range to and the codes and are printed in the with four hexadecimal digits, and character above this range are printed using the same notation, but with six hexadecimal digits. Certain characters in the range are printed using their names such as and which are documented in the Character Literals section. The character is printed as All other characters are printed as where is the actual character. Caution: read-print consistency is affected by trailing material. If additional digits are printed immediately after a number without intervening whitespace, they extend that number. If hex digits are printed after the character which is rendered as they look like a hex character code. The function prints a representation of on If the stream argument is not supplied, then the destination is the stream currently stored in the variable. For all object types except lists and vectors, behaves like If is a list or vector, then recurses: the function is applied to each element. An empty list or vector results in no output at all. This effectively means that an arbitrarily nested structure of lists and vectors is printed flattened, with one element on each line. The function calculates the number of places occupied by the printed representation of or on a monospace display which renders certain characters, such as the East Asian kanji and other characters, using two places. For a argument, this value is the sum of the individual display width of the string's constituent characters. The display width of an empty string is zero. Control characters are assigned a display width of zero, regardless of their display control semantics, if any. Characters marked by Unicode as being wide or full width, have a display width of two. Other characters have a display width of one. The function returns if is any type of stream. Otherwise it returns The function returns if is a stream marked as "real-time". If is not a stream, or not a stream marked as "real-time", then it returns Only certain kinds of streams accept the real-time attribute: file streams and tail streams. This attribute controls the semantics of the application of to the stream. For a real-time stream, returns a stream with "naive" semantics which returns data as soon as it is available, at the cost of generating spurious item when the stream terminates. The application has to recognize and discard that item. The ordinary lazy streams read ahead by one line and suppress this extra item, so their representation is more accurate. When TXR starts up, it automatically marks the stream as real-time, if it is connected to a TTY device (a device for which the POSIX function reports true). This is only supported on platforms that have this function. The behavior is overridden by the command-line option. The function creates a stream connected to the file which is located at the given which is a string. The argument is a string which uses the same conventions as the mode argument of the C language function, with greater permissiveness, and some extensions. The syntax of is described by the following grammar. Note that it permits no whitespace characters: If the argument is omitted, the behavior is the same as an empty mode string. The part of the mode string generates the following possibilities:
    If the is missing, then a default mode is implied. The default is specific to the particular stream-opening function. In the case of the default mode is A consisting of just the character is equivalent to This means that the file is opened for reading. The file is opened for reading and writing. It is not created if it doesn't exist. The file is opened for writing. If it exists, it is truncated to zero length. If it doesn't exist, it is created. The file is opened for reading and writing. If it exists, it is truncated to zero length. If it doesn't exist, it is created. The file is opened for modification. This is the same as except that the file is not truncated if it exists. The file is opened for reading and modification. This is the same as except that the file is not truncated if it exists. The file is opened for writing. If it doesn't exist, it is created. If it exists, the current position is advanced to one byte past the end of the file, so that newly written data are appended. The file is opened for reading and writing. If it doesn't exist, it is created. The read position is at the beginning of the file, but writes are appended to the end regardless of the position. This selector may be used on operating systems which support the mode of the POSIX C library function. The must specify a directory to which the calling process has write permission. An anonymous, unlinked file will be created in the filesystem which holds that directory, open for reading and writing. See additional notes at the end of this section.

    The meanings of the option characters are:
    The file is opened in binary mode: no line-ending translation takes place. In the absence of this option, files are opened in text mode, in which newline characters in the stream are an abstract indication of the end of a line, translate to a system-specific way of terminating lines in text files. The file is created and opened only if it does not already exist. Otherwise, a exception is thrown. This option is allowed only with the and modes. Specifies that the stream will be line buffered. This means that an implicit flush operation takes place whenever the newline character is output. Specifies that the stream will be unbuffered. It is erroneous for both and to be specified. Specifies that the stream will have the real-time property set. For a description of the semantics, see the function. Briefly, this property affects the semantics of lazy lists which draw input from the stream. In addition, for a stream opened for writing or reading and writing, the mode letter specifies that the stream will be line buffered, unless specified as unbuffered with Specifies that the operation shall not block. A decimal digit specifies the stream buffer size as binary exponential buffer size order, such that specifies 1024 bytes, specifies 2048 and so forth up to specifying 524288 bytes. If no such digit is specified, then the stream uses a default buffer size. It is erroneous for the size order digit to be present together with the option This option specifies compression. A is opened for the file rather than an ordinary which performs compression when writing and decompression when reading. The compression uses the Deflate algorithm, and a file format compatible with the utility. If is immediately followed by a digit, then that specifies the compression level. The default level is 6. A value of zero is an invalid level, silently accepted and treated as the default. When is specified, special restrictions apply to the If these are violated, an exception is thrown. A does not support an update mode; it may not be open simultaneously for reading and writing as, for example, requested by the mode. The options and are inapplicable. The digit option specifying the buffer size order also may not be used with The function may only be used in the forward direction on streams open for writing, and has the semantics of writing a region of zero bytes. Arbitrary seeking is supported in read mode, but via a costly emulation which decompresses data from the beginning of the file to the desired seek point. This option refers to a special syntax that only has an effect in mode strings that are passed to the function; the syntax performs I/O redirections in the child process created by that function, and is described in that function's documentation.

    The flag on which the mode selector depends was introduced by the Linux kernel, and is likely only supported on Linux systems. It is not supported on all filesystem types. The mode offers a way to create temporary files in a robust way in any file system which supports the mechanism. There is no concern about choosing a unique file name, since the file doesn't have one. The file is guaranteed to disappear if the process is terminated in any manner. In contrast, traditional temporary files which are initially named a name and then unlinked may remain if the process is abruptly terminated before it is able to call On Linux, it is possible to link a file created with into the filesystem, according to the following pattern:
      ;; atomically create file called "name" with content "hello"
      (let* ((stream (open-file "." "T"))
             (fd (fileno stream)))
        (put-string "hello\n" stream)
        (flush-stream stream)
        (rlink `/proc/self/fd/@fd` "name")) The atomic creation of a file can be simulated by the familiar pattern of writing to a visible temporary file and then renaming. However, the above pattern eliminates the risk that a temporary file will be left behind if the procedure is interrupted for any reason before reaching the call. Any reason includes process termination that cannot be intercepted and handled, and operating system failure or power loss. The function creates a tail stream connected to the file which is located at the given The argument is a string which uses the same conventions as the mode argument of the C language function. If this argument is omitted, then is used. See the function for a discussion of modes. The argument is a Boolean which determines whether the initial read/write position is at the start of the file, or just past the end. It defaults to This argument only makes a difference if the file exists at the time is called. If the file does not exist, and is later created, then the tail stream will follow that file from the beginning. In other words, controls whether the tail stream reads all the existing data in the file, if any, or whether it reads only newly added data from approximately the time the stream is created. A tail stream has special semantics with regard to reading at the end of file. A tail stream never reports an end-of-file condition; instead it polls the file until more data is added. Furthermore, if the file is truncated, or replaced with a smaller file, the tail stream follows this change: it automatically opens the smaller file and starts reading from the beginning (the flag only applies to the initial open). In this manner, a tail stream can dynamically grow rotating log files. Caveat: since a tail stream can reopen a new file which has the same name as the original file, it behave incorrectly if the program changes the current working directory, and the pathname is relative. The function tries to create a stream which reads the directory given by the string argument If a filesystem object exists under the path, is accessible, and is a directory, then the function returns a stream. Otherwise, a file error exception is thrown. The resulting stream supports the operation. Each call to the operation retrieves a string representing the next directory entry. The value is returned when there are no more directory entries. The and entries in Unix filesystems are not skipped. The function creates a new temporary binary file which is different from any existing file. It opens a stream for that file and returns the stream. The stream is created with the mode When the stream is closed, or the TXR image terminates, the file is deleted. Note: the function is implemented using the same-named ISO C and POSIX library function. On POSIX systems of sufficient quality, deletes the file before returning the open stream, such that the file object continues to exist while the stream is open, but is not known by any name in the file system. POSIX (IEEE Std 1003.1-2017) notes that in some implementations, "a permanent file may be left behind if the process calling tmpfile() is killed while it is processing a call to tmpfile". Notes: if a unique file is required which exists in the file system under a known name until explicitly deleted, the function may be used. If a unique directory needs to be created, the function may be used. These two functions are described in the Unix Filesystem Complex Operations section of the manual. The function produces an input stream object. Character read operations on the stream object read successive characters from Output operations and byte operations are not supported. The function produces an input stream object. Byte read operations on this stream object read successive byte values obtained by encoding into UTF-8. Character read operations are not supported, and neither are output operations. The function produces an input stream object based on a list of strings. Through the character read operations invoked on this stream, the list of strings appears as a list of newline-terminated lines. Output operations and byte operations are not supported. The function, which takes no arguments, creates a string output stream. Data sent to this stream is accumulated into a string object. String output streams support both character and byte output operations. Bytes are assumed to represent a UTF-8 encoding, and are decoded in order to form characters which are stored into the string. If an incomplete UTF-8 code is output, and a character output operation then takes place, that code is assumed to be terminated and is decoded as invalid bytes. The UTF-8 decoding machine is reset and ready for the start of a new code. The function is used to retrieve the accumulated string. If the null character is written to a string output stream, the behavior is unspecified. TXR strings cannot contain null bytes. The pseudo-null character also notated will produce a null byte when converted to UTF-8 and thus serves as an effective internal representation of the null character in external data. The argument must be a string output stream. This function finalizes the data sent to the stream and retrieves the accumulated character string. If a partial UTF-8 code has been written to and then this function is called, the byte stream is considered complete and the partial code is decoded as invalid bytes. After this function is called, further output on the stream is not possible. The function is similar to However, the stream object produced by this function does not produce a string, but a list of strings. The data is broken into multiple strings by newline characters written to the stream. Newline characters do not appear in the string list. Also, byte output operations are not supported. The function returns the string list which has accumulated inside a string output stream given by The string output stream is finalized, so that further output is no longer possible. The macro binds the symbol as a variable, initializing it with a newly created string input stream. The string input stream is constructed from as if by the expression. Then it evaluates the in the scope of the variable. The value of the last is returned, or else if no forms are present. The argument must be a bindable symbol, as defined by the function. The argument must be a form which evaluates to a character string value. The macro binds the symbol as a variable, initializing it with a newly created string byte input stream. The string input stream is constructed from as if by the expression. Then it evaluates the in the scope of the variable. The value of the last is returned, or else if no forms are present. The argument must be a form which evaluates to a character string value. The macro binds the symbol specified by the argument as a variable, initializing it with a newly created string output stream. The output stream is created as if by the function. Then it evaluates in the scope of that variable. After these forms are evaluated, the string is extracted from the string output stream, as if by the function, and returned as the result value of the form. The macro binds the symbol specified by the argument as a variable, initializing it with a newly created string list output stream. The output stream is created as if by the function. Then it evaluates in the scope of that variable. After these forms are evaluated, the string list is extracted from the string output stream, as if by the function, and returned as the result value of the form. The creates a stream which supports the operation for traversing a byte-wise representation of The function serves as a generic interface for calling one of several other stream constructing functions based on the type of the argument. The argument must be either a buffer, in which case behaves like or else a string, in which case the function behaves like Note: the repertoire of types handled by may expand in future language versions. The function performs a close operation on whose meaning is depends on the type of the stream. For some types of streams, such as string streams, it does nothing. For streams which are connected to operating system files or devices, will perform a close of the underlying file descriptor, and dissociate that descriptor from the stream. Any buffered data is flushed first. returns a Boolean true value if the close has occurred without errors, otherwise For most streams, "without errors" means that any buffered output data is flushed successfully. For command and process pipes (see and success also means that the process terminates normally, with a successful error code, or an unsuccessful one. An abnormal termination is considered an error, as is the inability to retrieve the termination status, as well as the situation that the process continues running in spite of the close attempt. Detecting these situations is platform specific. If the argument is specified, and isn't then the function throws an exception if an error occurs during the close operation instead of returning If is called in such a way that it returns a value, without throwing an exception, and that value isn't that value is retained. Additional calls to the function with the same object return that same value without having any effect on the stream. These additional calls ignore the argument. The may be associated with a process, in one of several ways: implicitly, by the functions and and related functions, or explicitly by the function, if a argument is specified. In this situation, waits for the termination of that process, after closing the underlying file descriptor. If the process terminates normally, then returns its termination status, which is zero if the termination is successful. If the status of the process cannot be obtained, or is an abnormal termination, then the return value is In that situation, if is true, an exception is thrown instead. The macro binds the variable whose name is given by the argument, and macro arranges for the evaluation of in the scope of that variable. The variable is initialized with the value produced by the evaluation of which must be an expression which evaluates to a stream. After each is evaluated, the stream is closed, as if by the expression. The value of the last then becomes the result value of the form, or else if these forms are absent. If the evaluation of the is abandoned, the stream is still closed. That is to say, the closure of the stream is a protected action, as if by the operator. When a stream operation fails, the and functions may be used to inquire about a more detailed cause of the error. Not all streams support these functions to the same extent. For instance, string input streams have no persistent state. The only error which occurs is the condition when the string has no more data. The inquires about its error condition. The function returns to indicate there is no error condition, to indicate an end-of-data condition, or else a value which is specific to the stream type indicating the specific error type. Note: for some streams, it is possible for the value to be returned even though no operation has failed; that is to say, the streams "know" they are at the end of the data even though no read operation has failed. Code which depends on this will not work with streams which do not thus indicate the end-of-data a priori, but by means of a read operation which fails. The function returns a text representation of the error code. The error code is represented as the string the error code as and other codes have a stream-specific representation. The function removes the error situation from a stream. On some streams, it does nothing. If an error has occurred on a stream, this function should be called prior to retrying any I/O or positioning operations. The return value is the previous error code, or if there was no error, or the operation is not supported on the stream. These fundamental stream functions perform input. The argument is optional. If it is specified, it should be an input stream which supports the given operation. If it is not specified, then the stream is used. The function pulls a character from a stream which supports character input. Streams which support character input also support the function which extracts a line of text delimited by the end of the stream or a newline character and returns it as a string. (The newline character does not appear in the string which is returned). Character input from streams based on bytes requires UTF-8 decoding, so that may actually read several bytes from the underlying low-level operating system stream. The function bypasses UTF-8 decoding and reads raw bytes from any stream which supports byte input. Bytes are represented as integer values in the range 0 to 255. Note that if a stream supports both byte input and character input, then mixing the two operations will interfere with the UTF-8 decoding. These functions return when the end of data is reached. Errors are represented as exceptions. See also: The function reads characters from a stream, and assembles them into a string, which is returned. If the argument is omitted, then the stream is used. The stream is closed after extracting the data, unless is specified as The default value of this argument is If the argument is missing, then all of the characters from the stream are read and assembled into a string. If present, the argument should be a positive integer indicating a limit on how many characters to read. The returned string will be no longer than but may be shorter. These functions put back, into a stream, a character or byte which was previously read. The character or byte must match the one which was most recently read. If the argument is omitted, then the stream is used. If the operation succeeds, the byte or character value is returned. A return indicates that the operation is unsupported. Some streams do not support these operations; some support only one of them. In general, if a stream supports it supports and likewise for and Streams may require a pushed back byte or character to match the character which was previously read from that stream position, and may not allow a byte or character to be pushed back beyond the beginning of the stream. Space may be available for only one byte of pushback under the operation. The number of characters that may be pushed back by is not limited. Pushing both a byte and a character, in either order, is also unsupported. Pushing a byte and then reading a character, or pushing a character and reading a byte, are unsupported mixtures of operations. If the stream is binary, then pushing back a byte decrements its position, except if the position is already zero. At that point, the position becomes indeterminate. The behavior of pushing back immediately after a positioning operation is unspecified. These functions perform output on an output stream. The argument must be an output stream which supports the given operation. If it is omitted, then is used. The function writes a character given by to a stream. If the stream is based on bytes, then the character is encoded into UTF-8 and multiple bytes are written. Streams which support also support and The function writes the characters of a string out to the stream as if by multiple calls to The argument may be a symbol, in which case its name is used as the string. The function is like but also writes an additional newline character. The string is optional in and defaults to the empty string. The function writes a raw byte given by the argument to if supports a byte write operation. The byte value is specified as an integer value in the range 0 to 255. All these functions return On failure, they do not return, but throw exceptions of type These functions assume to be a sequence of strings, or of symbols, or a mixture thereof. These strings are sent to the stream. The argument must be an output stream. If it is omitted, then is used. The function iterates over and writes each element to the stream as if using the function. The function iterates over and writes each element to the stream as if using the function. Both functions return The function is meaningful for output streams which accumulate data which is passed on to the operating system in larger transfer units. Calling causes all accumulated data inside to be passed to the operating system. If called on streams for which this function is not meaningful, it does nothing, and returns If is omitted, the current value of is used. The function is meaningful for file streams. It changes the current read/write position within It can also be used to determine the current position: see the notes about the return value below. The argument is a positive or negative integer which gives a displacement that is measured from the point identified by the argument. Note that for text files, there isn't necessarily a 1:1 correspondence between characters and positions due to line-ending conversions and conversions to and from UTF-8. The argument is one of three keywords: and These denote the start of the file, the current position in the file and the end of the file. If is zero, and is then returns the current absolute position within the stream, if it can successfully obtain it. Otherwise, it returns if it is successful. If a character has been successfully put back into a text stream with and is still pending, then the position value is unspecified. If a byte has been put back into a binary stream with and the previous position wasn't zero, then the position is decremented by one. On failure, it throws an exception of type The causes the length of the underlying file associated with to be set to bytes. The stream must be a file stream, and must be open for writing. If is omitted, then it defaults to the current position, retrieved as if by invoking the with an argument of zero and argument of Hence, after the operation, that position is one byte past the end of the file. These functions get and set properties on a stream. Only certain properties are meaningful with certain kinds of streams, and the meaning depends on the stream. If two or more stream types support a property of the same name, it is expected that the property has the same or similar meaning for both streams to the maximum extent that similarity is possible. The function sets a property on a stream. The argument is a symbol, usually a keyword symbol, denoting the property, and is the property value. If the stream understands and accepts the property, the function returns Otherwise it returns The function inquires about the value of a property on a stream. If the stream understands the property, then it returns its current value. If the stream does not understand a property, nil is returned, which is also returned if the property exists, but its value happens to be The property is widely supported by streams of various types. It associates the stream with a name. This property is not always modifiable. File, process and stream socket I/O streams have a property which can be accessed, but not modified. It retrieves the same value as the function. The "real time" property supported by these streams, connected with the function, also appears as the property. I/O streams also have a property called which, if set, suppresses the decoding of UTF-8 on character input. Rather, each byte of the file corresponds directly to one character. Bytes in the range 1 to 255 correspond to the character code points U+0001 to U+00FF. Byte value 0 is mapped to the code point U+DC00. The logging priority of the syslog stream is controlled by the property. If is a catenated stream (see the function then these functions transparently operate on the current head stream of the catenation. The function takes zero or more arguments which are input streams of the same type, and combines them into a single virtual stream called a catenated stream. The function takes a single list of input streams of the same type, and similarly combines them into a catenated stream. A catenated stream does not support seeking operations or output, regardless of the capabilities of the streams in the list. If the stream list is not empty, then the leftmost element of the list is called the head stream. The and functions delegate to the corresponding operations on the head stream, if it exists. If the stream list is empty, they return to the caller. If the or operation on the head stream yields and there are more lists in the stream, then the stream is closed, removed from the list, and the next stream, if any, becomes the head list. The operation is then tried again. If any of these operations fail on the last list, it is not removed from the list, so that a stream remains in place which can take the or operations. In this manner, the catenated streams appear to be a single stream. Note that the operations can fail due to being unsupported. It is the caller's responsibility to make sure all of the streams in the list are compatible with the intended operations. If the stream list is empty then an empty catenated stream is produced. Input operations on this stream yield and the and operations throw an exception. The function returns if is a catenated stream. Otherwise it returns The function pushes to the front of the stream list inside If an or operation was successfully performed on previously to a call to those operations were forwarded to the front stream. If those bytes or characters are still pending, they are pending inside that stream, and thus are logically preceded by the contents of The and functions create a list of streams by invoking the function on each element of By default, the mode string is passed to if the argument specified, it overrides this default. In that situation, the specified mode should permit reading. These streams are turned into a catenated stream as if they were the arguments of a call to The effect is that multiple files appear to be catenated together into a single input stream. If the optional argument is supplied, then if is empty, is returned instead of an empty catenated stream. The difference between and is that creates all of the streams up-front. So if any of the paths cannot be opened, the operation throws. The variant is lazy: it creates a lazy list of streams out of the path list. The streams are opened as needed: before the second stream is opened, the program has to read the first stream to the end, and so on. Collect lines from all files that are given as arguments on the command line. If there are no files, then read from standard input:
       @(next (open-files *args* *stdin*))
       @(collect)
       @line
       @(end) The function determines whether the two paths and are equal under a certain definition of equivalence, whose requirements are given below. The function returns if the paths are equal, otherwise If and are strings which are identical under the function, then they are considered equal paths. Otherwise, the two paths are equal if the relative path from to is (dot), as would be determined by the function, if it were applied to and as its arguments. If would return the dot path, then the two paths are equal. If would return any other value, or throw an exception, then the paths are unequal.
      ;; simple case
      (path-equal "a" "a") -> t
      (path-equal "a" "b") -> nil
      ;; trailing slashes don't matter
      (path-equal "a" "a/") -> t
      (path-equal "a/" "a/") -> t
      ;; .. components resolved:
      (path-equal "a/b/../c" "a/c") -> t
      ;; . components resolved:
      (path-equal "a" "a/././.") -> t
      (path-equal "a/." "a/././.") -> t
      ;; (On Microsoft Windows)
      ;; different drive:
      (path-equal "c:/a" "d:/b/../a") -> nil
      ;; same drive:
      (path-equal "c:/a" "c:/b/../a") -> t The and functions test whether the argument is an absolute path, returning a or indication. The function behaves in the same manner on all platforms, implementing a platform-agnostic definition of absolute path, as follows. An absolute path is a string which either begins with a slash or backslash character, or which begins with an alphanumeric word, followed by a colon, followed by a slash or backslash. The empty string isn't an absolute path. Examples of absolute paths under
      /etc
      c:/tmp
      ftp://user@server
      disk0:/home
      Z:\Users Examples of strings which are not absolute paths:
      .
      abc
      foo:bar/x
      $:\abc The is similar to except that it reports false for paths which are not absolute paths according to the host platform. The following paths are not absolute on POSIX platforms:
      c:/tmp
      ftp://user@server
      disk0:/home
      Z:\Users The function tests whether the string represents a pure relative path, which is defined as a path which isn't absolute according to which isn't the string (single period), which doesn't begin with a period followed by a slash or backslash, and which doesn't begin with an alphanumeric word terminated by a colon. The empty string is a pure relative path. Other examples of pure relative paths:
      abc.d
      .tmp/bar
      1234
      x
      $:/xyz Examples of strings which are not pure relative paths:
      .
      /
      /etc
      ./abc
      .\
      foo:
      $:\abc The and functions calculate, respective, the directory part and base name part of a pathname. The calculation is performed in a platform-dependent way, using the characters in the variable as path component separators. Both functions first remove from any further consideration all superfluous trailing occurrences of the directory separator characters from Thus input such as is reduced to just and is reduced to The resulting trimmed path is the effective path. If the effective path is an empty string, then returns and returns the empty string. If the effective path is not empty, and contains no path separator characters, then returns and returns the effective path. Otherwise, the effective path is divided into two parts: the raw directory prefix and the remainder. The raw directory prefix is the maximally long prefix of the effective path which ends in a separator character. The function returns the raw directory prefix, if that prefix consists of nothing but a single directory separator character. Otherwise it returns the raw directory prefix, with the trailing path separator removed. The function returns the remaining part of the effective path, after the raw directory prefix. If the argument is given to it specifies a proper suffix to be removed from the returned base name. First, the base name is calculated according to the foregoing rules. Then, if matches a trailing portion of the base name, but not the entire base name, it is removed from the base name. The parameter may be given a argument, which is treated exactly as if it were absent. Note: this requirement allows for the following idiom to work correctly even in cases when has no suffix:
      ;; calculate base name of p with short suffix removed
      (base-name p (short-suffix p))
      ;; calculate base name of p with long suffix removed
      (base-name p (long-suffix p))
      (base-name "") -> ""
      (base-name "/") -> "/"
      (base-name ".") -> "."
      (base-name "./") -> "."
      (base-name "a") -> "a"
      (base-name "/a") -> "a"
      (base-name "/a/") -> "a"
      (base-name "/a/b") -> "b"
      (base-name "/a/b/") -> "b"
      (base-name "/a/b///") -> "b"
      ;; with suffix
      (base-name "" "") -> ""
      (base-name "/" "/") -> "/"
      (base-name "/" "") -> "/"
      (base-name "." ".") -> "."
      (base-name "." "") -> "."
      (base-name "./" "/") -> "."
      (base-name "a" "a") -> "a"
      (base-name "a" "") -> "a"
      (base-name "a.b" ".b") -> "a"
      (base-name "a.b/" ".b") -> "a"
      (base-name "a.b/" ".b/") -> "a.b"
      (base-name "a.b/" "a.b") -> "a.b" The and functions calculate the long suffix and short suffix of which must be a string. If does not contain any occurrences of the character (period) in the role of a suffix delimiter, then does not have a suffix. In this situation, both functions return the argument, which defaults to if it is omitted. What it means for to have a suffix delimiter is that the character occurs somewhere in the last component of other than as the first character of that component. What constitutes the last component is specified in more detail below. If a suffix delimiter is present, then the long or short suffix is the substring of which includes the delimiting period and all characters which follow, except that if ends in a sequence of one or more path separator characters, those characters are omitted from the returned suffix. If multiple periods occur in the last component of the path, the delimiter for the long suffix is the leftmost period and the delimiter for the short suffix is the rightmost period. If the delimiting period is the rightmost character of or occurs immediately before a trailing path separator, then the suffix delimited by that period is the period itself. If contains only one suffix delimiter, then its long and short suffix coincide. For the purpose of identifying the last component of if ends a sequence of one or more path-separator characters, then those characters are removed from consideration. If the remaining string contains path-separator characters, then the last component consists of that portion of it which follows the rightmost path-separator character. Otherwise, the last component is the entire string. The suffix, if present, is identified and extracted from this last component.
      (short-suffix "") -> nil
      (short-suffix ".") -> nil
      (short-suffix "abc") -> nil
      (short-suffix ".abc") -> nil
      (short-suffix "/.abc") -> nil
      (short-suffix "abc" "") -> ""
      (short-suffix "abc.") -> "."
      (short-suffix "abc.tar") -> ".tar"
      (short-suffix "abc.tar///") -> ".tar"
      (short-suffix "abc.tar.gz") -> ".gz"
      (short-suffix "abc.tar.gz/") -> ".gz"
      (short-suffix "x.y.z/abc.tar.gz/") -> ".gz"
      (short-suffix "x.y.z/abc.tar.gz//") -> nil
      (long-suffix "") -> nil
      (long-suffix ".") -> nil
      (long-suffix "abc") -> nil
      (long-suffix ".abc") -> nil
      (long-suffix "/.abc") -> nil
      (long-suffix "abc.") -> "."
      (long-suffix "abc.tar") -> ".tar"
      (long-suffix "abc.tar///") -> ".tar"
      (long-suffix "abc.tar.gz") -> ".tar.gz"
      (long-suffix "abc.tar.gz/") -> ".tar.gz"
      (long-suffix "x.y.z/abc.tar.gz/") -> ".tar.gz" The and functions calculate the portion of long suffix and short suffix of the string argument and return a path with the suffix removed. Respectively, and calculate the suffix in exactly the same manner as and If is found not to contain a suffix, then it is returned. If contains a suffix, then a new string is returned from which the suffix is deleted. If the suffix is followed by one or more path separator characters, these are preserved in the return value.
      (trim-short-suffix "") -> ""
      (trim-short-suffix "a") -> "a"
      (trim-short-suffix ".") -> "."
      (trim-short-suffix ".a") -> ".a"
      (trim-short-suffix "a.") -> "a"
      (trim-short-suffix "a.b") -> "a"
      (trim-short-suffix "a.b.c") -> "a.b"
      (trim-short-suffix "a./") -> "a/"
      (trim-short-suffix "a.b/") -> "a/"
      (trim-short-suffix "a.b.c/") -> "a.b/"
      (trim-long-suffix "a.b.c") -> "a"
      (trim-long-suffix "a.b.c/") -> "a/"
      (trim-long-suffix "a.b.c///") -> "a///" The function combines the string arguments and in a way which harmonizes with the and functions. If does not end in a path separator character, that category being defined by the variable, then returns the trivial string catenation of and Otherwise, returns a string formed by inserting into just prior to the sequence of trailing path separator characters. The returned string is a catenation of that portion of which excludes the sequence of trailing path separators, followed by followed by the sequence of trailing path separators. A path separator which occurs as a part of syntax that indicates an absolute pathname is not considered a trailing separator. A path which begins with a separator is absolute. Other platform-specific path patterns may constitute an absolute pathname. Note: in cases when does not begin with a period, or is inserted in such a way that it is the start of a path component, then the functions and will not recognize in the resulting path.
      (add-suffix "" "") -> ""
      (add-suffix "" "a") -> "a"
      (add-suffix "." "a") -> ".a"
      (add-suffix "." ".a") -> "..a"
      (add-suffix "/" ".b") -> "/.b"
      (add-suffix "//" ".b") -> "/.b/"
      (add-suffix "//" "b") -> "/b/"
      (add-suffix "a" "") -> "a"
      (add-suffix "a" ".b") -> "a.b"
      (add-suffix "a/" ".b") -> "a.b/"
      (add-suffix "a//" ".b") -> "a.b//"
      ;; On MS Windows
      (add-suffix "c://" "x") -> "c:/x/"
      (add-suffix "host://" "x") -> "host://x"
      (add-suffix "host:///" "x") -> "host://x/" The function joins together zero or more paths, returning the combined path. All arguments are strings. The following description defines the behavior when is given exactly two arguments, which are interpreted as and A description of the variable-argument semantics follows. Firstly, the two-argument is related to the functions and in the following way: if is some path denoting an object in the file system, then produces a path which denotes the same object. The paths and might not be equivalent strings. The function ensures that paths are joined without superfluous path-separator characters, regardless of whether ends in a separator. If a separator must be added, the character (forward slash) is always used, even on platforms where (backslash) is also a pathname separator, and even if either argument includes backslashes. The function eliminates trivial occurrences of the (dot) path component. It preserves trailing separators in the following way: if ends in a path-separator character, then the returned string shall end in that character; and if vanishes entirely because it is equivalent to the dot, then the returned string is itself. If is an empty string, then is returned, and vice versa. The variadic semantics of are as follows. If is called with no arguments at all, it returns the path (period) denoting the relative path of the current directory. If is called with one argument, that argument is returned. If is called with three or more arguments, a left-associative reduction takes place using the two-argument semantics. The first two arguments are catenated into a single path, which is then catenated with the third argument, and so on. The above semantics imply that the following equivalence holds:
      [reduce-left path-cat list] <--> [apply path-cat list]
      (path-cat "" "") --> ""
      (path-cat "" ".") --> "."
      (path-cat "." "") --> "."
      (path-cat "." ".") --> "."
      (path-cat "abc" ".") --> "abc"
      (path-cat "." "abc") --> "abc"
      (path-cat "./" ".") --> "./"
      (path-cat "." "./") --> "./"
      (path-cat "abc/" ".") --> "abc/"
      (path-cat "./" "abc") --> "abc"
      (path-cat "/" ".") --> "/"
      (path-cat "/" "abc") --> "/abc"
      (path-cat "ab/cd" "ef") --> "ab/cd/ef"
      (path-cat "a" "b" "c") --> "a/b/c"
      (path-cat "a" "b" "" "c" "/") --> "a/b/c/" The function removes a consecutive run of one or more trailing separators from the end of the input string The function treats the in a system-independent way: both the backslash and forward slash are considered a trailing separator. The function preserves any necessary trailing separators, such as that of the absolute path or the trailing slashes in volume absolute paths such as
      (trim-path-seps "") -> ""
      (trim-path-seps "/") -> "/"
      (trim-path-seps "//") -> "/"
      (trim-path-seps "a///") -> "a"
      (trim-path-seps "/a///") -> "/a")
      (trim-path-seps "\\") -> "\\"
      (trim-path-seps "\\\\") -> "\\"
      (trim-path-seps "\\a\\\\\\") -> "\\a")
      (trim-path-seps "c:/") -> "c:/"
      (trim-path-seps "c://") -> "c:/"
      (trim-path-seps "c:///") -> "c:/"
      (trim-path-seps "c:a///") -> "c:a"
      ;; not a volume prefix:
      (trim-path-seps "/c:/a///") -> "/c:/a"
      (trim-path-seps "/c://///") -> "/c:")
      (trim-path-seps "c:\\") -> "c:\\"
      (trim-path-seps "c:\\\\") -> "c:\\"
      (trim-path-seps "c:a\\\\\\") -> "c:a"
      ;; mixtures
      (trim-path-seps "c:/\\/\\/") -> "c:/" The function calculates the relative path between two file system locations indicated by string arguments and The is assumed to be a directory. The return value is a relative path which could be used to access an object named by if were the current working directory. The calculation performed by is a pure calculation; it has no interaction with the host operating system. No component of either input path has to exist. Symbolic links are not resolved. This can lead to incorrect results, as noted below. Either both the inputs must be absolute paths, or must both be relative, otherwise an error exception is thrown. On the MS Windows platform, if one input specifies a drive letter prefix, the other input must specify the same prefix, or else an error exception is thrown; there is no relative path between locations on different drives. The behavior is unspecified if the arguments are two UNC paths indicating different hosts. The function first splits both paths into components according to the platform-specific pathname separators indicated by the variable. Next, it eliminates all empty components, (dot) components and (dotdot) components from both separated paths. All dot components are removed, and any component which is neither dot nor dotdot is removed if it is followed by dotdot. Then, a common prefix is determined between the two component sequences, and a relative component sequence is calculated from them as follows: If the component sequence corresponding to is longer than the common prefix, then the excess part of that sequence after the common prefix must not contain any (dotdot) components, or else an error exception is thrown. Otherwise, every component in this excess part of the component sequence is converted to in order to express the relative navigation from up to the directory indicated by the common prefix. Next, if the component sequence corresponding to has any components in excess of the common prefix, those excess components are appended to this possibly empty sequence of dotdot components, in order to express navigation from the common prefix down to the object. This excess sequence coming from may include components. Finally, if the resulting sequence is nonempty, it is joined together using the leftmost path separator character indicated in and returned. If it is empty, then the string is returned. Note: because the function doesn't access the file system and in particular does not resolve symbolic links or other indirection devices, the result may be incorrect. For example, suppose that the current working directory contains a symbolic link called which expands to (dotdot). The expression is oblivious to this, and calculates The correct result in light of being an alias for calls for a return value of The exact problem is that any symbolic links in the excess part of after the common prefix are assumed by to be simple subdirectory names, which can be navigated in reverse using a link. This reverse navigation assumption is false for any symbolic link which which does not act as an alias for a subdirectory in the same location. In situations where this possibility exists, it is recommended to use function to canonicalize the input paths. The following is an example of the algorithm being applied to arguments and where the assumption is that this is on a POSIX platform where the leftmost character in is Firstly, both inputs are converted to component sequences, those respectively being:
      ("a" "d" ".." "b" "x" "y" "")
      ("a" "b" "w") Next the and empty components are removed:
      ("a" "b" "x" "y")
      ("a" "b" "w") At this point, the common prefix is identified:
      ("a" "b") The has two components in excess of the prefix:
      ("x" "y") which are each replaced by The has one component in excess of the common prefix, These two sequences are appended together:
      (".." ".." "w") The resulting path is then formed by joining these with the separator character, resulting in the relative path
      ;; mixtures of relative and absolute
      (rel-path "/abc" "abc") -> ;; error
      (rel-path "abc" "/abc") -> ;; error
      ;; dotdot in excess part of from path:
      (rel-path "../../x" "y") -> ;; error
      (rel-path "." ".") -> "."
      (rel-path "./abc" "abc") -> "."
      (rel-path "abc" "./abc") -> "."
      (rel-path "./abc" "./abc") -> "."
      (rel-path "abc" "abc") -> "."
      (rel-path "." "abc") -> "abc"
      (rel-path "abc/def" "abc/ghi") -> "../ghi"
      (rel-path "xyz/../abc/def" "abc/ghi") -> "../ghi"
      (rel-path "abc" "d/e/f/g/h") -> "../d/e/f/g/h"
      (rel-path "abc" "d/e/../g/h") -> "../d/g/h"
      (rel-path "d/e/../g/h" ".") -> "../../.."
      (rel-path "d/e/../g/h" "a/b") -> "../../../a/b"
      (rel-path "x" "../../../y") -> "../../../../y"
      (rel-path "x///" "x") -> "."
      (rel-path "x" "x///") -> "."
      (rel-path "///x" "/x") -> "." The variable holds a string consisting of the characters which the underlying operating system recognizes as pathname separators. If a particular of these characters is considered preferred on the host platform, that character is placed in the first position of Altering the value of this variable has no effect on any TXR Lisp library function. The function converts text denoting TXR Lisp structure, into the corresponding data structure. The argument may be either a character string, or a stream. If it is omitted, then is used as the stream. The must provide the text representation of one complete TXR Lisp object. If and the function being applied is then if the object is followed by any non-whitespace material, the situation is treated as a syntax error, even if that material is a syntactically valid additional object. The function ignores this situation. Other differences between and are given below. Multiple calls to on the same stream will extract successive objects from the stream. To parse successive objects from a string, it is necessary to convert it to a string stream. The optional argument can be used to specify a stream to which diagnostics of parse errors are sent. If absent, the diagnostics are suppressed. The optional argument can be used to specify the file name which is used for reporting errors. If this argument is missing, the name is taken from the name property of the argument if it is a stream, or else the word is used as the name if is a string. The optional argument, defaulting to 1, specifies the starting line number. This, like the argument, is used for reporting errors. If there are no parse errors, the function returns the parsed data structure. If there are parse errors, and the parameter is present, its value is returned. If the parameter is not present, then an exception of type is thrown. The function ("interactive read") is similar to except that it parses a modified version of the syntax. The modified syntax does not support the application of the dot and dotdot operators on a top-level expression. For instance, if the input is or then will only read the token whereas will read the entire expression. This modified syntax allows to return immediately when an expression is recognized, which is the expected behavior if the input is being read from an interactive terminal. By contrast, waits for more input after seeing a complete expression, because of the possibility that the expression will be further extended by means of the dot or dotdot operators. An explicit end-of-input signal must be given from the terminal to terminate the expression. The special variable controls whether these functions record source location info similarly to Note: if these functions are used to scan data which is evaluated as Lisp code, it may be useful to set true in order to obtain better diagnostics. However, source location recording incurs a performance and storage penalty. The function has the same argument syntax and semantics as the function, except that rather than reading one object, it reads all the Lisp objects from the source, and returns a list of these objects. If the stream is empty, then returns the empty list whereas the function treats the situation as an error. The function retrieves information, from a pertaining to the status of the most recent parsing operation performed on that stream: namely, a previous call to or If the object has not been used for parsing, or else the most recent parsing operation did not encounter errors, then returns If the most recent parsing operation on encountered errors, then function returns a positive integer value indicating the error count. Otherwise it returns If a parse error operation encounters a syntax error before obtaining any token from the stream, then the error count is zero and returns Consequently, may be used after a failed parse operation to distinguish a true syntax error from an end-of-stream condition. The function returns a new stream object which acts as an adapter to the existing If an argument is not specified for then the stream is used. With the exception of all operations on the returned adapter transparently delegate to the original object. When the function is used on the adapter, it behaves differently. A string is extracted from and returned. However, the string isn't a line delimited by a newline character, but rather a record delimited by This record is extracted as if by a call to the function, invoked with the and arguments. All behavior which is built on the function is affected by the record-delimiting semantics of a record adapter's implementation. Notably, the and functions return a lazy list of delimited records rather than of lines. TXR Lisp streams provide support for establishing hanging indentations in text output. Each stream which supports output has a built-in state variable called indentation mode, and another variable indicating the current indentation amount. When indentation mode is enabled, then prior to the first character of every line, the stream prepends the indentation: space characters equal in number to the current indentation value. This logic is implemented by the and functions, and all functions based on these. The function does not interact with indentation. The column position tracking will be incorrect if byte and character output are mixed, affecting the placement of indentation. Indentation mode takes on four numeric values, given by the four variables and As far as stream output is concerned, the code and data modes represented by and behave the same way: both represent the "indentation turned on" state. The difference between them influences the behavior of the function. This function isn't used by any lower-level stream output routines. It is used by the object printing functions like and to break up long lines. The and modes are also treated the same way by lower level stream output, indicating "indentation turned off". The modes are distinguished by and in the following way: is a "soft" disable which allows these object-printing routines to temporarily turn on indentation while traversing aggregate objects. Whereas the ("force off") value is a "hard" disable: the object-printing routines will not enable indentation and will not break up long lines. These variables hold integer values representing output stream indentation modes. The value of is zero. These functions retrieve and manipulate the stream indent mode. The retrieves the current indent mode of The function sets the indent mode of to and returns the previous mode. Note: it is encouraged to save and restore the indentation mode, and in a way that is exception safe. If a block of code sets up indentation on a stream such as and is terminated by an exception, the indentation will remain in effect and affect subsequent output. The macro or operator may be used. The function sets the indent mode of to if and only if its current mode is equal to Whether or not it changes the mode, it returns the previous mode. The only differs in that it sets to if and only if the current mode is not equal to These functions manipulate the indentation value of the stream. The indentation takes effect the next time a character is output following a newline character. The function retrieves the current indentation amount. The function sets indentation to the value and returns the previous value. Negative values are clamped to zero. The function sets indentation relative to the current printing column position, and returns the old value. The indentation is calculated by adding to the current column position. If a negative indentation results, it is clamped to zero. The function sets indentation relative to the current indentation value. The indentation is calculated by adding to the current indentation amount. If a negative indentation results, it is clamped to zero. The function examines the state of the stream, taking into consideration the current printing column position, the indentation state, the indentation amount and an internal "force break" flag. It makes a decision either to introduce a line break by printing a newline character, or else to print the character. If a decision is made not to emit a line break, but is then the function has no effect at all. The return value is if the function has issued a line break, otherwise If the function is called on a stream, it sets an internal "force break" flag which affects the future behavior of The function examines this flag. If the flag is set, clears it, and issues a line break without considering any other conditions. The flag is also cleared whenever a newline character is output. The function returns Note: the is involved in line breaking decisions. Whenever a list or list-like syntax is being printed, whenever an element of that syntax is broken into multiple lines, a break is forced after that element, in order to avoid output which resembles the following diagonally-creeping pattern:
      (a b c (d e f
              g h i) j (k l
                        m n) o) but instead is rendered in a more horizontally compact pattern:
      (a b c (d e f
              g h i)
       j (k l
          m n)
       o) When the printer prints it uses the function between the elements; that function issues the break between the and The printer monitors the return value of it knows that since one of the calls returned the object had been broken into two or more lines. It then calls after printing the last element of that object. Then, due to the force flag, the outer recursion of the printer which is printing will experience a break when it calls before printing Custom methods defined on structure objects can take advantage of and in the same way so that user-defined output integrates with the formatting algorithm. Streams have two properties which are used by the TXR Lisp object printer to optionally truncate the output generated by aggregate objects. A stream can specify a maximum length for aggregate objects via the function. Using the function, the maximum depth can also be specified. This feature is useful when diagnostic output is being produced, and the objects involved are so large that the diagnostic output overwhelms the output device or the user, so as to become uninformative. Output limiting also prevents the printer's non-termination on infinite, lazy structures. It is recommended that functions which operate on streams passed in as parameters save and restore these parameters, if they need to manipulate them, for instance using
      (defun output-function (arg stream)
        ;; temporarily impose maximum width and depth
        (with-resources ((ml (set-max-length stream 42)
                            (set-max-length stream ml))
                         (mw (set-max-depth stream 12)
                            (set-max-depth stream mw)))
          (prinl arg stream)
          ...)) The function establishes the maximum length for aggregate object printing. It affects the printing of lists, vectors, hash tables, strings as well as quasiliterals and quasiword list literals (QLLs). The default value is 0 and this value means that no limit is imposed. Otherwise, the value must be a positive integer. When the list, vector or hash-table object being printed has more elements than the maximum length, then elements are printed only up to the maximum count, and then the remaining elements are summarized by printing the (three dots) character sequence as if it were an additional element. This sequence is an invalid token; it cannot be read as input. When a character string is printed, and the maximum length parameter is nonzero, a maximum character count is determined as follows. Firstly, if the maximum length value is less than 3, it is taken to be 3. Then it is multiplied by 8. Thus, a maximum length of 10 allows 80 characters, whereas a maximum length of 1 allows 24 characters. If a string which exceeds the maximum number of characters is being printed with read-print consistency, as by the function, then only a prefix of the string is printed, limited to the maximum number of characters. Then, the literal syntax is closed using the character sequence (backslash, dot, dot, dot, double quote) whose leading invalid escape sequence (backslash, dot) ensures that the truncated object is not readable. If a string which exceeds the maximum number of characters is being printed without read-print consistency, as by the function, then only a prefix of the string is printed, limited to the maximum number of characters. Then the character sequence is emitted. Quasiliterals are treated using a combination of behaviors. Elements of a quasiliteral are literal sequence of text, and embedded variables and expressions. The maximum length specifies both the maximum number of elements in the quasiliteral, and the maximum number of characters in any element which is a sequence of text. When either limit is exceeded, the quasiliteral is immediately terminated with the sequence (escaped dot, dot, dot, backtick). The maximum character limit is applied to the units of text cumulatively, rather than individually. As in the case of string literals, the limit is determined by multiplying the length by 8, and clamping at a minimum value of 24. When a QLL is printed, the space-separated elements of the literal are individually subject to the maximum character limit as if they were independent quasiliterals. Furthermore, the sequence of these elements is subject to the maximum length. If there are more elements in the QLL, then the sequence (escaped dot, dot, dot, backtick) is emitted and thus the QLL ends. The function returns the previous value. The function establishes the maximum depth for the printing of nested objects. It affects the printing of lists, vectors, hash tables and structures. The default value is 0 and this value means that no limit is imposed. Otherwise, the value must be a positive integer. The depth of an object not enclosed in any object is zero. The depth of the element of an aggregate is one greater than the depth of the aggregate itself. For instance, given the list the list itself has depth 0, the atom has depth 1, as does the sublist and the and atoms have depth 2. When an object is printed whose depth exceeds the maximum depth, then three dot character sequence is printed instead of that object. This notation is an invalid token; it cannot be read as input. Additionally, when a vector, list, hash table or structure is printed which itself doesn't exceed the maximum depth, but whose elements do exceed, then that object is summarized, respectively, as and rather than repeating the sequence for each of its elements. The function returns the previous value. These functions spawn external programs which execute concurrently with the TXR program. They all return a unidirectional stream for communicating with these programs: either an output stream, or an input stream, depending on the contents of In the argument is optional, defaulting to the value if it is missing. See the function for a discussion of modes. The function is implemented using POSIX Those elements of which are applicable to are passed to it, and hence their semantics follows from their processing in that function. The function accepts, via the string parameter, a system command, which is in a system-dependent syntax. On a POSIX system, this would be in the POSIX Shell Command Language. The function specifies a program to invoke via the argument. This is subject to the operating system's search strategy. On POSIX systems, if it is an absolute or relative path, it is treated as such, but if it is a simple base name, then it is subject to searching via the components of the PATH environment variable. If is not able to find or is otherwise unable to execute the program, the child process will exit, using the value of the C variable as its exit status. This value can be retrieved via The argument is a list of strings which specifies additional optional arguments to be passed to the program. The argument becomes the first argument, and becomes the second and subsequent arguments. If is omitted, it defaults to empty. If a coprocess is open for writing is specified as then writing on the returned stream feeds input to that program's standard input file descriptor. Indicating the end of input is performed by closing the stream. If a coprocess is open for reading is specified as then the program's output can be gathered by reading from the returned stream. When the program finishes output, it will close the stream, which can be detected as normal end of data. The standard input and error file descriptors of an input coprocess are obtained from the streams stored in the and special variables, respectively. Similarly, the standard output and error file descriptors of an output coprocess are obtained from the and special variables. These variables must contain streams on which the function is meaningful, otherwise the operation will fail. What this functionality means is that rebinding the special variables for standard streams has the effect of redirection. For example, the following two expressions achieve the same effect of creating a stream which reads the output of the program, which reads and produces the contents of the file
      ;; redirect input by rebinding *stdin*
      (let ((*stdin* (open-file "text-file")))
        (open-command "cat"))
      ;; redirect input using POSIX shell redirection syntax
      (open-command "cat < text-file") The following is erroneous:
      ;; (let ((*stdin* (make-string-input-stream "abc")))
           (open-command "cat")) A string input or output stream doesn't have an operating system file descriptor; it cannot be passed to a coprocess. The streams and are not synchronized with their underlying file descriptors prior to the execution of a coprocess. It is up to the program to ensure that previous output to or is flushed, so that the output of the coprocess isn't reordered with regard to output produced by the program. Similarly, input buffered in is not available to the coprocess, even though it has not yet been read by the program. The program is responsible for preventing this situation also. If a coprocess terminates abnormally or unsuccessfully, an exception is raised. The argument of supports a special syntax. This syntax specifies I/O redirections which are done in the context of the child process, before the specified program is executed. Instances of the syntax are considered options; if specifies a mode such as that mode must precede the redirections. Redirections may be mixed with other options. Up to four redirections may be specified using one of two forms: a short form or the long form. If more than four redirections are specified, the is considered ill-formed. The short form of the syntax consists of three characters: the prefix character a single decimal digit indicating the file descriptor to be redirected, and then a third character which is either another digit, or else one of the two characters or If the third character is a digit, it indicates the target file descriptor of the redirection. For instance indicates that file descriptor 2 is to be redirected to 1 (so that material written to standard error goes to the same destination as that written to standard output). If the third character is it means that the file descriptor will be redirected to the file For instance, indicates that descriptor 2 (standard error) will be redirected to the null device. If the third character is it indicates that the file descriptor shall be closed. For instance means to close descriptor 0 (standard input). The long form of the syntax allows file descriptors that require more than one decimal digit. It consists of the same prefix character which is immediately followed by an open parenthesis The parenthesis is immediately followed by one or more digits which give the to-be-redirected file descriptor. This is followed by one or more whitespace characters, and then either another multi-digit decimal file descriptor or one of the two letters or This second element must be immediately followed by the closing parenthesis Thus and may be written in the long form, respectively, as and while has no short form equivalent. Multiple redirections may be specified, in any mixture of the long and short form. For instance specifies a process pipe that is open for reading, capturing the output of the process. In that process, standard error is redirected to standard output, standard input is connected to the null device, and descriptor 27 is redirected to descriptor 31. The function is a variant of This function has all the same argument conventions and semantics as adding the argument. If this argument isn't then it must specify a function which can be called with no arguments. This function is called in the child process after any redirections are established, just before the program specified by the argument is executed. Moreover, the function allows to be specified as in which case must be specified. When returns, the child process terminates as if by a call to with an argument of zero. The functions in this group create a stream, perform an I/O operation on it, and ensure that it is closed, in one convenient operation. They operate on files or command streams. Several other functions in this category exist, which operate with buffers. They are documented in the Buffer Functions subsection under the FOREIGN FUNCTION INTERFACE section. Many of the functions described in this section take an optional argument. If this is specified, it must be a string which follows the portion of the syntax described for the function. This string must not specify the part. If specified, the must be compatible with the implied Functions that write a file have an implied mode of those which append have an implied mode of and those which read have an implied mode of For instance, a value of is useful with but not The function opens a text stream over the file indicated by the string argument for reading, reads the printed representation of a TXR Lisp object from it, and returns that object, ensuring that the stream is closed. The is similar to except that it reads the entire file as a text stream and returns its contents in a single character string. The function opens a text stream over the file indicated by and returns produces a lazy list of strings representing the lines of text of that file as if by a call to the function, and returns that list. The stream remains open until the list is consumed to the end, as indicated in the description of The and functions open a text stream over the file indicated by the string argument write the argument object into the file in their specific manner, and then close the file. If the file doesn't exist, it is created. If it exists, it is truncated to zero length and overwritten. The function writes a printed representation of using the function. The return value is that of The function writes to the stream using the function. The return value is that of The function writes to the stream using the function. The return value is that of The and functions open a text stream over the file indicated by the string argument write the argument object into the stream in their specific manner, and then close the stream. These functions are close counterparts of, respectively, and These functions behave differently when the indicated file already exists. Rather than being truncated and overwritten, the file is extended by appending the new data to its end. The function opens an input text stream over the file indicated by the argument, which is a string. All Lisp objects are read from the stream. Parse errors are reported to which defaults to (error output is discarded). If there is a parse error, the function throws an exception, otherwise the list of parsed objects is returned. The functions and open a text stream over the file indicated by the string argument and write each of the objects contained in sequence into the stream as if using the function on each individual element of The function opens the file using the mode, which overwrites the file if it exists, whereas uses which appends to the file. The function opens text stream over an input command pipe created for the command string as if by the function. It reads the printed representation of a TXR Lisp object from it, and returns that object, ensuring that the stream is closed. The is similar to except that it reads the entire file as a text stream and returns its contents in a single character string. The function opens a text stream over an input command pipe created for the command string and returns produces a lazy list of strings representing the lines of text of that file as if by a call to the function, and returns that list. The stream remains open until the list is consumed to the end, as indicated in the description of The and functions open an output text stream over an output command pipe created for the command specified in the string argument as if by the function. They write the argument object into the stream in their specific manner, and then close the stream. The function writes a printed representation of using the function. The return value is that of The function writes to the stream using the function. The return value is that of The function writes to the stream using the function. The return value is that of A stream type exists which allows objects to be manipulated through the stream interface. A buffer stream is created using the function, which can either attach the stream to an existing buffer, or create a new buffer that can later be retrieved from the stream using Operations on the buffer stream treat the underlying buffer much like if it were a memory-based file. Unless the underlying buffer is a "borrowed buffer" referencing the storage belonging to another object (such as the buffer object produced by the FFI type's get semantics) the stream operations can change the buffer's size. Seeking beyond the end of the buffer and then writing one or more bytes extends the buffer's length, filling the newly allocated area with zero bytes. The function is supported also. Buffer streams also support the property. Macros and are provided to simplify the steps involved in using buffer streams in some common scenarios. Note that in spite of the naming of these macros there is only one buffer stream type, which supports bidirectional I/O. The function return a new buffer stream. If the argument is supplied, it must be a object. The stream is then associated with this object. If the argument is omitted, a buffer of length zero is created and associated with the stream. The returns the buffer object associated with which must be a buffer stream. The and macros both bind variable to an implicitly created buffer stream, and evaluate zero or more in the environment where the variable is visible. The argument, which may be omitted in the use of the macro, must be an expression which evaluates to a object. The argument must be a symbol suitable for naming a variable. The implicitly allocated buffer stream is connected to the buffer specified by or, when is omitted, to a newly allocated buffer. The code generated by the macro, if it terminates normally, yields the buffer object as its result value. The returns the value of the last or else if no forms are specified.
      (with-out-buf-stream (*stdout* (make-buf 24))
        (put-string "Hello, world!"))
      -> #b'48656c6c6f2c2077 6f726c6421000000 0000000000000000'
      (with-out-buf-stream (*stdout*) (put-string "Hello, world!"))
      -> #b'48656c6c6f2c2077 6f726c6421' Objects of type are Lisp values which contain a foreign pointer ("C pointer"). This data type is used by the function and is generally useful in conjunction with the Foreign Function Interface (FFI). An arbitrary pointer emanating from a foreign function can be captured as a value, which can be passed back into foreign code. For this purpose, there exits also a matching FFI type called The type supports a symbolic type tag, which defaults to The type tag plays a role in FFI. The FFI type supports a tag attribute. When a object is converted to a foreign pointer under the control of the FFI type, and that FFI type has a tag other than the object's tag must exactly match that of the FFI type, or the conversion throws an error. In the reverse direction, when a foreign pointer is converted to a object under control of the FFI type, the object inherits the type tag from the FFI type. Although objects are conceptually non-aggregate values, corresponding to pointers, they are de facto aggregates due to their implementation as references to heap objects. When a object is passed to a foreign function by pointer, for instance using a parameter of type its internal pointer is potentially updated to the new value coming from the function. The function converts into a pointer in a system-specific way which is consistent with the system's addressing structure. Then it returns that pointer contained in a object. The argument must be an integer which is in range for a pointer value. Note: this range is wider than the range; a portion of the range of integers can denote pointers. An extended range of values is accepted. The entire addressable space may be expressed by non-negative values. A range of negative values also expresses a portion of the address space, in accordance with the platform's concept of a signed integer. For instance, on a system with 32-bit addresses, the values 0 to 4294967295 express all of the addresses as a pure binary value. Furthermore, the values -2147483648 to -1 also express the upper part of this range, corresponding, respectively, to the addresses 2147483648 to 4294967295. On that platform, values of outside of the range -2147483648 to 4294967295 are invalid. The argument should be a symbol. If omitted, it defaults to This symbol becomes the object's type tag. The function converts object directly to a The argument may be of any type. The raw representation of is simply stored in a new instance of and returned. The argument should be a symbol. If omitted, it defaults to This symbol becomes the object's type tag. The lifetime of the returned object is independent from that of If the lifetime of reaches its end before that of the the pointer stored inside the becomes invalid. The function retrieves the pointer value of the object as an integer. If an integer is in a range convertible to type, then the expression reproduces The function returns a object which holds a pointer to a buffer object's storage area. The argument must be of type The argument should be a symbol. If omitted, it defaults to This symbol becomes the object's type tag. The lifetime of the returned object is independent from that of If the lifetime of reaches its end before that of the the pointer stored inside the becomes invalid. The function produces a new object which has the same pointer as but whose type is given by Casting objects with circumvents the safety mechanism which type tagging provides. The function creates a new object similar to which has the same address and type symbol as The function changes the pointer value of the object to the null pointer. The argument must be of type. The return value is itself. Note: it is recommended to use when the program has taken some action which invalidates the pointer value stored in a object, where a risk exists that the value may be subsequently misused. The function passes the object's pointer to the C library function. After this action, it behaves exactly like The argument must be of type. The return value is itself. Note: this function is unsafe. If the pointer didn't originate from the family of memory allocation functions, or has already been freed, or copies of the pointer exist which are still in use, the consequences are likely catastrophic. The function tests whether is a It returns if this is the case, otherwise. The function retrieves the object's type tag. The function extracts a Lisp value by converting a C object at the memory location denoted by according to the FFI type The external representation at the specified memory location is is scanned according to the and converted to a Lisp value which is returned. If the argument is specified, it must be a FFI type object. If omitted, then the object's type tag is interpreted as a FFI type symbol and resolved to a type; the resulting type, if one is found is substituted for If the lookup fails an error exception is thrown. The object must be of type and point to a memory area suitably aligned for, and large enough to hold a foreign representation of at the byte offset indicated by the argument. If is a null pointer, an exception is thrown. The operation is similar to the "get semantics" performed by FFI in order to extract the return value of foreign function calls, and by the FFI callback mechanism to extract the arguments coming into a callback. The argument may not be a variable length type, such as an array of unspecified size. Note: the functions and are useful in simplifying the interaction with "semi-opaque" foreign objects: objects which serve as API handles that are treated as opaque pointers in API argument calls, but which expose some internal members that the application must access directly. The objects pass through the foreign API without undergoing conversion, as usual. The application uses these two functions to perform conversion as necessary. Under this technique, the description of the foreign object need not be complete. Structure members which occur after the last member that the application is interested in need not be described in the FFI type. The function converts a Lisp value into a C representation, which is stored at the memory location denoted by according to the FFI type The function's return value is If the argument is specified, it must be a FFI type object. If omitted, then the object's type tag is interpreted as a FFI type symbol and resolved to a type; the resulting type, if one is found is substituted for If the lookup fails an error exception is thrown. The argument must be an object compatible with the conversions implied by The object must be of type and point to a memory area suitably aligned for, and large enough to hold a foreign representation of at the byte offset indicated by the argument. If is a null pointer, an exception is thrown. It is assumed that is an object which was returned by an earlier call to and that the and arguments are the same objects that were used in that call. The function performs the "out semantics" encoding action, similar to the treatment applied to the arguments of a callback prior to returning to foreign code. The variable holds a null pointer as a instance. Two objects may be compared for equality using the function, which tests whether their pointers are equal. The variable compares to values which have been subject to or A null may be produced by the expression however, this creates a freshly allocated object on each evaluation. The expression also produces a null pointer on all platforms where TXR is found. The function indicates to the garbage collector that the given object is associated with of foreign memory that are otherwise invisible to the garbage collector. Note: this function should be used if the foreign memory is indirectly managed by the object in cooperation with the garbage collector. Specifically, should have a finalizer registered against it which will liberate the foreign memory. In TXR Lisp, stream objects aren't structure types, and therefore lie outside of the object-oriented programming system. However, TXR Lisp supports a delegation mechanism which allows a structure which provides certain methods to be used as a stream. The function takes as an argument the instance of a structure, which is referred to as the stream interface object. The function returns a stream object such that when stream operations are invoked on this stream, it delegates these operations to methods of the stream interface object. A structure type called is provided, whose instances can serve as stream interface objects. This structure has a slot called which holds a stream, and it provides all of the methods required for the delegation mechanism used by This operations simply invoke the ordinary stream operations on the slot. The type can be used as a base class for a derived class which intercepts certain operations on a stream (by defining the corresponding methods) while allowing other operations to transparently pass to the stream (via the base methods inherited from The function returns a stream whose operations depend on the a stream interface object. The argument must be a structure which implements certain subsets of, or all of, the following methods: and Implementing is mandatory, and that method must support the property. Failure to implement some of the other methods will impair the use of certain stream operations on the object. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. Note: there is a severe restriction on the use of the argument. The buffer object denoted by the argument may be specially allocated and have a lifetime which is scoped to the method invocation. The method shall not permit the object to be used beyond the duration of the method invocation. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. Note: there is a severe restriction on the use of the argument. The buffer object denoted by the argument may be specially allocated and have a lifetime which is scoped to the method invocation. The method shall not permit the object to be used beyond the duration of the method invocation. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. With two exceptions, the value returned from is retained by close-stream, such that repeated calls to then return that value without calling the method. The exceptions are the values and (the colon symbol). If either of these values is returned, and is invoked again on the same stream object, the method will be called again. Furthermore, if the symbol is returned by the method, this indicates a successful close, and the function returns the symbol rather than the symbol. The rationale for this mechanism is that it supports reference-counted closing. A struct delegate stream may be written which is shared by several owners, which must each call before the underlying real stream is closed. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The method is implemented on a stream interface object. It should behave in a manner consistent with the description of the stream I/O function. The class provides a trivial implementation of a stream interface. It has a single slot, which should be initialized with a stream object. Each methods of shown in its entirety in the above Syntax section, simply invoke the corresponding stream I/O library functions, passing the method arguments, and the value of the slot to that function, and consequently returning whatever that function returns. Note: the function is intended to useful as an inheritance base. A user-defined structure can inherit from and provide its own versions of some of the methods, thereby intercepting those operations to customize the behavior. For instance, a function equivalent to the function could be implemented by constructing an object derived from which overrides the behavior of the method, and then using the to return a stream based on this object.
       ;;; Implementation of my-record-adapter,
       ;;; a function resembling
       ;;; the record-adapter implementation
       (defstruct rec-input stream-wrap
         regex
         include-match-p
         ;; get-line overridden to use regex-based
         ;; extraction using read-until-match
         (:method get-line (me)
           (read-until-match me.regex me.stream
                            me.include-match-p))))
       (defun my-record-adapter (regex stream include-match-p)
         (let ((recin (new rec-input
                           stream stream
                           regex regex
                           include-match-p include-match-p)))
          (make-struct-delegate-stream recin))) TXR Lisp has a package system inspired by the salient features of ANSI Common Lisp, but substantially simpler. Each symbol has a name, which is a string. A package is an object which serves as a container of symbols; the package associates the name strings with symbols. A symbol which exists inside a package is said to be interned in that package. A symbol can be interned in more than one package. A symbol may also have a home package. A symbol which has a home package is always interned in that package. A symbol which has a home package is called an interned symbol. A symbol which is interned in one or more packages, but has no home package, is a quasi-interned symbol. When a quasi-interned symbol is printed, if it is not interned in the package currently held in the variable, it will appear in uninterned notation denoted by a prefix, even though it is interned in one or more packages. This is because in any situation when a symbol is printed with a package prefix, that prefix corresponds to the name of its home package. The reverse isn't true: when a symbol token is read bearing a package prefix, the token denotes any interned symbol in the indicated package, whether or not the package is the home package of that symbol. Packages are held in a global list which can be used to search for a package by name. The function performs this lookup. A package may be deleted from the list with the function, but it continues to exist until the program loses the last reference to that package. When a package is deleted with its symbols are uninterned from all other packages. A symbol existing in one package can be brought into another package via the function, causing it to be interned in the target package. A symbol which thus exists inside a package which is not its home package is called a foreign symbol, relative to that package. The contrasting term with foreign symbol is local symbol, which refers to a symbol, relative to a package, which is interned in that package and that package is also its home. Every symbol interned in a package is either foreign or local. An existing symbol can also be brought into a package under a different name using the function, causing it to be interned under an alternative name. This has the effect of creating a local alias for a foreign symbol, and is intended as a renaming mechanism for resolving name clashes. If a foreign symbol is introduced into a package, and has the same name as an existing local symbol, the local symbol continues to exist, but is hidden: it is not accessible via a name lookup on that package. While hidden, a symbol loses its home package and is thus degraded to either quasi-interned or uninterned status, depending on whether that symbol is interned in other packages. When a foreign symbol is removed from a package via then if a hidden symbol exists in that package of the same name, that hidden symbol is reinterned in that package and reacquires that package as its home package, becoming an interned symbol again. Finally, packages have a fallback package list: a list of associated packages, which may be empty. The fallback package list is manipulated with the functions and and with the clause of the macro. The fallback package list plays a role only in three situations: one in the TXR Lisp parser, one in the printer, and one in the interactive listener. Besides that, two library functions refer to it: and The parser situation involving the fallback list occurs when the TXR Lisp parser resolves an unqualified symbol token: a symbol token not carrying a package prefix. Such a symbol name is resolved against the current package (the package currently stored in the special variable). If a symbol matching the token is not found in the current package, then the packages in its fallback package list are searched for the symbol. The first matching symbol which is found in the fallback list is returned. If no matching symbol is found in the fallback list, then the token is interned as a new symbol is interned in the current package. The packages in the current package's fallback list may themselves have fallback lists. Those fallback lists are not involved; no such recursion takes place. The printer situation involving the fallback list is as follows. If a symbol is being printed in a machine-readable way (not "pretty"), has a home package and is not a keyword symbol, then a search takes place through the current package first and then its fallback list. If the symbol is found anywhere in that sequence of locations, and is not occluded by a same-named symbol occurring earlier in that sequence, then the symbol is printed without a package prefix. The listener situation involving the fallback list is a follows. When tab completion is used on a symbol without a package prefix, the listener searches for completions not only in the current package, but in the fallback list also. The TXR Lisp package system doesn't support the ANSI Common Lisp concept of package use, replacing that concept with fallback packages. Though the and functions exist and are similar to the ones in ANSI CL, they actually operate on individual foreign symbols, bringing them in or removing them, respectively. These functions effectively iterate over the local symbols of the used or unused package, and invoke or respectively. The TXR Lisp package system consequently doesn't support the concept of shadowing symbols, and conflicts do not exist. When a foreign symbol is introduced into a package which already has a symbol by that name, that symbol is silently removed from that package if it is itself foreign, or else hidden if it is local. The TXR Lisp package system also doesn't feature the concept of internal and external symbols. The rationale is that this distinction divides symbols into subsets in a redundant way. Packages are already subsets of symbols. A module can use two packages to simulate private symbols. An example of this is given in the Package Examples section below. The TXR Lisp fallback package list mechanism resembles ANSI CL package use, and satisfies similar use scenarios. However, this mechanism does not cause a symbol to be considered visible in a package. If a package contains no symbol but one of the packages in fallback list does contain that symbol is nevertheless not considered visible in The syntax will not resolve. The fallback mechanism only comes into play when a package is installed as the current package in the variable. It then allows unqualified symbol references to refer across the fallback list. The TXR Lisp package system does not feature package nicknames, which have been found to be a source of clashes in large Common Lisp software collections, leading to the development of a feature called package local nicknames that is not part of ANSI CL, but supported by a number of implementations. In TXR Lisp, packages have only one name, accessible via TXR Lisp packages are held in an association list called which is public, which associates string names with packages. The function which is used by the parser when looking up the package prefix of a qualified symbol, only uses the names which appears as keys in this association list. Usually those names are the same as the names of the package objects. However, it's possible to manipulate this association list to create alias names for packages. Thus, it is possible for to return if the name is associated, in with a package object named The TXR Lisp package system doesn't feature package local nicknames. There are three reasons for this. One is that it doesn't have global package nicknames. The second is that the mechanism would be cumbersome, and add delay to the resolution of qualified symbols, requiring nicknames in the to be searched for a package name, in addition to the dynamic The third reason is that package local nicknames do not actually solve the problem of clashing symbols, when an application uses multiple packages that each define a symbol by the same name. Package nicknames only shorten the qualified names required to refer to the symbols, Instead, TXR Lisp allows a foreign symbol to be interned in a package under a name which is different from its Thus, rather than creating aliases for package names, TXR Lisp packages can locally rename the actual clashing symbols, which can then be referenced by unqualified names. By manipulating a TXR Lisp source file can nevertheless achieve the creation of a de facto package nickname, which is local to a loaded file, by following the following example:
      ;; make sure that when this file finishes loading,
      ;; or the loading is interrupted by an exception,
      ;; the "u" package alias is deleted from *package-alist*
      (push-after-load
        (set *package-alist* [remqual "u" *package-alist* car]))
      ;; push an alias named u for the usr package.
      (push (cons "u" (find-package "usr")) *package-alist*)
      ;; u: can now be used, until the end of this file
      (u:prinl (u:list 1 2 3)) The following example illustrates a simple scenario of a module whose identifies are in a package, and which also has private identifiers in a private package.
      ;; Define three packages.
      (defpackage mod-priv
        (:fallback usr))
      (defpackage mod)
      (defpackage client
        (:fallback mod usr)
        (:use-from mod-priv other-priv))
      ;; Switch to mod-priv package
      (in-package mod-priv)
      (defun priv-fun (arg)
        (list arg))
      ;; Another function with a name in the mod-priv package.
      (defun other-priv (arg)
        (cons arg arg))
      ;; Define a function in mod; a public function.
      ;; Note that we don't have to change to the mod package,
      ;; to define functions with names in that package.
      ;; We rely on interning being allowed for the qualified
      ;; mod:public-fun syntax.
      (defun mod:public-fun (arg)
        (priv-fun arg)) ;; priv-fun here is mod-priv:priv-fun
      ;; Switch to client package
      (in-package client)
      (priv-fun) ;; ERROR: refers to client:priv-fun, not defined
      (mod:priv-fun) ;; ERROR: mod-priv:priv-fun not used in mod
      (mod-priv:priv-fun 3) ;; OK: direct reference via qualifier
      (public-fun 3) ;; OK: mod:public-fun symbol via fallback
      (other-priv 3) ;; OK: foreign symbol mod-priv:other-priv
                     ;; present in client due to :use-from The following example shows how to create a package called in which the symbol from the package is replaced with a local symbol. A function is then defined using the local symbol, which allows strings to be catenated with
      (defpackage custom
        (:fallback usr)
        (:local + - * /))
      (defmacro outside-macro (x) ^(+ ,x 42))
      (in-package custom)
      (defun binary-+ (: (left 0) (right 0))
        (if (and (numberp left) (numberp right))
          (usr:+ left right)
          `@left@right`))
      (defun + (. args)
        [reduce-left binary-+ args])
      (+) -> 0
      (+ 1) -> 1
      (+ 1 "a") -> "1a"
      (+ 1 2) -> 3
      (+ "a") -> "a"
      (+ "a" "b" "c") -> "abc"
      ;; macro expansions using usr:+ are not affected
      (outside-macro "a") -> ;; error: + invalid operands "a" 42 The TXR extraction language has a syntax in which certain Lisp symbolic expressions denoting directives or behave as if they were the tokens of a phrase structure. As a matter of implementation, these are processed specially in the parser and lexical analyzer, and are not read in the same way as ordinary Lisp forms. On the other hand, some directives are not this way. For instance the syntax is processed as a true Lisp expression, in which the token is subject to the usual rules for interning a symbol, sensitive to in the usual way. The following notes describe the treatment of "special" directives that are involved in phrase structure syntax. It applies to all directives which head off a block that must be terminated by all "punctuation" directives like or and all subphrase indicators like or Firstly, each such directive may have a package prefix on its main symbol, yet is still recognized as the same token. That is to say, is still treated by the tokenizer and parser as the token, regardless of the package prefix, and regardless of whether is the same symbol as the symbol. However, this doesn't mean that any is allowed to denote the directive. A qualified symbol such as must correspond to (be the same object as) precisely one of two symbols: either the same-named symbol in the package, or else the same-named symbol in the package. If this condition isn't satisfied, the situation is a syntax error. Note that this check uses the original and packages, not the packages which are currently named or in the current A check is also performed for an unqualified symbol. An unqualified symbol like must also resolve, in the context of the current value of the variable, to the same named-symbol in either the original or package. Thus if the current package isn't and is being processed, the current package must be such that resolves to either because that symbol is present in the current pack via import, or else visible via the fallback list. These rules are designed to approximate what the behavior would be if these directives were actually scanned as Lisp forms in the usual way and then recognized as phrase structure tokens according to the identity of their leading symbol. The additional restriction is added that that the directive symbol names are treated as reserved. If there exists a user-defined pattern function called it may not be invoked using the syntax which is erroneous; though it is invocable indirectly via the directive. Various functions in the package and symbol area of the library have a parameter. When the argument is optional, it defaults to the current value of the special variable. If specified, the argument may be a character string, which is taken as the name of a package. It may also be a symbol, in which case the symbol's name, which is a character string, is used. Thus the objects and all refer to the same package, the system package which is named A parameter may also simply be a package object. Some functions, like and functions accept a list of packages as their first argument. This may be a list of objects which follow the above conventions: strings, symbols or package objects. Also, instead of a list, an atom may be passed: a string, symbol or package object. It is treated as a singleton list consisting of that object. These variables hold predefined packages. The contains all of the public symbols in the TXR Lisp library. The holds keyword symbols, which are printed with a leading colon. The is for internal symbols, helping the implementation avoid name clashes with user code in some situations. These variables shouldn't be modified. If they are modified, the consequences are unspecified. The names of these packages, respectively, are and This variable holds the current package. The global value of this variable is initialized to a package called The package has the package in its fallback list; thus when is current, all of the symbols, comprising the content of the TXR Lisp library, are visible. All forms read and evaluated from the TXR command line, in the interactive listener, from files via or or from the TXR pattern language are processed in this default package, unless arrangement are made to change to a different package. The current package is used as the default package for interning symbol tokens which do not carry the colon-delimited package prefix. The current package also affects printing. When a symbol is printed whose home package matches the current package, it is printed without a package prefix. (Keyword symbols are always printed with the colon prefix, even if the keyword package is current.) The function creates and returns a new symbol object. The argument which must be a string, specifies the name of the symbol. The symbol does not belong to any package (it is said to be "uninterned"). Note: an uninterned symbol can be interned into a package with the function. Also see the function. The function is similar to It creates and returns a new symbol object. If the argument is omitted, it defaults to The difference between and is that creates the symbol's name by combining the with a numeric suffix. The suffix is obtained by incrementing the and taking the new value. The name string then calculated from the prefix and the counter value as if by evaluating a form similar to From this it can be inferred that can be an object of any kind. Note: the generated symbol's name, though varying thanks to the incrementing counter, is not the basis of its uniqueness. The basis of the symbol's uniqueness is that it is a freshly created object, distinct from any other object. The related function still returns unique symbols even if repeatedly called with the same string argument. This variable is initialized to 0. Each time the function is called, it is incremented. The incremented value forms the basis of the numeric suffix which uses to form the name of the new symbol. The function creates and returns a package named where is a string. It is an error if a package by that name exists already. Note: ordinary creation of packages for everyday program modularization should be performed with the macro rather than by direct use of If the parameter is given an argument which is a Boolean true, then the resulting package holds symbols weakly, from a garbage collection point of view. If the only reference to a symbol is that which occurs inside the weak package, then that symbol may be removed from the package and reclaimed by the garbage collector. Note: weak packages address the following problem. The application creates a package for the purpose of reading Lisp data. Symbols occurring in that data therefore are interned into the package. Subsequently, the application retains references to some of the symbols, discarding the others. If the package isn't weak, then because the application is retaining some of the symbols, and those symbols hold a reference to the package, and the package holds a reference to all symbols that were interned in it, all of the symbols are retained. If a weak package is used, then the discarded symbols are eligible for garbage collection. The breaks the association between a package and its name. After the object continues to exist, but cannot be found using Furthermore, iterates over all remaining packages. For each remaining package it performs the semantic action of the expression. That is to say, all of the remaining packages are scrubbed of any foreign symbols which are the local symbols of the deleted The iterates over all of the local symbols of and rehomes each symbol into Then, it deletes Note: the local symbols are identified as if using rehoming is performed as if using and deleting is performed as if using The function returns if is a package, otherwise it returns The argument should be a string. If a package called exists, then it is returned. Otherwise is returned. The variable contains the master association list which contains an entry about each existing package. Each element of the list is a cons cell whose field is the name of a package and whose is a package object. Note: the TXR Lisp application can overwrite or rebind this variable to manipulate the active package list. This is useful for sandboxing: safely evaluating code that is obtained as an input from an untrusted source, or calculated from such an input. The contents of have security implications because textual source code can refer to any symbol in any package by invoking a package prefix. For instance, even if the function's name is not available in the current package (established by the variable) that symbol can easily be obtained using the syntax However, the entire package itself can be removed from In that situation, the syntax is no longer valid. At the same time, selected symbols from the original can be nevertheless made available via some intermediate package, which is present in and which contains a subset of the symbols that has been curated for safety. That curated package may even be called so that if for instance is present in that package, it may be referred to as in the usual way. The function retrieves the value of Note: this function is obsolescent. There is no reason to use it in new code instead of just accessing directly. The function retrieves the name of a package. The function returns a list of all the symbols which are interned in The function returns a list of all the symbols which are interned in and whose home package is that package. The function returns a list of all the symbols which are interned in which do not have that package as their home package, or do not have a home package at all. The union of the local and foreign symbols contains exactly the same elements as the list returned by the symbols interned in a package are partitioned into local and foreign. The returns the current fallback package list associated with The replaces the fallback package list of with The argument must be a list which is a mixture of symbols, strings or package objects. Strings are taken to be package names, which must resolve to existing packages. Symbols are reduced to strings via The argument must be a string. The optional argument must be a package. If is not supplied, then the value taken is that of The function searches for a symbol called If that symbol is found, it is returned. If that symbol is not found, then a new symbol called is created and inserted into and that symbol is returned. In this case, the package becomes the symbol's home package. The function is very similar to except that if the symbol is not found in then the packages listed in the fallback list of are searched, in order. Only these packages themselves are searched, not their own fallback lists. If a symbol called is found, the search terminates and that symbol is returned. Only if nothing is found in the fallback list will create a new symbol and insert it into exactly like The function removes from The symbol may not be removed from the package; an error exception is thrown in this case. If isn't then is searched to determine whether it contains as an interned symbol (either local or foreign), or a hidden symbol. If is a hidden symbol, then it is removed from the hidden symbol store. Thereafter, even if a same-named foreign symbol is removed from the package via or those operations will no longer restore the hidden symbol to interned status. In this case, returns the hidden symbol that was removed from the hidden store. If is a foreign symbol, then it is removed from the package. If the package has a hidden symbol of the same name, that hidden symbol is reinterned in the package, and the package once again becomes its home package. In this case, is returned. If is a local symbol, the symbol is removed from the package. In this case also, is returned. If is not found in the package as either an interned or hidden symbol, then the function has no effect and returns The and functions search for a symbol called That argument must be a character string. If the argument is omitted, the parameter defaults to the current value of If the symbol is found in then it is returned. If the symbol is not found in then the function also searches the packages listed in the fallback list of are searched, in order. Only these packages themselves are searched, not their own fallback lists. If a symbol called is found, the search terminates and that symbol is returned. The function only searches ignoring its fallback list. If a symbol called isn't found, then these functions return is returned, which defaults to Note: an ambiguous situation exists when is a symbol, such as its default value because if that symbol is successfully found, it is indistinguishable from The arguments and must be a symbol and package object, respectively, and must not be the symbol The function moves into If is already interned in a package, it is first removed from that package. If a symbol of the same name exists in that symbol is first removed from Also, if a symbol of the same name exists in the hidden symbol store of that hidden symbol is removed. Then is interned into and becomes its home package, making it a local symbol of Note: if is currently the hidden symbol of some package, it is not removed from the hidden symbol store of that package. This is a degenerate case. The implication is that if that hidden symbol is ever restored in that package, it will once again have that package as its home package, and consequently it will turn into a foreign symbol of The function returns if is a symbol, otherwise it returns The function returns the name of The function returns the home package of If has no home package, it returns The function returns if is a keyword symbol, otherwise it returns The function returns if is a bindable symbol, otherwise it returns All symbols are bindable, except for keyword symbols, and the special symbols and The function brings an existing into The is similar, but allows an alternative to be specified. The will be interned under that name, rather than under its symbol name. In all cases, both function return The following equivalence holds:
      (use-sym s p) <--> (use-sym-as s (symbol-name s) p) Thus, in the following descriptions, when the remarks are interpreted as applying to the argument is understood as referring to the of the argument. If is the home package of then the function has no effect. Otherwise is interned in under If a symbol is already interned in under then that symbol is is replaced. If that replaced symbol is a local symbol of meaning that is its home package, then that replaced symbol turns into a hidden symbol associated with the package. It is placed into a special hidden symbol store associated with and is stripped of its home package, becoming quasi-interned or uninterned. Note: and are the basis for the clauses and Note: if is used to introduce a foreign symbol into a package under a different name, that symbol cannot be removed with It can only be removed using The function removes from If is not interned in the function does nothing and returns If is a local symbol of an error is thrown: a package cannot "unuse" its own symbol. Removing a symbol from its own home package requires the function. Otherwise is a foreign symbol interned in and is removed. If the package has a hidden symbol of the same name as that symbol is reinterned into as a local symbol. In this case, that previously hidden symbol is returned. If the package has no hidden symbol matching the removed then itself is returned. There are close similarities between the function and but the two are significantly different. Firstly, cannot be used to remove a symbol from its home package. As noted above, this requires Secondly, can be used to undo the effect of whereby a foreign symbol is introduced into a package under a different name. If is not found under its name, will search the package for that symbol to discover whether it is present under a different name, and proceed with the removal using that name. The function performs no such secondary check; if is not found in the package under its own name, the operation fails, and so cannot be used for undoing the effect of The and are convenience functions which perform a mass import of symbols from one package to another, or a mass removal, respectively. The function iterates over all of the local symbols of the packages in For each symbol it performs the semantic action implied by the expression. Similarly iterates in the same way, performing, effectively, the semantic action of the expression. The argument must be a list which is a mixture of symbols, strings or package objects. Strings are taken to be package names, which must resolve to existing packages. Symbols are reduced to strings via The macro provides a convenient means to create a package and establish its properties in a single construct. It is intended for the ordinary situations in which packages support the organization of programs into modules. The argument, giving the package name, may be a symbol or a character string. If it is a symbol, then the symbol's name is taken to be name for the package. If a package called already exists, then selects that package for further operations. Otherwise, a new, empty package is created. In either case, this package is referred to as the present package in the following descriptions. The may be optionally followed by one or more clauses, which are processed in the order that they appear. Each clause is a compound form headed by a keyword. The supported clauses are as follows:
    The clause specifies the packages to comprise the fallback list of the present package. If this clause is omitted, or if it is present with no arguments, then the present package has an empty fallback list. Each may be a string or symbol naming an existing package. It is permitted for the present package itself to appear in its own fallback list. This is useful for creating a package with a nonempty fallback list which doesn't actually provide access to any other package. The clause specifies packages whose local symbols are to be interned into the present package as foreign symbols. Each may be a string or symbol naming an existing package. The list of package names is processed as if by a call to The clause specifies individual symbols to be brought into the present package, as if by the function. The arguments are symbols. The clause specifies individual symbols to be brought into the present package, as if by the function. The arguments constitute a property list consisting of interleaved symbols and names. Each argument is a symbol, and each is either a symbol or a string. If it is a symbol, then its name is retrieved via and used in its place. The clause specifies the names of local symbols in a package denoted by to be used in the present package. All arguments of are either strings or symbols which are reduced to strings by mapping to their names. Each is interned in the package identified by which may have the effect of creating that symbol. This symbol is expected to be a local symbol of that package. If that is so, the symbol is brought into the present package via Otherwise if the symbol is foreign to package identified by then an error exception is thrown. The clause specifies the names of symbols to be interned in the new package as local symbols. Each argument must be either a character string or a symbol. If it is a symbol, its name is taken, thereby reducing the argument to a character string. The arguments are processed in the order in which they appear. Each name is first interned in the newly created package using the function. Then, if the resulting symbol is foreign to the package, it is removed with and the name is interned again.
The macro causes the special variable to take on the package denoted by The macro checks, at expansion time, that is either a string or symbol. An error is thrown if this isn't the case. The argument expression isn't evaluated, and so must not be quoted. The code generated by the macro performs a search for the package. If the package is not found at the time when the macro's expansion is evaluated, an error is thrown. The variable holds an object which encapsulates the state of a pseudorandom number generator. This variable is the default argument value for the and for the convenience of writing programs which are not concerned about the management of random state. On the other hand, programs can create and manage random states, making it possible to obtain repeatable sequences of pseudorandom numbers which do not interfere with each other. For instance objects or modules in a program can have their own independent streams of random numbers which are repeatable, independently of other modules making calls to the random number functions. When TXR starts up, the variable is initialized with a newly created random state object, which is produced as if by the call The special variable specifies the value which is used by in place of a missing argument. To "warm up" a pseudorandom number generator (PRNG) means to obtain some values from it which are discarded, prior to use. The number of values discarded is the warm-up period. The WELL512a PRNG used in TXR produces 32-bit values, natively. Thus each warm-up iteration retrieves and discards a 32-bit value. The PRNG has a state space consisting of a vector of sixteen 32-bit words, making the state space 4096 bits wide. Warm up is required because PRNG-s, in particular PRNG-s with large state spaces and long periods, produce fairly predictable sequences of values in the beginning, before transitioning into chaotic behavior. This problem is worse for low complexity seeds, such as small integer values. The sequences are predictable in two ways. Firstly, some initial values extracted from the PRNG may exhibit patterns ("problem 1"). Secondly, the initial values from sequences produced from similar seeds (for instance consecutive integers) may be similar or identical ("problem 2"). The default value of is only 8. This is insufficient to ensure good initial PRNG behavior for seeds even as large as 64 bits or more. That is to say, even if as many as eight bytes' worth of true random bits are used as the seed, the PRNG will exhibit predictable behaviors, and a poor distribution of values. Applications which critically depend on good PRNG behavior should choose large warm-up periods into the hundreds or thousands of iterations. If a small warm-up period is used, it is recommended to use larger seeds which initialize more of the 4096-bit state space. TXR's PRNG implementation addresses "problem 1" by padding the unseeded portions of the state space with random values (from a static table that doesn't change). For instance, if the integer 1 is used to seed the space, then one 32-bit word of the space is set to the value 1. The remaining 15 are populated from the random table. This helps to ensure that a good PRNG sequence is obtained immediately. However, it doesn't address "problem 2": that similar seed values generate similar sequences, when the warm-up period is small. For instance, if 65536 different random state objects are created, from each of the 16-bit seeds in the range [0, 65536), and then a random 16-bit value is extracted from each state, only 1024 unique values result. The function creates and returns a new random state, an object of the same kind as what is stored in the variable. The seed, if specified, must be an integer value, a buffer, an existing random state object, or else a vector returned from a call to the function Note that the sign of the seed is ignored, so that negative seed values are equivalent to their additive inverses. If seed is not specified, then produces a seed based on some information in the process environment, such as current time of day. It is not guaranteed that two calls to that are separated by less than some minimum increment of real time produce different seeds. The minimum time increment depends on the platform. On a platform with a millisecond-resolution real-time clock, the minimum time increment is a millisecond. Calls to less than a millisecond apart may predictably produce the same seed. If an integer or buffer seed is specified, then the integer value is mapped to a pseudorandom sequence, in a platform-independent way. If an existing random state is specified as a seed, then it is duplicated. The returned random state object is a distinct object which is in the same state as the input object. It will produce the same remaining pseudorandom number sequence, as will the input object. If a vector is specified as a seed, then a random state is constructed which duplicates the random state object which was captured in that vector representation by the function. The argument specifies the number of values which are immediately obtained and discarded from the newly-seeded generator before it is returned. This procedure is referred to as PRNG warm-up. Warm-up is not performed if is a vector or random state object. In this situation, if the is present, it may still be required to be an integer, even though it is ignored. If warm-up is performed, but the argument is missing, then the value of the special variable is used. Note: this variable has a default value which may be too small for some applications of pseudorandom numbers; see the Notes under The function returns if is a random state, otherwise it returns The function converts a random state into a vector of integer values. If the argument, which must be a random state object, is omitted, then the value of the is used. All three functions produce pseudorandom numbers, which are positive integers. The numbers are obtained from a WELL512a PRNG, whose state is stored in the random state object. The function produces a random fixnum integer: a reduced range integer which fits into a value that does not have to be heap-allocated. The and functions produce a value in the range [0, They differ only in the order of arguments. In the function, the random state object is the second argument and is optional. If it is omitted, the global object is used. The argument must be a positive integer. If is 1, then the function returns zero without altering the state of the pseudorandom number generator. The function produces a pseudorandom floating-point value in the range [0.0, 1.0). The produces a pseudorandom floating-point value in the range [0.0, 1.0], thus differing from by including the 1.0 limit value. The numbers are obtained from a WELL512a PRNG, whose state is stored in the random state object given by the argument to the optional parameter, which defaults to the value of Because the floating-point type does not provide a representation of every real value in the range 0.0 to 1.0, it is not possible to impose the requirement that every value shall occur with equal likelihood. Rather, these functions are intended to produce an a uniform distribution of values according to the following pragmatic requirements. A subset S of the real values in the specified range, [0.0, 1.0) or [0.0, 1.0] is identified whose elements are representable in the floating-point type and which are uniformly spaced along the interval. Then, a random element is chosen from S and returned, such that every element is equally likely to be selected. Note that these requirements do not correspond to the more mathematically ideal concept of uniformly choosing actual real numbers in the [0, 1] interval of the real number line, and then finding the closest floating-point representation. Such a requirement would mean that the boundary values 0.0 and 1.0 appear in the output half as frequently as all the interior values, because each of these two floating-point values is a representations of a range of numbers, half of which lies outside of the [0, 1] interval. The function creates a object of the specified fills it with pseudorandom bytes, and returns it. The bytes are obtained from the random state object given by the optional parameter, which defaults to the value of See the section Buffers for a description of objects. The function returns a vector of randomly selected elements from the sequence using reservoir sampling. If the number of elements in is equal to or smaller than then the function returns a vector of all the elements of in their original order. In other cases, the selected elements are not required to appear in their original order. No element of sequence is selected more than once; duplicate values can appear in the output only if itself contains duplicates. The function returns the number of seconds that have elapsed since midnight, January 1, 1970, in the UTC timezone: a point in time called the epoch. The function returns a cons cell whose field holds the seconds measured in the same way, and whose field extends the precision by giving number of microseconds as an integer value between 0 and 999999. The function is similar to except that the returned cons cell's field gives a number of nanoseconds as an integer value between 0 and 999999999. Note: on hosts where obtaining nanosecond precision is not available, the function obtains a microseconds value instead, and multiplies it by 1000. These functions take the numeric time returned by the function, and convert it to a textual representation in a flexible way, according to the contents of the string. The function converts the time to the local timezone of the host system. The function produces time in UTC. The argument is a string, and follows exactly the same conventions as the format string of the C library function The argument is an integer representing seconds obtained from the time function or from the field of the cons returned by the function. The functions and are equivalent, respectively, to and with the arguments reversed. Thus the following equivalences hold:
  (time-str-local F T) <--> (time-string-local T F)
  (time-str-utc F T) <--> (time-string-utc T F) Additionally, if no argument is supplied to the parameter, its value is obtained by invoking the function. These functions take numeric time in the format returned by the function and convert it to a list of seven fields. The function converts the time to the local timezone of the host system, whereas the function produces time in UTC. The fields returned as a list consist of six integers, and a Boolean value. The six integers represent the year, month, day, hour, minute and second. The Boolean value indicates whether daylight savings time is in effect (always in the case of The parameter is an integer representing seconds obtained from the function. If the argument is absent, the value is obtained by calling The structure represents a time broken down into individual fields. The structure almost directly corresponds to the type in the ISO C language. There are differences. Whereas the member represents a year since 1900, the slot of the structure represents the absolute year, not relative to 1900. Furthermore, the slot represents a one-based numeric month, such that 1 represents January, whereas the C member uses a zero-based month. The slot is a TXR Lisp Boolean value. The slots and correspond directly to and The slot represents the number of seconds east of UTC, and holds a string giving the abbreviated time zone name. On platforms where the C type has fields corresponding to these slots, values for these slots are calculated and stored into them by the and functions, and also the related and methods. On platforms where the corresponding fields are not present in the C language these slots are unaffected by those functions, retaining the default initial value or a previously stored value, if applicable. Lastly, the values of and are not ignored by functions which accept a structure as a source of input values. These functions take numeric time in the format returned by the function and convert it to an instance of the structure. The function converts the time to the local timezone of the host system, whereas function produces time in UTC. The parameter is an integer representing seconds obtained from the function. If the argument is absent, the value is obtained by calling The function scans a time description in according to the specification given in the string. If the scan is successful, a structure of type is returned, otherwise The argument follows the same conventions as the POSIX C library function Prior to obtaining the time from and the returned structure is created and initialized with a time which represents time 0 ("the epoch") if interpreted in the UTC timezone as by the method. The and functions return an integer time value: the same value that would be returned by the and methods, respectively, when applied to the structure object returned by Thus, these equivalences hold:
  (time-parse-local f s) <--> (time-parse f s).(time-local)
  (time-parse-utc f s) <--> (time-parse f s).(time-utc) Note: the availability of these three functions depends on the availability of Note: on some platforms, like the GNU C Library, the function supports the parsing of numeric and symbolic time zones. The slot of the structure ends up being set accordingly. The and functions take the field into account, adjusting the returned time accordingly. If these are specified. The structure has two methods called and The function considers the slots of the structure instance to be local time, and returns its integer representation as the number of seconds since the epoch. The function is similar, except it considers the slots of to be in the UTC time zone. Note: these functions work by converting the slots into arguments to which or is applied. Note: if the slot is not its value is subtracted from the returned result. The structure has a method called This method accepts a string argument, which it uses to convert the fields to a character string representation which is returned. The argument is a string, and follows exactly the same conventions as the format string of the C library function The method scans a time description in according to the specification given in the string. If the scan is successful, the structure is updated with the parsed information, and the remaining unmatched portion of is returned. If all of is matched, then an empty string is returned. Slots of which are originally are replaced with zero, even if these zero values are not actually parsed from If the scan is unsuccessful, then is returned and the structure is not altered. The argument follows the same conventions as the POSIX C library function Note: the method may be unavailable if the host system does not provide the function. In this case, the static slot of the struct is The function returns a time value, similar to the one returned by the function. The value is constructed not from the system clock, but from a date and time specified as arguments. The argument is a calendar year, like 2014. The argument ranges from 1 to 12. The argument is a 24-hour time, ranging from 0 to 23. These arguments represent a local time, in the current time zone. The argument specifies whether the time is expressed in daylight savings time (DST). It takes on three possible values: the keyword or else the symbol Any other value has the same interpretation as If is then the time is assumed to be expressed in DST. If the argument is then the time is assumed not to be in DST. If is then the function tries to determine whether DST is in effect in the current time zone for the specified date and time. The function is similar to except that it treats the time as UTC rather than in the local time zone. The argument is supported by for function call compatibility with It may or may not have any effect on the output (since the UTC zone by definition doesn't have daylight savings time). The calculates the CRC-32 sum over the bytes read from starting at the stream's current position. If the argument is specified, it should be a nonnegative integer. It gives the number of bytes which should be read and included in the sum. If the argument is omitted, then bytes are read until the end of the stream. The optional argument defaults to zero. It is fully documented under the function. The functions returns the calculated CRC-32 as a nonnegative integer. The function calculates the CRC-32 sum over which may be a character string or a buffer. If is a buffer, then the sum is calculated over all of the bytes contained in that buffer, according to its current length. If is a character string, then the sum is calculated over the bytes which constitute its UTF-8 representation. The optional argument defaults to zero. If specified, it should be a nonnegative integer in the 32-bit range. This argument is useful when a single CRC-32 must be calculated in multiple operations over several objects. The first call should specify a value of zero, or omit the argument. To continue the checksum, each subsequent call to the function should pass as the argument the CRC-32 obtained from the previous call. The function returns the calculated CRC-32 as a nonnegative integer. The parameters of the algorithm are as follows. The polynomial is the input and result are reflected; the initial value is and the final value is bitwise xor-ed with
  ;; Single operation
  (crc32 "ABCD") --> 3675725989
  ;; In two steps, demonstrating crc-prev argument:
  (crc32 "CD" (crc32 "AB")) -> 3675725989 The and functions calculate, respectively, the NIST SHA-1 and SHA-256 digests over the bytes read from starting at the stream's current position. The function calculates the MD5 digest, using the RSA Data Security, Inc. MD5 Message-Digest Algorithm. If the argument is specified, it should be a nonnegative integer. It gives the number of bytes which should be read and included in the digest. If the argument is omitted, then bytes are read until the end of the stream. If the argument is omitted, the digest value is returned as a new, buffer object. This buffer is 32 bytes long in the case of SHA-256, holding a 256-bit digest, and 16 bytes long in the case of MD5, holding a 128-bit digest. If the argument is specified, it must be a buffer that is at least 16 bytes long in the case of MD5, and at least 32 bytes long in the case of SHA-256. The hash is placed into that buffer, which is then returned. The and function calculate, respectively, the NIST SHA-1 and SHA-256 digests over which may be a character string or a buffer. Similarly, the functions calculates the MD5 digest over using the RSA Data Security, Inc. MD5 Message-Digest Algorithm. If is a buffer, then the digest is calculated over all of the bytes contained in that buffer, according to its current length. If is a character string, then the digest is calculated over the bytes which constitute its UTF-8 representation. If the argument is omitted, the digest value is returned as a new, buffer object. This buffer is 32 bytes long in the case of SHA-256, holding a 256-bit digest, and 16 bytes long in the case of MD5, holding a 128-bit digest. If the argument is specified, it must be a buffer that is at least 16 bytes long in the case of MD5, and at least 32 bytes long in the case of SHA-256. The hash is placed into that buffer, which is then returned. The three functions and implement a stateful computation of SHA-1 digest which allows multiple input sources to contribute to the result. Furthermore, the context object may be serially reused for calculating multiple digests. The function, which takes no arguments, returns a new SHA-1 digest-producing context object. The updates the state of the SHA-1 digest object by including into the digest calculation. The argument may be: a character or character string, whose UTF-8 representation is digested; a buffer object, whose contents are digested; or an integer, representing a byte value in the range 0 to 255 included in the digest. The function may be called multiple times to include any mixture of strings and buffers into the digest calculation. The function finalizes the digest calculation and returns the digest in a buffer. If the argument is omitted, then a new 20-byte buffer is created for this purpose. Otherwise, must specify a object that is at least 20 bytes long. The digest is stored into this buffer and that the buffer is returned. The function additionally resets the object into the initial state of a newly created context object, so that it may be used for another digest session. The three functions and implement a stateful computation of SHA-256 digest which allows multiple input sources to contribute to the result. Furthermore, the context object may be serially reused for calculating multiple digests. The function, which takes no arguments, returns a new SHA-256 digest-producing context object. The updates the state of the SHA-256 digest object by including into the digest calculation. The argument may be: a character or character string, whose UTF-8 representation is digested; a buffer object, whose contents are digested; or an integer, representing a byte value in the range 0 to 255 included in the digest. The function may be called multiple times to include any mixture of strings and buffers into the digest calculation. The function finalizes the digest calculation and returns the digest in a buffer. If the argument is omitted, then a new 32-byte buffer is created for this purpose. Otherwise, must specify a object that is at least 32 bytes long. The digest is stored into this buffer and that the buffer is returned. The function additionally resets the object into the initial state of a newly created context object, so that it may be used for another digest session. The three functions and implement a stateful computation of MD5 digest which allows multiple input sources to contribute to the result. Furthermore, the context object may be serially reused for calculating multiple digests. The function, which takes no arguments, returns a new MD5 digest-producing context object. The updates the state of the MD5 digest object by including into the digest calculation. The argument may be: a character or character string, whose UTF-8 representation is digested; a buffer object, whose contents are digested; or an integer, representing a byte value in the range 0 to 255 included in the digest. The function may be called multiple times to include any mixture of strings and buffers into the digest calculation. The function finalizes the digest calculation and returns the digest in a buffer. If the argument is omitted, then a new 16-byte buffer is created for this purpose. Otherwise, must specify a object that is at least 16 bytes long. The digest is stored into this buffer and that the buffer is returned. The function additionally resets the object into the initial state of a newly created context object, so that it may be used for another digest session. The TXR Lisp library provides a macro called which is inspired by the Unix utility Awk. The macro implements a processing paradigm similar to that of the utility: it scans one or more input streams, which are divided into records and fields, under the control of user-settable regular-expression-based delimiters. The records and fields are matched against a sequence of programmer-defined conditions (called "patterns" in the original Awk), which have associated actions. Like in Awk, the default action is to print the current record. Unlike Awk, the macro is a robust, self-contained language feature which can be used anywhere where a TXR Lisp expression is called for, cleanly nests with itself and can produce a return value when done. By contrast, a function in the Awk language, or an action body, cannot instantiate a local Awk processing machine. The macro implements some of the most important Awk conventions and semantics, in Lisp syntax, while eschewing others. It does not implement the Awk convention that variables become defined upon first mention; variables must be defined to be used. It doesn't implement Awk's weak type system. A character string which looks like a number isn't a number, and an empty string or undefined variable doesn't serve as zero in arithmetic expressions enclosed in the macro. All expression evaluation within is the usual TXR Lisp evaluation. The macro also does not provide a library of functions corresponding to those in the Awk library, nor does it provide counterparts various global variables in Awk such as the and arrays, or and Such features of Awk are extraneous to its central paradigm. The macro processes one or more input sources, which may be streams or files. Each input source is scanned into records, and each record is broken into fields. For each record, the sequence of condition-action clauses (except for certain special clauses) is processed. Every is evaluated, and if it yields true, the corresponding are evaluated. The and forms are understood to be in a scope in which certain local identifiers exist in the variable namespace as well as in the function namespace. These are called awk functions and awk macros. If is one of the following keyword symbols, then it is a special clause, with special semantics: and These clause types are explained below. In such a clause, the expressions are not necessarily forms to be evaluated; the treatment of these expressions depends on the clause. Otherwise, if is not one of the above keyword symbols, the clause is an ordinary condition-action clause, and is a TXR Lisp expression, evaluated to determine a Boolean value which controls whether the forms are evaluated. In every ordinary condition-action clause which contains no forms, the macro substitutes the single action equivalent to the form a call to the local function The behavior of this macro, when called with no arguments, as above, is to print the current record (contents of the variable followed by the output record terminator from the variable While the processing loop in scans an input source, it also binds the special variable to the open stream associated with that source. This binding is in effect across all ordinary clauses, as well as across the special clauses and The following is a description of the special clauses:
    The clause establishes the name of the implicit block contained within the expansion of the macro to be the object usually a symbol. Forms enclosed in the macro can use to abandon the form, specifying the same object as the argument. If the form is omitted, the implicit block is named It is an error for two or more forms to appear. Note: in TXR 255 and older, the clause must have an argument which is a symbol. The symbol is not permitted. Regardless of what order they appear in relation to other clauses in the same macro, clauses are evaluated first before the macro takes any other action. The argument forms of this clause are variables or variable-init forms. They are treated the same way as analogous forms in the special form. Note that these are not enclosed in an extra list as they are in the that form. The bindings established by the clause have a scope which extends over all the other clauses in the macro. If multiple clauses are present, they are effectively consolidated into a single clause, in the order they appear. Note that the lexical variables, functions and macros established by the macro (called, respectively, awk macros, awk functions and awk variables) are in an inner scope relative to bindings. For instance if creates a binding for a variable called that variable will be visible only to subsequent forms appearing in the same clause or later clauses, and also visible in and clauses. In and ordinary clauses, it will be shadowed by the variable which holds the field-separator regular expression or string. The clause introduces named functions which are visible inside the form, as if bound by a operator. Variables defined by are visible to these named functions. The reverse is not true: the functions are not visible to the of the clause. This is regardless of the order of appearance of the and clauses in the macro. Furthermore, functions defined by may refer to awk macros, functions and variables. The clause is evaluated by the macro after processing the clauses. Each is evaluated and the values of these forms are gathered into a list. This list then comprises the list of input sources for the processing task. Each input source must be one of three kinds of objects. It may be a stream object, which must be capable of character input. It may be a list of strings, which will convert to an input stream as if by the function. Or else it must be a character string, which denotes a filesystem pathname which will open for reading. If the clause is omitted, then a defaulting behavior occurs for obtaining the list of input sources. If the special variable isn't the empty list, then is taken as the input sources. Otherwise, the stream is taken as the one and only input source. If the macro uses via the above defaulting behavior, it copies and sets that variable to This is done in order that if is used from the TXR command line, for example using the command-line option, after terminates, TXR will not try to open the next argument as a script file or treat it as an option. Note: programs which want not to modify can explicitly specify as the argument to the keyword, rather than allow to be used through the defaulting behavior. Only the defaulting behavior consumes the arguments by overwriting with It is an error to specify more than one clause. The clause is processed just after the clause. It must have exactly one argument, which is an expression that evaluates to a string, or else to an output stream. If it evaluates to a string, then that string is used as the name of a file to open for writing, and the resulting stream is taken in place of that string. The clause, if present, has the effect of creating a local binding for the special variable. This new value of is visible to all forms within the macro. If a clause is present, it establishes bindings in a scope which is nested within the scope established by Therefore, in the may refer to the new value of established by Furthermore, can rebind causing the definition provided by to be shadowed. In the case when the argument is a string such that a new stream is opened on the file, the macro will close that stream when it finishes executing. Moreover, that stream is treated uniformly as a member of the set of streams that are implicitly managed by the redirection macros in the same macro invocation. In brief, the implication is that if creates a stream for the file pathname and somewhere in the same macro, there is a redirection of the form, or equivalent to (-> "out.txt") then this redirection shall refer to the same stream that was established by Note also that in this example situation, the expression (-> "out.txt" :close) has the effect of closing the stream. All clauses are processed in the order in which they appear, before input processing begins. Each is evaluated. These forms have in their scope the local variables and macros. The clause provides a shorthand which allows the frequently occurring pattern to be condensed to All clauses are processed, in the order in which they appear, when the input processing loop terminates. This termination occurs when all records from all input sources are either processed or skipped, or else by an explicit termination such as a dynamic nonlocal transfer, such as or the throwing of an exception. Upon termination, the end clauses are processed in the order they appear. Each is evaluated, left to right. In the normal termination case, the value of the last of the last end clause appears as the return value of the macro. Note that only termination of the macro initiated from condition-action clauses, clauses, or clauses triggers clause processing. If termination of the macro is initiated from within a or clause, then end clauses are not processed. If an clause performs a nonlocal transfer, the remaining forms in that clause and clauses which follow are not evaluated. All clauses are processed in the order in which they appear, before switches to each new input. If both and forms are specified, then before the first input is processed, clauses are processed first, then the clauses. The clause is a shorthand which translates to All clauses are processed after the processing of an input source finishes. If both and forms are specified, then before after the last input is processed, clauses are processed first, then the clauses. The clauses are processed unconditionally, no matter how the processing of an input source terminates, whether terminated naturally by running out of records, prematurely by invocation of the macro, or via a dynamic nonlocal control transfer such as a block return or exception throw. If a clause performs a nonlocal transfer, processing is not triggered, because the processing of the input source is deemed not to have taken place. The clause may be specified in order to give symbolic names to fields, and optionally specify conversions for them. Every argument must be one of three expressions. It may be a bindable symbol other than (minus). It may be a list whose first element is a symbol other than optionally followed the name of a function. Or else it may be the symbol, which has a special meaning. Symbols other than may not be repeated, and the clause may appear at most once in a given instance of the macro. Each argument is understood to correspond to a field expression for a successive field, starting with the leftmost corresponding with the first field, Each other than becomes the name of a symbol macro which denotes its corresponding field expression, expanded over the scope of the macro. The symbol is a placeholder which doesn't bind a symbol macro to the corresponding field. Additionally, every two-element entry which associates the field symbol with a function name specifies a field conversion. After each record is read and divided into fields, those fields for which is specified are updated by passing their value to this function and replacing them by the returned value. The symbol may also be one of the short-hand symbols available in the macro, such as and others. If at least one such conversion is specified in a clause, then the value of is updated from the converted fields in the usual manner, as if the fields had been assigned. Furthermore, it is ensured that every field for which a clause specifies a conversion exists. Fields with an empty string value are automatically added so that a field exists for the rightmost conversion, and the value of is updated to include these fields. Clauses which do not have one of the specially recognized keywords in the first position are ordinary condition-action clauses. After processing the clauses, enters a loop in which it extracts successive records from the input sources according to the (record separator) variable. Each record is divided into fields according to the (field separator) variable, and various variables are updated. Then, the condition-action clauses are processed, in the order in which they appear. Each is evaluated. If the resulting value is a regular expression or a function, then this regular expression or function is invoked on the value stored in the record variable and the result is taken to be the truth value of Otherwise, if the resulting value of is other than a function or regular expression, it is taken directly to be the truth value. If the condition is true, then its associated forms are evaluated. These forms have access to the truth value via the variable, which is freshly bound for each execution of the forms of that specific clause. For each input record, all condition-action clauses are processed in the order they appear, regardless of which of them have a true action, except in cases when some invokes the or macro, or abandons the execution of the macro entirely via a non-local exit. When an input source runs out of records, switches to the next input source. When there are no more input sources, the macro terminates.
The variable holds the current record. It is automatically updated prior to the processing of the condition-action clauses. Prior to the extraction of the first record, its value is It is possible to assign to The value assigned to must be a character string. Immediately upon the assignment, the character string is delimited into fields according to the field separator variable and these fields are assigned to the field list At the same time, the variable is updated to reflect the new number of fields. Likewise, modification of these variables causes to be reconstructed by a catenation of the textual representation of the fields in separated by copies of the output field separator The variable ("original record") also holds the current record. It is automatically updated prior to the processing of the condition-action clauses at the same time as with the same contents. Like it is initially before the first record is read. The variable is unaffected by modification of the variables and It may be assigned. Doing so has no effect on any other variable. The variable holds the list of fields. Prior to the first record being read, its value is Whenever a new record is read, it is divided into fields according to the field separator variable and these fields are stored in as a list of character strings. If the variable is assigned, the new value must be a sequence. The variable is automatically updated to reflect the length of this sequence. Furthermore, the variable is updated by catenating a string representation of the elements of this sequence, separated by the contents of the (output field separator) variable. Note that assigning to a DWIM bracket form which indexes such as for instance constitutes an implicit modification of and triggers the recalculation of Modifications of the list which do not involve an implicit or explicit assignment to the variable itself do not have this recalculating effect. Unlike in Awk, assigning to the nonexistent field where >= is erroneous. The variable holds the current number of fields in the sequence Prior to the first record being read, it is initially zero. If is assigned, then is modified to reflect the new number of fields. Fields are deleted from if the new value of is smaller. If the new value of is larger, then fields are added. The added fields are empty strings, which means that must be a sequence of a type capable of holding elements which are strings. If is assigned, then is also recalculated, in the same way as described in the documentation for the variable. The variable holds the current absolute record number. Record numbers start at 1. Absolute means that this value does not reset to 1 when switches to a new input source; it keeps incrementing for each record. See the variable. Prior to the first record being read, the value of is zero. The variable holds the current record number within the file. The first record is 1. Prior to the first record being read from the first input source, the value of is zero. Thereafter, it resets to 1 for the first record of each input source and increments for the remaining records of the same input source. The variable is an integer which indicates what input source is being processed. Prior to input processing, it holds the value zero. When the first record is extracted from the first input source, it is set to 1. Thereafter, it is incremented whenever switches to a new input source. The variable provides access to a character string which, if the current input is a file stream, is the name of the underlying file. Assigning to this variable changes its value, but has no effect on the input stream. Whenever a new input source is used by this variable is set from the file name on which it is opening a stream. When using an existing stream rather than opening a file, sets this variable from the property of the stream. Note that the redirection macros and have no effect on this variable. Within their scope, retains its value. The variable specifies a string or regular expression which is used for delimiting characters read from the inputs into pieces called records. Note: the record extraction is internally implemented using record streams instantiated by the function. The regular-expression pattern stored in is used to matches substrings in the input which separate or terminate records. Unless the variable is set true, the substrings which match are discarded and the records consist of the nonmatching extents between them. The initial value of is the newline character. This means that, by default, records are lines. If is changed to the value then record separation operates in paragraph mode, which is described below. If a match for the record separator occurs at the end of the stream, it is not considered to delimit an empty record, but acts as the terminator for the previous record. When a new value is assigned to it has no effect on the most recently scanned and delimited record which is still current, or previous records. The new value applies to the next, not yet read record. In paragraph mode, records are separated by a newline character followed by one or more blank lines (empty lines or lines containing only a mixture of tabs and spaces). This means that, effectively, the record-separating sequences match the regular expression There are two differences between paragraph mode and simply using the above regular expression as The first difference is that if the first record which is read upon entering paragraph mode is empty (because the input begins with a match for the separator regex), then that record is thrown away, and the next record is read. The second difference is that, if field separation based on the variable is in effect, then regardless of the value of newline characters separate fields. Therefore, the programmer-defined doesn't have to include a match for newline. Moreover, if it is a simple fixed string, it need not be converted to a regular expression which also matches a newline. The variable stands for "keep record separator". It is a Boolean variable, initialized to If it is set to a true value, then the separating text matched by the pattern in the variable is retained as part of the preceding record rather than removed. When a new value is assigned to it has no effect on the most recently scanned and delimited record which is still current, or previous records. The new value applies to the next, not yet read record. The variable and each specify a string or regular expression which is used for each record that is stored in the variable into fields. Both variables are initialized to in which case a default behavior is in effect, described below. Use of these variable is mutually exclusive; it is an error for both of these variables to simultaneously have a value other than The value stored in either variable must be a character string or a regular expression. If it contains a string or regex, it is said to contain a pattern. A string value effectively behaves as a fixed regular expression which matches the sequence of characters in the string verbatim, without treating any of them as regex operators. The splitting of into fields is influenced by the Boolean ("keep field separators") variable, whose effect is discussed in its description. If is false, the splitting is carried out as follows. If contains a pattern, then is treated specially when it is the empty string: in that case, the pattern in is ignored, and no fields are produced: the field list is the empty list, and is zero. A nonempty record is split by searching it for matches for the pattern. If a match does not occur, then the entire record is a field. If one match occurs, then the record is split into two fields, either of which, or both, might be empty. If two matches occur, the record is split into three fields, and so on. If finds only an empty string match in the record, then it is considered to match each of the empty strings between two consecutive characters of the record. Consequently, the record is split into its individual characters, each one becoming a field. Note: all of these behaviors, except for the special treatment of the empty record, are accomplished by a call to the function. If the variable ("field tokenize") contains a pattern, that pattern is used to positively recognize tokens within the input record, rather than to match separating material between them. Those matching tokens then constitute the fields. The tokenizing is performed using the function. If and are both as is initially the case, then the splitting into fields is performed as if the variable held the regular expression This means that, by default, fields are sequences of consecutive characters which are not spaces, tabs or newlines. Newlines are excluded from fields (and thus separate them) because they can occur in a record when the value of the record separator is customized. The variable is a Boolean flag which is initialized to If it is set to any other value, it indicates a request to retain the pieces of the record which separate the fields (even when they are empty strings). The retained pieces appear as fields, interspersed among the regular fields so that all of the fields appear in the order in which they were extracted from the record. When is set, and tokenization-style delimiting is in effect due to being set, there is always at least one field, even if the record is empty. If the record doesn't match the tokenizing regular expression in then a single field is generated, then the entire record is taken as one field, denoting the nonmatching space, even if the record is the empty string. If the record matches one or more tokens, then the first and last field will always contain the nonmatching material before the first and last token, respectively. This is true even if the material is empty. Thus always has the material before the first token, whether or not the first token is matched immediately at the first character position in the record. This behavior follows from the semantics of the parameter of the function. Similarly, when splitting based on is in effect and is set, there is always at least one field, even if the record is empty. If finds no match in the record, then the entire record, even if empty, is taken as one field. In that case, there are no separator to retain. When finds one or more matches, then these are included as fields. Separators are always between the fields. If a separator finds a nonempty match at the beginning of a record, that causes an empty field to be split off: the separator is understood as intervening between an empty string before the first character of the record, and subsequent material which follows the text matched by the separator. Thus the first field is an empty field, and the second is the matched text which is included due to being set. An analogous situation occurs at the end of the record: if matches a nonempty string at the tail of the record, it splits off an empty last field, preceded by a field holding the matched separator portion. Empty matches are only permitted to occur between the characters of the record, not before the first character of after the last. If matches the entire record, then there will be three fields: the first and last of these three will be empty strings, and the middle field, the separator, will be a copy of the record. Under empty matches cause empty string to be included among the fields. All of this follows from the semantics of the parameter of the function. The variable controls the fixed-width-based delimiting of records into fields. The variable is initialized to In that state, it has no effect. When this variable holds a value, it is expected to be a list of integers. The use of the or variables is suppressed, and fields are extracted according to the widths indicated by the list. The fields are consecutive, such that if the list is then the first five characters of the record are identified as field and the next three characters after that as Only complete fields are extracted from the record. If, after the extraction of the maximum possible complete fields, more characters remain, those characters are assigned to an extra field. An empty record produces an empty list of fields regardless of the integers stored in fw. A zero width extracts a zero length field, except when no more characters remain in the record. If is stored into then control over field separation is relinquished to the or variables, according to their current values. If holds a value other than or else a list of nonnegative integers, the behavior is unspecified. The following table shows how various combinations of the value the input record and field widths in the variable give rise to field values
  rec fw f
  ---------------------------------
  "abc" (0) ("" "abc")
  "abc" (2) ("ab" "c")
  "abc" (1 2) ("a" "bc")
  "abc" (1 3) ("a" "bc")
  "abc" (1 1) ("a" "b" "c")
  "abc" (3) ("abc")
  "abc" (4) ("abc")
  "" (4) nil
  "" (0) nil The variable hold the output field separator. Its initial value is a string consisting of a single space character. When the function prints two or more arguments, or fields, the value of is used to separate them. Whenever is implicitly updated due to a change in the variable or is used to separate the fields, as they appear in The variable though it stands for "output record separator" holds what is in fact the output record terminator. It is named after the variable in Awk. Each call to the function terminates its output by emitting the value of The initial value of is a character string consisting of a single newline, and so the function prints lines. The variable is implicitly bound over the scope of the action forms of every condition-action clause. It holds the result of the condition form. Because the action forms execute only if the condition yields true, it follows that is never observed with a value of unless the program explicitly assigns that value. Note: this is an original feature in the TXR Lisp macro, which has no counterpart in POSIX or GNU Awk.
  (awk
    (:inputs '("carpet"))
    (#/a.*p/ (prn res)))
  Output:
  arp In this example, the result of the regular expression being applied to the input is the string and so over that clause, takes on that string as its value. Thus, thanks to the action has access to the matching part of the record. The function performs output into the stream. The clause affects the destination by rebinding If called with no arguments, prints followed by Otherwise, it prints the values of the arguments, separated by followed by When a condition-action clause specifies no action forms, then a call to with no arguments is the default action. Each argument is printed by conversion to a string, as if by the expression where is some variable which holds the value produced by the evaluation of Thus if the value is the output for that argument is an empty string, rather than the text The function returns The macro may be invoked in a condition-pattern clause. It terminates the processing of that clause, and all subsequent clauses, causing to process the next record, if there is one. If there is no next record, terminates. The macro may be invoked in a condition-pattern clause. It terminates the processing of that clause, and all subsequent clauses. Then, the current value of the record, namely the datum stored in the variable is delimited into fields, and all of the condition-pattern clauses are processed again. No other state is modified. In particular, the record number and the variable holding the original record both retain their current values. Note: this is an original feature in the TXR Lisp macro, which has no counterpart in POSIX or GNU Awk. The macro may be invoked in a condition-pattern clause. It terminates the processing of that clause and all subsequent clauses. Then abandons the current input source and moves to the next one. If there is no next input source, terminates. The nine macros in the family may be used anywhere within an ordinary condition-pattern clause. Each provides a Boolean test which is true if the current record lands within a range of records delimited by conditions. Each provides its own distinct, useful nuance, which is identified by the mnemonic characters prefixed or suffixed to the name. The basic macro inclusively matches ranges of records. Each such range begins with a record for which yields true, and ends on the record for which is true. What it means to match is that the expression yields a Boolean true value when it is evaluated in the context of processing any of the records which are included in the range. The table below summarizes the semantic variations of these nine range macro operators. The leftmost column labeled represents the stream of records being processed. Each entry in this column gives the literal piece of text which comprises the content of one record in the stream. The remaining nine columns, labeled with the nine range operators, inform about the behavior of these operators with respect to these records. In each of these columns the letter marks those records for which the column's range operator yields true, if it is invoked with the arguments and as its and respectively. For example, the column shows the values of the expression, indicating that the expression starts being true when the record is seen, stays true for the record, and then reverts to false:
  DATA rng -rng rng- -rng- --rng --rng- rng+ -rng+ --rng+
  ----------------------------------------------------------
  PROLOG
  H1 X X X
  H2 X X X X X X
  H3 X X X X X X
  B1 X X X X X X X X X
  B2 X X X X X X X X
  T1 X X X X X X
  T2 X X X
  T3 X X X
  EPILOG The prefix and suffix characters of the operator names are intended to be mnemonic. A single (dash) indicates the exclusion of one record. A double (dash dash) indicates the exclusion of all leading records which match this appears on the left side only. The character, appearing on the right only, indicates that all consecutive records which match are included in the range, not only the first one. Ranges are oblivious to the division between successive sources of input; a range can start in one file of records and terminate in another. To prevent a range from spanning input transitions, additional complexity is required in the expression. Ranges expressed using the family macros may combine with other expressions, including other ranges, and allow arbitrary nesting: the or can be a range, or an expression containing ranges. The expressions and are ordinary expressions which are evaluated. However, their evaluation is unusual in two ways. Firstly, if either expression produces, as its result, a function or regular-expression object, then that function or regular-expression object is applied to the current record (value of the variable), and the result of that application is then taken as the result of the condition. This allows for expressions like which denotes a range which begins with a record which begins with the prefix and ends with a record which contains as a substring. Secondly, the conditions are evaluated out of order with respect to the surrounding expression in which they occur. Ranges and their constituent and are evaluated just prior to the processing of the condition-action clauses. Each expression is reduced to a Boolean value. Then, when the condition-action clauses are processed and their and forms are evaluated, each occurrence of a expression simply denotes its previously evaluated Boolean value. Therefore, it is not possible for expressions to short circuit the evaluation of ranges. Ranges cannot "miss" their starting or terminating conditions; every range occurring anywhere in the condition-action clauses is tested against every record that is processed. Because of this perturbed evaluation order, code which happens to place side effects into ranges may produce surprising results. For instance, the expression will produce output even though the condition is and, moreover, this output will happen before the clauses are processed in which this expression appears. At the time when the itself is evaluated, the expression merely fetches a previously computed Boolean value which indicates whether the range is active for this record. Also, the behavior is unspecified if range expressions attempt to modify any of the special variables and It is not recommended to place any side effects into range expressions. A more detailed description of the range operators follows.
    This type of range becomes active when a record is encountered for which the expression yields true. While the range is active, the expression evaluates true. If, when the range is active, a record is encountered for which the expression yields true, the range remains active for that record and is deactivated after the completion of processing for that record. If the range is inactive and a record is encountered or which both and are true, then the range is activated for that record and then deactivated when that record is processed. Records for which and are not true do not affect the range's activation state. This type of range is active under the same conditions as the type. However, the expression yields a Boolean false value for the first record which begins a range. That is to say, when the range is inactive, and a record is scanned for which is true, the range activates, but the range expression yields This is true regardless of whether the expression yields true for that record. If there are additional records in the range, the expression yields a true value for those records. This type of range is active under the same conditions as the type. However, the range expression yields for the record for which yields true which terminates the range. This occurs even if that is the same record which activated the range by triggering the condition. Note that if a range terminates abruptly due to no more records being available, the range expression still yields true for the last record. This type of range is active under the same conditions as the type. However, the range expression yields for the first record which activates the range, and for the last record which deactivates the range by activating the condition. If the range is active over fewer than three records, then the expression never yields true for that range. If the range terminates abruptly due to no more records being available, and if the last record processed isn't the one which activated the range due to triggering the condition, the expression yields true for that record. This type of range is active under the same conditions as However, the range expression yields for the entire leading sequence of consecutive records for which is true. If is true of the record which terminates the range, is returned for that record also. This type of range is active under the same conditions as However, the range expression yields for the entire leading sequence of consecutive records for which is true, and also yields nil for the last record which triggers the condition. This range is active under different conditions compared to Though it becomes active in the same way, when the expression yields true, the deactivation logic is different. The range is deactivated when a record for which is true is followed by a record for which is not true. That record is excluded from the range; if the expression happens to be true for that record, a new range begins at that record. Thus, effectively, the range is terminated not by single record which triggers but by a sequence of one or more such consecutive records. This range is active under the same conditions as However, the range expression yields for the first record in the range. If the range contains only one record, then it returns for that record. This range is active under the same conditions as However, the range expression yields for the entire leading sequence of consecutive records for which is true. This is the case even for those for which the expression is true.
The macro (filter fields) provides a shorthand for filtering the field list trough a pipeline of chained functions expressed using argument syntax. The following equivalence holds, except that refers to the variable even if the invocation occurs in code which establishes a binding which shadows
  (ff a b c ...) <--> (set f [(opip a b c ...) f])
  ;; convert all fields from string to floating-point
  (ff (mapcar flo-str)) The macro (map fields) provides a shorthand for mapping each field individually trough a pipeline of chained functions expressed using argument syntax. The following equivalence holds, except that refers to the variable even if the invocation occurs in code which establishes a binding which shadows
  (mf a b c ...) <--> (set f (mapcar (opip a b c ...) f))
  ;; convert all fields from string to floating-point
  (mf flo-str) The macro provides a succinct way to request conversions of the textual fields. Conversions are expressed by clauses which correspond with fields. Each is an expression which must evaluate to a function. The clause is evaluated in the same manner as the argument a operator, using Lisp-1-style name lookup. Thus, functions may be specified simply by using their name as a Furthermore, several local functions exist in the scope of each providing a shorthand notation. These are described below. Conversion proceeds by applying the function produced by a clause to the field to which that clause corresponds, positionally. The return value of the function applied to the field replaces the field. When a clause is specified as the symbol (minus) it has a special meaning: this minus clause occupies a field position and corresponds to a field, but performs no conversion on its field. The (colon) keyword symbol isn't a clause and does not correspond to a field position. Rather, it acts as a separator among clauses. It need not appear at all. If it appears, it may appear at most twice. Thus, the clauses may be separated into up to three sequences. If the colon does not appear, then all the clauses are prefix clauses. Prefix clauses line up with fields from left to right. If there are fewer fields than prefix clauses, the values of the excess clauses are evaluated, but ignored. Vice versa, if there are fewer prefix clauses than fields, then the excess fields are not subject to conversions. If the colon appears once, then the clauses before the colon, if any, are prefix clauses, as described in the previous paragraph. Clauses after the colon, if any, are interior clauses. Interior clauses apply to any fields which are left unconverted by the prefix clauses. All interior clauses are evaluated. If there are fewer fields than interior clauses, then the values of the excess interior clauses are ignored. If there are more fields than clauses, then the clause values are cycled: reused from the beginning against the excess fields, enough times to convert all the fields. If the colon appears twice, then the clauses before the first colon, if any, are prefix clauses, the clauses between the two colons are interior clauses, and those after the second colon are suffix clauses. The presence of suffix clauses change the behavior relative to the one-colon case as follows. After the conversions are performed according to the prefix clauses, the remaining fields are counted. If there are are only as many fields as there are suffix clauses, or fewer, then the interior clauses are evaluated, but ignored. The remaining fields are processed against the suffix clauses. If after processing the prefix clauses there are more fields remaining than suffix clauses, then a number of rightmost fields equal to the number of suffix clauses is reserved for those clauses. The interior fields are applied only to the unreserved middle fields which precede these reserved rightmost fields, using the same repeating behavior as in the one-colon case. Finally, the previously reserved rightmost fields are processed using the suffix clauses. The following special convenience functions are in scope of the clauses, effectively providing a shorthand for commonly-needed conversions:
    Provides conversion to integer. It is identical to the function, with the default radix. Converts a string value holding an octal representation to the integer which it denotes. It is equivalent to with a argument of 8. Converts a string value holding a hexadecimal representation to the integer which it denotes. It is equivalent to is equivalent to with a argument of 16. Converts a string value holding a binary (base two) representation to the integer which it denotes. It is equivalent to with a argument of 2. Converts a string value holding a C-language-style representation to the integer which it denotes, meaning that the prefix denotes a hexadecimal value, a leading zero octal, otherwise decimal. These prefixes follow the or sign, if present. The function is equivalent to invoked with a argument of Converts a string holding a floating-point representation to the floating-point value which it denotes. It is equivalent to Conversion similar to and but equivalent to using the functions and Thus fields which are non-numeric strings or the object get converted to 0, or 0.0 in the case of Performs no conversion: the corresponding field is taken as-is.

    Because macro destructively operates on the elements of the field list it has the same effect as an assignment to the fields: the value of is updated. The return value of is Note: because is when no fields have been extracted, a expression can be used as the condition in an clause which triggers the action if one or more fields have been extracted, and performs conversions on them. Note: although is intended for converting textual fields, and the semantic descriptions below consequently make references to string inputs, the behavior of with respect to non-string fields can be inferred. For instance if a field actually holds the floating-point value 3.14, and the conversion is applied to it, it will produce 3, because it works by means of the function. Note: a somewhat less flexible mechanism for converting fields, related to is present in the clause of the macro, which can specify names for the positional fields, along with conversion functions. The clause has different syntax, and doesn't support the (colon) separator, instead assuming a fixed number of fields enumerated from the left.
      ;; convert up to first three fields to integer:
      (awk ((fconv i i i)))
      ;; convert all fields to floating-point
      (awk ((fconv : r :)))
      ;; convert first and second fields to integer
      ;; from hexadecimal;
      ;; convert last field to integer from octal;
      ;; process pairs of fields in between
      ;; these by leaving the first element of
      ;; each pair unconverted and converting second
      ;; to floating-point;
      (awk ((fconv x x : - r : o)))
      ;; convert all fields, except the first,
      ;; from integer, turning empty strings
      ;; and non-integer junk as zero;
      ;; leave first field unconverted:
      (awk ((fconv - : iz))) These macros provide convenient redirection of output and input to and from files and commands. When at least one argument is present, the functions and evaluate each in a dynamic environment in which the variable is bound to a file output stream, for the first two functions, or output command pipe in the case of the last one. Similarly, when at least argument is present, the remaining functions and evaluate each in a dynamic environment in which is bound to a file input stream or input command pipe, respectively. The and arguments are treated as forms, and evaluated. They should evaluate to strings. The first evaluation of one of these macros for a given or being used in a particular direction (input or output) and type (file or command) creates a stream. That stream is then associated with the given or string, together with the direction and type. Upon a subsequent evaluation of one of these macros for the same or string, direction and type, a new stream is not opened; rather, the previously associated stream is used. The scope of these macros is the entire containing form; they may be used in the and clauses. The macro indicates that the file named is to be opened for writing and overwritten, or created if it doesn't exist. The macro indicates that the file named by is to be opened in append mode, created if necessary. The macro indicates that the file given by is to be opened for reading. The macro indicates that is to be opened as an output command pipe. The macro indicates that is to be opened as an input command pipe. If any of these macros is invoked without any arguments, then it yields the stream object associated with or argument, direction and type. If the association doesn't exist, the stream is first created. If arguments are present, then the value of the last one is yielded as a value, except in the case when the last form yields the keyword symbol. If the last yields the keyword symbol, the association between the or direction and type and the stream is removed, and the stream is closed. In this case, the result value of the macro isn't the symbol, but rather the return value of the call that is implicitly applied to the stream. Even if there is only one which yields the stream is created, if it doesn't exist prior to the macro invocation. In each invocation of these macros, after every is evaluated, the stream is implicitly flushed, if it is an output stream. The association between the or strings, direction and type is scoped to the innermost enclosing macro. An inner macro cannot refer to the associations established in an outer macro. An outer macro can obtain an association's stream object and communicate that stream to the nested macro where it can be used. When the surrounding macro terminates, all of the streams opened by these redirection macros are closed, without breaking those associations. If lexical closures are captured inside the macro, and then invoked after the macro has terminated, and inside those closures the redirection macros are used, those macro instances will with closed stream objects, and so attempts to perform I/O will fail. The following examples are macro equivalents of the examples of the POSIX utility given in IEEE Std 1003.1, 2013 Edition.

    1.  Print lines for which field 3 is greater than 5:
          ;; print lines with fields separated by ofs,
          ;; and [f 2] converted to integer:
          (awk ((and [f 2] (fconv - - iz) (> [f 2] 5))))
          ;; print strictly original lines from orec
          (awk ((and [f 2] (fconv - - iz) (> [f 2] 5))
                (prn orec)))

    2.  Print every tenth line:
          (awk ((zerop (mod nr 10))))

    3.  Print any line with a substring matching a regex:
          (awk (#/(G|D)(2\d[\w]*)/)) Note the subtle flaw here: the portion of the regular expression contributes nothing to what lines are matched. The following example has a similar flaw.

    4.  Print any line with a substring beginning with a or followed by a sequence of digits and characters:
          (awk (#/(G|D)([\d\w]*)/))

    5.  Print lines where the second field matches a regex, while the fourth one doesn't:
          (awk (:let (r #/xyz/))
               ((and [f 3] [r [f 1]] (not [r [f 3]]))))

    6.  Print lines containing a backslash in the second field:
          (awk ((find #\\ [f 1])))

    7.  Print lines containing a backslash using a regex constructed from a string. Note that backslash escapes are interpreted twice: once in the string literal, and once in the parsing of the regex, requiring four backslashes to encode one:
          (awk (:let (r (regex-compile "\\\\")))
               ((and [f 1] [r [f 1]])))

    8.  Print penultimate and ultimate field in each record, separating then by a colon:
          ;; original: {OFS=":";print $(NF-1), $NF}
          ;;
          (awk (t (set ofs ":") (prn [f -2] [f -1])))

        Note that the above behaves more correctly than the original Awk example because in the when there is only one field, reduces to which refers to the entire record, not to the field. This sort of bug is why the TXR Lisp does not imitate the design decision to make the record the first numbered field.

    9.  Output the line number and number of fields separated by colon, by producing a single string first:
          (awk (t (prn `@nr:@nf`)))

    10. Print lines longer than 72 characters:
          (awk ((> (len rec) 72)))

    11. Print first two fields in reverse order, separated by
          (awk (t (prn [f 1] [f 0])))

    12. Same as 11, but with field separation consisting of a comma, or spaces and tabs, or both in sequence:
          (awk (:set fs #/,[ \t]*|[ \t]+/)
               (t (prn [f 1] [f 0])))

    13. Add the values in the first column, then print sum and average:
          ;; original:
          ;; {s += $1}
          ;; END {print "sum is ", s, " average is", s/NR}
          ;;
          (awk (:let (s 0) (n 0))
               ([f 0] (fconv r) (inc s [f 0]) (inc n))
               (:end (prn `sum is @s average is @(/ s n)`))) Note that the original is not robust against blank lines in the input. Blank lines are treated as if they had a first column field of zero, and are counted toward the denominator in the calculation of the average.

    14. Print fields in reverse order, one per line:
          (awk (t (tprint (reverse f))))

    15. Print all lines between occurrences of and
          (awk ((rng #/start/ #/stop/)))

    16. Print lines whose first field is different from the corresponding field in the previous line:
          (awk (:let prev)
               ((nequal [f 0] prev) (prn) (set prev [f 0])))

    17. Simulate the utility:
          (awk (:begin (prn `@{*args* " "}`))) Note: if this is evaluated in the command line, for instance with the option, an explicit exit is required to prevent the arguments from being processed by TXR after completes:
          (awk (:begin (prn `@{*args* " "}`) (exit 0)))

    18. Print the components of the environment variable, one per line:
          ;; Process variable as if it were a file:
          (awk (:inputs (make-string-input-stream
                          (getenv "PATH")))
               (:set fs ":")
               (t (tprint f)))
          ;; Just get, split and print; awk macro is irrelevant
          (awk (:begin (tprint (split-str (getenv "PATH") ":"))))

    19. Given a file called which contains page headers of the format and a TXR Lisp file called which contains:
          (awk (:let (n (toint n)))
               (#/Page/ (set [f 1] (pinc n)))
               (t)) the command line:
          txr -Dn=5 prog.tl input prints the file, filling in page numbers starting at 5.
Note that environment variable names, their values, and command-line arguments are all regarded as being externally encoded in UTF-8. TXR performs the encoding and decoding automatically. The variable holds the original, complete list of arguments passed from the operating system, including the program executable name. During command-line-option processing, TXR may transform the argument list. The hash-bang mechanism, and the and options can inject new command-line arguments, as can code which is executed during argument processing via the options and others. The variable holds the list of effective arguments, which is the argument list after these transformations are applied. This variable is established and set to the same value as prior to command-line processing, but is not updated with its final value until after command-line processing. The variable holds a list of strings representing the remaining arguments which follow any options processed by the TXR executable, and the script name. This list is a suffix of Thus, the arguments before can be calculated using the expression The variable is available to TXR Lisp expressions invoked from the command line via the and other such options. During these evaluations, holds all the remaining options, after the invoking option and its argument expression. In other words, code executed from the command line has access to the remaining arguments which follow it. Furthermore, this code may modify the value of Such a modification is visible to the option processing code. That is to say code executed from the command line can rewrite the remaining list of arguments, and that list takes effect. The function retrieves the list of environment variables. Each variable is represented by a single entry in the list: a string which contains an (equal) character somewhere, separating the variable name from its value. Multiple calls to may return the same list, or lists which share structure. If a list returned by is modified, the behavior is unspecified. See also: the function. The function returns an hash whose keys and values are strings. The hash table is populated with the environment variables, represented as key-value character string pairs. The function allocates the hash table when it is first invoked; thereafter, it returns the same hash table. The hash table is updated by the functions and Note: calls to the underlying C library functions and and other direct manipulations of the environment, will not update the hash table. These functions provide access to, as well as manipulation of, environment variables. Of these three, and might not be available on some platforms, or might be be present in a simulated form which sets the variable to the empty string rather than deleting it. The function searches the environment for the environment variable whose name is If the variable is found, its value is returned. Otherwise is returned. The function creates or modifies the environment variable indicated by The string argument specifies the new value for the variable. If is then behaves like except that it observes the argument. That is to say, the meaning of a null is that the variable is to be removed. If the argument is specified, and is true, then the variable is overwritten if it already exists. If the argument is false, then the variable is not modified if it already exists. If the argument is not specified, it defaults to the value effectively giving rise to a two-argument form of which creates or overwrites environment variables. A variable removal is deemed to be an overwrite. Thus if both and are then does nothing. The function unconditionally returns regardless of whether or not it overwrites or removes an existing variable. The function removes the environment variable specified by if it exists. On some platforms, it instead sets the environment variable to the empty string. Note: supporting removal semantics in allows for the following simple save/modify/restore pattern:
  (let* ((old-val (getenv "SOME-VAR")))
    (unwind-protect
      (progn (setenv "SOME-VAR" new-val)
             ...)
      (setenv "SOME-VAR" old-val))) This works in the case when exists, as well as in the case that it doesn't exist. In both cases, its previous value or, respectively, non-existence, is restored by the cleanup form. These functions interact with the list returned by the function and with the hash table returned by the function as follows. A previously returned list returned by is not modified. The and functions may cause a subsequent call to to return a different list. The function has no effect on the list. The hash table previously returned by is modified by in the manner consistent with its semantics. A new entry is created in the table, if required, and an existing entry is overwritten only if the flag is specified. Likewise, if is invoked in a way that causes the environment variable to be deleted, it is removed from the hash also. The function causes the variable to be removed from the hash table also. The function accesses the underlying environment and updates the hash table with the name-value pair which is retrieved. The function replaces the environment with the environment variables specified in The argument is a list of character strings, in the same format as the list returned by the function: each element of the list describes an environment variable as a single character string in which the name is separated by the value by the character. As a special concession, if this character is missing, the function treats that entry as being a name with an empty value. The first empties the existing environment, rendering it devoid of environment variables. Then it installs the entries specified in The return value is Note: may be used to specify an exact environment to child programs executed by functions like or Note: the previous environment may be saved by calling and retaining the returned list. Then after modifying the environment, the original environment can be restored by passing that retained list to The variable specifies the list of command-line variables established for programs executed via the functions and The initial top-level value of this variable is the symbol which indicates that is to be ignored, such that the executed program inherits the current set of environment variables. If has any other value, it must be a possibly empty list of environment variables, in the same format as what is returned by function and accepted by That value completely specifies the environment that executed programs shall receive.
  (let ((*child-env* '("a=b")))
    ;; /usr/bin/env sees only "a" environment variable
    (get-lines (open-process "/usr/bin/env" "r")))
  -> ("a=b") TXR Lisp provides a support for recognizing, extracting and validating the POSIX-style options from a list of command-line arguments. The supported options can be defined as a list of option descriptor objects each of which is constructed by a call to the function. Each option can have a long name, a short name, a type, and a description. The function takes a list of option descriptors, and a list of arguments, producing a parse, or else throwing an exception of type if an error is detected. The returned object, an instance of struct type can then be queried for specific option values, or for the remaining non-option arguments. The function takes a list of option descriptors and an output stream, and generates help text on that stream. A program supporting a option can use this to generate that portion of its help text which describes the available options. Also provided are functions and which have the same interface as and print additional information. These may be used together with to provide more detailed help under a single option, or under separate options like The macro provides a more streamlined, declarative mechanism built on the same facility. The options are declared in a more condensed way, and using symbols instead of strings. Furthermore, the parsed option values become slot values of an object, named by the same symbols. A command-line option can have a short or long name. A short name is always one character long, and treated specially in the command-line syntax. Long options have names two or more characters long. An option can have both a long and short name. Options may not begin with the (ASCII dash) character. A long option may not contain the character. Short options are invoked by specifying an argument with a single leading followed by the option character. Multiple short options which take no argument can be "clumped": combined into a single argument consisting of a single followed by multiple short option characters. An option can take an argument, in which case the argument is required. An option which takes no argument is Boolean, and a Boolean option never takes an argument: "takes no argument" and "Boolean" effectively mean the same thing. Long options are invoked as an argument which begins with a (double dash) immediately followed by the name. When a long option takes an argument, it is mandatory. It must be specified in the same argument, separated from the name by the character. If that is omitted, then the next command-line argument is taken as the argument. That argument is removed, and not recognized as an option, even if it looks like one. A Boolean long option can be explicitly specified as false using the prefix rather than the prefix. Short options may be invoked using long name syntax; if is a short option, then it may be referenced on the command line as and treated as a long option in all other ways, including the use of to explicitly specify false for a Boolean option. If a short option takes an argument, it may not clump with other short option. The following command-line argument is taken as the options argument. That argument is removed and is not recognized as an option even if it looks like one. If the command-line argument occurs in the command line where an option would otherwise be recognized, it signifies the end of the options. The subsequent arguments are the non-option arguments, even if they resemble options. The following example illustrates a complete TXR Lisp program which parses command-line options:
  (defvarl options
    (list (opt "v" "verbose" :dec
               "Verbosity level. Higher values produce more chatter.")
          (opt nil "help" :bool
               "List this help text.")
          (opt "x" nil :hex
               "The X factor: a number with a mysterious\ \
                interpretation, affecting the program\ \
                behavior in strange ways.")
          (opt "z" nil) ;; undocumented option
          (opt nil "cee" :cint
               "C style integer.")
          (opt "g" "gravity" :float
               "Gravitational constant. This gives\ \
                the gravitational field\ \
                strength at the Earth's surface.")
          (opt "l" "lit" :str
               "A character string given in TXR Lisp notation.")
          (opt "c" nil 'upcase-str
               "Custom treatment: ARG is converted to uppercase.")
          (opt "b" "bool" :bool
               "A flag you can flip true.")))
  (defvarl prog-name *load-path*)
  (let ((o (getopts options *args*)))
    (when [o "help"]
      (put-line "Usage:\n")
      (put-line ` @{prog-name} [options] arg*`)
      (opthelp options)
      (exit 0))
    (put-line `args after opts are: @{o.out-args ", "}`)) The next example is equivalent to the previous, but using the macro:
  (define-option-struct prog-opts nil
    (v verbose :dec
                 "Verbosity level. Higher values produce more chatter.")
    (nil help :bool
                 "List this help text.")
    (nil extra-help
                 :bool
                 "List help text with more detailed information.")
    (x nil :hex
                 "The X factor: a number with a mysterious\ \
                  interpretation, affecting the program\ \
                  behavior in strange ways.")
    ;; undocumented Boolean:
    (z nil)
    (nil cee :cint
                 "C style integer.")
    (g gravity :float
                 "Gravitational constant. This gives\ \
                  the gravitational field\ \
                  strength at the Earth's surface.")
    (l lit :str
                 "A character string given in TXR Lisp notation.")
    (c nil upcase-str
                 "Custom treatment: ARG is converted to uppercase.")
    (b bool :bool
                 "A flag you can flip true."))
  (defvarl prog-name *load-path*)
  (let ((o (new prog-opts)))
    o.(getopts *args*)
    (when (or o.help o.extra-help)
      (put-line "Usage:\n")
      (put-line ` @{prog-name} [options] arg*`)
      o.(opthelp)
      (when o.extra-help
        o.(opthelp-types)
        o.(opthelp-conventions))
      (exit -1))
    (put-line `args after opts are: @{o.out-args ", "}`)) The structure describes a single command-line option. The and slots are either or else hold strings. The slot gives the option's short name: a one-character-long string which may not be the ASCII dash character The slot gives the option's long name: a string two or more characters long which doesn't begin with a dash. An option must have at least one of these names. The slot provides a descriptive string. This string may be long. The function displays this text, formatting into multiple lines as necessary. If is the option is considered undocumented. The slot may be a symbol naming a global function which takes one argument, or it may be such a function object. Otherwise it must be one of the following keyword symbols:
    This indicates that the type of the option is Boolean. Such an option doesn't take any argument. Its value is or This indicates that the option requires an argument, which is a decimal integer with an optional positive or negative sign. This argument is converted to an integer object. This type indicates that the option requires an argument consisting of a hexadecimal integer with an optional positive or negative sign. This is converted to an integer object. This type indicates that the option requires an argument consisting of a octal integer with an optional positive or negative sign. This is converted to an integer object. This type indicates that the option requires an integer argument whose format conforms to one of three C language conventions in most respects, other than that this integer may have an arbitrary range. All forms may carry an optional positive or negative leading sign at the very beginning. The first convention consists of decimal digits, which must not have a superfluous leading zero. The second convention consists of octal digits which are introduced by an extra leading zero. The third convention consists of hexadecimal digits introduced by the prefix. This type indicates a decimal floating-point argument, which is converted to a floating-point number. Its basic form is: an optional leading plus or minus sign, followed by a sequence of one or more digits which may contain a single decimal point anywhere, including the very beginning of the sequence or at the end, optionally followed by the letter or followed by a decimal integer which may have a leading positive or negative sign, and include leading zeros. This type indicates a simple textual argument. The argument is taken as verbatim UTF-8 text, converted to a string without interpreting the characters in any special way. This type indicates that the argument consists of the interior notation of a TXR Lisp character string. It is processed by adding a double quote at the beginning or end, and parsed as a string literal. This parsing must successfully yield a string object, otherwise the argument is ill-formed. If the type is specified as a compound form headed by the symbol, it indicates that the command-line option's argument is a list of elements. The argument appears on the command line as a single string contained within one argument. It may contain commas, and is split into pieces using the comma character as a separator. The pieces are then individually treated as of type and converted accordingly. The option's argument is then a list object whose elements are the converted pieces. For instance will convert a list of comma-separated decimal integer tokens into a list of integer objects. The argument must be a basic type other than The option type does not nest. If the type is specified as a compound form headed by the symbol, it indicates that if the option is specified multiple times, the values coming from the multiple occurrences are accumulated into a list. The argument must be a type or a basic type other than for example and This type specifier does not nest: combinations such as and are invalid. The option values are accumulated in reverse order, so that the rightmost repetition becomes the first item in the list. For instance, if the option has type and the arguments presented for parsing are then the option's value will be If a option is cumulative, then the option value will be a list of lists. Each repetition of the option produces a list, and the lists are accumulated.

    If is a function, then the option requires an argument. The argument string is passed to the function, and the value is whatever the function returns. The structure may have additional slots which are not specified. The convenience function is provided for constructing objects. The function provides a slightly condensed syntax for constructing an object of type The required arguments and are strings, corresponding to slots of the same name. The optional parameter corresponds to the same-named slot and defaults to The optional parameter corresponds to the same-named slot and defaults to (no help text provided for the option). The function follows this equivalence:
      (opt a b c d) <--> (new opt-desc short a long b
                            type c helptext d) The structure represents a parsed command line, containing decoded information obtained from the options and an indication of where the non-option arguments start. The structure supports direct indexing for option retrieval. That is the only documented interface for accessing the parsed options; the implementation of the information set describing the parsed options is unspecified. The slot holds the original argument list. The slot holds the tail of the argument list consisting of the non-option arguments. The mechanism by means of which is calculated, and by means of which the information about the options is populated, is unspecified. The only interface to that mechanism is the function. The object supports indexing, including indexed assignment. If is an instance of returned by then the expression tests whether the option is available in that is, whether it has been specified in the command line. If so, then its associated value is returned, otherwise is returned. This is ambiguous: for a Boolean option it indicates that either the option was not specified, or that it was explicitly specified as false. For a Boolean option that was specified (positively), the value is returned. The expression yields the value of option if that option has been specified. If the option hasn't been specified, then the expression yields the value Assigning to is possible. This replaces the value associated with option The assignment is erroneous if no such option was parsed from the command line, even if it is a valid option. If an option is defined with both a long form and a short form, and either form of that option occurs in the command line being processed, then the option appears under both names in the index. For instance if option has the short form and either option occurs, then both the keys and will exist in the structure returned by Note that this behavior is different from that of the structure produced macro. Under that approach, if an option is defined with a long and short name, the structure will have only a single slot for that option, named after the long name. The function takes a list of structures and a list of strings representing command-line arguments. The is parsed. If the parse is unsuccessful, an exception of type is thrown, derived from If there are problems in itself, then an exception of type is thrown. If the parse is successful, returns an instance of the structure describing the parsed options and listing the non-option arguments. The function processes the list of structures and compiles a customized body of help describing all of the options which have help text. These are presented in alphabetical order. Options which do not have help text, if any, are simply listed together under a heading which indicates their undocumented status. The text is formatted to fit within 79 columns, and begins and ends with a blank line. Its format consists of headings which begin in the first column, and paragraphs and tables which feature a two space left margin. A blank line follows each section heading. The heading begins with a capital letter. Its remaining words are uncapitalized, and it ends with a colon. The text is sent to if specified. This argument defaults to If there are problems in itself, then an exception of type is thrown. The supplementary help function processes the considering only those options which are documented. If any of them have typed arguments, then a legend is printed explaining the types. The legend includes only information about those option argument types which appear in The supplementary help function processes considering only those options which are documented. It prints a guide to the use of options, which includes information only about the kinds of options actually present in The macro defines a struct type, instances of which provide command-line option parsing. The and parameters are subject to the same requirements and have the same semantics as the same-named parameters of The arguments are lists of between two and four elements: The and must be symbols suitable for use as slot names. One of them may be specified as indicating that the option has no long form, or no short form. If a specifies both a and a then only a slot named by shall exist in the structure. The struct type defined by has four methods: and It also has two slots: and which function in a manner identical to their same-named counterparts in the class. The method takes a single argument: the argument list to be processed. When the argument list is successfully processed. The and methods take an optional stream argument. Note: to encode the option names or or option names which clash with the slot names and or the method names such as or symbols with these names from a package other than must be used. The function retrieves the current value of the C library error variable If the argument is present and is not then it specifies a value which is stored into The value returned is the prior value. The place form of does not take an argument. The returns a character string which provides the host platform's description of the integer obtained from the function. If the host platform fails to provide a description, the function returns The function terminates the entire process (running TXR image), specifying the termination status to the operating system. Values of the optional parameter may be or an integer value. The value indicates an unsuccessful termination status, whereas indicates a successful termination status. An absence of the argument also specifies a successful termination status. If is an integer value, it specifies a successful termination if it is otherwise the interpretation of the value is platform-specific. These variables correspond to the POSIX constants", namely and so forth. Variables corresponding to all of the constants from the Issue 6 2004 edition of POSIX are included. The variables and from Issue 7 2018 are subject to the availability of the corresponding constants in the host platform. The function terminates the entire process (running TXR image), specifying an abnormal termination status to the process. Note: calls the C library function which works by raising the signal, known in TXR as the variable. Abnormal termination of the process is this signal's default action. The function registers to be called when the process terminates normally. Multiple functions can be registered, and the same function can be registered more than once. The registered functions are called in reverse order of their registrations. The function removes all previous registrations of The function returns The function returns if it removed anything, if no registrations of were found. The function suspends the execution of the program for at least microseconds. The return value is if the sleep was successfully executed. A value indicates premature wakeup or complete failure. Note: the actual sleep resolution is not guaranteed, and depends on granularity of the system timer. Actual sleep times may be rounded up to the nearest 10 millisecond multiple on a system where timed suspensions are triggered by a 100 Hz tick. tries to create the directory named using the POSIX function. An exception of type is thrown if the function fails. Returns on success. The argument specifies the request numeric permissions for the newly created directory. If omitted, the requested permissions are (511): readable and writable to everyone. The requested permissions are subject to the system The function also creates a directory named Unlike it also attempt to create all the necessary parent directories, and does not throw an error if refers to an existing object, if that object is a directory or a symbolic link to a directory. Rather, in that case it returns instead of changes the current working directory to the object specified by the argument, and returns or else throws an exception of type If the argument is a string, it is interpreted as a in which case the POSIX function is used. If the argument is a then an integer file descriptor is retrieved from that stream using the function. That descriptor can be specified directly as a argument. In the case of these these two argument types, the function is used. The function retrieves the current working directory. If the underlying C library function fails with an other than an exception will be thrown. The function removes the directory named by If successful, it returns otherwise it throws an exception of type Note: calls the same-named POSIX function, which requires to be the name of an empty directory. The function tries to remove the filesystem object named by which may be a file, directory or something else. If successful, it returns The optional Boolean parameter which defaults to A failure to remove the object results in an exception of type being thrown, unless the failure reason is that the object indicated by doesn't exist. In this non-existence case, the behavior is controlled by the argument. If that argument is true, the exception is thrown. Otherwise, the function returns normally, producing the value to indicate that it didn't perform a removal. The function tries to rename filesystem path which may refer to a file, directory or something else, to the path If successful, it returns A failure results in an exception of type The function executes using the system command interpreter. The run function spawns a searching for it using the system PATH. Using either method, the executed process receives environment variables from the parent. TXR blocks until the process finishes executing. If the program terminates normally, then its integer exit status is returned. The value zero indicates successful termination. The return value indicates an abnormal termination, or the inability to run the process at all. In the case of the function, if the child process is created successfully but the program cannot be executed, then the exit status will be an value from the failed attempt. The standard input, output and error file descriptors of an executed command are obtained from the streams stored in the and special variables, respectively. For a detailed description of the behavior and restrictions, see the function, whose description of this mechanism applies to the and function also. Note: as of TXR 120, the function is implemented using and not by means of the C library function, as previously. The function is used to invoke the system interpreter by name. On Unix-like systems, the string is assumed to denote the system interpreter, which is expected to support a pair of arguments to specify the command to be executed. On MS Windows, the interpreter is assumed to be the relative pathname and expected to support as a way of specifying a command to execute. The functions and transform the argument string for safe insertion into commands. These functions are intended for use on POSIX systems, where the command interpreter used by the functions and and related functions is the POSIX Shell Command Language. The function adds quoting and escaping into its argument in such a way that the resulting string may be inserted as an argument into a command. The function performs a stricter escaping and quoting, such that the transformed string may be inserted into any syntactic context where a textual operand is required for any reason, such as the in the construct. The function escapes its argument for insertion into a double-quoted field in a shell command line. It does not add the double quotes themselves. The function escapes its argument for insertion into a single-quoted field in a shell command line. It does not add the single quotes themselves. The precise set of characters which, according to the function, require escaping or quoting, is the following:
      | & ; < > ( ) $ ` \ " ' tab newline space * ? [ # ~ If none of these characters occur in then returns The function considers all the above characters, and also these:
      = % The function escapes the following characters by preceding them with the \ (backslash) character:
     $ ` \ " The function replaces every occurrence of the character (single quote, apostrophe) with the sequence (single quote, backslash, single quotes, single quote). This sequence has the effect of terminating the enclosing single-quoted field, then producing a single quote via a backslash escape, and then opening a single-quoted field. The structure defines the type of object which is returned by the and functions. Except for and the slots are the direct counterparts of the members of POSIX C structure For instance the slot corresponds to The slot is set by the functions and Its value is when the path is not available. The and fields give the fractional parts of and respectively. They are derived from the newer style information in which the POSIX function provides the timestamps in format. If that is not available from the platform, these fields take on values of zero. The function retrieves information about a filesystem object whose pathname is given by the string argument or else about a system object associated with the open stream or one associated with the integer file descriptor If a is specified, that stream must be of a kind from which the function can retrieve a file descriptor, otherwise an exception of type is thrown. If the object is not found or cannot be accessed, an exception is thrown. Otherwise, if the argument is missing, information is retrieved and returned, in the form of a new structure of type If the argument is present, it must be either: an instance of the structure type, or of a type derived from that type by inheritance, or else structure type which has all the same slots as the type. The retrieved information is stored into and that object is returned rather than a new object. If refers to a symbolic link, the function retrieves information about the target of the link, if it exists, or else throws an exception of type The function behaves the same as on objects which are not symbolic links. For a symbolic link, it retrieves information about the link itself, rather than its target. The slot of the returned structure holds a copy of their argument value. When information is retrieved using a or argument, this slot is The function is an alias for Note: until TXR 231, and were distinct functions: accepted only arguments, whereas function accepted only or arguments. The following variables exist, having integer values. These are bitmasks which can be applied against the value given by the slot of the structure returned by the function and These variables correspond to the C language constants from POSIX: and so forth. The function can be used to test these against values of mode. For example tests for the group read permission. The function provides access to the Unix C library function of the same name, which controls which permissions are denied when files are newly created. If is called with no argument, it returns the current value of the mask. If the argument is present, it must be an integer specifying the new mask to be installed. The previous mask is returned. If is absent, then returns the previous mask. Note: the value of the argument may be calculated as a bitwise or of the following constants: and which correspond to the POSIX C constants and Implementation note: since the C library function provides no way to retrieve the current mask without overwriting with a new one, the TXR function, when given no argument, simulates the pure retrieval of the mask by calling the C function with an argument of to temporarily install the maximally safe mask. The value returned is then reinstated as the mask by another call to and that value is also returned. The parameters and are all integers. The function constructs a combined device number from a minor and major pair (by calling the Unix function). This device number is suitable as an argument to the function (see below). Device numbers also appear as values of the slot of the structure. The and functions extract the minor and major device number from a combined device number. The function changes the permissions of the filesystem object specified by It is implemented in terms of the POSIX functions and If is a character string representing a symbolic mode, then the function also makes use of or and The permissions are specified by which must be an integer or a string. An integer is a bitwise combination of permission mode bits. The value is passed directly to the POSIX or function. Note: to construct a mode value, applications may use to combine the values of the variables like or or take advantage of the well-known numeric structure of POSIX permissions to express them octal in octal notation. For instance the mode denotes that the owner has read, write and execute permissions, the group owner has read and execute, others have no permission. This value may also be calculated using If the argument to is a string, it is interpreted according to the symbolic syntax of the POSIX utility. For instance, a value of means to give all users (owner, group and others) write permission, and remove the setuid and setgid bits. The full syntax and semantics of symbolic strings is given in the POSIX standard IEEE 1003.1. The function throws a exception if an error occurs, otherwise it returns The argument may be a character string, in which case it specifies a pathname in the filesystem. In this case, the POSIX function is invoked. The argument may also be an integer file descriptor, or a stream. In these two cases, the POSIX function is invoked. For a stream the integer file descriptor is retrieved from the stream using function.
      ;; Set permissions of foo.txt to "rw-r--r--"
      ;; (owner can read and write; group owner
      ;; and other users can only read).
      ;; numerically:
      (chmod "foo.txt" #o644)
      ;; symbolically:
      (chmod "foo.txt" (logior s-irusr s-iwusr
                            s-irgrp
                            s-iroth)) Implementation note: The implementation of the symbolic processing is based on the descriptions given in IEEE 1003.1-2018, Issue 7 and also on the program from from GNU Coreutils 8.28: and experiments with its behavior, and its documentation. The and functions change the user and group ownership of the filesystem object specified by They implemented in terms of the POSIX functions and The ownership attributes are specified by and both integer arguments. The existing ownership attributes may be obtained using the function. These functions throw a exception if an error occurs, otherwise they returns The argument may be a character string, in which case it specifies a pathname in the filesystem. In this case, the same-named POSIX function is invoked by whereas likewise invokes its respective same-named POSIX counterpart. The difference is that if is a pathname denoting a symbolic link, then operates on the symbolic link, whereas dereferences the symbolic link. The argument may also be an integer file descriptor, or a stream. In these two cases, the POSIX function is invoked by either function. For a stream the integer file descriptor is retrieved from the stream using function. Note: in most POSIX systems, unprivileged processes may not change the user ownership denoted by They may change the group ownership indicated in if that value corresponds to the effective group ID of the calling process or one of its ancillary group IDs. To avoid trying to change the user ownership (and therefore failing), the caller should specify a value which matches the object's existing owner. The functions and change the access and modification timestamps of a file indicated by the argument. The difference between the two functions is that if is the pathname of a symbolic link, then operates on the symbolic link itself, whereas resolves the symbolic link. Note: the full, complete functionality of these functions requires the platform to provide the POSIX functions and functions. If these functions are not available, then other functions are relied on, with some reductions in functionality, that are documented below. The argument specifies the file to operate on. It may be an integer file descriptor, an open stream, or a character string representing a pathname. The and parameters specify the whole seconds part of the new access and modification times, expressed as seconds since the epoch. The and parameters specify the fractional part of the access and modification times, expressed in nanoseconds. If an integer argument is given to these parameters, it must lie in the range 0 to 999999999, or else the symbols or may be passed as arguments. If the symbol is passed as the nanoseconds part of the access or modification time, then the access or modification time, respectively, shall not be modified by the operation. The corresponding seconds argument is ignored. If the symbol is passed as the nanoseconds part of the access or modification time, then the access or modification time, respectively, shall be obtained from the current system time. The corresponding seconds argument is ignored. If the and functions are not available from the host system, then the above and convention in the nanoseconds arguments is not supported; the function will fail by throwing an exception if an attempt is made to pass these arguments. If the and functions are not available from the host system, then operating on a symbolic link with is only possible if the system provides the C library function, otherwise the operation fails by throwing an exception (if given a path argument for even if that path isn't a symbolic link). If the implementation falls back on the and functions, then the nanoseconds arguments are truncated to microsecond precision. If the implementation falls back on then the nanoseconds arguments are ignored; the times are effectively truncated to whole seconds. The function tries to create an entry in the filesystem: a file, FIFO, or a device special file, under the name If it is successful, it returns otherwise it throws an exception of type The argument is a bitwise or combination of the requested permissions, and the type of object to create: one of the constants or The permissions are subject to the system If a block or character special device or is being created, then the argument specifies the major and minor numbers of the device. A suitable value can be constructed from a major and minor pair using the function.
       ;; make a character device (8, 3) called /dev/foo
       ;; requesting rwx------ permissions
       (mknod "dev/foo" (logior #o700 s-ifchr) (makedev 8 3)) The function creates a POSIX FIFO object. If it is successful, it returns otherwise it throws an exception of type The argument is a bitwise or combination of the requested permissions, and is subject to the system Note: the function can also create FIFOs, specified via the bitwise combination of the type and the permission mode bits. The function creates a symbolic link called whose contents are the absolute or relative path does not actually have to exist. The function creates a hard link. The object at is installed into the filesystem at also. The function is like except that if is a symbolic link, it is resolved, and the link is made to the resulting object. On Linux, and some other platforms will create a hard link to the symbolic link. The behavior is not specified by POSIX. If these functions succeed, they return Otherwise they throw an exception of type If names a filesystem object which is a symbolic link, the function reads the contents of that symbolic link and returns it as a string. Otherwise, it fails by throwing an exception of type The function provides access to the same-named POSIX function. It processes the input string by expanding all symbolic links, removes all superfluous and path components, and extra component-separating slash characters, to produce a canonical absolute pathname. If the underlying POSIX function indicates failure, then is returned. In that situation the value is available using the function. Functions in this category are complex functionality implemented using a combination of multiple calls into the host system's POSIX API. The function creates a replica of the file at the destination path Both paths are opened using in binary mode, as if using and respectively. Then bytes are read from one stream and written to the other, in blocks which whose size is a power of two at least as large as 16834. If the optional Boolean parameter is specified, and is true, then the permissions of are propagated to If the optional Boolean parameter is specified, and is true, then the access and modification timestamps of are propagated to The function returns if it is successful, and throws an exception derived from on failure. The function copies multiple files, whose pathnames are given by the list argument into the target directory whose path is given by The target directory must exist. For each source path in the function forms a target path by combining the base name of the source path with (See the and functions). Then, the source path is copied to the resulting target path, as if by the function. The function returns if it is successful, and throws an exception derived from on failure. Additionally, provides an internal catch for the and restart exceptions. If the caller, using a handler frame established by catches an error emanating from the function, it can retry the failed operation by throwing the exception, or continue copying with the next file by throwing the exception.
      ;; Copy all "/mnt/cdrom/*.jpg" files into "images" directory,
      ;; preserving their time stamps,
      ;; continuing the operation in the face of
      ;; file-error exceptions.
      (handle
        (copy-files (glob "/mnt/cdrom/*.jpg") "images" nil t)
        (file-error (throw 'skip))) The function catenates the contents of zero or more files into one file. The destination path is specified by Regardless of whether there are any arguments, the file named by is created, if necessary or else truncated to zero length. Then, the files named by each are traversed in left-to-right order; the contents of each file is appended to the destination file. The function replicates a file system object identified by the pathname creating a similar object named If is a directory, it is recursively traversed and its structure and content is replicated under The arguments are keywords, which may be the following:

    :perms
        Propagate the permissions of all objects under onto their counterparts. In the absence of this option, the copied objects receive the same permissions as a newly created files. On POSIX systems this means: readable and writable to the owner, group and others, by default, subject to the that is in effect.

    :times
        Propagate the modification and access time stamps of all objects under onto their counterparts.

    :symlinks
        Copy symbolic links literally rather than dereferencing them. Symbolic links are not altered in any way; their exact content is preserved. Thus, relative symlinks which point outside of the tree may turn into dangling symlinks in the tree.

    :owner
        Propagate the ownership of all objects under to their counterparts. Ownership refers to the owner user ID and group ID. Without this option, the ownership of the copied objects is derived from the effective user ID and group ID of the calling process. Note that it is assumed that the host system may requires superuser privileges to set both ownerships IDs of an object, and to set them to an arbitrary value. An unprivileged process may not change the user ID of a file, and may only change the group ID of a file which they own, to one of the groups of which that process is a member, either via the effective GID, or the ancillary list. The function tests whether the application is running under superuser privileges; if not, then it only honors the option for those objects under which are owned by the caller, and owned by a group to which the caller belongs. Other objects are copied as if the option were not in effect, avoiding an attempt to set their ownership that is likely to fail.

    :all
        The keyword is a shorthand representing all of the options being applied: permissions, times, symlinks and ownership are replicated.

    The function creates all necessary pathname components required for to come into existence, as if by using the function. Whenever an object under has a counterpart in which already exists, the situation is handled as follows:

    1.  If a directory object is copied to an existing directory object, then that existing directory object is accepted as the copy, and the operation continues recursively within that directory. If any options are specified, then the requested attributes are propagated to that existing directory.

    2.  If a non-directory object is copied to a directory object, the situation throws an exception: the function refuses to delete an entire directory or subdirectory in order to make way for a file, symbolic link, special device or any other kind of non-directory object.

    3.  If any object is copied to an existing non-directory object, that target object is removed first, then the copy operation proceeds.

    Copying of files takes place similarly as what is described for the function. Special objects such as FIFOs, character devices, block devices and sockets are copied by creating a new, similar objects at the destination path. In the case of devices, the major and minor numbers of the copy are derived from the original, so that the copy refers to the same device. However, the copy of a socket or a FIFO is effectively a new, different endpoint because these objects are identified by their pathname. Processes using the copy of a socket or a FIFO will not connect to processes which are working with the original. The function returns if it is successful. It throws an exception derived from when encountering failures. Additionally provides an internal catch for the and restart exceptions. If the caller, using a handler frame established by catches an error emanating from the function, it can retry the failed operation by throwing the exception, or continue copying with the next object by throwing the exception. The function attempts to remove the filesystem object named by If refers to a directory, that directory is recursively traversed to remove all of its contents, and is then removed. The function returns if it is successful. It throws an exception derived from when encountering failures. Additionally provides an internal catch for the and restart exceptions. If the caller, using a handler frame established by catches an error emanating from the function, it can retry the failed operation by throwing the exception, or continue removing other objects by throwing the exception. Skipping a failed remove operation may cause subsequent operations to fail. Notably, the failure to remove an item inside a directory means that removal of that directory itself will fail, and ultimately, will still exist when completes and returns. The and functions are recursive counterparts of and The filesystem object given by is recursively traversed, and each of its constituent objects is subject to a permission change in the case of or an ownership change in the case of The function alters the permission of each object that is not a symbolic link using the function, and is interpreted accordingly: it may be an integer or string. Each object which is a symbolic link is ignored. The function alters the permission of each object encountered, including symbolic links, using the function. These functions establish restart catches, similarly to and allowing the caller to retry individual failed operations or skip the objects on which operations have failed. The function updates the modification timestamp of the filesystem object named by If the object doesn't exist, it is created as a regular file. If is specified, then the modification timestamp of the object denoted by is updated to be equivalent to the modification timestamp of the object denoted by Otherwise being absent, the modification timestamp of is set to the current time. If is a symbolic link, it is dereferenced; operates on the target of the link. The function combines the which is a string, with a generated suffix to create a unique directory name. The directory is created, and the name is returned. If the argument ends in with a sequence of one or more characters, the behavior is unspecified. Note: this function is implemented using the same-named POSIX function. Whereas the POSIX function requires the template to end in a sequence of at least six characters, which are replaced by the generated suffix, the TXR Lisp function handles this detail internally, requiring only the prefix part without those characters. The function create a unique file name by adding a generated infix between the and strings. The file is created, and a stream open in mode for the file is returned. If either the or contain characters, the behavior is unspecified. If is omitted, it defaults to the empty string. The name of the file is available by interrogating the returned stream's property using the function Notes: this function is implemented using the POSIX function or, if available, using the function which is not standardized, but appears in the GNU C Library and some other systems. If is unavailable, then the suffix functionality is not available: the argument must either be omitted, or must be an empty string. Whereas the C library functions require the template to contain a sequence at least six characters, which are replaced by the generated portion, the TXR Lisp function handles this detail internally, requiring no such characters in any of its inputs. Functions in this category perform various tests on the attributes of filesystem objects. The functions all have a parameter, which accepts three types of arguments. If a character string is specified, it denotes a filesystem path to be probed for properties such as ownership and permissions. The object is probed using the function except in the case of which uses If instead a stream is specified as then the associated filesystem descriptor is probed for these properties. If an integer value is specified, it is treated as a POSIX open file descriptor that is to be probed. Otherwise, a structure, for example one returned by the or function may be specified, in which case no system object is probed. The properties to be tested are those given in the object. Note: in a situation when it is necessary to use any of these functions to probe the properties of a symbolic link itself (other than the function which does so implicitly) it is necessary to first invoke on the symlink's path, and then pass the resulting structure to that function instead of the path. Some of the accessibility tests (functions which determine whether the calling process has certain access rights) may not be perfectly accurate, since they are based strictly on portable information available via together with the basic, portable POSIX APIs for inquiring about security credentials, such as They ignoring any special permissions which may exist such as operating system and file system specific extended attributes (for example, file immutability connected to a "secure level" and such) and special process capabilities not reflected in the basic credentials. With the exception of two functions, the accessibility tests use the real credentials of the caller, rather than the effective credentials. Thus, in a setuid process, where the real and effective privileges are different, the access tests inquire about whether the real user has the given access, not the effective user. In this aspect, the functions are similar to the POSIX function which also uses real credentials. The functions and use effective credentials, because they answer a different question: can the given filesystem object be trusted? The trust has to be determined from the point of view of the effective user, because security-sensitive actions are being performed in their context; and the effective user does not trust the real user. The function returns if is a string which resolves to a filesystem object. Otherwise it returns If the names a dangling symbolic link, it is considered nonexistent. If is an object returned by or unconditionally returns tests whether exists and is a regular file. tests whether exists and is a directory. tests whether exists and is a symbolic link. Similarly, tests for a block device, for a character device, for a socket and for a named pipe. The function returns if is an empty directory. Implementation note: this function performs a test similar to then, if it is confirmed that is a directory, a directory stream is opened and entries are read. If an entry is seen which has a name other than or then it is concluded that the directory is not empty and is returned. If no such entry is seen, then the directory is deemed empty and is returned. tests whether exists and has the set-group-ID permission set. tests whether exists and has the set-user-ID permission set. tests whether exists and has the "sticky" permission bit set. tests whether exists, and is effectively owned by the calling process; that is, it has a user ID equal to the real user ID of the process. tests whether exists, and is effectively owned by a group to which the calling process belongs. This means that the group owner is either the same as the real group ID of the calling process, or else is among the supplementary group IDs of the calling process. tests whether the calling process can read the object named by If necessary, this test examines the real user ID of the calling process, the real group ID, and the list of supplementary groups. tests whether the calling process can write the object named by If necessary, this test examines the real user ID of the calling process, the real group ID, and the list of supplementary groups. tests whether the calling process can both read and write the object named by If necessary, this test examines the real user ID of the calling process, the real group ID, and the list of supplementary groups. tests whether the calling process can execute the object named by or perform a search (name lookup, not implying sequential readability) on it, if it is a directory. If necessary, this test examines the real user ID of the calling process, the real group ID, and the list of supplementary groups. The and functions report whether the calling process can rely on the object indicated by to be, respectively, private or strictly private to the security context implied by its effective user ID. "Private" means that beside the effective user ID of the calling process and the superuser, no other user ID has write access to the object, and thus its contents may be trusted to be be free from tampering by any other user. "Strictly private" means that not only is the object private, as above, but users other than the effective user ID of the calling process and superuser also not not have read access. The rules which the function applies are as follows: A file to be examined is initially assumed to be strictly private. If the file is not owned by the effective user ID of the caller, or else by the superuser, then it is not private. If the file grants write permission to "others", then it is not private. If the file grants read permission to "others", then it is not strictly private. If the file grants write permission to the group owner, then it is not private if the group contains names other than that of the file owner or the superuser. If the file grants read permission to the group owner, then it is not strictly private if the group contains names other than that of the file owner or the superuser. Note that this interpretation of "private" and "strictly private" is vulnerable to the following time-of-check to time-of-use race condition with regard to the group check. At the time of the check, the group might be empty or contain only the caller as a member. But by the time the file is subsequently accessed, the group might have been innocently extended by the system administrator to include additional users, who can maliciously modify the file. Another issue is that if any components of can be subverted by another user, test may not be trusted. It becomes vulnerable to a time-of-check to time-of-use race condition. The function function is provided to perform a security check on an entire path. On Unix platforms, the performs a security check on an entire relative or absolute returning if the entire path is examined without encountering an error, and the check passes, otherwise On native Microsoft Windows, the function unconditionally returns true. An exception may be thrown if an an inaccessible or nonexistent path component is encountered, too many symbolic links have to be resolved or there is some other problem preventing the traversal of The objective of this function is to determine that every portion of is writable only to the effective user: that if the path is used for filesystem access, its meaning cannot be altered by an adversarial user who is able to control a symbolic link or a directory component. The function expands symbolic links on its own, one level at a time, and walks the components coming from a link target. Note: directories which are owned by root, and have the sticky bit, as is the usual configuration of are considered safe, even though multiple users have write permissions. The function compares two paths or stat results by modification time. It returns if exists, and either does not exist, or has a modification time stamp in the past relative to The function is equivalent to with the arguments reversed. Note: takes advantage of subsecond timestamp resolution information, if available. The implementation is based on using the field of the structure, if it isn't The function returns if and resolve to the same filesystem object: the same inode number on the same device. The function searches for the existence of a filesystem object named by in the directories specified If is the empty string or one of the two strings (dot) or (dotdot), then is returned. If contains any path separator characters (any of the set of characters found in the string) then the function returns without performing any search. In all these trivial cases, the argument is ignored. The argument, if present, may be a string or a list of strings. If omitted, then it takes on the value of the environment variable if that variable exists, or else takes on the value indicating an empty search path. If is a string, it is converted to a list of directories by splitting on the separator character, which may be (colon) or (semicolon) depending on the system. Then, for each directory in the list, affixes the to that component, as if using the function, and tests whether the resulting path refers to an existing filesystem object. If so, then the search terminates and that resulting path is returned. If the entire list is traversed without finding a filesystem object, then is returned. If any error whatsoever occurs while determining whether the resulting path exists, the situation is treated as nonexistence, and the search continues. Note: subtle discrepancies may exist between and the host platform's mechanisms for searching for an executable program. For instance, since is interested in existence only, it may return a path which exists, but is not executable. Whereas a path searching implementation which tests for executability will in that case continue searching, and not return that path. These functions directly correspond to the POSIX C library functions of the same name. They retrieve the real user ID, effective user ID, real group ID and effective group ID, respectively, of the calling process. These functions directly correspond to the POSIX C library functions of the same name. They set the real user ID, effective user ID, real group ID and effective group ID, respectively, of the calling process. On success, they return On failure, they throw an exception of type The function retrieves the list of supplementary group IDs of the calling process by calling the same-named POSIX C library function. Whether or not the effective group ID retrieved by is included in this list is system-dependent. Programs should not depend on its presence or absence. The function corresponds to a C library function found in some Unix operating systems, complementary to the function. The argument to must be a list of numeric group IDs. If the function is successful, this list is installed as the list of supplementary group IDs of the calling process, and the value is returned. On failure, it throws an exception of type These functions directly correspond to the POSIX C library functions of the same names available in some Unix operating systems. Each function retrieves a three element list of numeric IDs. The function retrieves the real, effective and saved user ID of the calling process. The function retrieves the real, effective and saved group ID of the calling process. These functions directly correspond to the POSIX C library functions of the same names available in some Unix operating systems. They change the real, effective and saved user ID or group ID, respectively, of the calling process. A value of -1 for any of the IDs specifies that the ID is not to be changed. Only privileged processes may arbitrarily change IDs to different values. Unprivileged processes are restricted in the following way: each of the new IDs that is replaced must have a new value which is equal to one of the existing three IDs. The structure corresponds to the C type Objects of this struct are produced by the password database query functions and The first time function is called, it returns the first password database entry. On subsequent calls it returns successive entries. Entries are returned as instances of the structure. If the function cannot retrieve an entry for any reason, it returns The function rewinds the database scan. The function releases the resources associated with the scan. The searches the password database for an entry whose user ID field is equal to the numeric If the search is successful, then a structure representing the database entry is returned. If the search fails, is returned. The searches the password database for an entry whose user name is equal to If the search is successful, then a structure representing the database entry is returned. If the search fails, is returned. The structure corresponds to the C type Objects of this struct are produced by the password database query functions and The first time function is called, it returns the first group database entry. On subsequent calls it returns successive entries. Entries are returned as instances of the structure. If the function cannot retrieve an entry for any reason, it returns The function rewinds the database scan. The function releases the resources associated with the scan. The searches the group database for an entry whose group ID field is equal to the numeric If the search is successful, then a structure representing the database entry is returned. If the search fails, is returned. The searches the group database for an entry whose group name is equal to If the search is successful, then a structure representing the database entry is returned. If the search fails, is returned. The function is a wrapper for the Unix C library function of the same name. It calculates a hash over the and arguments, which are strings. The hash is returned as a string. The and arguments are converted into UTF-8 prior to being passed into the underlying platform function. The hash value is assumed to be UTF-8 and converted to Unicode characters, though it is not expected to contain anything but 7 bit ASCII characters. Note: if C library function uses a static buffer for its return value. If that function is used, the Lisp string returned by the TXR Lisp function carries its own copy of that buffer. Where available, the function is used which avoids static storage. Implementations of the C function vary in their error reporting. Some implementations return a null pointer for invalid salts, whereas others return valid "error token" strings which vary between implementations. To work consistently across numerous implementations, the TXR Lisp function throws an exception if the C library function returns either a null pointer, or a valid pointer to a string that is less than 13 characters long, regardless of its content. On platforms where certain advanced features of POSIX signal handling are available at the C API level, TXR exposes signal-handling functionality. A TXR program can install a TXR Lisp function (such as an anonymous or the function object associated with a named function) as the handler for a signal. When that signal is delivered, TXR will intercept it with its own safe, internal handler, mark the signal as deferred (in a TXR sense) and then dispatch the registered function at a convenient time. Handlers currently are not permitted to interrupt the execution of most TXR internal code. Immediate, asynchronous execution of handlers is currently enabled only while TXR is blocked on I/O operations or sleeping. Additionally, the function can be used to dispatch and clear deferred signals. These handlers are then safely called if they were subroutines of and not asynchronous interrupts. These variables correspond to the C signal constants and so forth. The variables and may not be available since a system may lack the corresponding signal constants. See notes for the function The highest signal number is 31. The function is used to specify the handling for a signal, such as the installation of a handler function. It updates the signal handling for a signal whose number is (usually one of the constants like and so forth), and returns the previous value. The function returns the current value. The must be an integer the range 1 to 31. Initially, all 31 signal handling specifications are set to the value The parameter may be a function. If a function is specified, then the signal is enabled and connected to that function until another call to changes the handling for that signal. If is the symbol then the function previously associated with the signal, if any, is removed, and the signal is disabled. For a signal to be disabled means that the signal is set to the disposition (refer to the C API). If is the symbol then the function previously associated with the signal, if any, is removed, and the signal is set to its default disposition. This means that it is set to (refer to the C API). Some signals terminate the process if they are generated while the handling is configured to the default disposition. Note that the certain signals like and cannot be ignored or handled. Please observe the signal documentation in the IEEE POSIX standard, and your platform. A signal handling function must take two arguments. It is of the form: The argument is an integer indicating the signal number for which the handler is being invoked. The argument is a Boolean value. If it is it indicates that the handler is being invoked asynchronously—directly in a signal handling context. If it is then it is a deferred call. Handlers may do more things in a deferred call, such as terminate by throwing exceptions, and perform I/O. The return value of a handler is normally ignored. However if it invoked asynchronously (the argument is true), then if the handler returns a value, it is understood that the handler requesting that it be deferred. This means that the signal will be marked as deferred, and the handler will be called again at some later time in a deferred context, whereby is This is not guaranteed, however; it's possible that another signal will arrive before that happens, possibly resulting in another async call, so the handler must be prepared to deal with an async call at any time. If a handler is invoked synchronously, then its return value is ignored. In the current implementation, signals do not queue. If a signal is delivered to the process again, while it is marked as deferred, it simply stays deferred; there is no counter associated with a signal, only a Boolean flag. The function tests whether any signals are deferred, and for each deferred signal in turn, it executes the corresponding handler. For a signal to be deferred means that the signal was caught by an internal handler in TXR and the event was recorded by a flag. If a handler function is removed while a signal is deferred, the deferred flag is cleared for that signal. Calls to the function may be inserted into CPU-intensive code that has no opportunity to be interrupted by signals, because it doesn't invoke any I/O functions. The function sends to the process. It is a wrapper for the C function of the same name. The return value is if the function succeeds, otherwise The function is used for sending a signal to a process group or process. It is a wrapper for the POSIX function. If the argument is omitted, it defaults to the same value as The return value is if the function succeeds, otherwise The function returns a character string describing the specified signal number. It is based on the same-named POSIX C library function. The and functions are interfaces to the Unix functions and The function creates a child process which is a replica of the parent. Both processes return from the function. In the child process, the return value is zero. In the parent, it is an integer representing the process ID of the child. If the function fails to create a child, it returns rather than an integer. In this case, the function can be used to inquire about the cause. The function, if successful, returns a cons cell consisting of a pair of integers. The of the cons is the process ID of the process or group which was successfully waited on, and the is the status. If fails, it returns The function can be used to inquire about the cause. The argument, if not supplied, defaults to -1, which means that waits for any process, rather than a specific process. Certain other values have special meaning, as documented in the POSIX standard for the function. The argument defaults to zero. If it is specified as nonzero, it should be a bitwise combination (via the function) of the variables and If is used, then returns a cons cell whose specifies a process ID value of zero in the situation that at least one of the processes designated by exist and are children of the calling process, but have not changed state. In this case, the status value in the is unspecified. Status values may be inspected with the functions and These functions analyze process exit values produced by the function. They are closely based on the POSIX macros and so on. The value is either an integer, or a cons cell. In this case, the cons cell is expected to have an integer in its which is used as the status. The and functions have Lisp Boolean return semantics, unlike their C language counterparts: they return or rather than zero or nonzero. The others return integer values. The exec function replaces the process image with the executable specified by string argument The executable is found by searching the system path. The argument becomes the first argument of the executable, argument zero. If is specified, it is a list of strings. These are passed as the additional arguments of the executable. If fails, an exception of type is thrown. The function terminates the entire process (running TXR image), specifying the termination status to the operating system. The argument is treated exactly like that of the function. Unlike that function, this one exits the process immediately, cleaning up only low-level operating system resources such as closing file descriptors and releasing memory mappings, without performing userspace cleanup. is implemented using a call to the POSIX function These functions retrieve the current process ID and the parent process ID respectively. They are wrappers for the POSIX functions and This is a wrapper for the function which originated in BSD Unix. It returns if successful, otherwise, and the variable is set in that case. Unlike in the underlying same-named platform function, the and arguments are Boolean, rather than integer values. The function creates and returns a TXR stream over a file descriptor. The argument must be an integer denoting a valid file descriptor. For a description of see the function. If the argument is present, it must be a positive integer corresponding to a process ID. The function will associate the process ID with the returned stream. When the stream is closed with special handling takes place, as documented for that function. The function returns the underlying file descriptor of if it has one. Otherwise, it returns This is equivalent to querying the stream using for the property. The function provides an interface to the POSIX functions or when called with one or two arguments, respectively. The function, if successful, returns a pair of integer file descriptors as a cons-cell pair. The descriptor in the field of the pair is the read end of the pipe. The holds the write end. If the function fails, it throws an exception of type The function passes the integer descriptor to the POSIX function. If the operation is successful, then is returned. Otherwise an exception of type is thrown, unless the argument is present, with a true value. In that case, indicates failure by returning The function suspends execution while monitoring one or more file descriptors for specified events. It is a wrapper for the same-named POSIX function. The argument is a sequence of pairs. The of each pair is either an integer file descriptor, or else a stream object which has a file descriptor (the function can be applied to that stream to retrieve a descriptor). The of each pair is an integer bit mask specifying the events, whose occurrence the file descriptor is to be monitored for. The variables and several others are available which hold bitmask values corresponding to the constants used with the C language function. The argument, if absent, defaults to the value -1, which specifies an indefinite wait. A nonnegative value specifies a wait with a timeout, measured in milliseconds. The function returns a list of pairs representing the descriptors or streams which were successfully polled. If the function times out, it returns an empty list. If an error occurs, an exception is thrown. The returned list is similar in structure to the input list. However, it holds only entries which polled positive. The of every pair now holds a bitmask of the events which were to have occurred. The function provides access to the underlying POSIX function of the same name. If the argument is a object which has a property, then the file descriptor number is retrieved. The behavior is then as if that descriptor number were passed as the argument. If the argument is not a it must be a an integer in the representation range of the C type The POSIX is invoked on this integer. If it that returns 1, then is returned, otherwise These variables correspond to the POSIX file mode constants and so forth. The availability of the variables and depends on the host platform. Some of these flags may be set or cleared on an existing file descriptor using the command of the function, in accordance with POSIX and the host platform documentation. These variables correspond to the ISO C constants and These values, usually associated with the ISO C function, are also used in the file locking interface as values of the member of the structure. These variables correspond to the POSIX command constants and so forth. Availability of the depends on the host platform. The variable corresponds to the POSIX constant. It denotes the flag which may be set by the command of the function. These variables correspond to the POSIX lock type constants and They specify the possible values of the field of the structure. The structure corresponds to the POSIX structure of the same name. An instance of this structure must be specified as the third argument of the function when the argument is one of the values or All slots must be initialized with appropriate values before calling with the exception that the command does not access the existing value of the slot. The function corresponds to the same-named POSIX function. The and arguments must be integers. The TXR Lisp restricts the argument to the supported values for which symbolic variable names are provided. Other integer values are rejected by returning -1 and setting the variable to Whether the third argument is required, and what type it must be, depends on the value. Commands not requiring the third argument ignore it if it is passed. commands for which POSIX requires an argument of type require to be an integer. The file locking commands and require to be a structure. The function doesn't throw an error if the underlying POSIX function indicates failure; the underlying function's return value is converted to a Lisp integer and returned. Itimers ("interval timers") can be used in combination with signal handling to execute asynchronous actions. Itimers deliver delayed, one-time signals, and also periodically recurring signals. For more information, consult the POSIX specification. These variables correspond to the POSIX constants and Their values are suitable as the argument of the and functions. The function returns the current value of the specified timer, which must be or The current value consists of a list of two integer values, which represents microseconds. The first value is the timer interval, and the second value is the timer's current value. Like the function also retrieves the specified timer. In addition, it stores a new value in the timer, which is given by the two arguments, expressed in microseconds. On platforms where a Unix-like syslog API is available, TXR exports this interface. TXR programs can configure logging via the function, control the logging mask via and generate logs via or using special syslog streams. These variables take on the values of the corresponding C preprocessor constants from the header: etc. These integer values represent logging options used in the argument to the function. Note: is not in POSIX, and so might not be available. See notes about in the documentation for These variables take on the values of the corresponding C preprocessor constants from the header: and These are the integer facility codes specified in the function. Note: is not in POSIX, and so might not be available. For portability use code like to evaluate to 0 if doesn't exist, or else check for its existence using These variables take on the values of the corresponding C preprocessor constants from the header: etc. These are the integer priority codes specified in the function. The variable holds a special kind of stream: a syslog stream. Each newline-terminated line of text sent to this stream becomes a log message. The stream internally maintains a priority value that is applied when it generates messages. By default, this value is that of The stream holds the priority as the value of the stream property, which may be changed with the function. The latest priority value which has been configured on the stream is used at the time the newline character is processed and the log message is generated, not necessarily the value which was in effect at the time the accumulation of a line began to take place. Messages sent to are delimited by newline characters. That is to say, each line of text written to the stream is a new log. The function is a wrapper for the C function, and the arguments have the same semantics. It is not necessary to use in order to call the function or to write data to The call is necessary in order to override the default identifying string, to set options, such as having the PID (process ID) recorded in log messages, and to specify the facility. The argument is mandatory. The argument is a bitwise mask (see the logior function) of option values such as and If it is missing, then a value of 0 is used, specifying the absence of any options. The argument is one of the values or If it is missing, then is assumed. The function is a wrapper for the C function The function interfaces to the corresponding C function, and has the same argument and return value semantics. The argument is a mask of priority values to enable. The return value is the prior value. Note that if the argument is zero, then the function doesn't set the mask to zero; it only returns the current value of the mask. Note that the priority values like and are integer enumerations, not bitmasks. These values cannot be combined directly to create a bitmask. Rather, the function should be used on these values.
      ;; Enable LOG_EMERG and LOG_ALERT messages,
      ;; suppressing all others
      (setlogmask (mask log-emerg log-alert)) The function is the interface to the C function. The formatting capabilities of the function are not used; the argument follows the conventions of the TXR Lisp function instead. Note in particular that the convention for interpolating the value of strerror(errno) which is available in some versions of the C function is currently not supported. Note that syslog messages are not newline-terminated. On platforms where the POSIX function is available TXR provides this functionality in the form of a like-named function, and some numeric constants. TXR also provides access the function, where available. These variables take on the values of the corresponding C preprocessor constants from the header: etc. These values are passed as an argument to the optional argument of the function. They are bitmasks and so multiple values can be combined using the function. Note that the and variables may not be available. They are extensions in the GNU C library implementation of The standard flag is not represented as a TXR variable. The function uses it internally when calling the C library function multiple times, due to having been given multiple patterns. This value holds an integer bitmask value that may be given as an argument to the optional parameter of the function. It may be used alone, combine with the other mask values using the function. If used with it disables brace expansion, which is enabled in by default. If used with the function, it has no effect. This value is a TXR Lisp extension; it does not appear in the API of the C function. The function is a interface to the Unix function of the same name. The first argument must either be a single which is a string, or else sequence of strings specifying multiple which are strings. Each string is a glob pattern: a pattern which matches zero or more pathnames, similar to a regular expression. The function tries to expand the patterns and return a list of strings representing the matching pathnames in the file system. If there are no matches, then an empty list is returned. The function is a TXR Lisp extension built on The functions supports a ("double star") pattern which matches zero or more path components. The double star match is described in detail below. The function also supports brace expansion, independently of whether or not supports brace expansion. Brace expansion is enabled by default in and can be disabled using the flag. Brace expansion is described in detail below. Lastly, the function performs a path-aware sort of the emerging path names that is not influenced by locale, whereas the sort performed by is influenced by locale, defaulting to a lexicographic sort in the locale. The optional argument defaults to zero. If given, it may be a bitwise combination of the values of the variables and others. The If the argument is specified, it gives a callback function which is invoked when encounters errors accessing paths. The function takes two arguments: the pathname and the value which occurred for that pathname. The function's return value is Boolean. If the function returns true, then will terminate. The may terminate the traversal by a nonlocal exit, such as by throwing an exception or performing a block return. The may not reenter the function. This situation is detected and diagnosed by an exception. The may not capture a continuation across the error boundary. That is to say, code invoked from the error may not capture a continuation up to a prompt which surrounds the call. Such an attempt is detected and diagnosed by an exception. If a sequence of is specified instead of a single pattern, makes multiple calls to the underlying C library function. The second and subsequent calls specify the flag to add the matches to the result. The following equivalence applies:
      (glob (list p0 p1 ...) f e) <--> (append (glob p0 f e)
                            (glob p1 f e)
                            ...) Details of the semantics of the function, and the meaning of all the arguments are given in the documentation for the C function. The function supports brace expansion, which is enabled by default, and can be disabled with On some platforms, such as the GNU C Library, the function also supports brace expansion. If available, then the variable has a nonzero value and must be included in the argument. These two brace expansion features are independent; the TXR Lisp function does not rely on for brace expansion, even if it is available. The brace expansion supported by is a string generation mechanism driven by a syntax which specifies comma-separated elements enclosed in braces. When a single brace expansion appears in a pattern, that pattern turns into a list of patterns. There are as many elements in the list as there are elements between the braces. Each element replaces the braces with a different element from between the braces. For instance, denotes the list of strings The there are two elements in the list because the braces contain two elements. The first string replaces with and the second replaces it with When multiple braces occur in a pattern, then all combinations (Cartesian product) of the braces is produced. Braces may also nest. When the element of a brace itself uses braces, then that element is subject to brace expansion. The elements which emerge then become items of the enclosing brace, as if they were comma-separated elements. For instance is equivalent to which then expands to the three strings and Braces may be escaped by a backslash to disable their special meaning. Likewise, the commas may be escaped by a backslash to preserve their special meaning. Brace expansion preserves these backslashes; they appear in the resulting patterns, and must be recognized and removed by subsequent processing. When the arguments of use brace expansion, those arguments produce multiple patterns. The order of these patterns is preserved: the patterns are matched in that order. For each pattern, the matching path names are sorted, unless the flag is in effect. The ("double star") operator recognized by matches zero or more path components. It may be used more than once. It cannot be combined with other characters or globbing operators. It is valid for to be an entire pattern. This expands the relative path names of all files, directories and other objects in the current directory and its children. Otherwise the double star may appear at the start of a pattern if it is followed by a slash; at the end of a pattern if it is preceded by a slash, or in the middle of a pattern if it is surrounded by two slashes. The double star is not recognized in a bracket-enclosed character class. Thus, the following examples all contain one double star:
      **
      foo/**
      **/bar
      here/**/there These do not contain a double star; the two asterisks in these patterns will be passed to the underlying function without being processed as a double star by with unspecified consequences:
      foo**
      **bar
      here**/there
      etc/**conf
      foo[/**/]bar Each double star matches a maximum of ten path components, and all of the double stars in a single pattern together do not match more than 48 components. Using more than three double stars in a pattern is not recommended for performance reasons. If the double star is followed by a slash, it matches only directories. The function sorts paths in such a way that the slash character is ranked lower than all other characters. Thus the path sorts before even though in ASCII and Unicode, the character has a lower code than the character.
       ;; find all jpg and gif paths under the current directory,
       ;; (up to ten levels deep).
       (glob* "**/*.{jpg,gif}")
       ;; find all "2023" directories under the current directory,
       ;; which have .jpg or .gif files under them, listing those
       ;; .jpg and .gif paths:
       (glob* "**/2023/**/*.{jpg,gif}")
       ;; find all "2023" directories under the current directory.
       (glob* "**/2023/**/") These variables take on the values of the corresponding C preprocessor constants from the header: etc. These values are bit masks which may be combined with the function to form the optional third argument of the function. Note that the and functions may not be available. They are GNU extensions, found in the GNU C library. The function, if available, provides access to the like-named POSIX C library function. The argument specifies a POSIX-shell-style filename-pattern-matching expression. Its exact features and dialect are controlled by If matches then is returned. If there is no match, then is returned. If the C function indicates that an error has occurred, an exception is thrown. On platforms where the POSIX function is available TXR provides this functionality in the form of the analogous Lisp function accompanied by some numeric constants. Likewise, on platforms where the POSIX functions and are available, TXR provides the functionality in the form of same-named Lisp functions, a structure type named and some accompanying numeric constants. These variables hold numeric values that may be combined into a single bitmask bitmask value using the function. This value is suitable as the argument of the function. These variables correspond to the C constants etc. Note that is a GNU extension that is not available on all platforms. If the platform's function doesn't have this feature, then this variable is not defined. These variables provide symbolic names for the integer values that are passed as the argument of the callback function called by This argument classifies the kind of file system node visited, or error condition encountered. These variables correspond to the C constants etc. Not all of them are present. If the underlying platform doesn't have a given constant, then the corresponding variable doesn't exist in TXR. These variables are defined if the variable is defined. If the value of is included in the argument of then the callback function can use the values of these variables as return codes. Ordinarily, the callback returns zero to continue the search and nonzero to stop. These variables correspond to the C constants etc. The function provides access to the POSIX C library function. Note that the and arguments are reversed with respect to the C language interface. They are both optional; defaults to the value of and defaults to 20. If an argument is given to then the presence of the is no longer implied; the flag must be explicitly included in the argument in order to be present. Compatibility Note: the parameter defaults to an argument value of zero in TXR versions 283 or lower. The argument may be a string specifying the top-level pathname that shall visit. Or else, may be a list. If it is a list, then recursively invokes itself over each of the elements, taking that element as the argument of the recursive call, passing down all other argument values as-is. The traversal stops when any recursive invocation of returns a value other than or and that value is returned. If or is returned, the traversal continues with the application of to the next list element, if any. If the list is completely traversed, and some recursive invocations of return then the return value is If all recursive invocations return then is returned. If the list is empty, is returned. The function walks the filesystem, as directed by the argument and bitmask arguments. For each visited entry, it calls the supplied function, which receives five arguments. If this function returns normally, it must return either or an integer value in the range of the C type The function can continue the traversal by returning any non-integer value, or the integer value zero. If is included in the bitmask, then the only integer code which continues the traversal without any special semantics is and only stops the traversal. (Non-integer return values behave like The argument of gives the path of the visited filesystem object. The argument is an integer code which indicates the kind of object that is visited, or an error situation in visiting that filesystem entry. See the documentation for and for possible values. The argument provides information about the filesystem object as a structure, the same kind of object as what is returned by the function. The argument is an integer value representing the directory level depth. This value is obtained from the C structure in the C API. The argument indicates the length of the directory part of the argument. Characters in excess of this length are thus the base name of the visited object, and the expression calculates the base name. The function returns either upon successful completion, or an integer value returned by as described below. On failure it throws an exception derived from whose specific type is based on analyzing the POSIX value. The may return a value of any type. If it returns a value that is not of integer type, then zero is returned to the function and traversal continues. Similarly, traversal continues if the function returns an integer zero. If returns an integer value, that value must be in the range of the C type That value is returned to If the value is not zero, and is not -1, then will terminate, and return that value, which then returns. If the value is -1, then is deemed to have failed, and will thrown an exception of type whose specific type is based on analyzing the POSIX value. If the value is zero, then the traversal continues. The may also terminate the traversal by a nonlocal exit, such as by throwing an exception or performing a block return. The may not reenter the function. This situation is detected and diagnosed by an exception. The may not capture a continuation across the callback boundary. That is to say, code invoked from the callback may not capture a continuation up to a prompt which surrounds the call. Such an attempt is detected and diagnosed by an exception. Objects of the structure type are returned by the function. The slot is a character string giving the name of the directory entry. If the function's argument is specified as true, then operations produce structures whose slot is a path formed by combining the directory path with the directory entry name. The slot is an integer giving the inode number of the object named by the directory entry. The slot indicates the type of the object, which is an integer code. Support for this member is platform-dependent. If the directory traversal doesn't provide the information, then this slot takes on the value. In this situation, the function may be used to backfill the missing information. These variables give the possible type code values exhibited by the slot of the structure. If the underlying host platform does not feature a field in the C structure, then almost all these variables are defined anyway using the values that they have on GNU/Linux. These definitions are useful in conjunction with the function below. If the host platform does does not feature a field in the structure, then the variable is not defined. Note: the application can take advantage of this this to detect the situation, in order to conditionally define code in such a way that some run-time checking is avoided. The function initiates a traversal of the directory object named by the string argument which must be the name of a directory. If is not able to open the directory traversal, it throws an exception of type Otherwise an object of type is returned, which is a directory traversal handle suitable as an argument for the function. If the argument is specified and has a true value, then it indicates that the subsequent operations should produce the value of the slot of the structure by combining with the directory entry name using the function. The function returns the next available directory entry from the directory traversal controlled by which must be a object returned by If no more directory entries remain, then returns In this situation, the is also closed, as if by a call to Otherwise, the next available directory entry is returned as a structure object of type The function internally skips and does not report the (dot) and (dotdot) directory entries. If the argument is specified, then it must be a structure, or one which has all of the required slots. In this case, stores values in that structure and returns it. If is absent, then allocates a fresh structure. The function terminates the directory traversal managed by releasing its resources. If this has already been done before, returns otherwise it returns Further calls on the same return Note: the function implicitly closes when the handle indicates that no more directory entries remain to be traversed. The function invokes on the object represented by the structure sets the slot of the accordingly, and then returns the value that returned. If the argument is specified, it is passed to The parameter must be specified, if the slot of is a simple directory entry name, rather than the full path to the object. In that case, the slot's value gives the effective path. If the slot is already a path (due to, for instance, a true value of having been passed to then must not be specified. If is specified, then its value is combined with the slot of using to form the effective path. The function is invoked on the effective path, and if it succeeds, then type information is obtained from the resulting structure to set the value of the slot of The same structure that was returned by is then returned. On platforms where the underlying system interface is available, TXR provides a sockets library for communicating over Internet networks, or over Unix sockets. Stream as well as datagram sockets are supported. The classic Version 4 of the Internet protocol is supported, as well as IP Version 6. Sockets are mapped to TXR streams. The function creates a socket of a specified type, in a particular address family. This socket is actually a stream (always, even if it isn't used for data transfer, but only as a passive contact point). The functions and are used for enacting socket communication scenarios. Stream sockets use ordinary streams, reusing the same underlying framework that is used for file I/O and process types. Datagram socket streams are implemented using special datagram socket streams. Datagram socket streams eliminate the need for operations analogous to the and socket API functions, even in server programs which handle multiple clients. An overview of datagrams is treated in the following section, Datagram Socket Streams. The function is provided for resolving host names and services to IPv4 and IPv6 addresses. Several structure types are provided for representing socket addresses, and options for Various numeric constants are also provided: and others. Datagram socket streams are a new paradigm unique to TXR which attempts to unify the programming model of stream and datagram sockets. A datagram socket stream is created by the function, when the socket type is specified. Another way in which a datagram socket is created is when is invoked on a datagram socket, and returns a new socket. I/O is performed on datagram sockets using the regular I/O functions. None of the functions take or return peer addresses. There are no I/O functions which are analogous to the C library and functions which are usually used for datagram programming. Datagram I/O assumes that the datagram datagram socket is connected to a specific remote peer, and that peer is implicitly used for all I/O. Datagram streams solve the message framing problem by considering a single datagram to be an entire stream. On input, a datagram stream holds an entire datagram in a buffer. The stream ends (experiences the EOF condition) after the last byte of this buffer is removed by an input operation. Another datagram will be received and buffered if the EOF condition is first explicitly cleared with the function, and then another input operation is attempted. On output, a datagram stream gathers data into an ever-growing output buffer which isn't subject to any automatic flushing. An explicit operation sends the buffer contents to the connected peer as a new datagram, and empties the buffer. Subsequent output operations prepare data for a new datagram. The function implicitly flushes the stream in the same way, and thus also potentially generates a datagram. A client-style datagram stream can be explicitly connected to a peer with the function. This is equivalent to connecting a datagram socket using the C library function. Writes on the stream will be transmitted using the C library function A client-style datagram stream can also be "soft-connected" to a peer using the function. Writes on the stream will transmit data using the C library function to the peer address. A datagram server program which needs to communicate using multiple peers is implemented by means of the function which, unlike the C library function, works with datagram sockets as well as stream sockets. The server creates a datagram socket, and uses to bind it to a local address. Optionally, it may also call which is a no-op on datagram sockets. Supporting this function on datagram sockets allows program code to be more easily changed between datagram and stream operation. The server then uses to accept new clients. Note that this is not possible with the C library function which only works with stream sockets. The function receives a datagram from a client, and creates a new datagram socket stream which is connected to that client, and whose input buffer contains the received datagram. Input operations on this stream consume the datagram. Note that clearing the EOF condition and trying to receive another datagram is not recommended on datagram streams returned by since they share the same underlying operating system socket, which is not actually connected to a specific peer. The receive operation could receive a datagram from any peer, without any indication which peer that is. Datagram servers should issue a new call for each client datagram, treating it as a new stream. Datagram sockets ignore almost all aspects of the passed in and The only attribute not ignored is the buffer size specified with a decimal digit character; however, it cannot be the only item in the mode string. The string must be syntactically valid, as described under the function. The buffer size attribute controls the size used by the datagram socket for receiving a datagram: the capture size. A datagram socket has obtains a default capture size if one isn't specified by the The default capture size is 65536 bytes for a datagram socket created by If a size is not passed to via its argument when it is invoked on a datagram socket, that socket's size is used as the capture size of the newly created datagram socket which is returned. The structure represents the abstract base class for socket addresses, from which several other types are derived: and It has a single static slot named and a single instance slot both initialized to Note: the slot is optionally set by the function on address structures that it returns, if requested via the flag. The slot only provides information to the application, playing no semantic role in addressing. The address represents a socket address used in the context of networking over IP Version 4. It may be used with sockets in the address family. The slot holds an integer denoting an abstract IPv4 address. For instance the hexadecimal integer literal constant or its decimal equivalent represents the loopback address, whose familiar "dot notation" is Conversion of the abstract IP address to four bytes in network order, as required, is handled internally. The slot holds the TCP or UDP port number, whose value ranges from 0 to 65535. Zero isn't a valid port; the value is used for requesting an ephemeral port number in active connections. Zero also appears in situations when the port number isn't required: for instance, when the function is used with the aim of looking up the address of a host, without caring about the port number. The field is set by the function when it recognizes and parses a prefix field in the textual representation. The static slot holds the value The address represents a socket address used in the context of networking over IP Version 6. It may be used with sockets in the address family. The slot holds an integer denoting an abstract IPv6 address. IPv6 addresses are pure binary integers up to 128 bits wide. The slot holds the TCP or UDP port number, whose value ranges from 0 to 65535. In IPv6, the port number functions similarly to IPv6; see The and are special IPv6 parameters corresponding to the and slots of the C language structure. Their meaning and use are beyond the scope of this document. The field is set by the function when it recognizes and parses a prefix field in the textual representation. The static slot holds the value The address represents a socket address used for interprocess communication within a single operating system node, using the "Unix domain" sockets of the address family. This structure has only one slot, which holds the rendezvous name for connecting pairs of socket endpoints. This name appears in the filesystem. When the structure is converted to the C structure the slot undergoes conversion to UTF-8. The resulting bytes are stored in the member of the C structure. If the resulting UTF-8 byte string is larger than the array, it is silently truncated. Note: Linux systems have support for "abstract" names which do not appear in the filesystem. These abstract names are distinguished by starting with a null byte. For more information, consult Linux documentation. This convention is supported in the slot of the structure. If contains occurrences of the pseudo-null character U+DC00, these translate to null bytes in the member of the corresponding C structure For example, the path is valid and represents an abstract address consisting of the three bytes followed by null padding bytes. The static slot holds the value The structure is used in conjunction with the function. If that function's argument is specified, it is of this type. The purpose of the argument is to narrow down or possibly alter the selection of addresses which are returned. The slot holds a bitwise or combination (see the function) of flags: values given by the variables and These correspond to the C constants and so forth. If is specified, then every returned address structure will have its member set to a string value rather than This string is a copy of the canonical name reported by the underlying C library function, which that function places only into the first returned address structure. The slot holds an address family, which may be the value of or The slot holds, a socket type. Socket types are given by the variables and The returns a list of socket addresses based on search criteria expressed in its arguments. That is to say, the returned list, unless empty, contains objects of type and The function is implemented directly in terms of the like-named C library function. All parameters are optional. Omitting any argument causes a null pointer to be passed for the corresponding parameter of the C library function. The and parameters may be character strings which specify a host name, and service. The contents of these strings may be symbolic, like and or numeric, like and If an argument is given for the parameter, it must be of type The and parameters may also be given integer arguments. An integer argument value in either of these parameters is converted to a null pointer when calling the C function. The integer values are then simply installed into every returned address as the IP address or port number, respectively. However, if both arguments are numeric, then no addresses are returned, since the C library function is then called with a null node and service. These variables hold integers which give the values of address families. They correspond to the C constants and Address family values are used in the argument of the function, and in the function. Note that unlike the C language socket addressing structures, the TXR socket addresses do not contain an address family slot. That is because they indicate their family via their type. That is to say, an object of type is an address which is implicitly associated with the family via its type. These variables hold integers which give the values of address families. They correspond to the C constants and These variables hold integers which are bitmasks that combine together via bitwise or, to express the slot of the structure. They correspond to the C constants and so forth. They influence the behavior of the function. These integer-valued variables provide constants for commonly used IPv4 and IPv6 address values. The value of and is zero. This address is used in binding a passive socket to all of the external interfaces of a host, so that it can accept connections or datagrams from all attached networks. The variable is IPv4 loopback address, the same integer as the hexadecimal constant The is the IPv6 loopback address. Its value is 1.
      ;; Construct an IPv6 socket address suitable for binding
      ;; a socket to the loopback network, port 1234:
      (new sockaddr-in6 addr in6addr-loopback port 1234)
      ;; Mistake: IPv4 address used with IPv6 sockaddr.
      (new sockaddr-in6 addr inaddr-loopback) The function creates a socket, which is a kind of stream. The parameter specifies the address family of the socket. One of the values or should be used to create a Unix domain, Internet IPv4 or Internet IPv6 socket, respectively. The parameter specifies the socket type, either (stream socket) or (datagram socket). The specifies several properties of the stream; for a description of parameters, refer to the function. Note that the defaulting behavior for an omitted argument is different under from other functions. Because sockets are almost always used for bidirectional data flow, the default mode string is rather than the usual The rationale for including the flag in the default mode string is that network protocols are usually defined in a way that is independent of machine and operating system, down to the byte level, even when they are textual. It doesn't make sense for the same TXR program to see a network stream differently based on what platform it is running on. Line-ending conversion has to do with how a platform locally stores text files, whereas network streams are almost always external formats. Like other stream types, stream sockets are buffered and marked as non-real-time streams. Specifying the mode in marks a socket as a real-time stream, and, if it is opened for writing or reading and writing, changes it to use line buffering. The function provides an interface to the functionality of the C library function. If successful, it creates and returns a list of two stream objects, which are sockets that are connected together. Note: the Internet address families and are not supported. The is applied to each stream. For a description, see and These functions retrieve the integer values representing the address family and type of a socket. The argument to the parameter must be a socket stream or a file or process stream. For a file stream, both functions return An exception of type is thrown for other stream types. The function retrieves the peer address has most recently been assigned to Sockets which are not connected initially have a peer address value of A socket which is connected to a remote peer receives that peer's address as its If a socket is connected to a remote peer via a successful use of the function, then its address is set to match that of the peer. Sockets returned by the function are connected, and have the remote endpoint address as their address. Assigning an address to a form is equivalent to using to set the address. Implementation note: the function does not use the C library function; the association between a stream and struct is maintained by TXR. The function stores into as that socket's peer. Subsequently, the function will retrieve that address. If is not an appropriate address object in the address family of the behavior is unspecified. The function connects a socket stream to a peer address. The argument must be a object of type matching the address family of the socket. If the operation fails, an exception of type is thrown. Otherwise, the function returns If the argument is specified, it must be a fixnum integer. It denotes a connection timeout period in microseconds. If the connection doesn't succeed within the specified timeout, an exception of type is thrown. The function binds a socket stream to a local address after enabling the socket stream's option. The argument must be a object of type matching the address family of the socket. If the operation fails, an exception of type is thrown. Otherwise, the function returns The function prepares for listening for connections. The parameter, if specified, requires an integer argument. The default value is 16. The function waits for a client connection on which must have been prepared for listening for connections using and If the operation fails, an exception of type is thrown. Otherwise, the function returns a new socket which is connected to the remote peer. The peer's address may be retrieved from this socket using The parameter is applied to the new socket just like the similar argument in It defaults to If the argument is specified, it must be a fixnum integer. It denotes a timeout period in microseconds. If no peer connects for the specified timeout, throws an exception of type The values of these variables are useful as the second argument to the function. The function indicates that no further communication is to take place on in the specified direction(s). If the operation fails, an exception of type is thrown. Otherwise, the function returns The parameter is one of the values given by the variables or These values shut down communication in the read direction, write direction, or both directions, respectively. If the argument is omitted, defaults to closing the write direction. Notes: shutting down is most commonly requested in the write direction, to perform a "half close". The communicating process thereby indicates that it has written all the data which it intends to write. When the shutdown action is processed on the remote end, that end is unblocked from waiting on any further data, and effectively experiences an "end of stream" condition on its own socket or socket-like endpoint, while continuing to be able to transmit data. Shutting down in the reading direction is potentially abrupt. If it is executed before an "end of stream" indication is received from a peer, it results in an abortive close. The and functions configure, respectively, receive and send timeouts on socket The parameter specifies the value, in microseconds. It must be a integer. When a receive timeout is configured on a socket, then an exception of type is thrown when an input operation waits for at least microseconds without receiving input. Similarly, when a send timeout is configured, then an exception of type is thrown when an output operation waits for at least microseconds for the availability of buffer space in the socket. These variables represent the protocol levels of socket options and are suitable for use as the argument of the and functions. The variables correspond to the POSIX C constants and These variables represent socket options at the protocol level and are suitable for use as the argument of the and functions. The variables correspond to the POSIX C constants etc. Note that the and are a more convenient interface for setting the value of the and socket options. These variables represent socket options at the protocol level and are suitable for use as the argument of the and functions. The variables correspond to the POSIX C constants etc. This variable represents a socket option at the protocol level and is suitable for use as the argument of the and functions. The variable corresponds to the POSIX C constant The function retrieves the value of the specified socket option, at the specified protocol level, associated with which must be a socket stream. The argument should be one of the protocol levels and The argument should be one of the socket options ..., ..., and The argument, which must be a compiled FFI type, specifies the type of the socket option's value. The type is most commonly or but it can be any other fixed-size type, including (Variable-size types, such as C arrays, are unsupported.) The argument defaults to Assigning a value to a place is equivalent to calling with that value. Note: the and functions call the POSIX C and functions, respectively. Consult the POSIX specification for more information about these functions and in particular the various socket options (and the types they require). The function sets the value of the specified socket option, at the specified protocol level, associated with which must be a socket stream. See the documentation of the function for a description of the and arguments. Like the function, argument defaults to The and functions convert an IPv4 and IPv6 address, respectively, to textual notation which is returned as a character string. The conversion is done in conformance with RFC 5952, section 4. IPv6 addresses representing IPv6-mapped IPv4 addresses are printed in the hybrid notation exemplified by The parameter must be a nonnegative integer in the appropriate range for the address type. If the number argument is supplied, it is included in the returned character string, according to the requirements in section 6 of RFC 5952 pertaining to IPv6 addresses (including IPv6-mapped IPv6 addresses) and section 3.2.3 of RFC 3986 for IPv4 addresses. In brief, IPv6 addresses with ports are expressed as and IPv6 addresses follow the traditional pattern. The functions and convert, respectively, IPv4 and IPv6 network prefix addresses to the "slash notation". For IPv6 addresses, the requirements of section 2.3 of RFC 4291 are implemented. For IPv4, section 3.1 of RFC 4632 is followed. The condensed portion of the IP address is always determined by measuring the contiguous extent of all-zero bits in the least significant position of the address. For instance an IPv4 address which has at least 24 zero bits in the least significant position, so that the only nonzero bits are in the highest octet, is always condensed to a single decimal number: the value of the first octet. If the parameter is specified, then its value is incorporated into the returned textual notation as the width. No check is made whether this width large enough to span all of the nonzero bits in the address. If is omitted, then it is calculated as the number of bits in the address, excluding the contiguous all-zero bits in the least significant position: how many times the address can be shifted to the right before a 1 appears in the least significant bit. The and functions recover an IPv4 or IPv6 address from a textual representation. If the parse is successful, the address is returned as, respectively, a or structure. If is a malformed address, due to any issue such as invalid syntax or a numeric value being out of range, an exception is thrown. The function recognizes the dot notation consisting of four decimal numbers separated by period characters. The numbers must be in the range 0 to 255. Note: superfluous leading zeros are permitted, though this is a nonstandard extension; not all implementations of this notations support this. A prefix may be specified in the notation as a slash followed by a decimal number, in the range 0 to 32. In this case, the integer value of the prefix appears as the member of the returned structure. Furthermore, the address is masked, so that any bits not included in the prefix are zero. For instance, the address is equivalent to except that the if the returned structure is 1 rather than 32. When a prefix is not specified, the member of the structure retains its default value of 32. When the prefix is specified, the address part need not contain all four octets; it may contain between one and four octets. Thus, is a valid address, equivalent to A port number may be specified in the notation as a colon, followed by a decimal number in the range 0 to 65535. The integer value of this port number appears as the member of the returned structure. An example of this notation is A prefix and port number may both be specified; in this case the prefix must appear first, followed by the port number. For example, The function recognizes the IPv6 notation consisting of 16-bit hexadecimal pieces separated by colons. If the operation is successful, it returns a structure. Each piece must be a value in the range 0 to FFFF. The hexadecimal digits may be any mixture of uppercase and lowercase. Leading zeros are permitted. Up to eight such pieces must be specified. If fewer pieces are specified, then the token (double colon) must appear in the address exactly once. That token denotes the condensation of a sufficient number of zero-valued pieces to make eight pieces. The token must be in one of three positions: it may be the leftmost element of the address, immediately followed by a hexadecimal piece; it may be the rightmost element of the address, preceded by a hexadecimal piece; or else, it may be in the middle of the address, flanked on both sides by hexadecimal pieces. The also recognizes the special notation for IPv6-mapped IPv4 addresses. This notation consists of the address string which may appear in any uppercase/lowercase mixture, possibly with leading zeros, followed by an IPv4 address given in the four-octet dot notation. For example, A prefix may be specified using a slash, followed by a decimal number in the range 0 to 128. The handling of the prefix is similar to that of except that pieces of the address may not be omitted. Condensing the pieces of the IPv6 address is always done by means of the token, whether or not a prefix is present. Furthermore, the octets specified in the IPv6-mapped IPv4 notation must all be present, regardless of the prefix. A port number may be specified in the notation as follows: the entire address, including any slash-separated prefix, must appear surrounded in square brackets. The closing square bracket must be followed by a colon and one or more digits denoting a decimal number in the range 0 to 65535. For instance The function analyzes the argument to determine whether it represents a valid IPv4, IPv6 or Unix domain address. If so, it constructs an object, representing that address, of type or The slash prefix notation, and port numbers are handled, and represented in the returned structures accordingly. The function works by applying simple tests to the input, and then invoking the functions or or constructing a structure whose slot is The precise procedure followed is:

    1.  If starts with then it is handled via

    2.  If starts with then it is assumed to be the path of a Unix socket. A structure is constructed whose slot is This is the only case in which a structure of type is returned.

    3.  If contains as a substring, it is handled via

    4.  If contains then it is handled via

    5.  If the above tests fail, is passed to and if that call returns normally, returns that value.

    6.  Otherwise, is passed to
A method named is defined for the struct types and It returns a text representation of the address as a string. If the slot of or is a nonzero integer, then it is incorporated into the text representation. Likewise if the slot has a non-default value specifying fewer bits than the width of the address, the prefix notation is produced. The intent is that the representations produced are suitable as input to the function which will reproducing an address object of the same type, featuring the same and In the case of the function will reproduce the same address only if the slot is a string starting with TXR provides access to the terminal control "termios" interfaces defined by POSIX, and some of the extensions to it in Linux. By using termios, programs can control serial devices, consoles and virtual terminals. Terminal control in POSIX revolves around a C language structure called This is mirrored in a TXR Lisp structure also called Like-named TXR Lisp functions are provided which correspond to the C functions and These have somewhat different argument conventions. The TTY device is specified last, so that it can conveniently default to the stream. A TTY device can be specified as either a stream object or a numeric file descriptor. The functions and are not provided, because they are unnecessary. Device speed (informally, "baud rate") is specified directly as a integer value in the structure. The TXR Lisp termios functions automatically convert between integer values and the speed constants (like used by the C API. All of the various termios-related constants are provided, including some nonstandard ones. They appear in lowercase. For instance and are simply known as the predefined TXR Lisp variables and The structure represents the kernel level terminal device configuration. It holds hardware related setting such as serial line speed, parity and handshaking. It also holds software settings like translations, and settings affecting input behaviors. The structure closely corresponds to the C language structure which exists in the POSIX API. The and slots correspond to the and members of the C structure. They hold integer values representing bitfields. The slot corresponds to the member of the C structure. Whereas the C structure's member is an array of the C type the slot is a vector of integers, whose values must have the same range as the type. These variables specify bitmask values for the slot of the structure. They correspond to the C language preprocessor symbols and so forth. The and variables are specific to Linux and may not be present. Portable code should test for their presence with The variable is a legacy feature not found on all systems. Note: the methods and provide a convenient means for setting and clearing combinations of these flags. These variables specify bitmask values for the slot of the structure. They correspond to the C language preprocessor symbols and so forth. The variable is Linux-specific. Portable programs should test for its presence using The variable is a legacy feature not found on all systems. Likewise, whether the following groups of symbols are present is platform-specific: and and and and and and Note: the methods and provide a convenient means for setting and clearing combinations of these flags. These variables specify bitmask values for the slot of the structure. They correspond to the C language preprocessor symbols and so forth. The following are present on Linux, and may not be available on other platforms. Portable code should test for them using and Note: the methods and provide a convenient means for setting and clearing combinations of these flags. These variables specify bitmask values for the slot of the structure. They correspond to the C language preprocessor symbols and so forth. The following are present on Linux, and may not be available on other platforms. Portable code should test for them using and Note: the methods and provide a convenient means for setting and clearing combinations of these flags. These variables specify integer offsets into the vector stored in the slot of the structure. They correspond to the C language preprocessor symbols and so forth. The following are present on Linux, and may not be available on other platforms. Portable code should test for them using and These variables hold integer values suitable as the argument of the function. They correspond to the C language preprocessor symbols and These variables hold integer values suitable as the argument of the function. They correspond to the C language preprocessor symbols and These variables hold integer values suitable as the argument of the function. They correspond to the C language preprocessor symbols and The and functions, respectively, retrieve and install the configuration of the terminal driver associated with the specified device. These functions are wrappers for the like-named POSIX C library functions, but with different argument conventions, and operating using a TXR Lisp structure. The function, if successful, returns a new instance of the structure. The function requires an instance of a structure as an argument to its parameter. A program may alter the settings of a terminal device by retrieving them using manipulating the structure returned by this function, and then using to install the modified structure into the device. The argument of may be given as the value of one of the variables or If it is omitted, the default is If an argument is given for it must be either a stream, or an integer file descriptor. In either case, it is expected to be associated with a terminal (TTY) device. If the argument is omitted, it defaults to the stream currently stored in the stream special variable, expected to be associated with a terminal device. The C structure usually does not have members for representing the input and output speed. TXR Lisp does not use such members, in any case, even if they are present. The speeds are encoded in the and bitmasks. When retrieving the settings, the function uses the POSIX functions and to retrieve the speed values from the C structure. These values are installed as the and slots of the Lisp structure. A reverse conversion takes place when setting are installed using the speed values are taken from the slots, and installed into the C structure using and before the structure is passed to the C function. On Linux, TTY devices do not have a separate input and output speed. The C structure stores only one speed which is taken as both the input and output speed, with a special exception. The input speed may be programmed as zero. In that case, it is independently represented. speed may be programmed as zero. The function generates a break signal on serial devices. The parameter specifies the length of the break signal in milliseconds. If the argument is omitted, the value 500 is used. The parameter is exactly the same as that of the function. The function waits until all queued output on a terminal device has been transmitted. It is a direct wrapper for the like-named POSIX C function. The parameter is exactly the same as that of the function. The function discards either untransmitted output data, or received and yet unread input data, depending on the value of the argument. It is a direct wrapper for the like-named POSIX C function. The argument should be the value of one of the variables and which specify the flushing of input data, output data or both. The parameter is exactly the same as that of the function. The function provides bidirectional flow control on the specified terminal device. It is a direct wrapper for the like-named POSIX C function. The argument should be the value of one of the variables and The parameter is exactly the same as that of the function. These methods of the structure set or clear multiple flags of the four bitmask flag fields. The arguments specify zero or more integer values. These values are combined together bitwise, as if by the function to form a single effective mask. If there are no arguments, then the effective mask is zero. The method sets, in the slot of the structure, all of the bits which are set in the effective mask. That is to say, the effective mask is combined with the value in by a operation, and the result is stored back into Similarly, the and methods operate on the and slots of the structure. The method clears, in the slot of the structure, all of the bits which are set in the effective mask. That is to say, the effective mask is bitwise inverted as if by the function, and then combined with the existing value of the slot using The resulting value is stored back into the slot. Similarly, the and methods operate on the and slots of the structure. Note: the methods and are provided for changing the settings to raw, "cbreak" and canonical mode. These methods should be preferred to directly manipulating the flag and slots. In this example, is assumed to be a variable holding an instance of a struct:
  ;; clear the ignbrk, brkint, and various other flags:
  tio.(clear-iflags ignbrk brkint parmrk istrip
                    inlcr igncr icrnl ixon)
  ;; set the csize and parenb flags:
  tio.(set-cflags csize parenb) The and methods of the structure manipulate the flag slots, as well as certain elements of the slot, in order to prepare the terminal settings for, respectively, "raw" and "cbreak" mode, described below. Note that manipulating the structure doesn't actually put these settings into effect in the terminal device; the settings represented by the structure must be installed into the device using There is no way to reverse the effect of these methods. To precisely restore the previous terminal settings, the program should retain a copy of the original structure. "Raw" mode refers to a configuration of the terminal device driver in which input and output is passed transparently and without accumulation, conversion or interpretation. Input isn't buffered into lines; as soon as a single byte is received, it is available to the program. No special input characters such as commands for generating an interrupt or process suspend request are processed by the terminal driver; all characters are treated as input data. Input isn't echoed; the only output which takes place is that generated by program output requests to the device. "Cbreak" mode is named after a concept and function in the "curses" terminal control library. It refers to a configuration of the terminal device driver which is less transparent than "raw" mode. Input isn't buffered into lines, and line editing commands are ordinary input characters, allowing character-at-a-time input. However, most input translations are preserved, except that the conversion of CR characters to NL is disabled. The signal-generating characters are processed in this mode. This latter feature of the configuration is the likely inspiration for the word "cbreak". Unless otherwise configured, the interrupt character corresponds to the key, and "break" is another term for an interactive interruption. The method converts the terminal state stored in a structure into a textual format, returning that representation as a character string. The method parses the character representation produced by and populates the structure with the settings are encoded in that string. If a string is passed to which wasn't produced by the behavior is unspecified. An exception may or may not be thrown, and the contents of may or may not be affected. Note: the textual representation produced by is intended to be identical to that produced by the option of the GNU Coreutils version of the utility, on the same platform. That is to say, the output of may be used as input into and the output of may be used as an argument to The structure corresponds to the POSIX structure of the same name. An instance of this structure is returned by the function. The function corresponds to the POSIX function of the same name. It returns an instance of the structure. Each slot of the returned structure is initialized with a character string that identifies the corresponding attribute of the host system. The host system might not support the reporting of the NIS domain name. In this case, the slot of the returned structure will have the value The structure is required by the functions and It is analogous to the C structure by the same name described in POSIX. These variables correspond to the POSIX constants and They have the same values, and are suitable as slot values of the structure. Variables @, rlimit-core @, rlimit-cpu @, rlimit-data @, rlimit-fsize @, rlimit-nofile @ rlimit-stack and @ rlimit-as These variables correspond to the POSIX constants and so forth. The function retrieves information about the limits imposed for a particular parameter indicated by the integer. The function changes the limit information for a resource parameter. The parameter is the value of one of the variables and so forth. The argument is a structure of type If this argument is given to the function, then it fills in that structure with the retrieved parameters. Otherwise it allocates a new structure and fills that one. In either situation, the filled structure is returned, if the underlying call to the host operating system is successful. In the case of the object must have non-negative integer values which are in the range of the platform's type. If the underlying system call fails, then these functions throw an exception. In the successful case, the function returns the structure, and returns Further information about resource limits is available in the POSIX standard and platform documentation. The TXR Lisp interface to the POSIX family of functions is based around the type. The function returns a special variant of a object which keeps track of the memory mapping. When such an object becomes unreachable and is reclaimed by garbage collection, the mapping is automatically unmapped. In addition to the functions and are provided, all taking a as their leftmost argument. The TXR Lisp functions do not strictly follow the argument conventions of the same-named, corresponding POSIX functions. Adjustments which are likely to be defaulted are moved to the right. For instance, the operation is often applied to the entire memory mapping. Therefore, the first argument is the object which keeps track of the mapping. The second argument specifies the flags to be applied, which constitute the last argument of the underlying POSIX function. The remaining two arguments are the size and offset. If these are omitted, then applies to the entire region, whose address and size are known to the object. Cautionary note: misuse of and related functions can easily cause the TXR image to receive a fatal signal due to a bad memory access. Care must be taken to prevent such a situation, or else to catch such signals and recover. The function provides access to the same-named POSIX platform function for creating memory mappings. The POSIX function can be used for creating virtual memory views of files and special devices. Views can be read-only, and they can be mutable. They can be in such a way that changes appear only in the mapping itself, or in such a way that the changes are actually propagated to the mapped object itself. Mappings can be shared among processes, providing a shared memory mechanism: for instance, if is called, any mappings created by the parent are shared with the child: the child process does not get a copy of a shared mapping, but a reference to it. The function can also be used simply for allocating memory: on some platforms, the POSIX function is used as the basis for the function. It behaves as a pure allocator when asked to create a mapping which is private, and anonymous (not backed by an object). The TXR Lisp function is integrated with the type and the FFI type system. A mapping returned by is represented by a object. The required argument specifies the element type of the array; it must be a compiled FFI type. Note: this may be produced by the macro. For instance, the type may be specified using the expression The type must be a complete type suitable as the element type of an array; a type with a zero size such as is invalid. The argument specifies the requested length of the mapping in bytes. Note that allocates or configures virtual memory pages, not bytes. Internally to the system, the argument is converted to a number of pages. If it specifies a fractional number of pages, it is rounded up. For instance, if the page size is 4096 bytes, and is specified as 5000, it will be internally rounded up to 8192. The returned TXR Lisp object is oblivious to this padding: it works with the given 5000-byte size. Note: the variable holds the system's page size. However, by the use of extensions, it is possible for individual mappings to have their own page size. Mixed page size virtual memory systems exist. The function determines the number of elements in the array by dividing the by the size of using a division that truncates toward zero. The returned shall have that many elements. If the division is inexact, it means that some bytes from the underlying memory mapping are unused, even if is a multiple of the page size. The required argument must be some bitwise combination of the portable values and Additional system-specific values may be available also for specifying additional properties. If is specified as zero, then the mapping, if successfully created, may be inaccessible: must be present to ensure read access, and to ensure write access. The argument is a bitwise combination of values given by various variables. At the very least, it must contain exactly one of or to request a shared or private mapping, respectively. If a mapping is requested which is neither shared nor private, the underlying POSIX function will likely fail. If a is specified, indicating a filesystem object to be mapped, the flag must be omitted. Vice versa, if is not specified, this means that the mapping will be anonymous. In this situation, the flag must be present. The argument may be an integer file descriptor. If so, this value will be passed to the underlying POSIX function directly. The argument may be a stream object, in which case the function will be applied to it, which must retrieve an integer file descriptor which will be passed to the POSIX function. The argument may be a filename. The specified file is opened as if via with a which is if the argument includes the flag, otherwise The integer file descriptor from this open stream is used in the underlying call. The file is immediately closed when returns. In all cases, the integer file descriptor passed to the POSIX function must be a value suitable for conversion to the type. Note: in the context of "anonymous" means "not associated with a filesystem object referenced by a descriptor". It does not mean "without a name", but refers to a pure memory allocation from virtual memory. Memory maps do not have a name, whether anonymous or not. Moreover, the filesystem object associated with a memory map itself does not necessarily have a name. An open file that has been deleted from the directory structure is anonymous, yet a memory mapping can be created using its descriptor, and that mapping is not "anonymous". The argument is used with a non-anonymous mapping. It specifies that the mapping doesn't begin at the start of the file or file-like object, but rather at the specified offset. The offset may not be an arbitrary integer; it must be a multiple of the page size. Unless certain nonportable are used to specify an alternative page size, the value of the variable may be relied upon to indicate the page size. If an is specified for an anonymous mapping, with a nonzero value, the underlying POSIX function may indicate failure. If the and values cause one or more pages to be mapped which are beyond the end of the file, then accessing those pages may produce a signal which is fatal if not handled. The argument is used for specifying the address in conjunction with the flag. Possibly, certain nonportable values in the argument may similarly require If no bit is present in which requires then should either not be specified, or specified as zero. A nonzero value of must be a multiple of the page size. The function returns a object if successful. Upon failure, an exception derived from is thrown. Note: when a object returned by is identified by the garbage collector as unreachable, and reclaimed, the memory mapping is unmapped. The function can be invoked on the to release the mapping before the object becomes garbage. The function cannot be used on a mapped The function releases the memory mapping tracked by which must be an object previously returned by An exception is thrown if the object is any other kind of Note: the memory mapping is released by means of the same-named POSIX function. No provision is made for selectively unmapping the pages of a mapping; the entire mapping associated with a is removed. When the memory mapping is released, returns Thereafter, the contents may no longer be accessed, subject to exceptions being thrown. If is called again on a on which it had previously been successfully called, the additional calls return The functions and perform various operations and adjustments on a memory mapping, using the same-named, corresponding POSIX functions. All functions follow the same argument conventions with regard to the argument and the optional and arguments. The respective second arguments and are all integers. Of these, and are bitmapped flags, whereas specifies an enumerated command. The argument is a bitwise combination of values such as and The function adjusts the protection bits of the mapping accordingly. The argument of should specify one of the following portable values, or else some system-specific nonportable value: or The argument of should specify exactly one of the values and Additional values such as may be combined in. If and are omitted, they default to zero and the size of the entire mapping, respectively, so the operation applies to the entire mapping. If only is specified, it must not exceed the mapping size, or an error exception is thrown. The argument defaults to zero. If only is specified, it must not exceed the length of the mapping, or else an error exception is thrown. The size is calculated as the difference between the offset and the length. It may be zero. If both and are specified, they must not specify a region any portion of which lies outside the mapping. If is zero, may be equal to the length of the mapping. The must be a multiple of the page size, or else the operation will fail, since these functions work with virtual memory pages, and not individual bytes. The is adjusted by the system to a multiple of the applicable page size, as noted in the description of When any of these three functions succeeds, it returns Otherwise, it throws an exception. The integer values of these variables are bitmasks, intended to be combined with to prepare a value for the argument of Additional nonportable, system-dependent variables may be available. Their names are derived by taking the symbol from the platform header file, converting it to lowercase and replacing underscores by hyphen characters. Any such variable which exists, but has a value of zero, is present only for compatibility with other systems. For instance may be present in non-Linux ports of TXR, but with a zero value; it has a nonzero value on Linux systems to which it is specific. Applications critically relying on certain flags should test the corresponding variables for nonzero to make sure they are actually available. The integer values of these variable are bitmasks, intended to be combined with to prepare a value for the argument of and Additional nonportable, system-dependent variables may be available. Their names are derived by taking the symbol from the platform header file, converting it to lowercase and replacing underscores by hyphen characters. Any such variable which exists, but has a value of zero, is present only for compatibility with other systems. The integer values of these variable are bitmasks, intended to be combined with to prepare a value for the argument of the function. Additional nonportable, system-dependent variables may be available. Their names are derived by taking the symbol from the platform header file, converting it to lower case and replacing underscores by hyphen characters. Any such variable which exists, but has a value of zero, is present only for compatibility with another system. The integer values of these variable are bitmasks, intended to be combined with to prepare a value for the argument of the function. As described under exactly one of and should be present; is optional. These functions convert character strings to and from a form which is suitable for embedding into the request portions of URL syntax. Encoding a string for URL use means identifying in it certain characters that might have a special meaning in the URL syntax and representing it using "percent encoding": the percent character, followed by the ASCII value of the character. Spaces and control characters are also encoded, as are all byte values greater than or equal to 127 (7F hex). The printable ASCII characters which are percent-encoded consist of this set:
  :/?#[]@!$&'()*+,;=% More generally, strings can consists of Unicode characters, but the URL encoding consists only of printable ASCII characters. Unicode characters in the original string are encoded by expanding into UTF-8, and applying percent-encoding the UTF-8 bytes, which are all in the range Decoding is the reverse process: reconstituting the UTF-8 byte sequence specified by the URL-encoding, and then decoding the UTF-8 sequence into the string of Unicode characters. There is an additional complication: whether or not to encode spaces as plus, and to decode plus characters to spaces. In encoding, if spaces are not encoded to the plus character, then they are encoded as since spaces are reserved characters that must be encoded. In decoding, if plus characters are not decoded to spaces, then they are left alone: they become plus characters in the decoded string. The function performs the encoding process. If the argument is omitted or specified as then spaces are encoded as If the argument is a value other than then spaces are encoded as the character (plus). The function performs the decoding process. If the argument is omitted or specified as then (plus) characters in the encoded data are retained as characters in the decoded strings. Otherwise, plus characters are converted to spaces. The and functions convert between an HTML and raw representation of text. The function returns a string which is based on the content of but in which all characters which have special meaning in HTML have been replaced by HTML codes for representing those characters literally. The returned string is the HTML-encoded verbatim representation of The function converts which may contain HTML character encodings, into a string which contains the actual characters represented by those encodings. The function composition returns a string which is equal to The reverse composition does not necessarily return a string equal to For instance if html is the string then produces From this, produces The function is similar to except that it does not encode the single and double quote characters (ASCII 39 and 34, respectively). Text prepared by this function may not be suitable for insertion into a HTML template, depending on the context of its insertion. It is suitable as text placed between tags but not necessarily as tag attribute material. The function converts the UTF-8 representation of or the contents of to Base64 and returns that representation as a string. The Base64 encoding is described in RFC 4648, section 5. The second argument must either be a character string, or a buffer object. The functions performs the opposite conversion; it extracts the bytes encoded in a Base64 string, and decodes them as UTF-8 to return a character string. The extracts the bytes encoded in a Base64 string, and returns a new buffer object containing these bytes. The Base64 encoding divides the UTF-8 representation of or the bytes contained in into groups of six bits, each representing the values 0 to 63. Each value is then mapped to the characters to to the digits to and the characters and One or two consecutive occurrences of the character are added as padding so that the number of non-whitespace characters is divisible by four. These characters map to the code 0, but are understood not to contribute to the length of the encoded message. The function enforces this convention, but doesn't require these padding characters. Base64-encoding an empty string or zero-length buffer results in an empty string. If the argument is passed to then the Base64 encoded string, unless empty, contains newline characters, which divide it into lines which are long, except possibly for the last line. The and perform, respectively, bulk Base64 encoding and decoding between streams. This format is described in RFC 4648, section 5. The and arguments must be stream objects. The stream must support output. In the decode operation, it must support byte output. The stream must support input. In the encode operation it must support byte input. The function reads a sequence of bytes from the stream and writes characters to the stream comprising the Base64 encoding of that sequence. If the argument is specified, it must be a nonnegative integer. At most bytes will be read from the stream. If is omitted, then the operation will read from the stream without limit, until that stream indicates that no more bytes are available. The optional argument influences the formatting of Base64 output, in the same manner as documented for the function. The function reads the characters of a Base64 encoding from the stream and writes the corresponding byte sequence to the stream. It keeps reading and decoding until it encounters the end of the stream, or a character not used in Base64: a character that is not whitespace according to isn't any of the Base64 coding characters (not an alphanumeric character, and not one of the characters or If the function stops due to a non-Base64 character, that character is pushed back into the stream. The function returns the number of bytes encoded; the function returns the number of bytes decoded. The and functions conform, in nearly every respect, to the descriptions of, respectively, and The difference is that these functions use the encoding described in section 6 of RFC 4648, rather than section 5. This means that, in the encoding alphabet, instead of the symbols (plus) and (slash) the symbols (minus) and (underline) are used. The and functions conform, in nearly every respect, to the descriptions of, respectively, and The difference is that these functions use the encoding described in section 6 of RFC 4648, rather than section 5. This means that, in the encoding alphabet, instead of the symbols (plus) and (slash) the symbols (minus) and (underline) are used. The filter module provides a trie (pronounced "try") data structure, which is suitable for representing dictionaries for efficient filtering. Dictionaries are unordered collections of keys, which are strings, which have associated values, which are also strings. A trie can be used to filter text, such that keys appearing in the text are replaced by the corresponding values. A trie supports this filtering operation by providing an efficient prefix-based lookup method which only looks at each input character once, and which does not require knowledge of the length of the key in advance. The function creates an empty trie. There is no special data type for a trie; a trie is some existing type such as a hash table. The function adds the string to the trie, associating it with If already exists in then the value is updated with The must not have been compressed with A trie can contain keys which are prefixes of other keys. For instance it can contain and When a trie is used for matching and substitution, the longest match is used. If the input presents the text then the match is If the input is then matches. The function changes the representation of to a representation which occupies less space and supports faster lookups. The new representation is returned. The compressed representation of a trie does not support the function. The function destructively manipulates and may return an object that is the same object as or it may return a different object, while at the same time still modifying the internals of Consequently, the program should not retain the input object but use the returned object in its place. The function returns a context object for performing an open-coded lookup traversal of a trie. The argument is expected to be a trie that was created by the function. The function performs a one character step in a trie lookup. The argument must be a trie context returned by or by some previous call to The argument is the next character to match. If the lookup is successful (the match through the trie can continue with the given character) then a new trie context object is returned. The old trie context remains valid. If the lookup is unsuccessful, is returned. Note: determining whether a given string is stored in a trie can be performed looking up every character of the string successively with using the newly returned context for each successive operation. If every character is found, it means that either that exact string is found in the trie, or a prefix. The ambiguity can be resolved by testing whether the trie has a value at the last node using For instance, if is inserted into an empty trie with value then will look up successfully, being a prefix of however, the value at is indicating that is only a prefix of one or more entries in the trie. The function returns the value stored at the node in in the trie given by Nodes which have not been given a value hold the value The function returns a tree structure similar to in which all of the string atoms have been filtered through The argument is a string-tree structure: either the symbol denoting an empty structure; a string; or a list of tree structures. If is then returns The argument is a filter: it is either a trie, a function, or nil. If is then just returns If is a function, it must be a function that can be called with one argument. The strings of the string tree are filtered by passing each one into the function and substituting the return value into the corresponding place in the returned structure. Otherwise if is a trie, then this trie is used for filtering, the string elements similarly to a function. For each string, a new string is returned in which occurrences of the keys in the trie are replaced by the values in the trie. The function tests whether two string trees are equal under the given filters. The precise semantics can be given by this expression: The string tree is filtered through as if by the function, and similarly, is filtered through The resulting structures are compared using and the result of that is returned. The function returns a representation of as regular-expression abstract syntax, suitable for processing by the function. The values stored in the trie nodes are not represented in the regular expression. The may be one that has been compressed via in fact, a compressed results in more compact syntax. Note: this function is useful for creating a compact, prefix-compressed regular expression which matches a list of strings. The special variable holds a hash table which associates symbols with filters. This hash table defines the named filters used in the TXR pattern language. The names are the hash-table keys, and filter objects are the values. Filter objects are one of three representations. The value represents a null filter, which performs no filtering, passing the input string through. A filter object may be a raw or compressed trie. It may also be a Lisp function, which must be callable with one argument of string type, and must return a string. The application may define new filters by associating symbolic keys in with values which conform to the above representation of filters. The behavior is unspecified if any of the predefined filters are removed or redefined, and are subsequently used, or if the variable is replaced or rebound with a hash-table value which omits those keys, or associates them with different values. Note that functions and use, respectively, the HTML-related and It is useful to be able to invoke the abilities of the TXR pattern Language from TXR Lisp. An interface for doing this provided in the form of the function, which is used for invoking a TXR pattern function. The function has a cumbersome interface which requires the TXR Lisp program to explicitly deal with the variable bindings emerging from the pattern match in the form of an association list. To make it the interface easier to use, TXR provides the macros and The function invokes a TXR pattern function whose name is given by which must be a symbol. The argument is a list of expressions. The expressions may be symbols which will be interpreted as pattern variables, and may be bound or unbound. If they are not symbols, then they are treated as expressions (of the pattern language, not TXR Lisp) and evaluated accordingly. The optional argument is an object of one of several types. It may be a stream, character string or list of strings. If it is a string, then it is converted to a list containing that string. A list of strings represents zero or more lines of text to be processed. If the argument is omitted, then it defaults to interpreted as an empty list of lines. The argument is a list of filename specifications, which follow the same conventions as files given on the TXR command line. If the pattern function uses the directive, it can process these additional files. If this argument is omitted, it defaults to The function's return value falls into three cases. If there is a match failure, it returns Otherwise it returns a cons cell. The field of the cons cell holds the list of captured bindings. The of the cons cell is one of two values. If the entire input was processed, the cdr field holds the symbol Otherwise it holds another cons cell whose is the remainder of the list of lines which were not matched, and whose is the line number.
  @(define foo (x y))
  @x:@y
  @line
  @(end)
  @(do
     (format t "~s\n"
               (match-fun 'foo '(a b)
                        '("alpha:beta" "gamma" "omega") nil)))
  Output:
  (((a . "alpha") (b . "beta")) ("omega") . 3) In the above example, the pattern function is called with arguments These are unbound variables, so they correspond to parameters and of the function. If and get bound, those values propagate to and The data being matched consists of the lines and Inside and bind to and and then the line variable binds to The input stream is left with Hence, the return value consists of the bindings of and transferred to and and the second cons cell which gives information about the rest of the stream: it is the part starting at which is line 3. Note that the binding for the variable does not propagate out of the pattern function it is local inside it. The function returns or if, respectively, is the name of an existing pattern function. The macro invokes the TXR pattern-matching function on some input given by the parameter, whose semantics are the same as the argument of the function. If succeeds, then is evaluated, and if it fails, is evaluated instead. In the successful case, is evaluated in a scope in which the bindings emerging from the function are turned into TXR Lisp variables. The result of is that of In the failed case, is evaluated in a scope which does not have any new bindings. The result of is that of If is missing, the result is The forms supply arguments to the pattern function There must be as many of these arguments as the function has parameters. Any argument which is a symbol is treated, for the purposes of calling the pattern function, as an unbound pattern variable. The function may or may not produce a binding for that variable. Also, every argument which is a symbol also denotes a local variable that is established around if the function succeeds. For any such pattern variable for which the function produces a binding, the corresponding local variable will be initialized with the value of that pattern variable. For any such pattern variable which is left unbound by the function, the corresponding local variable will be set to Any can be a form other than a symbol. In this situation, the argument is evaluated, and will be passed to the pattern function as the value of the binding for the corresponding argument.
  @(define date (year month day))
  @{year /\d\d\d\d/}-@{month /\d\d/}-@{day /\d\d/}
  @(end)
  @(do
     (each ((date '("09-10-20" "2009-10-20"
                    "July-15-2014" "foo")))
       (txr-if date (y m d) date
         (put-line `match: year @y, month @m, day @d`)
         (put-line `no match for @date`))))
  Output:
  no match for 09-10-20
  match: year 2009, month 10, day 20
  no match for July-15-2014
  no match for foo The macro is based on It is equivalent to If the pattern function produces a match, then each is evaluated in the scope of the variables established by the expressions. The result of the form is that of the last If the pattern function fails then the forms are not evaluated, and the result value is The macro evaluates and then uses the value as an input to zero or more test clauses. Each test clause invokes the pattern function named by that clause's argument. If the function succeeds, then each is evaluated, and the value of the last is taken to be the result value of which terminates. If there are no forms, then terminates with a result. The forms are evaluated in an environment in which variables are bound based on the forms, with values depending on the result of the invocation of the pattern function, in the same manner as documented in detail for the macro. If the function fails, then the forms are not evaluated, and control passes to the next clause. A clause which begins with the symbol executes unconditionally and causes to terminate. If it has no forms, then yields otherwise the forms are evaluated in order and the value of the last one specifies the result of The value of the input is expected to be one of the same kinds of objects as given by the requirements for the argument of the functions. If evaluates to a stream object according to the function, then the stream is converted to a lazy list of lines, as if by invoking the function on that stream; that list then serves as input to the clauses. The function converts textual TXR query syntax into a Lisp data structure representation. The argument may be either a character string, or a stream. If it is omitted, then is used as the stream. The must provide the text representation of one complete TXR query. The optional argument can be used to specify a stream to which diagnostics of parse errors are sent. If absent, the diagnostics are suppressed. The optional argument can be used to specify the file name which is used for reporting errors. If this argument is missing, the name is taken from the name property of the argument if it is a stream, or else the word is used as the name if is a string. If there are no parse errors, the function returns the parsed data structure. If there are parse errors, and the parameter is present, its value is returned. If the parameter is not present, then an exception of type is thrown. These functions map an expression in a TXR program to the file name and line number of the source code where that form came from. The function returns the raw information as a cons cell whose consist of the line number, and file name. The function formats the information as a string. Forms which were parsed from a file have source location info tracking to their origin in that file. Forms which are the result of macro-expansion are traced to the form whose evaluation produced them. That is to say, they inherit that form's source location info. More precisely, when a form is produced by macro-expansion, it usually consists of material which was passed to the macro as arguments, plus some original material allocated by the macro, and possibly literal structure material which is part of the macro code. After the expansion is produced, any of its constituent material which already has source location info keeps that info. Those nodes which are newly allocated by the macro-expansion process inherit their source location info from the form which yields the expansion. If is not a piece of the program source code that was constructed by the TXR parser or by a macro, and thus it was neither attributed with source location info, nor has it inherited such info, then returns In the same situation, and if its argument is missing, the returns a string whose text conveys that the source location is not available. If the argument is present, it is returned. The function copies the source code location info ("rl" means "read location") from the object to the object. These objects are pieces of list-based syntax. If already has source code location info, then no copying takes place. The function copies the source code location info from into every cons cell in the tree structure which doesn't already have location info. It may be regarded as a recursive application of via recursion on the tree structure. However, the traversal performed by gracefully handles circular structures. Note: these functions are intended to be used in certain kinds of macros. If a macro transforms to this function can be used to propagate the source code location info also, so that when the TXR Lisp evaluator encounters errors in transformed code, it can give diagnostics which refer to the original untransformed source code. The macro expander already performs this transfer. If a macro call form has location info, the expander propagates that info to that form's expansion. In some situations, it is useful for a macro or other code transformer to perform this action explicitly. The Boolean special variable controls whether the and functions record source location info. The variable is by default, so that these functions do not record source location info. If it is true, then these functions record source location info. Regardless of the value of this variable, source location info is recorded for Lisp forms which are read from files or streams under the function or specified on the TXR command line. Source location info is also always recorded when reading the TXR pattern language syntax. Note: recording and propagating location info incurs a memory and performance penalty. The individual cons cells and certain other literal objects in the structure which emerges from the parser are associated with source location info via a global weak hash table. The function returns information about the macro-expansion ancestor of The ancestor is the original form whose expansion produced If is not the result of macro-expansion, or the ancestor information is unavailable, the function returns The operator evaluates the enclosed forms from left to right similarly to while determining the memory allocation requests and time consumed by the evaluation of the forms. If there are no forms, the prof operator measures the smallest measurable operation of evaluating nothing and producing If the evaluation terminates normally (not abruptly by a nonlocal control transfer), then yields a list consisting of: where is the value returned by the rightmost or if there are no forms, is the total number of bytes of all memory allocation requests (or at least those known to the TXR runtime, such as those of all internal objects), is the total number of bytes drawn from the garbage-collected heaps, and is the total processor time consumed over the execution of those forms. Notes: The bytes allocated by the garbage collector from the C function to create heap areas are not counted as includes storage such as the space used for dynamic strings, vectors and bignums (in addition to their gc-heap-allocated nodes), and the various structures used by the type objects such as streams and hashes. Objects in external libraries that use uninstrumented allocators are not counted: for instance the C streams. The (pretty-printing macro is similar to It evaluates and returns the rightmost one, or if there are no forms. Over the evaluation of it counts memory allocations, and measures CPU time. If terminate normally, then just prior to returning, prints these statistics in a concise report on the The macro relies on the operator. The function triggers garbage collection. Garbage collection means that unreachable objects are identified and reclaimed, so that their storage can be reused. The function returns if garbage collection is disabled (and consequently nothing is done), otherwise The Boolean argument, defaulting to indicates whether a full garbage collection should be requested. Even if this argument is a full garbage collection may occur due to having been scheduled. The function sets the GC delta parameter. Note: This function may disappear in a future release of TXR or suffer a backward-incompatible change in its syntax or behavior. When the amount of new dynamic memory allocated since the last garbage collection equals or exceeds the GC delta, a garbage collection pass is triggered. From that point, a new delta begins to be accumulated. Dynamic memory is used for allocating heaps of small garbage-collected objects such as cons cells, as well as the satellite data attached to some objects: like the storage arrays of vectors, strings or bignum integers. Most garbage collector behaviors are based on counting objects in the heaps. Sometimes a program works with a small number of objects which are very large, frequently allocating new, large objects and turning old ones into garbage. For instance a single large integer could be many megabytes long. In such a situation, a small number of heap objects therefore control a large amount of memory. This requires garbage collection to be triggered much more often than when working with small objects, such as conses, to prevent runaway allocation of memory. It is for this reason that the garbage collector uses the GC delta. There is a default GC delta of 64 megabytes. This may be overridden in special builds of TXR for small systems. The function registers to be invoked in the situation when is identified by the garbage collector as unreachable. A function registered in this way is called a finalizer. If and when this situation occurs, the finalizer will be called with as its only argument. Multiple finalizer functions can be registered for the same object, up to an internal limit which is not required to be greater than 255. If the limit is exceeded, throws an error exception. All registered finalizers are called when the object becomes unreachable. Finalizers registered against an object may also be invoked and removed using the function. If the argument isn't specified, or is then finalizer is registered at the end of the list. If is true, then the finalizer is registered at the front of the list. Finalizers which are activated in the same finalization processing phase are called in the order in which they appear in the registration list. After a finalization call takes place, its registration is removed. However, neither nor are reclaimed immediately; they are treated as if they were reachable objects until at least the next garbage collection pass. It is therefore safe for to store somewhere a persistent reference to or to itself, thereby reinstating these objects as reachable. A finalizer is itself permitted to call to register the original or any other object for finalization. Finalization processing can be understood as taking place in one or more rounds. At the start of each round, finalizers are identified that are to be called, arranged in order, and removed from the registration list. If this identification stage produces no finalizers, then finalization ends. Otherwise, those finalizers are processed, and then another round is initiated, to look for eligible finalizers that may have been registered during the previous round. Note: it is possible for the application to create an infinite finalization loop, if one or more objects have finalizers that register new finalizers, which register new finalizers and so on. Note: if a finalizer is invoked by the garbage collector rather than explicit finalization via and that finalizer calls to make a registration, that registration will not be eligible for processing in the same phase, because the criteria for finalization is unreachability. The function invokes and removes the finalizers, if any, registered against If any finalizers are called, it returns otherwise Finalization performed by works in the manner described under the specification of the function. It is permissible for a finalizer function itself to call Such a call can happen in two possible contexts: finalization initiated by by garbage collection, or under the scope of a invocation from application code. Doing so is safe, since the finalization logic may be reentered recursively. When finalizers are being called during a round of processing, those finalizers have already been removed from the registration list, and will not be redundantly invoked by a recursive invocation of finalization. Under the scope of garbage-collection-driven reclamation, the order of finalizer calls may not be what the application logic expects. For instance even though a finalizer registered for some object itself invokes it may be the case during GC reclamation that both and are identified as unreachable objects at the same time, and some or all finalizers registered against have already been called before the given finalizer performs the explicit invocation against Thus the call either has no effect at all, or only calls some remaining finalizers that have not yet been processed, rather than all of them, as the application expects. The application must avoid creating a dependency on the order of finalization calls, to prevent the situation that the finalization actions are only correct under an explicit but incorrect under spontaneous reclamation driven by garbage collection. TXR features a rudimentary mechanism for guarding against stack overflows, which cause the TXR process to crash. This capability is separate from and exists in addition to the possibility of catching a (segmentation violation) signal upon stack overflow using The stack-overflow guard mechanism is based on TXR, at certain key places in the execution, checking the current position of the stack relative to a predetermined limit. If the position exceeds the limit, then an exception of type derived from is thrown. The stack-overflow guard mechanism is configured on startup. On platforms where it is possible to inquire the system's actual stack limit, and where the stack limit is at least 512 kilobytes, TXR sets the limit to within a certain percentage of the actual value. If it is not possible to determine the system's stack limit, or if the system indicates that the stack size is unlimited, then a default limit is imposed. If the system's limit is configured below a certain small value, then that small value is used as the stack limit. The and functions are provided to manipulate the stack limit. The mechanism cannot contain absolutely all sources of stack-overflow threat under all conditions. External functions are not protected, and not all internal functions are monitored. If TXR is close to the limit, but a function is called whose stack growth is not monitored, such as an external function or unmonitored internal function, it is possible that the stack may overflow anyway. The returns the current value of the stack limit. If the guard mechanism is not enabled, it returns otherwise it returns a positive integer, which is measured in bytes. The configures the stack limit according to possibly enabling or disabling the guard mechanism, and returns the previous stack limit in exactly the same manner as The must be a non-negative integer or else the symbol The values zero or disable the guard mechanism. Positive integer values set the limit. The value may be truncated to a multiple of some denomination or otherwise adjusted, so that a subsequent call to need not retrieve that exact value. If is too close to the system's stack limit or beyond, the effectiveness of the stack-overflow detection mechanism is compromised. Likewise, if is too low, the operation of TXR shall become unreliable. Values smaller than 32767 bytes are strongly discouraged. This variable holds the invocation pathname of a TXR program that was specified on the command line. The value of when TXR Lisp expressions are being evaluated in command-line arguments is the string The value of when a TXR query is supplied on the command line via the command-line option is the string When a file is being compiled using the option, the value of is the source file path. When the interactive listener is entered, is set to the value even if prior to that, a file was compiled or executed, for which had been set to the name of that file. Note that for programs read from a file, holds the resolved name, and not the invocation name. For instance if is invoked using the name whereby TXR infers the suffix, then holds the suffixed name. Note that the functions and have no effect on the value of The variable is set strictly by command line processing. The variable expands to the directory where the TXR standard library is installed. It includes the trailing slash. Note: there is no need to use the value of this variable to load library modules. Library modules are keyed to specific symbols, and lazily loaded. When a TXR Lisp library function, macro or variable is referenced for the first time, the library module which defines it is loaded. This includes references which occur during the code expansion phase, at "macro time", so it works for macros. In the middle of processing a syntax tree, the expander may encounter a symbol that is registered for autoloading, and trigger the load. When the load completes, the symbol might now be defined as a macro, which the expander can immediately use to expand the given form that is being traversed. The function causes a file containing TXR Lisp or TXR code to be read and processed. The argument is a string. The function can load TXR Lisp source files as well as compiled files. Firstly, the value in is converted to a tentative pathname as follows. If specifies a pure relative pathname, as defined by the function, then a special behavior applies. If an existing load operation is in progress, then the special variable has a binding. In this case, will assume that the relative pathname is a reference relative to the directory portion of that pathname. If has the value then a pure relative pathname is used as-is, and thus resolved relative to the current working directory. Once the tentative pathname is determined, determines whether the name is suffixed. The name is suffixed if it ends in any of these four suffixes: or Depending on whether the tentative pathname exists, and whether or not it is suffixed, tries to make one or more attempts to open several variations of that name. These variations are called actual paths. If any attempt fails due to an error other than non-existence, such as a permission error, then no further attempts are made; the error exception propagates to caller. Regardless of whether the tentative pathname is suffixed, tries to open a file by that actual pathname first. If that attempt fails for a suffixed pathname, or fails due to a reason other than non-existence, no other names are tried. If an unsuffixed tentative pathname refers to a nonexistent file, is appended to the name, and an attempt is made to open a file with the resulting path. If that file is not found, then the suffixes and are similarly tried. If the above initial attempts to find the file fail, and the failure is due to the file not being found rather than some other problem such as a permission error, and isn't an absolute path according to then additional attempts are made by searching for the file in the list of directories given in the variable. For each directory taken from this variable, the directory is combined with the relative as if using the function, and the resulting path is tried, with all the same suffix probing that is performed by the initial attempts. If any such a path is pure relative, it is interpreted relative to the current working directory, and not relative only the initial attempts have that special behavior. An exception is thrown if a file is not found, or if any attempt to open a file results in an error other than non-existence. If an unsuffixed file is successfully opened, its contents are treated as interpreted Lisp. Files ending in are also treated as interpreted Lisp. Files ending in are treated as compiled Lisp, and those ending in are treated as the TXR Pattern Language. The suffix denotes a file which is expected to be compressed in the format, and to contain compiled Lisp. If the file is treated as TXR Lisp, then Lisp forms are read from it in succession. Each form is evaluated as if by the function, before the next form is read. If a syntax error is encountered, an exception of type is thrown. If a file is treated as a compiled TXR Lisp object file, then the compiled images of top-level forms are read from it, converted into compiled objects, and executed. If the file treated as TXR Pattern Language code, then its contents are parsed in their entirety. If the parse is successful, the query is executed. Previous TXR pattern variable and function bindings are in effect. If the query binds new variables and functions, these emerge from the and take effect. If the parse is unsuccessful, an exception of type is thrown. Parser error messages are directed to the stream. Over the evaluation of either a TXR Lisp, compiled file, or TXR file, establishes a new dynamic binding for several special variables:
    This variable is bound to the actual pathname being loaded. The values of the arguments which follow are combined into a list which is bound to By this mechanism, can pass arguments to the loaded file. is given a new dynamic binding, whose value is the same as its existing binding. Thus if the processing of the loaded file has the effect of altering the value of that effect will be undone when the binding is removed after the load completes.

    Over the evaluation of either a TXR Lisp, compiled file, or TXR file, establishes a block named which makes it possible for the loaded module to abort the loading using the expression. In this situation, the value of will appear as the return value of the function. When a TXR Lisp file, or compiled file, is executed from the TXR command line in such a way that TXR will terminate when that file's last form has been evaluated, then if that file performs a the block, the value of will turn into the termination status in exactly the same way as if that value were used as an argument to the function. However, if TXR has been instructed to enter into the Listener after executing the file, then the value of is discarded. A block named is also established by the directive in the pattern language. That directive provides no access to the returned value. The block is also visible to the file processed from the command line. When a such a file aborts the load via the returned value is discarded. If the interactive option was specified, the interactive listener will be entered, otherwise the process will terminate successfully. When the function terminates normally after processing a file, it returns If the file contains a TXR pattern query which is processed to completion, the matching success or failure of that query has no bearing on the return value of Note that this behavior is different from the directive which itself fails if the loaded query fails, causing subsequent directives not to be processed. A TXR pattern language file loaded with the Lisp function does not have the usual implicit access to the command-line arguments, unlike a top-level TXR query. If the directives in the file try to match input, they work against the stream. The directive behaves as it does when no more arguments are available. If the source or compiled file begins with the characters usually indicating a hash-bang script, reads reads the first line of the file and discards it. Processing of the file then begins with the first byte following that line. Two or more files produced by the same version of TXR may be catenated together (for instance, using the function) to produce a single file. Such a combined file can be loaded with the function. The same is true of files, because the format supports catenation. Mixing is not possible: and files cannot be catenated together. Note: this is a single operation: all of the binding and unbinding of variables like and is performed once over the entire contents of the combined file, and any are performed one time after the load operation. Therefore it is possible that the load-time behavior differs from that of loading the original files individually. The is bound to the name of the combined file. The special variable has a top-level value which is When a file is being loaded, it is dynamically bound to the pathname of that file. This value is visible to the forms are evaluated in that file during the loading process. The variable is bound when a file is loaded from the command line. If the command-line option is used to enter the interactive listener, and a file to be loaded is also specified, then the variable remains bound to the name of that file inside the listener. The function establishes a binding for prior to processing and evaluating all the top-level forms in the target file. When the forms are evaluated, the binding is discarded and returns. The function also establishes a binding for The directive, also similarly establishes a binding around the parsing and processing of a loaded TXR source file. Also, during the processing of the profile file (see Interactive Profile File), the variable is bound to the name of that file. The variable holds a list of directories which are searched for a file to be loaded by the function, the and directives, as well as by TXR's command line processing. Each of these situations first searches for a file in its characteristic way. If that fails due to the file not being found, and the name is a relative path, then the directories in are probed, in order. The variable is initialized to a list which contains exactly one directory: a directory dynamically calculated relative to TXR the executable location. Then intent is that third-party library modules may be installed there, and easily found by For more information, see the section Deployment Directory Structure. The isn't influenced by any environment variables, which is deliberate. If a system has multiple installations of different versions of TXR in different locations, an environment variable intended for one installation could be mistakenly used by the others, resulting in chaos. The variable is at the centre of a mechanism which associates the deferred execution of actions, associated with a loaded module or program termination. The application may push values onto this list which are expected to be functions, or objects that may be called as functions. These objects must be capable of being called with no arguments. In the situations specified below, the list of functions is processed as follows. First is examined, the list which it holds is remembered. Then the variable is reset to following which the remembered list is traversed in order. Each of the functions in the list is invoked, with no arguments. The list is processed, as described above, whenever the function terminates, whether normally or by throwing an exception. In this situation, the variable which is accessed is that binding which was established by that invocation of The execution of the functions from the list takes place in the dynamic environment of the all of the dynamic variable bindings established by that are still visible, including that of The list is also processed after processing a TXR or TXR Lisp file that is specified on the command line. If the interactive listener is also being entered, this processing of occurs prior to entering the listener. This situation occurs in the context of the top-level dynamic environment, and so the global value of is referenced. Lastly, is also processed if the TXR process terminates normally, regardless of its exit status. This processing executes in whatever dynamic environment is current at the point of exit, using its value of the variable is used. It is unspecified whether, at exit time, the functions are executed first, or whether the functions registered by are executed first. However, their executions do not interleave. Note that is not processed after the listener reads the file. Hooks installed by the profile file will activate when the process exits. The function loads multiple files, passing down the current to each one. It may be invoked with a single argument which is a list of files, or else it may be given multiple arguments which are files. Each is passed to the function, along with extra arguments coming from the current value. Note: the purpose of is to support a module organization of system whereby modules have local top-level files that respond to various actions specified via actions such as compiling, loading or cleaning. The function allows such modules to not only perform the actions requested in locally, but also pass it down to submodules which then do the same. The function performs one of several actions over the specified files, those actions being distinguished by the value in In addition, some of the actions are also performed for the file indicated in the current value of It may be invoked with a single argument which is a list of files, or else it may be given multiple arguments which are files. If there is exactly one argument in the function responds to the following values of that argument:
    First, the current file in is processed with Then each file in the argument list is also processed with Whenever that function returns for any file, that file is loaded with No additional arguments are passed to this invocation. The current file in as well as the files passed as arguments, are processed with

    Any other value of causes the function to the files passed in the argument, as if by Note: The function supports a protocol for organizing a program into library modules. Suppose a module located in the path consists of the files and Further, suppose that there are two submodules in the directory relative to this directory: and Then the application might have a file called with this content:
      (compile-only
        (load-args-recurse
          "utils/database/db"
          "utils/date/date")
        (load-args-process
          "command"
          "data"
          "reports"
          "main")) Furthermore, the module similarly provides a file with this content:
      (compile-only
        (load-args-process
          "postgres"
          "mariadb"
          "sqlite")) Lastly, the module provides a file with this content:
      (compile-only
        (load-args-process
          "src/date.tl")) Then, to load the application and the submodules, all that is needed is Furthermore, the modules may be compiled using Now the being passed is which tells every invocation to compile the file in which it occurs as well as its arguments. First, the module's call is executed, causing the and modules to compile. First, the module's top file is compiled, if necessary, and then likewise the and files. Then the module is similarly processed, due to its own invocation of Finally the call in the module compiles and If the keyword is passed via instead of then compiled files are recursively removed. The next time the application is loaded, source files will be loaded rather than compiled files. Note that the and forms are placed into a form so that the file compiler refrains from executing them. The and macros work with the list. The macro's arguments are zero or more These forms are converted into the body of an anonymous function, which is pushed onto the list. The return value is the new value of The removes the first item from The return value is the new value of The following equivalences hold:
      (push-after-load ...) <--> (push (lambda () ...) *load-hooks*)
      (pop-after-load) <--> (set *load-hooks* (cdr *load-hooks*)) The macro takes multiple arguments, each of which is a three-element clause. Each clause specifies that a given file is to be conditionally loaded based on whether a symbol has a certain kind of binding. Each argument clause has the syntax where is one of the five symbols or The element is a symbol suitable for use as a variable, function or structure name, and is an expression which is evaluated to produce a value that is suitable as an argument to the function. First, all expressions in all clauses are unconditionally evaluated in left-to-right order. Then the clauses are processed in that order. If the symbol of a clause is then tests whether has a binding in the variable namespace using the function. If a binding does not exist, then the value of the expression is passed to the function. Otherwise, is not called. Similarly, if is the symbol then is instead tested using if is then is tested using if is then is tested using and if is then is tested using When invokes the function, it confirms whether loading file has had the expected effect of providing a definition of of the right If this isn't the case, an error is thrown. The function is invoked with any arguments specified in the clause. The expressions of all clauses are unconditionally evaluated in order before performs any other action. The function returns the value returned by the rightmost that was actually performed. If no loads are performed, it returns This variable holds the absolute pathname of the executable file of the running TXR instance. The special variable holds a stream to which all trace output is sent. Trace output consists of diagnostics enabled by the macro. The and macros control function tracing. When is called with one or more arguments, it considers each argument to be the name of a global function. For each function, it turns on tracing, if it is not already turned on. If an argument denotes a nonexistent function, or is invalid function name syntax, terminates by throwing an exception, without processing the subsequent arguments, or undoing the effects already applied due to processing the previous arguments. When is called with no arguments, it lists the names of functions for which tracing is currently enabled. In other cases it returns When is called with one or more arguments, it considers each argument to be the name of a global function. For each function, it turns off tracing, if tracing is enabled. When is called with no arguments, it disables tracing for all functions. The macro always returns and silently tolerates arguments which are not names of functions currently being traced. Tracing a function consists of printing a message prior to entry into the function indicating its name and arguments, and another message upon leaving the function indicating its return value, which is syntactically correlated with the entry message, using a combination of matching and indentation. These messages are posted to the stream. When traced functions call each other or recurse, these trace messages nest. The nesting is detected and translated into indentation levels. Tracing works by replacing a function definition with a trace hook function, and retaining the previous definition. The trace hook calls the previous definition and produces the diagnostics around it. When is used to disable tracing, the previous definition is restored. Methods can be traced; their names are given using syntax: see the function. Macros can be traced; their names are given using syntax. Note that will not show the destructured internal macro arguments, but only the two arguments passed to the expander function: the whole form, and the environment. The and functions return The function provides access to the POSIX C library function of the same name. The argument to the optional parameter may be a character string, or If it is then the POSIX function is called with a null pointer for its name argument, returning the handle for the main program, if possible. The argument should be expressed as some bitwise combination of the values of the variables or other variables which give names to the flags. If the argument is omitted, the default value used is If the function succeeds, it returns an object of type which represents the open library handle ("dlhandle"). Otherwise it throws an exception, whose message incorporates, if possible, error text retrieved from the POSIX function. The handle returned by will automatically be subject to when reclaimed by the garbage collector. The closes the library indicated by which must be a object previously returned by The handle is closed by passing the stored pointer to the POSIX function. The internal pointer contained in the object is then reset to null. It is permissible to invoke more than once on a object which was created by The first invocation resets the object's pointer to null; the subsequent invocations do nothing. The function returns if the POSIX function reports a successful result (zero), otherwise it returns It also returns if invoked on a previously closed, and hence nulled-out handle. The function provides access to the same-named POSIX function. The function provides access to the same-named GNU C Library function, if available. The argument must be a handle previously returned by and not subsequently closed by or altered in any way. The and arguments are character strings. If these functions succeed, they return a value which holds the address of the symbol which was found in the library. If they fail, they return a object containing a null pointer. The and functions are alternatives to and respectively. Instead of returning a null on failure, these functions throw an exception. These variables provide the same values as constants in the POSIX C library header named etc. The macro exists in support of the JSON literal and quasiliteral and notations, which use the macro as their target abstract syntax. The macro transforms itself by deleting the symbol, producing either the quote syntax, or else the quasiquote syntax, depending on which quoting symbol is present. If the application produces and expands a macro form which does not conform to this syntax, or does not specify one of the above two quoting symbols, the behavior is unspecified. The function converts into JSON notation, and writes that notation into as a sequence of characters. If is an external stream such as a file stream, then the JSON is rendered by conversion of the characters into UTF-8, in the usual manner characteristic of those streams. The behavior is unspecified if or any component of is an object incompatible with the JSON representation conventions. An exception may be thrown. An object conforms to the JSON representation conventions if it is:

    1.  one of the symbols or which map to the JSON keywords and respectively.

    2.  a floating-point number or integer.

    3.  a character string.

    4.  a vector or list of JSON-conforming objects.

    5.  a hash table whose keys and values are JSON-conforming objects.

    Note that unless the keys in a hash table are all strings, nonstandard JSON is produced, since RFC 8259 requires JSON object keys to be strings. A list of object is rendered in the same way as vector, in the JSON notation. When such JSON notation is parsed, a vector is produced. When integer objects are output, they may not constitute valid JSON, since the JSON specification supports only IEEE 64 bit floating-point numbers. JSON numbers are read as floating-point. If the argument is present and has a true value, then the JSON is generated without any line breaks or indentation. Otherwise, the JSON output is subject to such formatting. The difference between and is that the latter emits a newline character after the JSON output. When a string object is output as JSON string syntax, the following rules

    1.  The characters (backslash, reverse solidus) and (double quote) are preceded by a backslash escape.

    2.  The characters U+0008 (BS), U+0009 (TAB), U+000A (LF), U+000C (FF) and U+000D (CR) are rendered as, respectively, and

    3.  If the character sequence occurs in a string, then in the JSON representation the slash is escaped, such that the sequence is rendered as Instances of (forward slash, solidus) in other situations are unescaped. Rationale: this is a feature of JSON which allows for safer embedding of the resulting JSON into HTML tags.

    4   If the character sequence occurs in a string, then in the JSON representation, the sequence is rendered as Instances of (exclamation mark) in other situations are not encoded. Rationale: safe embedding in HTML tags.

    5   If the character sequence occurs in a string, then in the JSON representation, the sequence is rendered as Instances of (hyphen) in other situations are not encoded. Rationale: safe embedding in HTML tags.

    6.  The code point U+DC00 (TXR's pseudo-null character) is translated into the escape syntax.

    7.  The code points U+DC01 through U+DCFF are send to the stream as-is. If the stream performs UTF-8 encoding, these characters turn into individual bytes in the range 0 to 255.

    8.  Control characters in the U+0001 to U+001F other than the ones subject to rule 1 above are rendered as escape sequences. Likewise, code points in the range U+007F to U+00BF, the range U+D800 to U+DBFF, U+DD00 to U+DFFF, and the code points U+FFFE and U+FFFF are also encoded as escape sequences.

    9.  A character outside of the BMP (Basic Multilingual Plane) in the range U+10000 to U+10FFFF is encoded using as a pair of consecutive escape sequences, specifying the code points of a UTF-16 surrogate pair encoding that character. This representation is described in RFC 8259.
The and functions return Some of the JSON-related functions carry a optional parameter. These functions open a file as if using the function, using a appropriate to their direction of data transfer. If an argument is given to it specifies the part to be added to the The function converts into JSON notation, returned as a character string. The function can be understood as constructing a string output stream, calling the function to write the object into that stream, and then retrieving and returning the constructed string. The argument is passed to The function closely resembles the function, and follows the same argument and error reporting conventions. Rather than reading a Lisp object from the input source, it reads a JSON object, with support for TXR's JSON extensions. If an object is successfully read, its Lisp representation is returned. JSON numbers produce floating-point number objects. JSON strings produce string objects. The keywords and map to the Lisp symbols and respectively. JSON objects map to hash tables, and JSON arrays to vectors. The function writes multiple JSON representations into The objects are specified by the argument, which must be an iterable object. The function iterates over and writes each element to the stream as if by using the function. Consequently, a newline character is written after each object. If the argument is not specified, the parameter takes on the value of The argument has the same meaning as in with regard to the individual elements. If it is specified and true, then exactly as many lines of text are written to as there are elements in The function returns The function reads zero or more JSON representations from until an end-of-stream or error condition is encountered. If is a character string, then the input takes place from a stream created from the character string using Otherwise, if is specified, it must be an input stream supporting byte input; input takes place from that stream. If the argument is omitted, it defaults to The objects are read as if by calls to and accumulated into a list. If the end-of-stream condition is read, then the list of accumulated objects is returned. If an error occurs, then an exception is thrown and the list of accumulated objects is not available. If an end-of-stream condition occurs before any character is seen other than JSON whitespace, then the empty list is returned. The and function open a text stream over the file indicated by the string argument for reading. The functions ensure that the stream is closed when they terminate. The function invokes to read a single JSON object, which is returned if that function returns normally. The function invokes to retrieve a list of JSON objects from the stream, which is returned if that function returns normally. The and functions open a text stream over the file indicated by the string argument using the function with a argument of write the argument object into the stream in their specific manner, and then close the stream. The function writes a JSON representation of using the function. The argument is passed to that function, defaulting to The value returned is that of The function writes zero or more JSON representations of objects from which must be an iterable object, using the function. The argument is passed to that function, defaulting to The value returned is that of The and are identical in almost all requirements to the functions and The only difference is that when these functions open a text stream using they specify a argument of rather than in order to append data to the target file rather than overwrite it. The and functions opens text stream over an input command pipe created for the command string as if by the function. They ensure that the stream is closed when they terminate. The function calls on the stream, and returns the value returned by that function. Similarly, function calls on the stream, and returns the value returned by that function. The and functions open an output text stream over an output command pipe created for the command specified in the string argument using the function function, write the argument object into the stream, in their specific manner, and then close the stream. The function writes a JSON representation of using the function. The argument is passed to that function, defaulting to The value returned is that of The function writes zero or more JSON representations of objects from which must be an iterable object, using the function. The argument is passed to that function, defaulting to The value returned is that of The variable controls the formatting style exhibited by and related functions. The initial value of this variable is If the value is the keyword symbol then a widely-used format is used, in which the opening and closing braces and brackets of vectors and dictionaries are printed on separate lines, as are the elements of those objects. If the variable has any other value, including the initial value then a default format is used in which braces, brackets and elements appear on the same line, subject to automatic breaking and indentation, similar to the way Lisp nested list structure is printed. This dynamic variable, initialized to a value of controls whether the parser is tolerant to certain non-conformances in the syntax of JSON data, which are ordinarily syntax errors. If the value of this variable is true, then the last element in a JSON array or the last element pair in a JSON object may be followed by spurious trailing comma, which is ignored. Note: in the future, the variable may be extended to enable other instances of tolerance in the area of JSON parsing.
  (get-json "{ 3:4, }") -> ;; syntax error
  (let ((*read-bad-json* t))
    (get-json "{ 3:4, }"))
  --> #H(() (3.0 4.0)) On platforms where it is supported, TXR provides a feature called the foreign function interface, or FFI. This refers to the ability to interoperate with programming interfaces which are defined by the binary data type representations and calling conventions of the platform's principal C language compiler. TXR's FFI module provides a succinct Lisp-based type notation for expressing C data types, together with memory-management semantics pertinent to the transfer of data between software components. The notation is used to describe the arguments and return values of functions in external libraries, and of Lisp callback functions that can be called from those libraries. Driven by the compiled representation of the type notation, the FFI module performs transparent conversions between Lisp data types and C data types, and automatically manages memory around foreign calls and incoming callbacks, for many common interfacing conventions. The FFI module consists of a library of functions which provide all of its semantics. On top of these functions, the FFI module provides a number of macros which comprise an expressive, convenient language for defining foreign interfaces. The FFI module supports passing and returning both structures and arrays by value. Passing arrays by value isn't a feature of the C language syntax; from the C point of view, these by-value array objects in the TXR FFI type system are equivalent to C arrays encapsulated in A type is provided for situations when foreign code generates arrays of undeclared, dynamic length, other than strings, and returns these arrays by the usual convention of pointer to the first element. The handling of requires more responsibility from the application. The FFI feature is inherently unsafe. If the FFI type language is used to write incorrect type definitions which do not match the actual binary interface of a foreign function, undefined behavior results. Improper use of FFI can corrupt memory, creating instability and security problems. It can also cause memory leaks and/or use-after-free errors due to inappropriate deallocation of memory. The implicit memory management behaviors encoded in the FFI type system are convenient, but risky. A minor declarative detail such as writing instead of in the middle of some nested type can make the difference between correct code and code which causes a memory leak, or instability by freeing memory which is in use. FFI developers are encouraged to unit test their FFI definitions carefully and use tools such as Valgrind to detect memory misuses and leaks. When a function call takes place from the TXR Lisp arena into a foreign library function, argument values must be prepared into the foreign representation. This takes place by converting Lisp objects into stack-allocated temporary buffers representing C objects. For aggregate objects containing pointers, additional buffers are allocated dynamically. For instance, suppose a structure contains a string and is passed by value. The structure will be converted to a stack-allocated equivalent C structure, in which the string will appear as a pointer. That pointer may use dynamically allocated (via string data. The operation which prepares argument material before a foreign function call is the put operation. In FFI callback dispatch, the operation which propagates the callback return value to the foreign caller is also the put operation. After a foreign function call returns from a foreign library back to the TXR Lisp arena, the arguments have to be examined one more time, because two-way communication is possible, and because some of the material has temporary, dynamically allocated buffers associated with it which must be released. For instance a structure passed by pointer may be updated by the foreign function. FFI needs to propagate the changes which the foreign function performed to the C version of the structure, back to the original Lisp structure. Furthermore, a structure passed by pointer uses a dynamically allocated buffer. This buffer must be freed. The operation which handles the responsibility for propagating argument data back into TXR Lisp objects, and frees any temporary memory that had been arranged by the put operation is the in operation. The in operation has two nuances: the by-value nuance and the by-pointer nuance. Data passed into a function by value such as function arguments or via are subject to the by-value nuance. Updates to the foreign representation of these objects does not propagate back to the Lisp representation; however, those objects may contain pointers requiring the by-pointer nuance of the in operation of those pointers to be invoked. After a foreign call completes, it is also necessary to retrieve the call's return value, convert it to a Lisp object, and free any dynamic memory. This is performed by the get operation. The get operation is also used by a Lisp callback function, called from a foreign library, to convert the arguments to Lisp objects. When a Lisp callback invoked by a foreign library completes, it must provide a return value, and also update any argument objects with new values. The return value is propagated using the put operation. Updates to arguments are performed by the operation. This operation is like the reverse of the in operation. Like that operation, it has a by-value and by-pointer nuance. For instance, if a callback receives a structure by value, upon return, there is no use in reconstructing a new version of the structure from the updated Lisp structure; the caller will not receive the change. However, if the structure contains pointers to data that was updated, by the callback, those changes must materialize. This is achieved by triggering the by-value nuance of the structure type's out operation, which will recursively invoke the out operation of embedded pointers, which will in turn invoke the by-pointer nuance. The FFI type system consists of a notation built using Lisp syntax. Basic, unparametrized types are denoted by symbolic atoms. Similarly to a concept in the C language, names can be globally defined, using the function, or the macro. Like in the C language, names are aliases for an existing type, and not distinct types. However, this is of no consequence, since the FFI doesn't perform any type checking between two foreign types, and thus never takes into consideration whether two such types are equal. The main concern in FFI is correspondence between Lisp values and foreign types. For instance, a Lisp string argument will not convert to a foreign function parameter of type Compound expressions denote the construction of derived types, or types which are instantiated with parameters. Each such expression has a type constructor symbol in the operator position, from a limited, fixed vocabulary, which cannot be extended. Some constituents of compound type syntax are expressions which evaluate to integer values: the dimension value for array types, the size for buffers, the width for bitfields and the value expressions for enumeration constants are such expressions. These expressions allow full use of TXR Lisp. They are evaluated without visibility into any apparent surrounding lexical scope. Some predefined types which are provided are in fact typedef names. For instance, the type is a typedef name for some other integral type, defined in a platform-specific way. Which type that is may be determined by passing the syntax to the type compiler function using the expression The type compiler converts the syntax to the compiled type object, resolving the typedef name to the type which it denotes. The printed representation of that object reveals the identity of the type. For instance, it might be indicating that is an alias for the basic type, which corresponds to the C type These first two of these types, and correspond to the C character type The and types correspond to Both Lisp integers and character values convert to these representations if they are in their numeric range. Out-of-range values produce an exception. A foreign and value converts to a Lisp character, whereas a value converts to an integer. If these types are used for representing individual scalar values, there is no difference among and What is different among these three types is that the and type constructors treat them specially. Arrays of these types are subject to conversion to and from Lisp strings. The variation among these types expresses different conversion semantics. That is to say, an array of converts between the foreign and native Lisp representation differently from an array of which in turn converts differently from an array of Note: it is recommended to avoid using the types and other than for expressing the element type of an or These types correspond to the C integer types and Lisp characters and integers convert to these foreign representations, if they are in their numeric range. Foreign values of these types convert to Lisp integers. These types are names for integer types whose representation corresponds to the C types and These types correspond to 8-bit signed and unsigned integers. They convert like integer types: both Lisp integers and characters convert to these types, if in a suitable range; and under the reverse conversion, the foreign values become Lisp integers. These types correspond denote precisely sized C integer types. They convert like integer types: both Lisp integers and characters convert to these types, if in a suitable range; and under the reverse conversion, the foreign values become Lisp integers. These types correspond to the same-named C types. They convert Lisp integers, characters and floating-point numbers to these C types. Because the TXR Lisp is represented as a C it converts directly to without the possibility of range error or loss of precision. A conversion to type is subject to a range check; an exception is thrown if the Lisp floating-point value is out of range of this type. Even when the conversion is possible, it alters the value, results in a loss of precision. In the reverse direction, values of both types convert to the one and only TXR Lisp type. The type is a typedef name for the instance of the parametrized type, which is to say, The FFI type denotes the machine representation of a Lisp value cell, which is corresponds to a C pointer. Not all cell values are actually pointers, but values that are heap objects, such as vectors and conses, are. The type transparently converts any Lisp object to a foreign pointer value with no representation change at all; and performs the reverse conversion from pointer to Lisp value. Note: this is utterly dangerous. Lisp values that aren't pointers must not be dereferenced by foreign code. Foreign code must not generate Lisp pointer values that aren't objects which came from a Lisp heap. Interpreting a Lisp value in foreign code requires a correct decoding of its type tag, and, if necessary, stripping the tag bits to recover a heap pointer and interpreting the type code stored in the heap object. The conversion from foreign bit pattern to Lisp value is subject to a validity checks; an exception will be thrown if the bit pattern isn't a valid Lisp object. Nevertheless, the checks has cases which report as false positives: admit some invalid objects may be admitted into the Lisp realm, possibly with catastrophic results. This type corresponds to a foreign pointer of any type, including a pointer to a function. The type converts between a foreign pointer and a Lisp object of type Lisp objects of type are tagged with a symbolic tag, which may be The unparametrized converts foreign pointers to objects which are tagged with In the reverse direction, it converts Lisp objects of type to foreign pointer, without regard for their type tag. There is a parametrized version of the FFI type, which provides a measure of type safety. Note: the type, in the context of FFI, is particularly useful for representing C pointers that are used in C library interfaces as "opaque" handles. For instance a FFI binding for the C functions and may use the to represent the type. That is to say, can be specified as the return type for thereby capturing the stream handle in a object when that function is invoked through FFI. Then, the captured object can be passed as the argument of to close the stream. These FFI types correspond to the C pointer type providing automatic conversion between Lisp strings and null-terminated C strings. The null pointer corresponds to the nil symbol. The related types and are also provided; these are described in the following sections. The type behaves as follows. The put operation allocates, using a buffer large enough to hold the UTF-8 encoded version of the Lisp string, encodes the string into that buffer, and then stores the pointer into the argument space. The in operation deallocates the buffer. If is passed by pointer, the in operation also takes the current value of the pointer, which may have been replaced by a different pointer, and creates a new Lisp string by decoding UTF-8 from that buffer. The get operation retrieves the C pointer and duplicates a new string by decoding the UTF-8 contents. The type has no out operation: a string is not expected to be modified in-place. The type type differs in behavior from as follows. Firstly, it has no in operation. Consequently, doesn't deallocate the buffer that had been allocated by put. Under the get operation, the type assumes that ownership over the C pointer has been granted, and after duplicating a new string from the decoded UTF-8 data in the C string, it deallocates that C string by invoking the C library function on it. Type type is similar to it also has no in-operation, and doesn't deallocate the buffer allocated in the put operation. Under the get operation, the type does not assume ownership of memory, and therefore does not free the pointer received from the foreign function. The type is intended for receiving strings via a pointer-to-pointer argument, in situations when the string must not be freed. Like other types, the string types combine with the type family. Because the family has memory management semantics, as does the string family, it is important to understand the memory management implications of the combination of the two. The derived pointer types and are effectively equivalent. They denote a string passed by pointer, with in-out semantics. The effect is that the string is dynamic in both directions. What that means is that the foreign function either must not free the pointer it was given, or else it must replace it with one which the caller can also free (or with a null pointer). The two are equivalent because has no in operation, so its get operation is used instead; but that operation is similar to the in operation of the type: both decode the string currently referenced by the pointer, and then pass that pointer to the C function. Receiving a string by pointer from a foreign function is achieved by treating the situation as a pointer to an array of one element. So that is to say, an argument like can be treated as either if the foreign function passes ownership of the string, or else if the foreign function retains ownership of the string. In either case, the argument is a vector of one element, which will be updated to the returned string, or else if the function passes back a null pointer. The type combination refers to a string pointer passed to a foreign function by pointer, whereby the foreign function will retain and free the pointer. The type combination passes the string pointer in the same way, but the foreign module mustn't use the pointer after returning. FFI will free the pointer that had been passed. The family corresponds to null-terminated C strings, like the family, and the family members have memory management semantics similar to their counterparts. Likewise, under these types also, the null pointer corresponds to The prefix in the naming denotes "byte". It indicates that unlike the family, the family does not use UTF-8 encoding; only Lisp strings which contain strictly code points in the range U+0000 to U+00FF may convert to these types; out-of-range characters trigger an error exception. Likewise, in the reverse direction, no UTF-8 decoding is performed: every byte value turns into the corresponding character code. The byte 0 is interpreted as the string terminator. Note: the type may be advantageous in situations when character handling is known to be confined to the ASCII range, since UTF-8 conversion is then unnecessary overhead. Because TXR strings use wide characters internally, converting to and from the type still requires memory management overhead, just like in the case of the type. The type described in the next section avoids memory management and conversion overhead. Thus, even in situations in which characters are confined to the ASCII range, if wide functions are available in the foreign API, it may be more efficient to use them, particularly if the foreign component uses that representation internally. The FFI type corresponds to the C type pointing to the first character of a null terminated wide string. It converts between Lisp strings and symbols, and C strings. The family members of have memory management semantics similar to their counterparts, Likewise, under these types also, the null pointer corresponds to Note: because wide characters do not require UTF-8 conversion, the family is more efficient. A string passes into foreign code directly: the Lisp object already contains a null-terminated wide character string, and so the pointer to that is given directly to the foreign code. Similarly, ownership transfer in either direction is a pointer passage with no memory management or conversion overheads. Whenever some foreign API offers a choice between UTF-8 strings, and wide strings, the wide version should be targeted by FFI, particularly if the API is known to works with wide strings internally also. The type creates a correspondence between the TXR Lisp type and a C pointer to a block of arbitrary data. Note that there also exists a parametrized version of the and type syntax which specifies a size. Under the type's put operation, no memory allocation takes place. The pointer to the buffer object's data is written into the argument space, so the foreign function can manipulate the buffer directly. If the object isn't a buffer but rather the symbol then a null pointer is written. The in operation has semantics as follows. In the pass-by-pointer nuance, the buffer pointer currently in the argument space is compared to the original one which had been written there from the buffer object. If they are identical, then the in operation yields the original buffer object. Otherwise, if the altered pointer is non-null, it allocates a new buffer equal in size to the original one and copies in the new data from the new pointer that was placed into the argument space by the foreign function. If the altered pointer is null, then instead of allocating a new buffer, the object is returned. The by-value nuance of the in operation does nothing. The get operation is not meaningful for an unsized it yields a zero length object. For this reason, parametrized type should be used for retrieving a buffer with a specific fixed size. The type has different memory management from The put operation of allocates a copy of the buffer and writes into the argument space a pointer to the copy. It is assumed that the foreign function takes ownership of the copy. The in operation of is also different. The by-value nuance of the in operation is a no-op, like that of The by-pointer nuance doesn't attempt to compare the previously written pointer to the current value. Rather, it assumes that if there is any non-null pointer value in the argument space, then it should take ownership of that object and return it as a new buffer. Thus if two-way dynamic buffer passing is requested using it means that the foreign function must replace the pointer with a null to indicate that it has consumed the buffer. Any non-null value in the argument space indicates that the foreign function has either rejected the pointer (not taken ownership), or has replaced it with a new object, whose ownership is being passed. Unidirectional by-pointer passing of a can be performed using the types or The former type will not invoke put operation. It will only allocate a pointer-sized space, without initializing it. After the foreign call, the by-pointer semantics of the in operation will be triggered If the foreign function places a non-null pointer into the space, its ownership will be seized by a newly instantiated buffer object. Otherwise the function must place a null pointer, which results in a value emerging from the in operation as documented above. The latter type will achieve a transfer of ownership in the other direction, by invoking the put operation, which places a copy of the buffer into the pointer-sized location prepared in the argument space. After the call, it will invoke the by-value in semantics of which is a no-op: thus no attempt is made to extract a buffer, even if the foreign function alters the pointer. The type converts three kinds of Lisp objects to a C pointer: the object the type, or the special type. When the symbol is converted to a type, it becomes a null function pointer. A object of any kind converts to a the internal pointer is converted to a function pointer. Instances of the type are produced by the function, or by calls to functions defined by the macro. The type is useful for passing callbacks to foreign functions: Lisp functions which appear to be C functions to foreign code. In the reverse direction, when a object is converted from the foreign function pointer representation to a Lisp object, it becomes a object whose tag is the symbol. The type is useful for indicating the return type of foreign functions and callbacks which return no value. It corresponds to a zero-sized object. It will convert any lisp value into zero bytes, and convert zero bytes into The following following parametrized type operators are available. The type specifies an enumerated type, which establishes a correspondence between a set of Lisp symbols and foreign integer values of type The argument must either be or a symbol for which the function returns true. It gives the tag name of the enumerated type. The remaining arguments specify the enumeration constants. In the enumeration constant syntax, each occurrence of They must be a bindable symbol according to the function. The symbols may not repeat within the same enumerated type. Unlike in the C language, different enumerations may use the same symbols; they are in separate spaces. If a is given, it is associated with an integer value which is one greater than the integer value associated with the previous symbol. If there is no previous symbol, then the value is zero. If the previous symbol has been assigned the highest possible value of the FFI type, then an error exception is thrown. If is given, then is given the specified value. The is an expression which must evaluate to an integer value in range of the FFI type. It is evaluated in an environment in which the previous symbols from the same enumeration appear as variables whose bindings are their enumeration values, making it possible to use earlier enumerations in the definition of later enumerations. The FFI type converts two kinds of Lisp values to the foreign type symbols which are in the set defined by the type, and integer values which are in the range which that foreign type can represent. Out-of-range integer values, symbols not defined in the enumeration, and objects not of symbol or integer type all trigger an exception. In the reverse direction, the type extracts from the foreign representation values of FFI type and converts them, if possible, to symbols. If an integer value occurs which is not assigned to any enumeration symbol, then the conversion produces that integer value itself rather than a symbol. If an integer value occurs which is assigned to multiple enumeration symbols, it is not specified which of those symbols is produced. The type operator is a generalization of which allows the base integer type of the enumeration to be specified. The following equivalence holds:
  (enum n a b c ...) <--> (enumed int n a b c ...) Any integer type or name may be specified for including any one of the endian types. The enumeration inherits its size, alignment and other foreign representation details from The values associated with the enumeration symbols must be in the representation range of which is not checked until the conversion of a symbol through the enumeration is attempted at run time. The type is a clone of the underlying type, inheriting most of its properties. In particular, it is possible to derive an type from an underlying bitfield type. The resulting type is still a bitfield, and may only be used as a or member. Moreover, because it is a bitfield type, there is a restriction against creating aliases for it with An bitfield allows the values of a bit field to be specified symbolically. The FFI type maps between a Lisp and a C The argument of the syntax gives the structure type's name, known as the tag. If this argument is the symbol then the structure type is named by a newly generated uninterned symbol (with The is entered into a global namespace of tags which is shared by structures and unions. The also specifies the Lisp name associated with the FFI type. The and pairs specify the structure members. The elements must be symbols, and the elements must be FFI type expressions. A definition with no members refers to a previously defined or type which has the same in the global tag space. If no prior or exists, then a definition with no slots specifies a new, incomplete structure type. A definition with no members never causes a Lisp structure type to be created. A definition that specifies one or more members either defines a new structure type, or completes an existing one. If an incomplete structure or union type which has the same exists, then the newly appearing definition is understood to provide a completion of that type. If the incomplete type is a it thereby converted to a type. If a complete structure type which has the same already exists, then the newly appearing definition replaces that type in the tag namespace. A definition with members is entered into the tag space immediately as an incomplete type (if it isn't already), before the members are processed. Therefore, the member definitions can refer to the type. The type becomes complete when the last member is processed, except in the special situation when that member causes the type to become a flexible structure, described several paragraphs below. A definition that specifies members causes a Lisp having the same to exist, if such a type doesn't already exist. If such a type is created, instance slots are defined for it which correspond to the member definitions in the FFI definition. For any which specifies an expression, that expression is evaluated during the processing of the type syntax, in the global environment. The resulting value then becomes the initial value for the slot. The semantics of this value is similar to that of a quoted object appearing as an in the macro's syntax. For example, if the type expression which specifies a slot initialized by generates a Lisp struct type, the manner in which that type is generated will resemble that of where denotes the substitution of the value of which had been obtained by evaluation in the global environment. Note: if more flexible initialization semantics is required, the application must define the Lisp struct type first with the desired characteristics, before processing the FFI struct type. The FFI struct type will then related to the existing Lisp struct type. Those members whose name is specified as is ignored; no instance slots are created in the Lisp type. If a is specified for such a slot, there exists is no situation in which that form will be evaluated. When a Lisp object is converted to a struct, it must, firstly, be of the struct type specified by Secondly, that type must have all of the slots defined in the FFI type. The slots are pulled from the Lisp structure in the order that they appear in the FFI definition. They are placed into the target memory area in that order, with all required padding between the members, and possibly after the last member, for alignment. Whenever a member is defined using as the name, that member represents anonymous padding. The corresponding expression is used only to determine the size of the padding only. Its data transfer semantics is completely suppressed. When converting from Lisp, the anonymous padding member simply generates a skip of the number of byte corresponding to the size of its type, plus any necessary additional padding for the alignment of the subsequent member. Structure members may be bitfields, which are described using the and compound type operators. A structure member must not be an incomplete or zero-sized array, unless it is the last member. If the last member of FFI structure is an incomplete array, then it is a flexible structure. A structure member must not be a flexible structure, unless it is the last member; the containing structure is then itself a flexible structure. Flexible structures correspond to the C concept of a "flexible array member": the idea that the last member of a structure may be an array of unknown size, which allows for variable-length data at the end of a structure, provided that the memory is suitably allocated. Flexible structures are subject to special restrictions and requirements. See the section Flexible Structures below. In particular, flexible structures may not be passed or returned by value. See also: the function and the macro. The FFI type resembles the type syntactically. It provides handling for foreign objects of C type. The argument specifies the name for the union type, known as a tag. If this argument is the symbol then the union type is named by a newly generated uninterned symbol (with The is entered into a global namespace of tags which is shared by structures and unions. The and pairs specify the union members. The elements must be symbols, and the elements must be FFI type expressions. A definition with no member refers to a previously defined or type which has the same in the global tag space. If no prior or exists, then a definition with no slots specifies a new, type that is incomplete. A definition that specifies one or more members either defines a new structure type, or completes an existing one. If an incomplete structure type which has the same exists, then the newly appearing definition is understood to provide a completion of that type. If the prior incomplete type is a it is converted to type. If a complete structure or union type which has the same already exists, then the newly appearing definition replaces that type in the tag namespace. A struct definition with members is entered into the tag space immediately as an incomplete type (if it isn't already), before the members are processed. Therefore, the member definitions can refer to the type. The type becomes complete when the last member is processed. Unlike the FFI type, the type doesn't provide automatic conversion between C and Lisp data. This is because the is inherently unsafe, due to its placement of multiple types into the same storage, and lack of any information to discriminate which type is currently stored. Instead, the FFI creates a correspondence between a C union that is regarded as just a region of memory, and a TXR Lisp data type called An instance of the Lisp type holds a copy of the C union memory, and also contains type information about the unions members. Functions are provided to store and retrieve the members; it is these functions which provide the conversion between the Lisp types and the foreign representations stored in the C union. This is done under control of the application, because due to the inherent lack of safety of the C only the application program knows which member of the union may be accessed. Conversion between the C and the Lisp consists of just a memory copying operation. The following functions are provided for manipulating unions: instantiates a new union object; retrieves a list of the symbols serving as the union's member names; retrieves a specified member from the union's storage, converting it to a Lisp object; places a Lisp object into a union, using the specified member's type to convert it to a foreign representation; performs the "in semantics" on the specified member of a union, propagating modifications in that member back to a Lisp object; and performs "out semantics" on the specified member of a union, propagating modifications done on a previously retrieved Lisp object back into the union. The FFI type creates a correspondence between Lisp sequences and "by value" fixed size arrays in C. It converts Lisp sequences to C arrays, and C arrays to Lisp vectors. Arrays passed by values do not exist in the C language syntax. Rather, the C type which corresponds to the FFI array is a C array that is encapsulated in a For instance the type can be visualized as corresponding to the C type Thus, in the FFI syntax, we can specify arrays as function parameters passed by value and as return values. On conversion from Lisp to the foreign type, the FFI simply iterates over the Lisp sequence, and performs an element for element conversion to If the sequence is shorter than the array, then the remaining elements are filled with zero bits. If the sequence is longer than the array, then the excess elements in the sequence are ignored. Since Lisp arrays and C arrays do not share the same representation, temporary buffers are automatically created and destroyed by FFI to manage the conversion. The argument is an ordinary Lisp expression expanded and evaluated in the top-level environment. It must produce a nonnegative integer value. In addition, several types are treated specially: when is one of or the array type establishes a special correspondence with Lisp strings. When the C array is decoded, a Lisp string is created or updated in place to reflect the new contents. This is described in detail below. The second form, whose syntax omits the element, it denotes a variable length array. It corresponds to the concept of an incomplete array in the C language, except that no implicit array-to-pointer conversion concept is implemented in the FFI type system. This type may not be used as an array element or structure member, other than as the last structure member. It also may not be passed or returned by value, only by pointer. If the last member of a structure has this type, then it is a flexible array member; see the Flexible Structures section below. Since the type has unknown length, it has a trivial get operation which returns It is useful for passing a variable amount of data into a foreign function by pointer. An array of represents non-null-terminated UTF-8 character data, which converts to and from a Lisp string. Any null bytes in the data correspond to the pseudo-null character also notated as An array of represents a field of optionally null-terminated UTF-8 character data. If a null byte occurs in the data then the text terminates before that null byte, otherwise the data comprises the entire foreign array. Thus, null bytes do not occur in the data. A null byte in the array will not generate a pseudo-null character in the Lisp string. An array of values represents 8-bit character data that isn't UTF-8 encoded, and is not null terminated. Each byte holds a character whose code is in the range 0 to 255. If a null byte occurs in the data, is interpreted as a string terminator. The type is a variant of When converting from Lisp to C, it ensures that the array is null-terminated. This means that if the is dimensioned, then the element of the C array is written out as all zero bytes, ignoring the corresponding Lisp value in the Lisp array. If the is undimensioned, then the size of the C array is deemed to be one greater than the actual length of the Lisp array. The elements in the Lisp array are converted to the corresponding elements of the C array, and then the last element of the C array is filled with null bytes. The type is useful for handling null terminated character arrays representing strings, and for null terminated vectors. Unlike allows the Lisp object to be one element short. For instance, when a passed by pointer a foreign function is converted back to Lisp, the Lisp object is required to have only four elements. If the Lisp object has five elements, then the fifth one will be decoded from the C array in earnest; it is not expected to be null. However, when that Lisp representation is converted back to C, that extra element will be ignored and output as a zero bytes. Lastly, the further extends the special treatment which the type applies to the types and The type assumes, and depends on the incoming data being null-terminated, and converts it to a Lisp string accordingly. The regular type doesn't assume null termination. In particular, this means that whereas will decode 42 bytes of UTF-8, even if some of them are null, converting those null bytes to the U+DC00 pseudo-null, in contrast, a will treat the 42 bytes as a null-terminated string, and decode UTF-8 only up to the first null. In the other direction, when converting from Lisp string to foreign array, ensures null termination. Note that the type combination of behaves in a manner indistinguishable from a of The one-argument variant of the syntax which omits the argument specifies a null-terminated variant of the variable-length array. Like that type, it corresponds to the concept of an incomplete array in the C language. It may not be used as an array element, and may not be used as a structure member other than the last member. It cannot be passed as an argument or returned as a value. If the last member of a structure has this type, then it is a flexible array member; see the Flexible Structures section below. Unlike the ordinary variable-length the type supports the get operation, which extracts elements, accumulating them into a resulting vector, until it encounters an element consisting of all zero bytes. That element terminates the decoding, and isn't included in the resulting array. The variable-length also has a special in operation. Like the get operation, the in operation extracts all elements until a terminating null, decoding them to a vector. Then, the entire original vector is replaced with the new vector, even if the original vector is longer. The denotes the passage of a value by pointer. The argument gives the pointer's target type. The type converts a single Lisp value, to and from the target type, using a C pointer as the external representation. When used for passing a value to a foreign function, the type has in-out semantics: it supports the interfacing concept that the called function can update the datum which has been passed to it "by pointer", thereby altering the caller's object. Since a Lisp value requires a conversion to the FFI external representation, it cannot be directly passed by pointer. Instead, this semantics is simulated. The put semantics of allocates a temporary buffer, large enough to hold the representation of The Lisp value is then encoded into this buffer, recursively relying on the type's put semantics. After the foreign call, triggers the in semantics of to update the Lisp object from the temporary buffer, and releases the buffer. The get semantics of is used in retrieving a return value, or, in a FFI callback, for retrieving the values of incoming arguments that are of type. The get semantics assumes that the memory referenced by the C pointer is owned by foreign code. The Lisp object is merely decoded from the data area, which is then not touched. The semantics of used by callbacks for updating the values of arguments passed by pointer, assumes that the argument space already contains a valid pointer. The pointer is retrieved from the argument space, and the Lisp value is encoded into the memory referenced by that pointer. Note that only Lisp objects with mutable slots can be meaningfully passed by pointer with in-out semantics. If a Lisp object without immutable slots, such as an integer, is passed using the incoming updated value of the external representation will be ignored. Concretely, if a C function has the argument signature with in-out semantics such that it updates the object which is passed in, this function can be called as a foreign function using a FFI type for the argument. However, the argument of the foreign call on the TXR Lisp side is just an integer value, and that cannot be updated. On the other hand, if a FFI member is declared as of type then the Lisp is expected to have an integer-valued slot corresponding to that member. The slot is then subject to a bidirectional transfer. FFI will create an temporary data area, encode the slot into that area and place that area's pointer into the encoded structure. After the call, the new value of the will be extracted from the temporary buffer, which will then be released. The Lisp structure's slot will be updated with the new integer. This will happen even if the Lisp structure is being passed as a by-value argument. type is a variation of which denotes the passing of a value by pointer into a function, but not out. The put semantics of is the same as that of but after the completion of the foreign function call, the in semantics differs. The type only frees the temporary buffer, without decoding from it. The out semantics of differs also. It effectively treats the object as if it were "by value", since the reverse data transfer is ruled out. In other words, simply triggers the by-value nuance of out semantics. The get semantics of is the same as that of The type is a variant of which denotes a by pointer data transfer out of a function only, not into. The put semantics of prepares a data area large enough to hold and stores a pointer to that area into the argument space. The Lisp value isn't encoded into the data area. The in semantics is the same as that of the by-pointer nuance of in semantics is invoked to decode the external representation to Lisp data. The type is a variant of which transfers ownership of the allocated buffer to the invoked function. That is to say, the in semantics of doesn't involve the freeing of memory that was allocated by put semantics. The type is useful when a function expects a pointer to an object that was allocated by and expects to take responsibility for freeing that object. Since the function may free the object even before returning, the pointer must not be used once the function is called. This is ensured by the in semantics of which is the same as that of The type also has get semantics which assumes that ownership of the C object is to be seized. FFI will automatically free the C object when get semantics is invoked to retrieve a value through a The type is a variant of which is useful for capturing return values or, in a callback producing return values. The type has empty put semantics. If it put semantics is invoked, it does nothing: no area is allocated for and no pointer is stored into the argument space. The in semantics is the same as that of a pointer is retrieved from the argument space, the object is subject to in semantics to recover the updated Lisp value, and then the object is freed. The get semantics of is identical to that of The out semantics is identical to that of The type is a variant of similar to which assumes that the C object being received has an indefinite lifetime, and doesn't need to be freed. The suffix stands for "static". Like the has no put semantics. Its in semantics recovers a Lisp value from the external object whose pointer has been stored by the foreign function, but doesn't free the external object. The get semantics retrieves a Lisp value without freeing. The parametrized type can be derived from any integer or floating-point type. There is also an unparametrized which is a for the type The type family represents Boolean values, converting between a Lisp Boolean and foreign Boolean. A given instance of the type inherits all of its characteristics from such as its size, alignment and foreign representation. It alters the get and put semantics, however. The get semantics converts a foreign zero value of to the Lisp symbol and all other values to the symbol The put semantics converts the Lisp symbol to a foreign value of zero. Any other Lisp object converts to the foreign value one. The types are not integers, and cannot be used as the basis of bitfields: syntax like is not permitted. However, Boolean bitfields are possible when this syntax is turned inside out: the type can be derived from a bitfield type, as exemplified by This simply applies the above described Boolean conversion semantics to a three-bit field. A zero/nonzero value of the field converts to and a or Lisp value converts to a 0 or 1 field value. The and types denote C-language-style bitfields. These types can only appear as members of structures. A bitfield type cannot be the argument or return value of a foreign function or closure, and cannot be a foreign variable. Arrays of bitfields and pointers, of any kind, to bitfields are a forbidden type combination that is rejected by the type system. The type denotes a bitfield of type corresponding to an bitfield in the C language. The type denotes a bitfield of type Unlike in the C language, it is not implementation-defined whether such a bitfield represents signed values; it converts between Lisp integers that may be positive or negative, and a foreign representation which is two's complement. Bitfields based on some other types are supported using the more general operator, which is described below. The parameter of is an expression evaluated in the top-level environment, indicates the number of bits. It may range from zero to the number of bits in the type. In a structure, bitfields produced by and are allocated out in storage units which have the same width and alignment requirements as a These storage units themselves can be regarded as anonymous members of the structure. When a new unit needs to be allocated in a structure to hold bitfields, it is allocated in the same manner as a named member of type would be at the same position. A zero-length bitfield is permitted. It may be given a name, but the field will not perform any conversions to and from the corresponding slot in the Lisp structure. Note that in situations when the FFI struct definition causes the corresponding Lisp structure type to come into existence, the Lisp structure type will have slots for all the zero width named bitfields, even though those slots don't participate in any conversions in conjunction with the FFI type. The presence of a zero-length bitfield ensures that a subsequent structure member, whether bitfield or not, is placed in a new storage unit of the size of the bitfield's base type. Details about the algorithm by which bitfields are allocated within a structure are given in the paragraph below entitled Bitfield Allocation Rules. A field stores values which follow a pure binary enumeration. For instance, a bitfield of width 4 stores values from 0 to 15. On conversion from the Lisp structure to the foreign structure, the corresponding member must be a integer value in this range, or an error exception is thrown. On conversion from the foreign representation to Lisp, the integer corresponding to the bit pattern is recovered. Bitfields follow the bit order of the underlying storage word. That is to say, the most significant binary digit of the bitfield is the one which is closest to the most significant bit of the underlying storage unit. If a four-bit field is placed into an empty storage unit and the value 8 its stored, then on a big-endian machine, this has the effect of setting to 1 the most significant bit of the underlying storage word. On a little-endian machine, it has the effect of setting bit 3 of the word (where bit 0 is the least significant bit). The field creates a correspondence between a range of Lisp integers, and a foreign representation based on the two's complement system. The most significant bit of the bitfield functions as a sign bit. Values whose most significant bit is clear are positive, and use a pure binary representation just like their counterparts. The representation of negative values is defined by the "two's complement" operation, which maps each value to its additive inverse. The operation consists of temporarily treating the entire bitfield as unsigned, and inverting the logical value of all the bits, and then adding 1 with "wraparound" to zero if 1 is added to a field consisting of all 1 bits. (Thus zero maps to zero, as expected.) An anomaly in the two's complement system is that the most negative value has no positive counterpart. The two's complement operation on the most negative value produces that same value itself. A field of width 1 can only store two values: -1 and 0, represented by the bit patterns 1 and 0. An attempt to convert any other integer value to a field of width 1 results in an error. A field of width 2 can represent the values -2, -1, 0 and 1, which are stored as the bit patterns 10, 11, 00 and 01, respectively. The operator is more general than and It allows for bitfields based on on any integer type up to 64 bits wide. When the character types and are used as the basis of bitfields, they convert integer values, not characters. In the case of the bitfield is signed. All remarks about and apply to also. Details about the algorithm by which bitfields are allocated within a structure are given in the paragraph below entitled Bitfield Allocation Rules. Under the operator, the endian types such as or may also be used as the basis for bitfields. If is an endian type, the bitfield is then allocated in the same way that a bitfield of the corresponding ordinary type would be allocated on a target machine which has the byte order of that endian type. When a bitfield member follows a member which has a different byte order, the bitfield is placed into a new allocation cell. This is true even if the previous member has the same alignment. Note: the allocation of bits within a bitfield based on a byte storage cells also differs between different endian systems. However, the FFI type system does not offer one byte endian types such as The workaround is to switch to a wider type. Note: endian bitfields may be used to match the image of a C structure which contains bitfields, without having to conditionally define the FFI struct type differently based on whether the current machine is big or little endian. Conditionally defining a structure for two different byte orders adds verbiage to the program and is highly error-prone, since the bitfields change order within an allocation unit. For instance, on a big endian system, the definition of a structure representing an IPv4 packet might begin like this:
  (struct ipv4-header
    (ver (bit 4 uint16))
    (ihl (bit 4 uint16))
    (dscp (bit 6 uint16))
    (ecn (bit 2 uint16))
    (len uint16)
    ...) to port this to a little endian system, the programmer has to recognize that the first pair of fields is packed into one byte, and the next pair of fields into a second byte. The bytes stay in the same order, but the pairs are reversed:
  (struct ipv4-header
    (ihl (bit 4 uint16)) ;; reversed pair
    (ver (bit 4 uint16))
    (ecn (bit 2 uint16)) ;; reversed pair
    (dscp (bit 6 uint16))
    (len be-uint16)
    ...) Endian bitfields allow this to be defined naturally. The IPv4 header is based on network byte order, which is big-endian, so big endian types are used. The little endian version above already uses for the field. This just has to be done for the bitfields also:
  (struct ipv4-header
    (ver (bit 4 be-uint16))
    (ihl (bit 4 be-uint16))
    (dscp (bit 6 be-uint16))
    (ecn (bit 2 be-uint16))
    (len be-uint16)
    ...) The parametrized and types are variants of the unparametrized and respectively. The argument is an expression which is evaluated in the top-level environment, and must produce a nonnegative integer. Because they have a size, these types have useful get semantics. The get semantics of is that a Lisp object of type is created which takes direct ownership of the memory. The get semantics of is that a Lisp object is created using a dynamically allocated copy of the memory. The type corresponds to a C pointer, in connection with the concept of representing a variable length array that is passed and returned as a pointer to the base element. On the Lisp side, the FFI type corresponds to the Lisp type. The Lisp type is similar to but supports array indexing operations, and some other features. It can be regarded as a semantic cross between and The get semantics of is simply that a pointer is retrieved from memory and converted to a freshly allocated object which holds that pointer, and is marked as having an unknown size. No copy is made of the underlying array. When the application determines the size of the array, it can inform that object by calling the function. The put semantics of the FFI type is simply to write, into the argument space, the pointer which the object holds. The object must be a whose element type matches that of the FFI type. The type has in semantics. When a is passed to a foreign function as an argument to a or parameter to either a or type, what is passed to the function is a pointer to the pointer. The foreign function may update this pointer to a new value, and this value is stored back into the object. The array's length is reset to zero. If it is an owned arranged by then the current array freed before the new pointer is assigned, and the object's type is reset to borrowed array. The object must not be memory mapped coming from the function. The type lacks out semantics, since Lisp code cannot change its address; so there is no new pointer to propagate back to a foreign caller which passes a to a Lisp callback, and no other memory management tasks to perform. The type is particularly useful in situations when foreign code generates such an array, and the size of that array isn't known from the object itself. It is also useful, instead of a variable-length for passing a dynamic array to foreign code in situations when the application benefits from managing the memory for the array. The variable-length FFI type's disadvantage relative to is that the converts an entire Lisp sequence to a temporarily allocated array, which is used only for one call. By contrast, the object holds the C representation which Lisp code can manipulate; and that representation is passed directly, just like in the case of Unlike there is no dynamic variant of The transfer of ownership of a requires the use of explicit operations like and It is possible to create a view over a buffer, using Lastly, the type is the basis for the TXR Lisp function, which is documented in the section Unix Memory Mapping. The parametrized type is similar to the unparametrized It also converts between Lisp objects of type and foreign pointers. Unlike the unparametrized type, it provides a measure of type safety, and also supports the conversion of objects. When a foreign pointer is converted to a Lisp object under control of the parametrized the resulting Lisp object is tagged with the symbol. In the reverse direction, when a Lisp object is converted to the parametrized type, its type tag must match or else the conversion fails with an error exception. This rule contains a slight relaxation: a object with a tag can be converted to a foreign representation using any parametrized type, if its value is null. In other situations, the function must be used to coerce the pointer object to the matching type. Note that if is specified as then this is precisely equivalent to the unparametrized which doesn't provide the above safety measure. A object may also be converted to a foreign pointer under the control of a parametrized type. The object's internal pointer becomes the foreign pointer value. The conversion is only permitted if the following two restrictions are not met, otherwise an error exception is thrown. Firstly, the of the type must be the name of an FFI type, at the time when the type expression is processed, otherwise the is not associated with a type. Secondly, the object being converted must have an element type which matches the FFI type denoted by the object's Pointer type safety is useful, because FFI can be used to create bindings to large application programming interfaces (APIs) in which objects of many different kinds are referenced using pointer handles. The erroneous situation can occur that a FFI call passes a handle of one kind to a function expecting a different kind of handle. If all pointer handles are represented by a single type, then such a situation proceeds without diagnosis. If handles of different types are all mapped to types with different tags, the situation is intercepted and diagnosed with an error exception. The FFI type operators and define a type which is a copy of but with adjusted alignment requirements. In some cases, (but not works by replacing itself with a transformed version of the syntax. If the argument is present, it is an expression which is evaluated in the top-level environment. It must produce a positive integer which is a power of two. If is absent, a different default value is used depending on which type operator is specified. For it defaults to some platform-specific maximum useful alignment value, typically 16. For a missing defaults to 1. The operator can be used to create a version of which is aligned at least as strictly as the specified That is to say, values of which are less than or equal to existing alignment have no effect on alignment, except when the type is used as a bitfield. The operator can be used to create a version of which is less strictly aligned than its existing alignment. Alignment affects the placement of the type as a structure member, and as an array element. A type with alignment 1, like the default alignment for can be placed at any byte offset, and thus is effectively unaligned. A type with alignment 2 can be placed only at even addresses and offsets. Alignment can be applied to all types, including arrays and structs. It may also be applied to bitfields, but special considerations have to be observed to obtain the intended effect, described below. However, out of the elementary types, only the integer and floating point types are required to support a weakening of alignment. Whether a type which corresponds to a pointer, such as a or can be written at an offset which doesn't meet that type's default alignment is machine-dependent. If a FFI struct type is declared with a weakened alignment, whether or not such a structure can be read or written at the misaligned offsets depends on whether the individual members support it. If they are integer or floating-point types, or aggregates thereof, the usage is supported in a machine-independent manner. Alignment interacts with the allocation of bitfields in special ways. If is greater than 1, or regardless of if the operator is the type is marked with a Boolean attribute indicating that it has altered alignment. Then, when a bitfield is based on a type which has altered alignment, then that bitfield isn't packed together with the previous field, even if the allocation rules otherwise call for it. Due to the alignment request, the byte offset is first adjusted according to the requested alignment and the bit offset is reset to zero. The bit field is then allocated at the new alignment. This requirement applies even if the requested alignment is 1, which is possible via a combination of both and both specified with a of 1. If the requested alignment for the type of a bitfield is 1, and the previous member is a bitfield which has left a byte partially filled, then the new bitfield starts on a fresh byte, even if it would otherwise be packed with the previous bitfield. If a named bitfield has weakened alignment, other than one byte alignment produced by the bitfield's original type's alignment is used for the purposes of determining its contribution to the alignment of the structure. When is one of two kinds of types, the type operator exhibits special behaviors, as follows. In these situations, the operator has no semantics other than these behaviors.

    1.  When is or syntax which defines at least one member, then the operator performs the following syntactic transformation: each member of is edited, by specifying a operator around its type, with the given The surrounding operator is deleted. The effect is that is applied not to the struct or union type itself, but to its members. For example is transformed into The 1 comes from the defaulting of The rationale for this behavior is that alignment weakening is often required for all members of a structure, rather than select members. Moreover, specifying weak alignment for a structure type itself, while leaving members with strict alignments, rarely makes sense. Weakening the alignment of a structure will not eliminate the padding between the members or at the end; it will only have any useful effect when that structure is itself used as the member of another structure. An important rationale also is that the GNU C attribute works this way, and so C structures declarations using that attribute are easier to translate to the TXR Lisp FFI type system. Deriving a less strictly aligned version of a structure or union type without any effect on the alignment of its members may be obtained by applying the operator to either name for a structure or union type, or else to syntax which refers to an existing type without defining members. Given the definition the type is an eight byte structure with three bytes of padding at the end, which has four byte alignment. The type expression produces a version of this type which has one byte alignment. The expression likewise. The resulting unaligned type is still eight bytes wide, and has three padding bytes. In other words, the operator does not transform the syntax of a structure which is already defined as an object,

    2.  When is a operation, then transforms the syntax as follows: the operator surrounding the expression is removed, and introduced around the type expression that is own operand. Thus is transformed into The rationale for this transformation is that when both and are applied to a type, the combination only makes sense when is first. For a non-structure type like is equivalent to just because will set the alignment to regardless of the effect of Whereas is meaningful in that the takes precedence over if The main rationale is that may be applied to structure members via a code transformation. Those members may already have types which use This transformation ensures that the semantics is applied in a useful order. For example is first transformed into If this is left as-is, then the on is obliterated by the rendering it useless. A further transformation takes place to Now the directive is increasing the alignment of to 2, so that will be placed at offset 2, leaving one byte of padding after the member. This is how attributes work in GNU C also: the attribute on the member of a packed structure can take precedence and increase its alignment.

    After these transformations are applied, the nested forms which occur in the transformed syntax may perform more such transformations, depending on their operands. Note that the two-argument form of with a value greater than 1 doesn't directly correspond to any single attribute specifier in GNU C. The GNU C attribute is Boolean, implicitly reducing alignment to 1. A combination of the GNU C attributes and is used to produce the effect of for values of > 1. In GNU C, the attribute, when applied to a structure, distributes to its members, but isn't capable of distributing an alignment exceeding 1. So the expression, for values of > 1, doesn't correspond to anything in GNU C; its effect can be simulated by attributing the structure type with and then individually applying the required alignment to the member declarations. These additional FFI types for common C language types are provided as aliases. The and types are provided only if the host platform's is no wider than 64 bits. If the host platform lacks then the above two FFI types are defined as aliases for and respectively. The FFI type operator provides a way to reference the type of a member of a struct or union. The argument must be a type expression denoting a struct or union. The argument and any additional arguments must be symbols. If is a struct or union type, and is a member, then is a type expression denoting the type of Moreover, if itself is a struct or union, which has a member named then the type of can be denoted by the expression Similarly, additional symbols reference through additional struct/union nestings. Note: the referencing dot syntax can be used to write expressions. For instance, can be written as instead. The FFI type operator denotes the element type of which must be a pointer, array or enum. Note: there is also a macro The macro expression is equivalent to the expression The additional names of various common POSIX types may also be available, depending on platform. They are provided as aliases. In addition to the type system described in the previous section. the FFI type system supports endian types, which are useful for dealing with data formats defined by networking protocols and other kinds of standards, or data structure definitions from other machines. There are two kinds of endianness: Little endian refers to the least-significant byte of a data type being stored at the lowest address in memory, lowest offset in a buffer, lowest offset in a file, or earlier byte in a communication stream. Big endian is the opposite: it refers to the most significant byte occurring at the lowest address, offset or stream position. For each of the signed integral types through the corresponding unsigned types through and the two floating-point types and the FFI type system provides a big-endian and little-endian version, whose names are derived by prefixing the or prefix to its related type. Thus, the exhaustive list of the endian types is: and These types have the same size and alignment as their plain, unprefixed counterparts. Alignment can be overridden with the type construction operator to create versions of these types with alternative alignment. Endian types are supported as arguments to functions, return values, members of structs and elements of arrays. TXR Lisp's FFI performs the automatic conversion from the abstract Lisp integer representation to the foreign representations exhibiting the specified endianness. In the TXR Lisp FFI type system, the following types are incomplete: the type arrays of unspecified size, and any whose last element is of incomplete type. An incomplete type cannot used as a function parameter type, or a return value type. It may not be used as an array element or union member type. A struct member type may be incomplete only if it is the last member. An incomplete structure whose last member is an array is a flexible structure. If a FFI type is defined with an incomplete array (an array of unspecified size) as its last member, then it specifies an incomplete type known as a flexible structure. That array is the terminating array. The terminating array corresponds to a slot in the Lisp structure; that slot is the last slot. A structure which has a flexible structure as its last member is also, effectively, a flexible structure. When a Lisp structure is being converted to the foreign representation under the control of a flexible structure FFI type, the number of elements in the terminating array is determined from the length of the object stored in the last slot of the Lisp structure. The length includes the terminating null element for types. The conversion is consistent with the semantics of an incomplete array that is not a structure member. In the reverse direction, when a foreign representation is being converted to a Lisp structure under the control of a flexible structure FFI type, the size of the array that is accessed and extracted is determined from the length of the object stored in the last slot, or, if the array type is a from detecting null-termination of the foreign array. The conversion of the array itself is consistent with the semantics of an incomplete array that is not a structure member. Before the conversion takes place, all of the members of the structure prior to the terminating array, are extracted and converted to Lisp representations. The corresponding slots of the Lisp structure are updated. Then if the Lisp structure type has a method, that method is invoked. The return value of the method is used to perform an adjustment on the object in the last slot. If the existing object in the last slot is a vector, its length is adjusted to the value returned by the method. If the existing object isn't a vector, then it is replaced by a new vector, whose length is given by the return value of The conversion of the terminating array to Lisp representation the proceeds after this adjustment, using the adjusted last slot object. The TXR Lisp FFI type system follows rules for bitfield allocation which were experimentally derived from the behavior of the GNU C compiler on several mainstream architectures. The allocation algorithm can be imagined to walk through the structure from the first member to the last, maintaining a byte offset O which indicates how many whole bytes have been allocated to members so far, and a bit offset B which indicates, additionally, how many bits have been allocated in the byte which follows these O bytes, between 0 and 7. When a non-bitfield member is placed, then there are two cases: either B is zero (only O bytes have been allocated, with no fractional byte) or else B is nonzero. In this latter case, B is reset to zero and O is incremented by one. In either case, O is adjusted up to the required alignment boundary for the new member. The member is placed, and O is incremented again by the size of that member. When a bitfield member is placed, the algorithm considers the structure to be allocated in units of the base type of that bitfield member. For instance if the bitfield is derived from type then the structure's layout is considered to have been allocated in units. The algorithm examines the value of O and B to determine the first available unit in which at least one bit of unallocated space remains. Then, if the unit at that offset has enough space to hold the new bitfield, according to the bitfield's width, then the bitfield is placed into that unit. Otherwise, the bitfield is placed into the next available unit. After a bitfield is placed, the values of O and B are adjusted so that O reflects the whole number of bytes which have been allocated to the structure so far, and B indicates the 0 to 7 additional bits of any bitfield material protruding past those whole bytes. A zero-width bitfield is also considered with regard to the storage unit size indicated by its type. As in the case of the nonzero-width bitfield, the offset of the first available unit is found which has at least one bit of unallocated space. Then, if that unit is entirely empty, the zero-width bitfield has no effect. If that unit is partially filled, then O is adjusted to point to the next unit after that, and B is reset to zero. Note that according to this semantics, a zero-width bitfield can have an effect even if placed between non-bitfield members, or appears as the last member of a structure. Also, a structure containing only a zero-width bitfield has size zero. If, after the placement of all structure members, B has a nonzero value, then the offset O is incremented by one to cover that byte. As the last allocation step, the size of the structure is then padded up to a size which is a multiple of the alignment of the most strictly aligned member. A named bitfield contributes to the alignment of the structure, according to its type, the same way as a non-bitfield member of the same type. An unnamed bitfield doesn't contribute alignment, or else may be regarded as having the weakest possible alignment, which is byte alignment. If all of the members of a structure are unnamed bitfield members of any type, it exhibits byte alignment. The description isn't complete without a treatment of byte and bit order. Bitfield allocation follows an imaginary "bit endianness" whose direction follows the machine's byte order: most-significant bits are allocated first on big endian, least significant bits first on little endian. If a one-bit-wide bitfield is allocated into a hitherto empty structure, it will be placed into the first byte of that structure, regardless of the machine's endianness, and regardless of the underlying storage unit size for that bitfield. Within that first byte, it will be placed into the most significant bit position on a big-endian machine (bit 7); and on a little-endian machine, it will be placed into the least significant bit position (bit 0). If another one-bit-wide is allocated, it is placed into bit 6 on big endian, and bit 1 on little endian. More generally, whenever a bitfield is allocated for a big-endian machine, and the storage unit is determined into which that bitfield shall be placed, the most significant bits of that storage unit are filled first on a big-endian machine, whereas the least significant bits are filled first on a little-endian machine. From this it follows that on either type of machine, that field shall be placed at the lowest-addressed byte or bytes in which unallocated bits remain. There are situations in which an a foreign function takes the address of a storage location, and writes a new value into that location. Informally, this referred to as an "out parameter" or "in-out parameter", in the case of bidirectional data transfer. In the C language, the familiar pattern looks like this:
      void function(int *ptr);
      int val = 0;
      function(&val); In the case of an aggregate type, such as a structure, being an in-out or out parameter, this pattern is easily handled in FFI because the corresponding Lisp object is also an aggregate, and therefore has reference semantics: it can be updated to receive the new value. In the case of a scalar, however, such as in the above example, this may not be not possible. A Lisp integer doesn't have the referential semantics required to receive a new value by pointer, and there is no "address-of" concept to create a reference to its location. To understand the following FFI trick, it helps to first rework the C into a different form:
      void function(int *ptr);
      int val[1] = { 0 };
      abc_function(val); Instead of a scalar value, we can declare an array of 1 element of that same type, and pass the array (which converts into a pointer to that element). This approach inspires a similar trick in the FFI domain:
      (with-dyn-lib (...)
        (deffi abc-function "abc_function" void ((ptr (array 1 int)))))
      (let ((val (vec 0)))
        (abc-function val)
        ;; [vec 0] has updated value coming from function
        ) We define the parameter of as being a pointer to an array of 1 rather than an int, and then pass a vector as the argument. If the parameter is in-out, then the vector must be constructed or initialized to contain a value that will convert to the C type. If the parameter is out only, then the FFI definition can use and the vector can contain the value. The FFI mechanism makes use of a type-like representation called the "call descriptor". A call descriptor is an object which uses FFI types to describe function arguments and return values. A FFI descriptor is required to call a foreign function, and to create a FFI closure to use as a callback function from a foreign function back into TXR Lisp. A FFI descriptor object can be constructed from a return value type, and a list of argument types, and several other pieces of information using the function This object can then be passed to to specify the C type signature of a foreign function, or to to specify the C type signature of a FFI closure to bind to a Lisp function. The FFI macros and provide a simplified syntax for expressing FFI call descriptors, which includes a notation for expressing variadic calls. A note about variadic foreign functions: although there is support in the call descriptor mechanism for expressing a variadic function, it expresses a particular instance of a variadic function, rather than the variadic function's type per se. To call the same variadic function using different variadic arguments, different call descriptors are required. For instance to perform the equivalent of the C function call printf("hello\n") requires a certain descriptor. To perform the equivalent of printf("hello, %s\n", name) requires a different descriptor. This group of functions comprises the basic interface to the TXR Lisp's FFI type system module. The function produces and returns a compiled type object from a argument which specifies valid FFI syntax. If the type syntax is invalid, or specifies a nonexistent type specifier or operator, an exception is thrown. Note: whenever a function argument is required to be of FFI type, what it means is that it must be a compiled object, and not a Lisp expression denoting FFI syntax.
      (ffi-type-compile 'int) -> #<ffi-type int>
      (ffi-type-compile
        '(array 3 double)) -> #<ffi-type (array 3 double)>
      (ffi-type-compile 'blarg) -> ;; error The function constructs a FFI call descriptor. The argument must be a nonnegative integer; it indicates the number of arguments in the call. If the call denotes a variadic function, the argument must be an integer at least 1 and less than denoting the number of fixed arguments. If the call denotes an ordinary, non-variadic function, then must either be specified specified as or else equal to the argument. The parameter must be an FFI type. It specifies the function return type. Functions which don't return a value are specified by the (compiled version of) the return type The argument must be a list of types, containing at least elements. If the function takes no arguments, this list is empty. If the function is variadic, then the first elements of this list specify the types of the fixed arguments; the remaining elements specify the variadic arguments. The argument gives the name of the function for which this description is intended, or some other identifying symbol. This symbols is used in diagnostic messages related to errors in the construction of the descriptor itself or its subsequent use. If this parameter is omitted, then the involved FFI functions use their own names in reporting diagnostics. Note: variadic functions must not be called using a non-variadic descriptor, and vice versa, even if the return types and argument types match. Note: unlike the and macros, the function doesn't perform any special treatment of variadic parameter types. When any of the types or occur in the variadic portion of it is unspecified whether a descriptor is successfully produced and returned or whether an exception is thrown. If a descriptor is successfully produced, and then subsequently used for making or accepting calls, the behavior is undefined.
      ;;
      ;; describe a call to the variadic function
      ;;
      ;; type void (*)(char *, ...)
      ;;
      ;; with these actual arguments
      ;;
      ;; (char *, int)
      ;;
      (ffi-make-call-desc
        2 ;; two arguments
        1 ;; one fixed
       (ffi-type-compile 'void) ;; returns nothing
       (list (ffi-type-compile 'str) ;; str -> char *
             (ffi-type-compile 'int))) ;; int
      -->
      #<ffi-call-desc #<ffi-type void>
        (#<ffi-type str> #<ffi-type int>)> The function return if is a type operator symbol: a symbol used in the first position of a recognized compound type form in the FFI type system. Otherwise, it returns The function returns if denotes a type in the FFI type system: either a built-in type or an alias type name established by Otherwise, it returns The function binds a Lisp function which may be a lexical closure, or any callable object, with a FFI call descriptor to produce a FFI closure. A FFI closure is an object of type which is suitable as an argument for the type denoted by the type specifier keyword in the FFI type language. This type appears a C function pointer in the foreign code, and may be called as such. When it is called by foreign code, it triggers a call to The optional parameter controls whether the closure dispatch is "safe", the meaning of which is described shortly. The default value is so that unsafe closure dispatch must be explicitly requested with a argument for this parameter. A callback closure which is safely dispatched, firstly, does not permit the capture of delimited continuations across foreign code. Delimited continuations can be captured inside a closure dispatched that way, but the delimiting prompt must be within the callback's local stack frame, without traversing across the foreign stack frames. Secondly, a callback closure which is safely dispatched doesn't permit direct nonlocal control transfers across foreign code, such as exception handling. Such transfers, however, appear to work anyway (with caveats): this is because they are specially handled. The closure dispatch mechanism intercepts all dynamic control transfers, converts them to an ordinary return from the callback to the foreign code, and resumes the control transfer when the foreign code itself finishes and returns. If the callback returns a value (its return type is other than then in this situation, the callback returns an all-zero-bits return value to the foreign caller. If the parameter is specified and its value is other than then that value will be used as the return value instead of an all-zero bit pattern. An unsafely dispatched closure permits the capture of continuations from the callback across the foreign code and direct dynamic control transfers which abandon the foreign stack frames. Unsafe closure dispatch is only compatible with foreign code which is designed with that usage in mind. For instance foreign code which holds dynamic resources in stack variables will leak those resources if abandoned this way. There are also issues with capturing continuations across foreign code. Note: the C function pointer is called a "closure" because it carries environment information. For instance, if is a lexical closure, invocations of it through the FFI closure occur in its proper lexical environment, even though its external representation is a simple C function pointer. This requires a special trampoline trick: a piece of dynamically constructed machine code with the closure binding embedded inside it, with the C function pointer pointing to the machine code. Note: the same call descriptor can be reused multiple times to create different closures. The same Lisp function can be involved in multiple FFI closures.
      ;; Package the TXR cmp-str function as a string
      ;; comparison callback compatible with:
      ;;
      ;; int (*)(const char *, const char *)
      ;;
      (ffi-make-closure
        (fun cmp-str)
        (ffi-make-call-desc 2 nil ;; two args, non-variadic
          (ffi-type-compile 'int) ;; int return
          [mapcar ffi-type-compile '(str str)])) ;; args The function invokes a foreign function. The argument which must be a object. It is assumed to point to a foreign function. The argument must be a FFI call descriptor, produced by The must correctly describe the foreign function. The zero or more arguments are values which are converted into foreign argument values. There must be exactly as many of these arguments as are required by The function converts every to a corresponding foreign object. If these conversions are successful, the converted foreign arguments are passed by value to the foreign function indicated by An unsuccessful conversion throws an error. When the call returns, the foreign function's return value is converted to a Lisp object and returned, in accordance with the return type that is declared inside The function installs the compiled FFI type given by as a typedef name under the symbol given by After this registration, whenever the type compiler encounters that symbol being used as a type specifier, it will replace it by the type object it represents. The function returns
      ;; define refcount-t as an alias for uint32
      (ffi-typedef 'refcount-t (ffi-type-compile 'uint32)) The function returns an integer which gives the storage size of the given FFI type: the amount of storage required for the external representation of that type. Bitfield types do not have a size; it is an error to apply this function to a bitfield. The size is machine:specific.
      (ffi-size '(ffi-type-compile 'double)) -> 8
      (ffi-size '(ffi-type-compile 'char)) -> 1
      (ffi-size '(ffi-type-compile
                   '(array 42 char))) -> 42 The function returns an integer which gives the alignment the given FFI type. When an instance of is placed into a structure as a member, it is placed after the previous member at the smallest available offset which is divisible by the alignment. The bytes skipped from the smallest available offset to the smallest available aligned offset are referred to as padding. Bitfield types do not have an alignment; it is an error to apply this function to a bitfield. Bitfields are allocated in storage cells, and those cells have alignment which is the same as that of the type The alignment is machine-specific. It may be more strict than what the hardware architecture requires, yet at the same time be smaller than the size of the type. For instance, the size of the type is commonly 8, yet the alignment is often 4, and this is so even on processors like Intel x86 which can load and store a double at a misaligned address. The alignment of an array is the same as that of its element type. The alignment of a structure is that of its member which has the most strict (largest-valued) alignment. It is a property of arrays, derived from requirements governing the C language, that if the first element of an array is at a correctly aligned address, then all elements are. To ensure that this property holds for for arrays of structures, structures sometimes must include padding at the end. This is because the size of a structure without any padding might not be multiple of its alignment, which is derived from the most strictly aligned member. For instance, if we assume an architecture on which the size and alignment of is 4, the size of the structure type would be 5 if no padding were included. However, in an array of these structures, the second element's member would be placed at offset 5, rendering it misaligned. To ensure that every is placed at an offset which is multiple of 4, the struct type is extended with anonymous padding so that its size is 8.
      (ffi-alignof (ffi double)) -> 4 The function calculates the byte offset of within the FFI type If isn't a FFI struct type, or if isn't a symbol naming a member of that type, the function throws an exception. An exception is also thrown if is a bitfield.
      (ffi-offsetof (ffi (struct ab (a int) (b char))) 'b) -> 4 The function reports the number of elements in which must be an array type: an or
      (ffi-arraysize (ffi (array 5 int))) -> 5 The function reports the size of the element type of an array, of the target type of a pointer, or of the base integer type of an enumeration. The argument must be an array, pointer or enumeration type: a type constructed by one of the operators or
      (ffi-elemsize (ffi (array 5 int))) -> 4 ;; (sizeof int) The function retrieves the element type of an array type, target type of a pointer type, or base integer type of an enumeration. The argument must be an array, pointer or enumeration type: a type constructed by one of the operators or
      (ffi-elemtype (ffi (ptr int))) -> #<ffi-type int> This group of macros provides a higher-level language for working with FFI types and defining foreign function bindings. The macros are implemented using the Foreign Function Type API described in the previous section. The macro works in conjunction with the and macros. When a form appears as one of the of the macro, that form is permitted to use the simplified forms of the argument, to refer to library functions succinctly, without having to specify the library. The same remark applies to and regarding their parameter. A form invoking the macro should be a top-level form. The macro creates a global variable named by a symbol generated by whose initializing expression binds it to a dynamic library handle. The macro then creates an environment in which the enclosed and forms can implicitly refer to that library via the global variable. The argument can take on three different forms:
    If is then arranges for the library to refer to the TXR executable itself. If is a literal string, then will arrange for the hidden variable to be initialized with an expression which opens a handle to the specified library. If is any other form, then it is assumed to denote syntax for opening the handle to a library. That syntax is used verbatim as the initializing expression for the generated global variable which holds the library handle.

    The result value of a form is the symbol which names the generated variable which holds the library handle.
      ;; refer to malloc and free functions
      ;; in the executable
      (with-dyn-lib nil
        (deffi malloc "malloc" cptr (size-t))
        (deffi free "free" void (cptr)))
      ;; refer to "draw" function in fictitious
      ;; "libgraphics" library:
      (with-dyn-lib "libgraphics.so.5"
        (deffi draw "draw" int (cptr cptr)))
      ;; refer to "init_foo" function via specific
      ;; library handle.
      (defvarl foo-lib (dlopen "libfoo.so.1"))
      (with-dyn-lib foo-lib
        (deffi init-foo "init_foo" void (void))) The macro arranges for a Lisp function to be defined, via which calls a foreign function. The argument must be a symbol suitable as a function name in a form. This specifies the function's Lisp name. The parameter specifies the foreign function which is to be called. The syntactic variants permitted for its argument are described below. The argument must specify the return type, using the FFI type syntax, as an unquoted literal. The macro arranges for the compilation of this syntax via The argument must specify a list of the argument types, as an unquoted literal list, using FFI type syntax. The macro arranges for these types to be compiled. Furthermore, a special convention may be used for specifying a variadic function: if the (colon) keyword symbol appears as one of the elements of then the form specifies a fixed call to a foreign function which is variadic. The argument types before the colon keyword are the types of the fixed arguments. The types after the colon, if any, are of the variadic arguments. Special considerations apply to some variadic argument types, described below. The following syntactic variants are permitted of the argument:
    If is a literal string, then the form must be enclosed in the macro, appearing as one of that macro's In this situation the literal character string specifies a symbol to be found within the library established by the macro. This manner of specifying the also requires the form to be enclosed in a It selects a particular version of a symbol from the library. If is any other form, then it must specify an expression which evaluates to a object giving the address of a foreign library symbol. If this form is used, then the form need not be surrounded by a call to the macro.

    When the FFI type is used as the type of a variadic parameter, replaces it by the FFI type This treatment is necessary because the C variadic argument mechanism promotes values to Note: due to this substitution, it is possible to pass floating-point values which are out of range of the type, without any diagnosis. The behavior of is undefined in the Lisp-to-C direction, if the C function extracts an out-of-range argument as if it were of type The FFI types and cannot be used for specifying the types of a variadic argument. If any of these occur in that position, throws an error. Rationale: these types are related to the C type type, which requires promotion in variadic passing. Promotion cannot be performed on floating-point values whose byte order has been rearranged, because promotion is a value-preserving conversion.

    The result value of a form is The macro defines, using a Lisp function called Thus the argument must be a symbol suitable as a function name in a form. The and arguments are processed exactly as in the corresponding arguments in the macro. The macro arranges for and to be compiled into a FFI call descriptor. The generated function called then serves as a combinator which takes a Lisp function as its argument, and binds it to the FFI call descriptor to produce a FFI closure. That closure may then be passed to foreign functions as a callback. The macro generates a callback which uses safe dispatch, which is explained in the description of the function. The optional parameter specifies an expression which evaluates to the value to be returned by the callback in the event that a dynamic control transfer is intercepted. The purpose of this value is to indicate to the foreign code that the callback wishes to abort operation; it is useful in situations when a suitable return value will induce the foreign code to cooperate and itself return to the Lisp code which will then continue the dynamic control transfer. The macro is a variant of with the same argument conventions. The difference is that it arranges for to be invoked with for the parameter. This macro has no parameter, since unsafe callbacks do not use it.
      ;; create a closure combinator which binds
      ;; Lisp functions to a call descriptor has the C type
      ;; signature void (*)(int).
      (deffi-cb void-int-closure void (int))
      ;; use the combinator
      ;; some-foreign-function's second arg is
      ;; of type closure, specifying a callback:
      (some-foreign-function
        42
        (void-int-closure (lambda (x)
                            (puts `callback! @x`)))) The macro defines a global symbol macro which expands to an expression accessing a foreign variable, creating the illusion that the variable is available as a Lisp variable holding a Lisp data type. The argument gives the name of the symbol macro to be defined. The argument is one of several permitted syntactic forms which specify the address of the foreign variable. They are described below. The argument expresses the variable type in FFI type syntax. Once the variable is defined, accessing the macro symbol performs a get operation on the foreign variable, yielding the conversion of that variable to a Lisp value. An assignment to the symbol performs a put operation, converting a Lisp object to a value which overwrites the object. Note: FFI memory management is not helpful in the use of variables. Suppose a string value is stored in a variable of type This means that FFI dynamically allocates a buffer which stores the UTF-8 encoded version of the string, and this buffer is placed into the foreign variable. Then suppose another such assignment takes place. The previous value is simply overwritten without being freed. The following syntactic variants are permitted of the argument:
    If is a literal string, then the form must be enclosed in the macro, appearing as one of that macro's In this situation the literal character string specifies a symbol to be found within the library established by the macro. This manner of specifying the also requires the form to be enclosed in a It selects a particular version of a symbol from the library. If is any other form, then it must specify an expression which evaluates to a object giving the address of a foreign library symbol. If this form is used, then the form need not be surrounded by a call to the macro.
The macro defines a global lexical variable called whose value is a object that refers to a symbol in a foreign library. The argument gives the name for the variable to be defined. This definition takes place place as if by the macro. The is syntax which specifies the foreign pointer, using exactly the same conventions as described for the macro, allowing for a shorthand notation if this form is enclosed in a macro invocation. The optional argument must be a symbol. If it is absent, it defaults to nil. This argument specifies the type label for the object which holds the pointer to the foreign symbol. The result value of is the symbol The macro provides a convenient way to define type aliases. The expression is compiled as FFI syntax, and the symbol is installed as an alias denoting that type. The macro yields the compiled version of as its value. The and macros provide a more compact notation for defining FFI structure and union types together with matching names. The semantics follows from these equivalences:
  (deffi-struct S ...) <--> (typedef S (struct S ...))
  (deffi-union U ...) <--> (typedef U (union U ...))
  (deffi-struct point
    (x double)
    (y double)) The macro calculates the size of the FFI type denoted by The expression is compiled to a type using The expression is evaluated to an object value. If denotes an incomplete array or structure type, and the argument is present, then a dynamic size is computed: the actual number of bytes required to store that object value as a foreign representation. The macro arranges for the size calculation to be carried out at macro-expansion time, if possible, so that the form is replaced by an integer constant. This is possible when the is omitted, or if it is a constant expression according to the function. For the type incomplete array types, and bitfield types, the one-argument form of reports zero. For incomplete structure types, the one-argument reports a size which is equivalent to the offset of the last member. The size of an incomplete structure does not include padding for the most strictly aligned member. The macro calculates the alignment of the FFI type denoted by at macro-expansion time, and produces that integer value as its expansion, such that there is no run-time computation. It uses the function. The macro calculates the offset of the structure member indicated by a symbol, inside the FFI struct type indicated by This calculation is performed by a macro-expansion-time call to the function, and produces that integer value as its expansion, such that there is no run-time computation. The macro calculates the number of elements of the array type indicated by This calculation is performed by a macro-expansion-time call to the function, and produces that integer value as its expansion, such that there is no run-time computation. The macro calculates the size of the element type of an array type, or the size of target type of a pointer type indicated by This calculation is performed by a macro-expansion-time call to the function, and produces that integer value as its expansion, such that there is no run-time computation. The macro produce the element type of an array type, or the target type of a pointer type indicated by Note: the macro may be understood in terms of several possible implementations. The form is equivalent to Since there exists an type operator, the expression is also equivalent to The macro provides a shorthand notation for compiling a literal FFI type expression to the corresponding type object. The following equivalence holds:
  (ffi expr) <--> (load-time (ffi-type-compile 'expr)) Communicating with foreign interfaces sometimes requires representations to be initialized consisting of all zero bits, or mostly zero bits. TXR provides convenient ways to prepare Lisp objects such that when those objects are converted to a foreign representation, they generate zero-filled representations. The function provides a convenient means of instantiating a structure for use in foreign function calls, imitating a pattern of initialization often seen in the C language. It instantiates a Lisp by conversion of zero-filled memory through FFI, thus creating a Lisp structure which appears zero-filled when converted to the foreign representation. This simplifies application code, which is spared from providing individual slot initializations which have this effect. The argument must be a compiled FFI type. The remaining arguments must occur pairwise. Each argument must be a symbol naming a slot in the FFI type. The argument which follows it specifies the value for that slot. The function operates as follows. Firstly, the Lisp type is retrieved which corresponds to the FFI type given by A new instance of the Lisp type is instantiated, as if by a one-argument call to Next, each slot indicated by a argument is set to the corresponding Finally, each slot of the struct which is not initialized via and pair, and which is known to the FFI type, is reinitialized by a conversion from a foreign object of all-zero bits to a Lisp value. argument. The object is then returned. Note: the macro provides a less verbose notation based on Note: slots which are not known to the FFI type may be initialized by Each must be a slot of the Lisp type; but need not be declared as a member in the FFI type. The macro provides a convenient way of using using syntax which resembles that of the macro. The macro generates a call, arranging for the argument to be compiled to a FFI type object, and applies quoting to every argument. The following equivalence holds:
  (znew s a i b j ...) <--> (make-zstruct (ffi s)
                        'a i 'b j ...) Given the following FFI type definition
  (typedef foo (struct foo (a (cptr bar)) (b uint) (c bool))) the following results are observed:
  ;; ordinary instantiation
  (new foo) -> #S(foo a nil b nil c nil)
  ;; Under znew, a is null cptr of correct type:
  (znew foo) -> #S(foo a #<cptr bar: 0> b 0 c nil)
  ;; value of b is specified; others come from zeros:
  (znew foo b 42) -> #S(foo a #<cptr bar: 0> b 42 c nil) The function invokes the by-reference in semantics of FFI type against a zero-filled buffer, and a Lisp object This means that if is an aggregate such as a vector, list or structure, it is updated as if from an all-zero-bit foreign representation. In that situation, is also returned. An object which has by-value semantics, such as an integer, is not updated. In this case, nevertheless, the return value is a Lisp object produced by converting an all-zero-bit buffer to The following group of functions provides the means for working with foreign unions, in conjunction with the FFI type. The function instantiates a new object of type based on the FFI type specified by the parameter, which must be compiled FFI type. The object provides storage for the foreign representation of and that storage is initialized to all zero bytes. Additionally, if is specified, but is not, then is stored into the union's via the first member, as if by If the union type has no members, an error exception is thrown. If both and are specified, then is stored into the union using the specified member, as if by The function retrieves the list of symbols which name the members of These are derived from the object's FFI type. It is unspecified whether the list is freshly allocated on each call, or whether the same list is returned; applications shouldn't destructively manipulate this list. The function performs the get semantics (conversion from a foreign representation to Lisp) on the member of which is specified by the argument. That argument must be a symbol corresponding to one of the member names. The object's storage buffer is treated as an object of the foreign type indicated by that member's type information, and converted accordingly to a Lisp object that is returned. The function performs the put semantics (conversion from a Lisp object to foreign representation) on the member of which is specified by the argument. That argument must be a symbol corresponding to one of the member names. The object given as is converted to the foreign representation according to the type information of the indicated member, and that representation is placed into the object's storage buffer. The return value is The and functions perform the FFI in semantics and out semantics, respectively. These semantics are involved in two-way data transfers between foreign representations and Lisp objects. The argument must be a object and the argument a symbol which matches one of that object's member names. In the case of is a Lisp object that was previously stored into using the operation, into the same member that is currently indicated by In the case of is a Lisp object that was previously retrieved from using the operation, from the same member that is currently indicated by The performs the by-value nuance of the in semantics on the indicated member: if the member contains pointers to any objects, those objects are updated from their counterparts in using their respective by-reference in semantics, recursively. Similarly performs the by-value nuance of the out semantics on the indicated member: if the member contains pointers to any objects, those objects are updated with their Lisp counterparts in using their respective by-reference out semantics, recursively. Note: is intended to be used after a FFI call, on a union-typed by-value argument, or a union-typed object contained in an argument, in situations when the function is expected to have updated the contents of the union. The function is intended to be used in a FFI callback, on a union-typed callback argument or union-typed object contained in such an argument, in cases when the callback has updated the Lisp object corresponding to a union member, and that change needs to be propagated to the foreign caller. These functions provide a way to perform I/O on stream using the foreign representation of Lisp objects, performing conversion between the Lisp representations in memory and the foreign representations in a stream. The argument used with these functions must be a stream object which, in the case of input functions, supports and, in the case of output, supports The function encodes into a foreign representation, according to the FFI type The bytes of the foreign representation are then written to If is omitted, it defaults to If the operation successfully writes all bytes of the representation to the value is returned. A partial write causes the return value to be All other stream error situations throw exceptions. The function reads from the bytes corresponding to a foreign representation according to the FFI type If is omitted, it defaults to If the read is successful, these bytes are decoded, producing a Lisp object, which is returned. If the read is incomplete, the value returned is All other stream error situations throw exceptions. The function reads from the bytes corresponding to a foreign representation according to the FFI type If the read is successful, then is updated, if possible, from that representation, using the by-value in semantics of the FFI type and returned. If a by-value update of isn't possible, then a new object is decoded from the data and returned. If the read is incomplete, the value returned is All other stream error situations throw exceptions. Functions in this area provide a way to perform conversion between Lisp objects and foreign representation to and from objects of the type. The function encodes the Lisp object according to the FFI type and returns a new buffer object of type which holds the foreign representation. The function is similar, except that it uses an existing buffer which must be large enough to hold the foreign representation. The argument must be a compiled FFI type. If is has a variable length, then the actual size of the foreign representation is calculated from The argument must be an object compatible with the conversions implied by The optional argument specifies a byte offset from the beginning of the data area of where the foreign representation of is stored. The default value is zero. These functions perform the "put semantics" encoding action similar to what happens to the arguments of an outgoing foreign function call. Caution: incorrect use of this function, or its use in isolation without a matching call, can cause memory leaks, because, depending on temporary resources may be allocated, and pointers to those resources will be stored in the buffer. The function performs the "out semantics" encoding action, similar to the treatment applied to the arguments of a callback prior to returning to foreign code. It is assumed that is an object that was returned by an earlier call to and that the and arguments are the same objects that were used in that call. The argument is a Boolean flag which is true if the buffer represents a datum that is being passed by pointer. If is true, then is converted to a foreign representation which is stored into If it is false, it indicates that the buffer itself is a pass-by-value object. This means that the object itself will not be copied, but if it is an aggregate which contains pointers, the operation will recurse on those objects, invoking their "out semantics" action with pass-by-pointer semantics. The required pointers to these indirect objects are obtained from The optional argument specifies a byte offset from the beginning of the data area of where the foreign representation of is understood to be stored, and where it is updated if requested by The default value is zero. The function returns The function performs the "in semantics" decoding action, similar to the treatment applied to the arguments of a foreign function call after it returns, in order to free temporary resources and recover the new values of objects that have been modified by the foreign function. It is assumed that is a buffer that was prepared by a call to or and that and are the same values that were passed as the corresponding arguments of those functions. The function releases the temporary memory resources that were allocated by or which are obtained from the buffer itself, where they appear as pointers. The function recursively performs the in semantics across the entire type, and the entire object graph rooted at the buffer. The argument is a Boolean flag which is true if the buffer represents a datum that is being passed by pointer. If it is false, it indicates that the buffer itself is a pass-by-value object. Under pass-by-pointer semantics, either a whole new object is extracted from the buffer and returned, or else the slots of are updated with new values from the buffer. Under pass-by-value semantics, no such extraction takes place, and is returned. However, regardless of the value of if the object is an aggregate which contains pointers, the recursive treatment through those pointers involves pass-by-pointer semantics. This is consistent with the idea that we can pass a structure by value, but that structure can have pointers to objects which are updated by the called function. Those indirect objects are passed by pointer. They get updated, but the parent structure cannot. If is has a variable length, then the actual size of the foreign representation is calculated from The optional argument specifies a byte offset from the beginning of the data area of from which the foreign representation of is taken. The function returns either or a new object which is understood to have been produced as its replacement. The function extracts a Lisp value from buffer according to the FFI type The argument is an object of type large enough to hold a foreign representation of at the byte offset indicated by the argument. The argument is compiled FFI type. The optional argument defaults to zero. The external representation in at the specified offset is scanned according to and converted to a Lisp value which is returned. The operation is similar to the "get semantics" performed by FFI in order to extract the return value of foreign function calls, and by the FFI callback mechanism to extract the arguments coming into a callback. The argument may not be a variable length type, such as an array of unspecified size. Functions in this area provide a means for working with foreign arrays, in connection with the FFI type. The and functions allocate storage for the representation of a foreign array, and return a object which holds a pointer to that storage. The argument which must be a compiled FFI type, is retained as the object's element type. Prior to returning, the functions initializes the foreign array by converting the elements of or, respectively, into elements of the foreign array. The conversion is performed using the put semantics of which is a compiled FFI type. The length of the returned is determined from the length of or and from the value of the Boolean argument If is then the length of the is the same as that of the input or A true value of indicates null termination. This causes the length of the to be one greater than that of or and the extra element allocated to the foreign array is filled with zero bytes. The function returns if is a otherwise it returns The function allocates storage for the representation of a foreign array, filling that storage with zero bytes, and returns a object which holds a pointer to that storage. The argument which must be a compiled FFI type, is retained as the object's element type. The argument must be a nonnegative integer; it specifies the number of elements in the foreign array and is retained as the object's length. The size of the foreign array is the product of the size of as reported by the function, and of The function creates a object which refers to the storage provided and managed by the buffer object providing a view of that storage, and manipulation thereof, as an array. The optional parameter specifies an offset from the start of the buffer to the location which is interpreted as the start of the which extends from that offset to the end of the buffer. The default value is zero: the covers the entire buffer. If a value is specified, it must be in the range zero to the length of The argument must be a compiled FFI type whose size is nonzero. The is overlaid onto the storage of as follows: First, is subtracted from the bytewise length of as reported by function to produce the effective length of the storage to be used for the array. The effective length is divided by the size of as reported by The resulting quotient represents the length (number of elements) of the object. Note: the returned object holds a reference to preventing from being reclaimed by garbage collection, thereby protecting the underlying storage from becoming invalid. A subsequent invocation of operation releases this reference. Note: the relationship between the object and is inherently unsafe: if is subsequently subject to operations which reallocate the storage, such as the pointer stored inside the referencing object becomes invalid, and operations involving that pointer have undefined behavior. Note: if the length of the buffer is not evenly divisible by the size of the type, the calculated number of elements is rounded down. The trailing portion of the buffer corresponding to the division remainder, being insufficient to constitute a whole array element, is excluded from the array view. The function requires to be a object which refers to a object for its storage. Such objects are created by the function The function retrieves and returns the buffer object associated with and at the same time also updates the internal properties of using the current information: the pointer to the data, and the length of are altered to reflect the current state of the buffer. The function duplicates the underlying storage of and returns that storage represented as an object of type. The storage size is calculated by multiplying the object's element size by the number of elements. Only that extent of the storage is duplicated. The function creates a object based on a pointer derived from a object. The argument must be of type The object's type tag is ignored. The argument must specify a compiled FFI type, which will become the element type of the returned If is specified as or not specified, then the returned object will be of unknown length. Otherwise, must be a nonnegative integer which will be taken as the length of the array. Note: this conversion is inherently unsafe. The function returns a object which holds a pointer to a object's storage area. The argument must be of type The argument should be a symbol. If omitted, it defaults to This symbol becomes the object's type tag. The lifetime of the returned object is independent from that of If the lifetime of reaches its end before that of the the pointer stored inside the becomes invalid. The function returns the length of the argument, which must be an object of type If has an unknown length, then is returned. The function returns a duplicate of The duplicate has the same element type and length, but has its own copy of the underlying storage. This is true whether or not owns its storage or not. In either case, the duplicate owns its copy of the storage. The attempts to change the length of which must be an object of type. The argument indicates the new length, which must be a nonnegative integer. The operation throws an exception if is negative. An exception is also thrown if is an object which owns the underlying storage. There is no provision in the type to change the storage size. It is permissible to change the length of a object which acts as a view into a buffer (as constructed via the operation). This creates a potentially unsafe situation in which the length requires a larger amount of backing storage than is provided by the buffer. The function accesses an element of the foreign array converting that element to a Lisp value, which is returned. The argument must be a nonnegative integer. If has a known length, must be less than the length. If has an unknown length, then the access is permitted regardless of how positive is the value of Whether the access has well-defined behavior depends on the actual extent of the underlying array storage. The validity of any access to the underlying storage depends on the validity of the pointer to that storage. The access to the array storage proceeds as follows. Every object has an element type, which is a compiled FFI type. A byte offset address is calculated by multiplying the size of the element type of by Then, the get semantics of the element type is invoked to convert, to a Lisp object, a region of data starting at calculated byte offset in the array storage. The resulting object is returned. Assigning an a value to a form is equivalent to using to store the value. The function accesses an element of the foreign array overwriting that element with a new value obtained from a conversion of the Lisp value The return value is The argument must be a nonnegative integer. If has a known length, must be less than the length. If has an unknown length, then the access is permitted regardless of how positive is the value of Whether the access has well-defined behavior depends on the actual extent of the underlying array storage. The validity of any access to the underlying storage depends on the validity of the pointer to that storage. The access to the array storage proceeds as follows. Every object has an element type, which is a compiled FFI type. A byte offset address is calculated by multiplying the size of the element type of by Then, the put semantics of the element type is invoked to convert to a foreign representation, which is written into the array storage started at the calculated byte offset. If has a type which is not compatible with the element type, or a value which is out of range or otherwise unsuitable, an exception is thrown. The function acts upon a object which doesn't own its underlying array storage. It allocates a duplicate copy of the array storage referenced by and assigns to the new copy. Then it marks as owning that storage. Lastly, if references another object, that reference is removed; no longer prevents the other object from being reclaimed by the garbage collector. If already owns its storage, then this function has no effect. If has an unknown size, then an error exception is thrown. A produced by the functions or already owns its storage. A object does not own its storage if it is produced by or by the conversion of a foreign pointer under the control of the FFI type. Because objects derived from foreign pointers via FFI have an unknown size, before using the application must determine the length of the array, and call to establish that length. After the length may not be altered. The function returns if it has performed the duplication operation. If it has done nothing, it returns The function resembles differing from that function only in two ways. Instead of allocating a duplicate copy of the underlying array storage, causes to assume ownership of the existing storage. Secondly, it is an error to use on a which references a buffer object. The function always returns In all other regards, the descriptions of apply to If is a object which owns the storage to which it refers, then function liberates that storage by passing the pointer to the C library function It then replaces that pointer with a null pointer, and changes the size to zero. If doesn't own the storage, an exception is thrown. The function returns the element type of a compiled FFI type. The and functions convert the array storage of to a freshly constructed object representation: vector, and list, respectively. The new vector or list is returned. The object must have a known size; an exception is thrown if these functions are invoked on a object of unknown size. The effective length of the new vector or list is derived from the length of taking into account the value of The Boolean parameter defaults to If specified as true, then it has the effect that the effective length of the returned vector or list is one less than that of in other words, a true value of indicates that holds storage which represents a null-terminated array, and the terminating null element is to be excluded from the conversion. If is true, but the length of is already zero, then it has no effect; the effective length remains zero, and a zero-length vector or list is returned. Conversion of the foreign array to the vector or list is performed by iterating over all of its elements, starting from element zero, up to the element before the effective length. The and functions treat the contents of as a FFI and type, respectively. They invoke the get semantics to convert the FFI array to a Lisp object, and return that object. If the element type is one of or then the expected string conversion semantics applies. The and functions treat the contents of as a FFI and type, respectively. They invoke the put semantics to convert the Lisp object array to the foreign array representation, which is placed into the array storage referenced by If the element type is one of or then the expected string conversion semantics applies. Both of these functions return The function extracts a subrange of a object, returning a new object denoting that subrange. The semantics of and work exactly like the corresponding arguments of the accessor, following the same conventions. The returned shares the array has the same element type as the original and shares the same array storage. If, subsequently, elements of the original array are modified which lie in the range, then the modifications will affect the previously returned subrange The returned references the original object, to ensure that as long as the returned object is reachable by the garbage collector, so is the original. This relationship can be severed by invoking on the returned object, after which the two no longer share storage, and modifications in the original are not reflected in the subrange. If is used as a syntactic place, the argument expressions and are evaluated just once. The prior value, if required, is accessed by calling and is then stored via The function is a specialized version of which works on objects. It replaces a sub-range of with elements from The replacement sequence need not have the same length as the range which it replaces. The semantics of and work exactly like the corresponding arguments of the function, following the same conventions. The semantics of the operation itself differs from the semantics on sequences in one important regard: the object's length always remains the same. The range indicated by and is deleted from and replaced by elements of which undergo conversion to the foreign type that defines the elements of If this operation would make the longer, any elements in excess of the object's length are discarded, whether they are the original elements, or whether they come from Under no circumstances does write an element beyond the length of the underlying storage. If this operation would make the shorter (the range being replaced is longer than then the downward relocation of items above the replacement range creates a gap at the end of which is filled with zero bytes. The return value is itself. The creates a new object which provides an aliased view of the same data that is referenced by the original object. The argument specifies the element type used by the returned aliasing array. If the argument is specified, then the aliased view is displaced by that many bytes from the start of the object. The argument must not be larger than the bytewise length of the array, or an error exception is thrown. The bytewise length of the array is the product of the number of elements and the element size. The default value of is zero: no displacement. If is specified, it indicates the size, in bytes, of the aliased view. This limit must not be such that the aliased view would extend beyond the array, or an error exception is thrown. If omitted, defaults to the entire remainder of the array, after the offset. The number of elements of the returned array are then calculated from The function calculates how many elements of fit into This value becomes the length of the aliasing array which is returned. Since the returned aliasing array and the original refer to the same storage, modifications performed in one view are reflected in the other. The aliasing array holds a reference to the original, so that as long as it is reachable by the garbage collector, so is the original. That relationship is severed if is invoked on the aliasing array. The meaning of the aliasing depends entirely on the bitwise representations of the types involved. Note: does not check whether is a value that is suitably aligned for accessing elements of on some platforms that must be ensured. The function may be invoked on an object that was itself returned by The and functions convert an integer, to a binary image, which is then used as the underlying storage for a The argument, a compiled FFI type, determines the element type for the returned If it is omitted, it defaults to the type, so that the array is effectively of bytes. Regardless of these functions first determine the number of bytes required to represent in a big endian format. Then the number of elements is determined for the array, so that it provides at least as that many bytes of storage. The representation of is then placed into this storage, such that its least significant byte coincides with the last byte of that storage. If the number is smaller than the storage provided by the array, it extended with padding bytes on the left, near the beginning of the array. In the case of must be a nonnegative integer. An unsigned representation is produced which carries no sign bit. The representation is as many bytes wide as are required to cover the number up to its most-significant bit whose value is 1. If any padding bytes are required due to the array being larger, they are always zero. The function encodes negative integers also, using a variable-length two's complement representation. The number of bits required to hold the number is calculated as the smallest width which can represent the value in two's complement, including a sign bit. Any unused bits in the most-significant byte are filled with copies of the sign bit: in other words, sign extension takes place up to the byte size. The sign extension continues through the padding bytes if the array is larger than the number of bytes required to represent the padding bytes are filled with the value (255) if the number is negative, or else 0 if it is nonnegative. The and functions treat the storage bytes object as the representation of an integer. The function simply treats all of the bytes as a big-endian unsigned integer in a pure binary representation, and returns that integer, which is necessarily always nonnegative. The function treats the bytes as a two's complement representation. The returned number is negative if the first storage byte of has a 1 in the most significant bit position: in other words, is in the range to In this case, the two's complement of the entire representation is calculated: all of the bits are inverted, the resulting positive integer is extracted. Then 1 is added to that integer, and it is negated. Thus, for example, if all of the bytes are the value -1 is returned. The and functions perform stream output using the object as a buffer. The semantics of these functions is as follows. A temporary buffer is created which aliases the storage of and this buffer is used as an argument in an invocation of, respectively, the buffer I/O function or The value returned by the buffer I/O function is returned. The and arguments are defaulted exactly in the same manner as by and and have the same meaning. In particular, indicates a byte offset into the object's storage, not an array index. TXR Lisp supports interfacing with modules that make use of the C and feature across their boundaries. It is possible to save a jump location in Lisp code with the macro, such that a foreign function can perform a to that saved context. The jump context buffer, known as the type in C, is modelled as a object whose element type is The function returns such an object. Foreign functions that return a pointer to a may be suitably defined via such that the pointer is mapped to a object whose element type is The resulting object will then be usable as a jump buffer. The features described here are unsafe. When used in certain incorrect ways, the behavior is undefined. Using the macro and function as control primitives in Lisp code not interacting with foreign functions is strongly discouraged. There are situations in which the foreign function calling mechanism allocates temporary dynamic memory for converting between Lisp and C objects. These situations occur when objects are referenced by pointers, and so are are outside of the stack-based argument space. In such a situation, if the foreign function performs a terminating in a macro in Lisp code, that temporary storage will leak. The function returns a new object suitable for use as a jump buffer with the macro and function. The function restores the context saved into the object by the macro. If that macro already terminated, the behavior is undefined. The must be an integer in range of the FFI type That value will be observed in the form, as described. If is (zero) the value is used instead. This is a behavior of the underlying C library function. Note: a context abandoned via will not perform unwinding, similarly to The form which is abandoned by should not be using scoped management of resources that relies on for clean-up. The macro saves the jump context into the object, and evaluates the expression. If the expression terminates normally then the value it produces becomes the result of which terminates. If the performs a to the context saved in then that form is abruptly terminated, without performing any unwinding. Then, the zero or more are evaluated. The form terminates, yielding the value of the last or else The are evaluated in a scope in which the symbol is bound as a variable, taking on the integer value passed to which is never zero. The argument must be a object suitable for use as a jump buffer. The argument must be a bindable symbol. Once terminates, the contents of become indeterminate. Any attempt using an indeterminate is undefined behavior.
  (let ((jb (jmp-buf)))
    (setjmp jb result
      (progn (put-line "setjmp") ;; "setjmp" is printed
             (longjmp jb 42))
      (put-line `result is: @result`))) ;; "result is: 42" is printed

    Note: this example is for illustration only. Using and as Lisp control flow constructs in code not interacting with foreign functions is strongly discouraged. TXR supports two modes of processing of Lisp programs: evaluation and compilation. Expressions entered into the listener, loaded from source files via processed by the function, or embedded into the TXR pattern language, are processed by the evaluator. The evaluator expands all macros, and then interprets the program by traversing its raw syntax tree structure. It uses an inefficient representation of lexical variables consisting of heap-allocated environment objects which store variable bindings as Lisp association lists. Every time a variable is accessed, the chain of environments is searched for the binding. TXR also provides a compiler and virtual machine for more efficient execution of Lisp programs. In this mode of processing, top-level expressions are translated into the instructions of Lisp-oriented virtual machine. The virtual machine language is traversed more efficiently compared to the traversal of the cons cells of the original Lisp syntax tree. Moreover, compiled code uses a much more efficient representation for lexical variables which doesn't involve searching through an environment chain. Lexical variables are always allocated on the stack (the native one established by the operating system). They are transparently relocated to dynamic storage only when captured by lexical closures, and without sacrificing access speed. TXR provides the function for compiling individual functions, both anonymous and named. File compilation is supported via the function The function is provided for compiling expressions in the global environment. This function is the basis for both and The function is provided to list the compiled code in a more understandable way; takes a compiled code object and decodes it into an assembly language presentation of its virtual-machine code, accompanied by a dump of the various information tables. File compilation via refers to a processing step whereby a source file containing TXR Lisp forms (typically named with a file name suffix) is translated into an object file (named with a suffix) containing a compiled version of those forms. The compiled object file can then be loaded via the function instead of the source file. Usually, loading the compiled file produces the same effect as if the source file were loaded. However, note that the behavior of compiled code can differ from interpreted code in a number of ways. Differences in behavior can be deliberately induced. Certain erroneous or dubious situations can also cause compiled code to behave differently from interpreted code. Compilation not only provides faster execution; compiled files also load much faster than source files. Moreover, they can be distributed unaccompanied by the source files, and resist reverse engineering. An important concept in file compilation via is that of the top-level form, and how that term is defined. The file compiler individually processes top-level forms; for each such form, it emits a translated image. In the context of file compilation, a top-level form isn't simply any Lisp form which is not enclosed by another one. Rather, in this specific context, it has this specific definition, which allows some enclosed forms to still be considered top-level forms:

1.  If a form appearing in a TXR Lisp source file isn't enclosed in another form, it is a top-level form.

2.  If a form is top-level form, then each of its constituent forms is also a top-level form.

3.  If a form is top-level form, then each of its constituent forms is also a top-level form.

4.  If an form is top-level form, then each of its constituent forms is also a top-level form.

5.  If a form is top-level form, then its argument is a top-level form.

6.  When a macro form is identified as a top-level form, it is macro-expanded as if by before considering whether it contains top-level forms under rules 2–5.

7.  Rules 2–6 are applied recursively.

8.  No other forms are top-level forms.

    A top-level form is a primary top-level form if it doesn't contain any other top-level forms. This means that it is not a form based on any of the operators or Note that the constituent body forms of a or top-level form are not individual top-level forms, even if the expansion of the construct combines the expanded versions of those forms with Note: the function implements a similar concept, specially recognizing and top-level forms, taking care to macro-expand and evaluate their constituents separately. In turn, the function, when processing Lisp source, evaluates each primary top-level form as if by using the function. The result is that the behavior of loaded source and compiled files is consistent in this regard. The file compiler reads each successive forms from a file, performs a partial expansion on that form, then traverses it to identify all of the top-level forms which it contains. Each top-level form is subject to three actions, either of the latter two of which may be omitted: compilation, execution and emission. Compilation refers to the translation to compiled form. Execution is the invocation of the compiled form. Emission refers to appending an externalized representation of the compiled form (its image) to the output which is written into the compiled file. By default, all three actions take place for every top-level form. Using the operators or execution or emission, or both, may be suppressed. If both are suppressed, then compilation isn't performed; the forms processed in this mode are effectively ignored. When a compiled file is loaded, the images of compiled forms are read from it and converted back to compiled objects, which are executed in sequence. Partial expansion means that file compilation doesn't fully expand each form that is encountered. Rather, an incremental expansion is performed, similar to the algorithm used by the function:

    1.  First, if is a macro, it is macro-expanded as if by an application of the function

    2.  If the resulting expanded form is a or form, then iterates over that form's argument expressions, compiling each expression recursively as if it were a separate expression.

    3.  Otherwise, if the expanded form isn't one of the above three kinds of expressions, it is subject to a full expansion and compilation.

    Note: the structure of these three processing rules above closely resembles that of the three rules given in the description of the function, which is the basis for handling source files in Consequently, macro expansion behaves consistently between and of a source file. Programs specify not only code, but also data. Data embedded in a program is called literal data. There are restrictions on what kinds of object may be used as literal data in programs subject to file compilation. Programs which stray outside of these restrictions will produce compiled files which load incorrectly or fail to load. Literal objects arise not only from the use of literal such as numbers, characters and strings, and not only from quoted symbols or lists. For instance, compiled forms which define or reference free variables or global functions require the names of these variables or functions to be represented as literals. An object used as a literal in file-compiled code must be externalizable which means that it has a printed representation which can be scanned to produce a similar object. An object which does not have a readable printed representation will give rise to a compiled file which triggers an exception. Literals which are themselves read from program source code naturally meet this restriction; however, with the use of macros, it is possible to embed arbitrary objects into program code. If the same object appears in two or more places in the code specified in a single file, the file compilation and loading mechanism ensures that the multiple occurrences of that object in the compiled file become a single object when the compiled file is loaded. For example, if macros are used in such a way that the compiled file defines a function which has a name generated by and there are calls to that function throughout that file, this will work properly: the multiple occurrences of the gensym will appear as the same symbol. However: that symbol in the loaded file will not be identical to any other symbol in the TXR image; it will be newly allocated each time the compiled file is loaded. Interned symbols are recorded in a compiled file by means of their textual names and package prefixes. When a compiled file is loaded, the interned symbols which occur as literals in it are entered into the specified packages under the specified names. The value of the special variable has no influence on this. Circular structures in compiled literals are preserved; on loading, similar circular structures are reproduced. TXR supports the hash-bang mechanism in compiled files, thereby allowing compiled scripts to be executable. When a source file begins with the (hash bang) character sequence, the file compiler propagates that line (all characters up to and including the terminating newline) to the compiled file, subject to the following transformation steps:

1   The line is divided into arguments, on the assumption that they are separated by exactly one space.

2   Then, all occurrences of the argument are replaced by

3   Next, all arguments which end in the suffix have that suffix replaced by

4   The hash bang line is then reconstituted by joining the arguments with a single space.

Furthermore, certain permissions are propagated from a hash-bang source file to the target file. If the source file is executable to its owner, then the target file is made executable as if by using with the mode: all the executable permissions that are allowed by the current are are enabled on the target file. If the target file is thus being marked executable, then additional permissions are also treated as follows. If the target file has the same owner as the source file, and the source file's setuid permission bit is set, then this is propagated to the target file. Similarly, if the target file has the same group owner as the source file, and the source file's group execute bit and setgid permission bit are set, then the setgid bit is set on the target file. TXR's virtual-machine architecture for executing compiled code is evolving, and that evolution has implications for the compatibility between compiled files and the TXR executable image. The basic requirement is that a given version of TXR can load and execute the compiled files which that same version has produced. Furthermore, these files are architecture-independent, except that their encoding is in the local byte order ("endianness") of the host machine. The byte order is explicitly indicated in the files, and the function resolves it. Thus a file produced by TXR running on a 64-bit big-endian Power PC can be loaded by TXR running on 32-bit x86, which is little endian. A given TXR version may also be capable of loading files produced by an older version, or even ones produced by a newer version. Whether this is possible depends on the versions involved. Furthermore, there is a general issue at play: code compiled by newer versions of TXR may require functions that are not present in older versions, preventing that code from running. Newer TXR may support new syntax not recognized by older TXR, and that syntax may end up in compiled files. Compiled files contain a minor and major version number (which is independent of the TXR version). The function examines these numbers and decides whether the file is loadable, or whether it must be rejected. The first version of TXR which featured the compiler and virtual machine was 191. Older versions therefore cannot load compiled files. Versions 191 and 192 produce version 1 compiled files, and load only that version. Versions 193 through 198 produce version 2 compiled files and load only that version. Version 199 produces version 3 files and loads versions 2 and 3. Versions 200 through 215 produce version 4 files and load versions 2, 3 and 4. Versions 216 through 243 produce version 5.0 files and load versions 2, 3, 4 and 5, regardless of minor version. Versions 244 through 251 produce version 5.1 files and load versions 2, 3, 4 and 5, regardless of minor version. Versions 252 through 259 produce version 6.0 files and load only version 6, regardless of minor version. Versions 260 through 294 produce version 7.0 files and load versions 6 and 7, regardless of minor version. Version 261 introduces JSON syntax. Compiled code which contains embedded JSON literals is not loadable by TXR 260 and older. By default, the diagnostic option is enabled in causing unused variables to be diagnosed. The first step in resolving an unused variable diagnostic is to determine whether it is caused by a bug in the code. If so, the resolution is to address the bug. If the situation isn't a bug, then the diagnostic is a false positive, and may be silenced. There are multiple ways to do that, six of which are given here:

1.  Disable the diagnostic: for instance, compile with unused warnings disabled:
      (with-compile-opts (nil unused)
         (compile-file "foo.tl"))

2.  Use the function: the compiler specially recognizes the function such that when any of its arguments are lexical variables, they are marked used:
       (defun stub-function (arg1 arg2)
         (ignore arg1 arg2)) Note that an call may be elided if it occurs in dead code, in which case it won't have the right effect:
      (defun unused-arg (arg) ;; diagnosed as unused
        (when (= (+ 2 2) 5)
          (ignore arg) ;; wrongly placed
          (dead-code)))
      (defun unused-arg (arg) ;; no diagnostic
        (ignore arg) ;; correctly placed
        (when (= (+ 2 2) 5)
          (dead-code)))

3.  Use the function. In the following code, parameter is diagnosed as unused on platforms in which the equality being tested is false, since the expression is constant. In situations like this, the variable is not unused, but only conditionally so. Therefore the name of the function doesn't express the intent very well. The function may be stylistically preferred:
       (defun platform-specific-action (arg)
          (use arg)
          (if (eql (sizeof wchar) 2)
             (do-something arg))) However, unlike takes exactly one argument, and returns that argument rather than

4.  Use an uninterned symbol: unused variable diagnostics are not reported against variables named by uninterned symbols.
      (lambda (x y) y) ;; unused x diagnosed
      (lambda (#:x y) y) ;; no diagnostic

5.  In destructuring and pattern matching, put catch-all variable to use: Examples:
       (tree-case obj
          ((a b) (calculate-something a b))
          (else (transform obj))) ;; unused else
       (tree-case obj
          ((a b) (calculate-something a b))
          (else (transform else))) ;; diagnostic gone
       (match-case obj
          ((@a @b) (calculate-something a b))
          (@else (transform obj))) ;; unused else
       (match-case obj
          ((@a @b) (calculate-something a b))
          (@else (transform else))) ;; diagnostic gone

6.  In pattern matching, use the pattern:
       (match-case obj
          ((@a @nil) (calculate-something a))
          (@nil (transform obj)))

7.  In macro parameter lists use t symbol : in macro-style parameter lists, any variable may be replaced by the symbol to consume a value without binding a variable. This is intended for suppressing unused variable warnings:
       (defmacro foo (x y) ;; y unused
         ^(a b c ,x))
       (defmacro foo (x t) ;; no diagnostic
         ^(a b c ,x))
       (tree-bind (a . (b . c)) obj ;; a, b, unused
         c)
       (tree-bind (t . (t . c)) obj ;; no diagnostic
         c) The and operators can be used to deliberately produce code which behaves differently when compiled and interpreted. In addition, unwanted differences in behavior can also occur. The situations are summarized below. Forms evaluated by are treated differently by the compiler. When a top-level form is compiled, its embedded forms are factored out such that the compiled image of the top-level form will evaluate these forms before other evaluations take place. The interpreter doesn't perform this factoring; it evaluates a form when it encounters it for the first time. The compiler identifies multiple occurrences of equivalent strings and bignum integers that occur as literals, and condenses each one to a single instance, within the scope of the compilation. The scope is possibly as wide as a file. If the literal appears in multiple places in the same file that is processed by in the resulting compiled file, there may be just a single object. For instance, if the file contains two functions:
      (defun f1 () "abc")
      (defun f2 () "abc") when compiled, these will return the same object such that
      (eq (f1) (f2)) -> t No such de-duplication is performed for interpreted code. Consequently, code which depends on multiple occurrences of these objects to be distinct objects may behave correctly when interpreted, but misbehave when compiled. Or vice versa. One example is code which modifies a string literal. Under compilation, the change will affect all occurrences of that literal that have been merged into one object. Another example is an expression like which yields under interpretation because the two strings are distinct object in spite of appearing side by side in the syntax, but when compiled, since they denote the same string object. In the future, objects other than strings and bignums may be similarly consolidated, such as lists and vectors, which means that interpreted code which works today when compiled may misbehave in the future. Note that objects which are literally notated in source code are not the only kinds of objects considered to be literals. Objects which are constructed by macros and inserted into macro-expansions are also literals. Literals are self-evaluating objects that appear as expressions in the syntax which remains after macro-expansion, as well as arguments of the operator. If a macro calculates a new string each time it is expanded, and inserts it into the expansion as a literal, the compiler will identify and consolidate groups of such strings that are identical. A source file may contain unqualified symbol tokens which are interned in the current package. In contrast, a compiled file encodes symbols with full package qualification. When a compiled file is loaded, the current package at that time has no effect on the symbols in the compiled file, even if those symbols were specified as unqualified in the original source file. This difference can lead to surprising behaviors. Suppose a source file contains references to functions or variables or other entities which do not exist. Furthermore, suppose the entities were referenced, in that file, using unqualified symbols which didn't exist, and were expected to come from a different package from the one where they ended up interned. For instance, supposed the file is being processed in a package called and is expecting to use a function which should come from the package. Unfortunately, no such symbol exists. Therefore, the symbol is interned as and not In that case, it should be sufficient to ensure that the function exists, and then reload the source file. The unqualified symbol token in that file will be correctly resolved to that time. However, if the file is compiled, reloading will not be sufficient. Even though the symbol exists, the file will continue to try to refer a function using the symbol which comes from a fully qualified representation stored in the compiled file. The file will have to be recompiled to fix the issue. Unbound variables are treated differently by the compiler. A reference to an unbound variable is treated as a global lexical access. This means that if a variable access is compiled first and then a is processed which introduces the variable as a dynamically scoped ("special") variable, the compiled code will not treat the variable as special; it will refer to the global binding of the variable, even when a dynamic binding for that variable exists. The interpreter treats all variable references that do not have lexical bindings as referring to dynamic variables. The compiler treats a variable as dynamic if a has been processed which marked that variable as special. Arguments of a form (or the equivalent bracket notation) which are unbound symbols are treated differently by the compiler. The code is compiled under the assumption that all such symbols refer to global functions. For instance, if neither nor are defined, then will be compiled under the assumption that they are functions. If they are later defined as variables, the compiled code will fail because no function named exists. The interpreter resolves each symbol in a form at the time the form is being executed. If a symbol is defined as a variable at that time, it is accessed as a variable. If it defined as a function, it is accessed as a function. The symbolic arguments of a form that refer to global bindings are also treated differently by the compiler. For each such symbol, the compiler determines whether it refers to a function or variable and, further, whether the variable is global lexical or special. This treatment of the symbol is then cemented in the compiled code; the compiled code will treat that symbol that way regardless of the run-time situation. By contrast, the interpreter performs this classification each time the arguments of a form are evaluated. The rules are otherwise the same: if the symbol is bound as a variable, it is treated as a variable. If it is bound as a function, it is treated as a function. If it has both bindings, it is treated as a variable. The difference is that this is resolved at compile time for compiled code, and at evaluation time for interpreted code. The following degenerate situation occurs, illustrated by example. Suppose the following definitions are given:
      (defvarl %gensym%)
      (defmacro define-secret-fun ((. args) . body)
        (set %gensym% (gensym))
        ^(defun ,%gensym% (,*args) ,*body))
      (defmacro call-secret-fun (. args)
        ^(,%gensym% ,*args)) The idea is to be able to define a function whose name is an uninterned symbol and then call it. An example module might use these definitions as follows:
      (define-secret-fun (a) (put-line `a is @a`))
      (call-secret-fun 42) The effect is that the second top-level form calls the function, which prints 42 to standard out. This works both interpreted and compiled with Each of these two macro calls generates a top-level form into which the same gensym is inserted. This works under file compilation due to a deliberate strategy in the layout of compiled files, which allows such uses. Namely, the file compiler combines multiple top-level forms into a single object, which is read at once, and which uses the circle notation to unify gensym references. However, suppose the following change is introduced:
      (define-secret-fun (a) (put-line `a is @a`))
      (defpackage foo) ;; newly inserted form
      (call-secret-fun 42) This still works when interpreted, and compiles successfully. However, when the compiled file is loaded, the compiled version of the form fails with an error complaining that the (or other gensym name) function is not defined. This is because for this modified source file, the file compiler is not able to combine the compiled forms into a single object. It would not be correct to do so in the presence of the form, because the evaluation of that form affects the subsequent interpretation of symbols. After the package definition is executed, it is possible for a subsequent top-level form to refer to a symbol in the package such as to occur, which would be erroneous if the package didn't exist. The file compiler therefore arranges for the compiled forms after the to be emitted into a separate object. But that division in the output file consequently prevents the occurrences of the gensym to resolve to the same symbol object. In other words, the strategy for allowing global gensym use is in conflict with support for forms which have a necessary read-time effect such as The solution is to rearrange the file to unravel the interference, or to use interned symbols instead of gensyms. There are differences in behavior between compiled and interpreted code with regard to delimited continuations. This is covered in the Delimited Continuations section of the manual. The function takes the Lisp form and compiles it. The return value is a virtual-machine description object representing the compiled form. This object isn't of function type, but may be invoked as if it were a function with no arguments. Invoking the compiled object is expected to produce the same effect as evaluating the original using the function. The argument indicates that has already been expanded and is to be compiled without further expansion. If is then it is subject to a full expansion. Note: in spite of the name, makes no consideration whether or not is a "top-level form" according to the definition of that term as it applies to processing. Note: a form like will not be processed by in a manner similar to the processing by or In this example, form will not be evaluated prior to the expansion of (and in fact not evaluated at all) and so the latter expression isn't correctly referring to that macro. The form can be processed by however, the macro definition now takes place during expansion, and isn't compiled. The function has no such issue when it encounters such a form at the top-level, because that function will consider a top-level form to consist of multiple top-level forms that are compiled individually, and also executed immediately after being compiled.
      ;; compile (+ 2 2) form and execute to calculate 4
      ;;
      (defparm comp (compile-toplevel '(+ 2 2)))
      (call comp) -> 4
      [comp] -> 4 The function compiles functions. It can compile named functions when the argument is a A function name is a symbol denoting an existing interpreted function, or compound syntax such as to refer to methods. The code of the interpreted function is retrieved, compiled in a manner which produces an anonymous compiled function, and then that function replaces the original function under the same name. If the argument is a lambda expression, then that function is compiled. If the argument is a function object, and that object is an interpreted function, then its code and lexical environment are retrieved and compiled. In all cases, the return value of is the compiled function. Note: when an interpreted function object is compiled, the compiled environment does not share bindings with the original interpreted environment. Modifications to the bindings of either environment have no effect on the other. However, the objects referenced by the bindings are shared. Shared bindings may be arranged using the or macros. The function reads forms from an input file, and produces a compiled output file. First, is converted to a tentative pathname as follows. If specifies a pure relative pathname, as defined by the function, then a special behavior applies. If an existing load operation is in progress, then the special variable has a binding. In this case, will assume that the relative pathname is a reference relative to the directory portion of that pathname. If has the value then a pure relative pathname is used as-is, and thus resolved relative to the current working directory. The tentative pathname is converted to an actual input pathname as follows. Firstly, if the tentative pathname ends with one of the suffixes or then it is considered suffixed, otherwise it is considered unsuffixed. If it is suffixed, then the actual pathname is the same as the tentative pathname. In the unsuffixed case, two possible actual input pathnames are considered. First, if the unsuffixed path refers to a file that can be opened, then that unsuffixed path is taken as actual path. Otherwise, the suffix is added to the tentative pathname, and that becomes the actual path. If the actual path ends in the suffix then the behavior is unspecified. If the parameter is given an argument, then that argument specifies the output path. Otherwise the output path is derived from the tentative input path as follows. If the tentative input path is unsuffixed, then is added to it to produce the output path. Otherwise, the suffix is removed from the tentative input path and replaced with the suffix. The function binds the variables and similarly to the function. Over the compilation of the input file, establishes a new dynamic binding for several special variables. The variable is given a new binding containing the actual input pathname. The variable is also given a new dynamic binding, whose value is the same as the existing binding. Thus if the compilation of the file has side the effect of altering the value of that effect will be undone when the binding is removed after the compilation completes. Compilation proceeds according to the File Compilation Model. If the compilation process fails to produce a successful translation for each form in the input file, the output file is removed. The function differs from in the following regard: compilation is performed only if the input file is newer than the output file, or else if the output file doesn't exist. The always returns if it terminates normally, which occurs if it successfully translates every form in the input file, depositing the translation into the output file. If compilation fails, terminates by throwing an exception. The function returns if it successfully compiles, similarly to If compilation is skipped, the function returns Note: the following idiom may be used to load a file, compiling it if necessary:
      (or (compile-update-file "file")
          (load-file "file")) However, note that it relies on the effect of compiling a source file being the same as the effect of loading the compiled file. This can only be true if the source file contains no or top-level forms. Two or more compiled files that are compiled by the same version of TXR may be catenated together to produce a single file. Such a file may be loaded by the function. The behavior of loading such a file may differ from loading the individual files, because such a is treated as a single operation. The special variable provides control over compiler optimizations. The variable takes on integer values. If the value is it is interpreted as zero. The meaningful range is from 0 to 7. The initial value of the variable is 7. The meanings of the values are as follows:

    0   Almost all optimizations are disabled, except for some strength reductions of instances of he function, to take advantage of certain conditional instructions.

    1   Constant folding is applied, as well as algebraic reductions to list processing and arithmetic code. Two-argument calls to several common arithmetic operators are translated into calls to more efficient two-argument internal functions.

    2   Blocks which can be easily confirmed not to be used as exit points are removed. Variable frames in which no lexically captured variables are bound, and no dynamic variables are bound, are eliminated.

    3   Lambda expressions and calls to combinator functions such as and are lifted to load time, if possible.

    4   Control flow optimizations are applied: jump threading and elimination of unreachable code. Some peephole optimizations are applied to improve certain instruction patterns.

    5   Data flow optimizations are applied, such as elimination of dead register moves, or useless propagations of values from one register to another. More peephole optimizations are applied.

    6   Additional iterations of the levels 4 and 5 optimizations are performed, if the previous iterations have coalesced some basic blocks of the program graph. Also, at this level, expressions containing lambdas are inlined, eliminating the closures. These expressions arise out of syntax and its derivatives.

    7   Certain more rarely applicable optimizations are applied which reduce code size by merging some identical code blocks, or improving some more rarely occurring instruction patterns.
The function removes a previously compiled file associated with if such a file exists. In situations when it successfully removes a file, it returns otherwise The function may also throw an exception, in situations such as encountering a nonexistent directory component or permission problem. First, if specifies a pure relative pathname, as defined by the function, and if the variable contains a value other than then calculates the directory name of as if by using and catenates that directory name with to produce an intermediate path. Otherwise is considered to be the intermediate path. Next, the suffix of the intermediate path is examined. If it ends with or then an attempt is made to remove that path, and the function terminates. If the intermediate path ends with or then two attempts are made to remove a file: first, the suffix is replaced with and that is attempted to be removed. If that fails due to non-existence, then the suffix is tried. Otherwise, if the intermediate path doesn't have any of the above suffixes, then an attempt is made to remove the path with the suffix added, and then with the suffix added. Note: no attempt is made to remove the unmodified intermediate path itself, except in the cases when it ends with or because that risks removing a source file rather than a compiled file. When a file is processed by certain actions, such as the issuance of diagnostics about undefined functions and variables, are delayed until the file is completely processed. The macro allows these actions to be collectively deferred until multiple files are completely processed. The macro evaluates each enclosed in a single compilation environment. After the last is evaluated, deferred actions of any enclosed forms are performed, and then the value of the last is returned. It is permissible to nest forms, lexically or dynamically. The outermost invocation of dominates; all deferred actions are held until the outermost enclosing terminates. These operators take on a special behavior only when they appear as top-level forms in the context of file compilation. When a or form is processed by the evaluator rather than the compiler, or when it is processed outside of file compilation, or when it is appears as other than a top-level form even under file compilation, then these operators behave in a manner identical to When a form appears as a top-level form under file compilation, it indicates to the file compiler that the enclosed in it are not to be evaluated. By default, the file compiler executes each top-level form after compiling it. The operator suppresses this evaluation. When an form appears as a top-level form under file compilation, it indicates to the file compiler that the enclosed in it are not to be emitted into the output file. By default, the file compiler includes the compiled image in the output written to the output file. The operator suppresses this inclusion. Forms which are surrounded by both an form and a form are neither executed nor emitted into the output file. In this situation, the forms are skipped entirely; no compilation takes place. The function not only compiles, but also executes every form for the following reason: the correct compilation of forms can depend on the execution of earlier forms. For instance, code may depend on macros. Macros may in turn depend on functions and variables. All those definitions are required in order to compile the dependent code. Those dependencies may be in a separate file which is loaded by a form; that form must be executed. Note that execution of a form implies that the forms that it contains are evaluated (prior to other evaluations). Suppression of the execution of a form also suppresses the evaluation of forms. Situations in which is useful are those in which it is desirable to stage the execution of some top-level form into the compiled file, and not have it happen during compilation. For instance:
   ;; in a main module
   (compile-only (start-application)) It is not desirable to have the file compiler try to start the application as a side effect of compiling the main module. The right behavior is to compile the top-level form so that this will happen when that module is loaded. Situation in which is useful is for specifying forms which have a compile-time effect only, but are not propagated into the compiled file. For example, since the correct treatment of literal symbols occurring in a compiled file does not depend on the variable, in many cases, the invocation in the file can be wrapped with
  (eval-only (in-package app)) The form must be evaluated during compilation so that the remaining forms are read in the correct package. However the loading of the compiled versions of those forms doesn't require that package to be in effect; thus a compiled image of the form need not appear in the compiled file. Macros definitions may be treated with if the intent is only to make the expanded code available in the compiled file, and not to propagate compiled versions of the macros which produced it. The structure represents compiler options: its slots are variables which affect compiler behavior. The compiler expects the special variable to hold a structure. It is recommended to manipulate options using the macro. Currently, all of the options are diagnostic. In the future, there may be other kinds of options. Diagnostic options which are Boolean take on the values or Numeric options take integer values. The and value are synonyms. A value of means that the option is disabled. The and values mean that the diagnostic controlled by the option will be emitted as a warning. The value indicates that the diagnostic will be an error. The slots of are as follows:
    Diagnostic option, off by default. This option controls whether a diagnostic is emitted whenever a lexical function shadows another lexical function, a global function or a global macro. Note: shadowing of local macros is not diagnosed, because the compiler operates on code in which macros no longer exist. Diagnostic option, off by default. This option controls whether a diagnostic is emitted whenever a lexical variable shadows another lexical variable, a global variable, or a global macro. Note: shadowing of local macros is not diagnosed, because the compiler operates on code in which macros no longer exist. Note: special variables are not diagnosed for shadowing. Diagnostic option, off by default. This option controls whether a diagnostic is emitted whenever a lexical function shadows a variable, or vice versa: whether a lexical variable shadows a function. Diagnostic option, set to warn by default. This option controls whether a diagnostic is emitted whenever a lexical variable is defined whose value is not used. Variables whose names are uninterned symbols are exempt from this diagnostic. The rationale is that uninterned symbols are used for naming machine-generated variables, in generated code such as macro expansions. Situation in which a machine-generated variable is unused arise fairly often, and are and are not the result of a programming error. For the purpose of this diagnostic, what constitutes use of a variable is an access to its value, which isn't optimized away before being noted. Storing a value isn't use. An example of an access which is optimized away before being noted is an access which occurs in trivially dead code: for instance does not access because the compiler discards the expression of an whose expression is constantly false. The discarded expression is never traversed in a way that would cause it to be noted as accessing the variable. Diagnostic option, by default. When set to a positive integer value, it enables logging, with increasing values implying more detailed logging. The value 1 causes and to emit an informational message whenever a file is compiled. The value 2 causes informational messages emitted for each compound top-level that is compiled, if it is a compound form beginning with a symbol. Diagnostic option, by default. Controls whether the compiler issues diagnostics when it encounters a constant expression, whose evaluation throws an exception, such as
The special variable holds a value of type which is a structure type. It is recommended to manipulate options using the macro. The macro takes zero or more arguments. Each argument is either a clause which affects compiler options or else an ordinary which is processed in a context in which the variable has been affected by all of the previous clauses. It is unspecified whether the clauses operate destructively on or freshly bind it. However, the macro dynamically binds at least once, so that when it terminates, its previous value is restored. This binding is performed using When occurs in code processed by the compiler, all of the clause-driven compile option manipulation is performed in the compiler's own context. The changes to the variable are not visible to the code being compiled. Thus the macro may be used to transparently change compiler options over individual subexpressions in compiled code. When occurs in interpreted code, the manipulations of are visible to the This allows interpreted build steps to configure compiler options around functions such as The clauses which operate on options have list syntax consisting of a value followed by one or more symbols which must be the names of options which are compatible with that value. The clause indicates that all those options take on that value. The possible values are: and These values are documented under the description of the structure. The following expression specifies that the file is to be compiled with function and variable shadowing treated as error, but unused variable checking disabled. Then compile with unused variable checking enabled.
  ;; this form must be interpreted in order for
  ;; the compile-file call to "see" the effect of the
  ;; option manipulation.
  (with-compile-opts
     (:error shadow-var shadow-fun)
     (nil unused)
     (compile-file "foo.tl")
     (:warn unused)
     (compile-file "bar.tl"))
  ;; when the following form is compiled, the unused
  ;; variable warning will be disabled just around
  ;; the (let (y) x).
  (lambda (x)
    (with-compile-opts (nil unused)
      (let (y) x)))
  ;; Show detailed traces of what forms are
  ;; compiled in these two files.
  (with-compile-opts
    (2 log-level)
    (compile-file "foo.tl")
    (compile-file "bar.tl")) The operator strongly resembles but has different semantics, relevant to compilation. It also has a stricter syntax in that variables may not be symbols without a only variable binding specifications of the form are allowed. Symbols bound using are expected to be special variables. For every the expression should be true. The behavior is unspecified for any which doesn't name a special variable. When the compiler encounters the construct, the compiler itself establishes a dynamic scope in which the implied special variable bindings are in effect. This effect is not incorporated into the compiled code. The compiler then implicitly places the into a from, and compiles that form. While the implicit is being compiled, the dynamic bindings established by are in scope. Thus may be used to bind special variables which influence compiler behavior. The form is treated like by the interpreter, provided that every names a special variable. The macro makes it possible for a program to evaluate a form, such that, subsequently, the value of that form is then treated as if it were a literal object. Literals are pieces of the program syntax which are not evaluated at all. On the other hand, the values of expressions are not literals. From time to time, certain situations benefit from the program being able to perform an evaluation, and then have the result of that evaluation treated as a literal. The macro makes this possible in its particular manner: that macro allows one or more expressions to be evaluated during macro expansion. The result of the is then quoted and substituted in place of the expression. That result then appears as a true literal to the executing code. The macro similarly arranges for the single form to be evaluated. However, this evaluation doesn't take place at expansion time. It is delayed until the program executes. What exactly "delayed until the program executes" means depends on whether is used in compiled or interpreted code, and in what situation is it compiled. If the form appears in interpreted code, then the exact time when is evaluated is unspecified. The evaluator may identify all forms which occur anywhere in a top-level expression, and perform their evaluations immediately, before evaluating the form itself. Then, when the forms are encountered again during the evaluation of the form, they simply retrieve the previously evaluated values as if they were literal. Or else, the evaluation may be performed late: when the form itself is encountered during normal evaluation. In that case, will still be evaluated only once and then its value will be be inserted as a literal in subsequent reevaluations of that form, if any. If a form appears in a non-top-level expression which is compiled, the compiler arranges for the compiled version of to be executed when compiled version of the entire expression is executed. This execution occurs early, before the execution of forms that are not wrapped in The value produced by is entered into the static data vector associated with the compiled top-level expression, which also holds ordinary literals. Whenever the value of that form is required, the compiled code references it from the data vector as if it were a true literal. When a top-level form is processed by it has no unusual semantics; the effect is that it is replaced by its argument which is in that case also considered a top-level form. The implications of the translation scheme may be understood separately from the perspective of code processed with and A form appearing in a form passed to is translated such that its embedded will be executed each time the virtual-machine description returned by is executed, and the execution of all such forms is placed ahead of other code. A form appearing in an interpreted function which is processed by is evaluated immediately, and its value becomes a literal in the compiled version of the function. A form appearing as a non-top-level form inside a file that is processed by is compiled along with that form and deposited into the object file. When the object file is loaded, each compiled top-level form is executed. Each compiled top-level form's calculations are executed first, and the corresponding values become literals at that point. This execution order is individually ensured for each top-level form. Thus, the forms in a given top-level form may rely on the side-effects of prior top-level forms having taken place. Note that, by default, also immediately executes each top-level form which it compiles and deposits into the output file. This execution is equivalent to a load; it causes forms to be evaluated. The operator must be used around forms which must be evaluated only when the compiled file is loaded, and not at compile time. In all situations, the evaluation of takes place in the global environment. Even if the form is surrounded by constructs which establish lexical bindings, those lexical bindings aren't visible to Which dynamic bindings are visible to depends on the exact situation. If a form occurs in code that had been processed by and is now being loaded by then the dynamic environment in effect is the one in which the occurred, with any modifications to that environment that were performed by previously executed forms. If a form occurs in code that had been processed by then is evaluated in the dynamic environment of the caller which invokes the execution of the resulting compiled object. When a form occurs in the code of a function being processed by then is evaluated in the dynamic environment of the caller which invokes If a form occurs in a form processed processed by the evaluator, it is unspecified whether it takes place in the original dynamic environment in which the evaluator was invoked, or whether it is in the dynamic environment of the immediately enclosing form which surrounds the form. A form may be nested inside another form. In this situation, two cases occur. If the two forms are not embedded in a or else are embedded in the same then the inner form is superfluous due to the presence of the outer That is to say, the inner expression is equivalent to because the outer form already establishes its evaluation to be in a load-time context. If the inner form occurs in a but the outer form occurs outside of that then the semantics of the inner form is relevant and necessary. This is because expressions occurring in a are evaluated when the is called, which may take place from a non-load-time context, even if the itself was produced in a load-time context. An expression being embedded in a means that it appears either in the body, or else in the parameter list as the initializing expression for an optional parameter. When interpreted code containing is evaluated, a mutating side effect may take place on the tree structure of that code itself as a result of the evaluation. If that previously evaluated code is subsequently compiled, the compiled translation may be different from compiling the original unevaluated code. Specifically, the compiler may take advantage of the evaluation which had already taken place in the interpreter, and simply take that value, and avoid compiling entirely. This also has implications on the dynamic environment that is in effect when is evaluated. If is evaluated by the interpreter, then it interacts with the dynamic environment which as in effect in that situation; then when the compiler later just takes the result of that evaluation, the compiler's dynamic environment is irrelevant since isn't being evaluated any more. If when evaluated multiple times, potentially produces a different value on each evaluation, this has implications for the situation when an object produced by is invoked multiple times. Each time such an object is invoked, the forms are evaluated. If they produce different values, then it appears that the values of literals are changing. All lexical closures derived from the same compiled object share the same literal data. The function never evaluates a compiled expression more than once. If the same compiled file is loaded more than once, a new compiled object instance is produced from each compiled expression, carrying its own storage area for literals. The function also never evaluates a compiled expression more than once; it produces a compiled object, and then executes it once in order to obtain a lexical closure which is returned. Invoking the closure doesn't cause the expressions to be evaluated. The form is subject to compiler optimizations. A top-level expression is assumed to be evaluated at load time, so does nothing in a top-level expression. It becomes active inside forms embedded in a expressions. Since may be used to hoist calculations outside of loops, is also active in those parts of loops which are repeatedly evaluated. The use of is similar to defining a variable and then referring to the variable. For instance, a file containing this:
  (defvarl a (list 1 2))
  (defun f () (cons 0 a)) is similar to
  (defun f () (cons 0 (load-time (list 1 2)))) When either file is loaded, in source or compiled form, expression is evaluated at load time, and then when is invoked, it retrieves the list. Both approaches have advantages. The variable-based approach gives the value a name. The semantics of the variable is straightforward. The variable can easily be assigned a new value. Using its name, the variable can be inspected from the interactive listener. The variable can be referenced from multiple top-level forms directly; it is not a static datum tied to a table of literal values that is tied to a single top-level form. Furthermore, the use of versus controls whether the variable gets replaced with a new value when the file is reloaded. The advantage of is that it doesn't require a separate top-level form to achieve its load-time effect: the expression is simply nested at the point where it is needed. The form can therefore be generated by macros, whose expansions cannot inject extra top-level forms into the site where they are invoked. If a macro writer would like some form to be evaluated at load time and its value accessible in a macro expansion that appears arbitrarily nested in code, then may provide the path to a straightforward implementation strategy. Access to a value is fast because it doesn't involve referencing through a variable binding; compiled code accesses the value directly via its fixed position in the static data table associated with that code. This advantage is insignificant, however, because access to lexical variables in compiled code is similarly fast, and a value can easily be propagated from a global variable to a lexical for the sake of speed. That said, eliminates that copying step too. A is also useful when the value is not required, and instead the form produces a useful effect, which should be hoisted to load time. For instance, consider a macro which produces the following expansion:
  (progn (load-time (defvar #:g0025)) (other-logic ... #:g0025)) no matter where this expansion is inserted, and will ensure that the is executed once, when the compiled file is loaded, as if that appeared on its own as a top-level form. Then the form can refer to the variable, without the being evaluated on each execution of the The author of a macro can use to stage the evaluation of global effects that the macro expansion depends on simply by bundling these effects into the expansion, wrapped in The macro is similar to the ANSI Common Lisp special operator. It doesn't support the argument featured in the ANSI CL operator. The semantics of is somewhat more precisely specified in terms of concrete implementation concepts. The ANSI CL may evaluate more than once in interpreted code; effectively, the ANSI CL implementation may treat as This is not true of TXR Lisp's which requires once-only evaluation even in interpreted code. The name is used instead of for several reasons. Firstly, is useful for staging effects, like definitions, to load time, even when the resulting value is not used. Secondly, unlike TXR Lisp, ANSI CL features multiple values: a form can yield zero or more values. The ANSI CL operator, however, is restricted to yielding a single value, and its name may have been chosen to emphasize this aspect/restriction. That doesn't apply in the context of TXR Lisp in which all expressions which terminate normally yield exactly one value, making a suffix that adds no value. Lastly, is shorter, and harmonizes with which existed earlier. The function presents a disassembly listing of the virtual-machine code of a compiled function or form. It also presents the literal data contained in that compiled object in a tabular form which is readily cross-referenced with the disassembly listing. If the argument is a then the function object is retrieved from the binding indicated by the name, in the global namespace. That object is then treated as if it were the argument. A argument is one that is a function object. Only compiled virtual-machine functions can be disassembled; other kinds of functions are rejected by The function will also process the object that is returned by the function. In the case of the entire compiled form containing is disassembled. That form usually contains code which is external to the function, even possibly other functions. The disassembly listing indicates the entry point in the code block where the execution of begins. The function returns its argument. The function writes compiled objects into in the same format as the function. Unlike under the output is written into an arbitrary stream rather than a named file. The objects aren't specified by the to-be-compiled syntax processed from a source file, but rather as zero or more arguments which specify objects that are already compiled. Each must be be one of three kinds of values:

    1.  a virtual-machine-description object returned by function; or

    2.  a compiled function object, satisfying the function or else

    3.  the name of a compiled function object, which may take any of the forms suitable as arguments to the function.

    First, writes some preamble information into Then, for each that is not already a virtual-machine description, its corresponding virtual-machine description is retrieved. The virtual-machine description is converted into the externalized format required for the object format and that externalized format is written into The argument are thus processed in left-to-right order. If exactly one call to is used to populate an initially empty file, and no other data are written into the file, then that file is a valid compiled file. If that file is processed by then each of the externalized forms is converted to a virtual-machine description and executed. Note that virtual-machine descriptions are not functions. A function's virtual-machine description is the compiled version of the top-level form whose evaluation produced that function. For example, if the following top-level form is compiled and executed, two functions are defined:
      (let ()
        (defun a ())
        (defun b ())) Then, the following two expressions all have the same effect on stream
      (dump-compiled-objects s 'a)
      (dump-compiled-objects s 'b) Whether the or symbol is used to specify the object to be dumped, the same virtual-machine description is externalized and deposited into the stream. That machine description, when loaded and executed, defines two functions. On some target platforms, TXR provides an interactive listener, which is invoked using the command-line option, or by executing with no arguments. The interactive listener provides features like visual editing of the command line, tab completion on TXR Lisp symbols, and history recall. The interactive listener prints a numbered prompt. The number in the prompt increments with every command. The first command line is numbered 1, the second one 2 and so forth. The listener accepts input characters from the terminal. Characters are either interpreted as editing commands or other special characters, or else are inserted into the editing buffer. However, control characters which don't correspond to commands are silently rejected. The carriage return character generated by the key indicates that a complete line has been entered, and it is to be interpreted. The listener parses the line as a TXR Lisp expression, evaluates it, and prints the resulting value. If the evaluation of the line throws an exception, the listener intercepts the exception and prints information about it preceded by two asterisks and a space. These asterisks distinguish an exception from a result value. If an empty line is entered, or a line containing only spaces, tabs or embedded carriage returns or linefeeds, the prompt is repeated without incrementing the number. Such a line is not entered into the history. A line which only contains a TXR Lisp comment (optional spaces, tabs or embedded carriage returns or linefeeds, followed by a semicolon), also causes the prompt to be repeated without incrementing the number. However, such a line is entered into the history. The listener does not allow lines containing certain bad syntax to be submitted with If the buffer contains an expression with unbalanced parentheses or brackets, or unterminated literals, then generates a newline character which is inserted into the buffer. In that situation, if that newline character is being added at the very end of the buffer, the listener flashes the exclamation mark character (!) two times to warn the user that line has not been submitted: no computation is taking place, and the listener is waiting for more input. It is possible to force the submission of an unbalanced line using the sequence The interactive listener can only accept up to 4095 abstract characters of input in a single command line. Though the edit buffer is referred as the "command line", it may contain multiline input. The carriage return characters which separate multiple lines count as one abstract character each, and are understood to occupy two display positions. Until TXR 286, the command line had to contain exactly one complete TXR Lisp expression, or a comment. Multiple expressions were not evaluated. This restriction has been lifted: multiple expressions in the command line are parsed as one unit, and evaluated as if they were placed into a form. If all the expressions evaluate and terminate normally, the value of the last expression is printed. In multiline mode, if the number of lines exceeds the number of lines of the terminal display, the editing experience is adversely affected in unspecified ways. The screen updating logic in the listener is based on the assumption that the display terminal uses ANSI emulation. No other terminal emulation is supported. The environment variable is ignored. Pressing in a completely empty command line terminates the listener. Another way to quit is to enter the keyword symbol. When the form input into the listener consists of this symbol, the listener will terminate:
      1> (+ 2 2)
      4
      2> :quit
      os-shell $ Another way to terminate is to evaluate a call to the function. This method allows a termination status to be specified:
      1> (exit 1)
      os-shell $ However, if a TXR interactive session is terminated this way, it will not save the listener history. Raising a fatal signal with the function is another way to quit:
      1> (raise sig-abrt)
      Aborted (core dumped)
      os-shell $ The previous remark about not saving the listener history applies here also. typed while editing a command line is interpreted as an editing command which causes that command line to be canceled. The listener prints the string and repeats the same prompt. If a command line is submitted for evaluation, the evaluation might take a long time or block for input. In these situations, typing will issue an interrupt signal. The listener has installed a handler for this signal which generates an exception of type which is caught by the listener. The exception's message is the string so that the listener ends up printing like in the case of the editing command. In this situation, though, a new command-line prompt is issued with an incremented number, and the exception is recorded as a value. The listener provides useful variables which allow commands to reference the results of previous commands. As noted previously, the commands are enumerated with an incrementing number. Each command's number, modulo 100, corresponds to one of the variables ..., Thus, up to the previous hundred results can be referenced:
      ...
      99> (+ 2 2) ;; stored in *99
      4
      100> (* 3 2) ;; stored in *0
      6
      101> (+ *99 *0) ;; i.e. (+ 4 6)
      10 The listener provides small number of symbol macros for referencing the results of previous commands in a relative. The macro refers to the value of the immediately previous command. The macro refers to the value of the command before that one and so on. Note: each of these macros expands to a reference to the vector, according to the following pattern:
       *-1 --> [*r (mod (- *v 1) 100)]
       *-2 --> [*r (mod (- *v 2) 100)]
       ...
       *-20 --> [*r (mod (- *v 20) 100)] The listener variable evaluates to the current command-line number: the number of the command in which the variable occurs:
      5> *n
      5
      6> (* 2 *n)
      12 The listener variable evaluates to the current variable number: the command number modulo 100:
      103> *v
      3
      104> *v
      4 The listener variable evaluates to a hash table which associates variable numbers with command results:
      213> 42
      42
      214> [*r 13]
      42 The result hash allows relative addressing. For instance the expression refers to the result of the previous command. The interactive listener catches all exceptions. Each caught exception is associated with the command's variable number, and stored as a value in the appropriate listener variable as well as the result hash. Exceptions are turned into values by creating a cons cell whose is the exception symbol and whose holds the exception's arguments. For each caught exception, a message is printed beginning with the sequence Exactly how the message appears depends on the type and content of the exception. The following sections describe the interactive editing commands available in the listener. Terminals can often be configured with different choices of cursor shape: such as a block-shaped cursor, an underline cursor or a vertical line or "I-beam" cursor. In the following sections, the phrase "character under the cursor" refers to the character that is currently covered by a block cursor, underlined by an underline cursor, or that is immediately to the right of an I-beam cursor. Moving within the line is achieved using the left and right arrow keys and In addition, ("back") and ("forward") perform this movement. The command moves to the beginning of the line. ("A" is the beginning of the alphabet). The ("end") command jumps to the end of the line, such that the last character of the line is to the left of the cursor position. On terminals which have the Home and End keys, these may also be used instead of and In line mode, these commands move the cursor to the beginning or end of the edit buffer. In multiline mode, if the cursor is not already at the beginning of a physical line, then moves it to the first character of the physical line. Otherwise, moves the cursor to the beginning of the edit buffer. Similarly, in multiline mode, if the cursor not already at the end of a physical line, moves it there. Otherwise, the cursor moves to the end of the edit buffer. If the cursor is on an opening or closing parenthesis, brace or bracket, the command tries to jump to the matching character. The logic for finding the matching character is identical to that of the Parenthesis Matching feature. If no matching character is found, then no movement takes place. If the cursor is not on an opening or closing parenthesis, brace or bracket, then the closest such character is found. The cursor is moved to that character and then an attempt is made to jump to the matching one from that new position. If the cursor is equidistant to two such characters, then one of them is chosen as follows. If the two characters are oriented in the same way (both are opening and closing), then that one is chosen whose convex side faces the cursor position. Thus, effectively, an inner enclosure is favored over an outer one. Otherwise, if the two characters have opposite orientation (one is opening and the other closing), then the one which is to the right of the cursor position is chosen. Note: the character can be produced on some terminals using (using the keyboard home row 5, not the numeric keypad 5). This the same key which produces the % character when Shift is used. The % character is used in the Vi editor for parenthesis matching. The (twiddle) command exchanges the character under the cursor with the previous character. The Backspace key erases the character to the left of the cursor, and moves the cursor to the position which that character occupied. It doesn't matter whether this key generates ASCII characters 8 (BS) or 127 (DEL): either one is acceptable. The command also performs the same action, since it corresponds to ASCII BS. The command deletes the character under the cursor, if the cursor is block-shaped, or to the right of the cursor if the cursor is an I-beam. the cursor maintains its current character position relative to the start of the line. In multiline mode, if is at the end of a line that is not the last line, it deletes the newline character, causing the following line to be joined to the end of the current line. If the cursor is at the end of the buffer, then does nothing, except if the buffer is completely empty, in which case it is a quit indication. The Delete key, if available on the terminal, is a near synonym of It performs all the same functions, except that it does not act as a quit indication; Delete has no effect when the buffer is empty. When a visual selection is in effect, then and delete that selection, and copy it to the clipboard. The ("word") command deletes the word to the left of the cursor position. More precisely, this command first deletes any consecutive whitespace characters (spaces or tabs) to the left of the cursor. Then, it deletes consecutive non-whitespace characters. Material under the cursor or to the right remains. The deleted material is copied into the clipboard. The ("undo typing") command is a "super backspace" operation: it deletes all characters to the left of the cursor position. The cursor is moved to the leftmost position. In multiline mode, deletes only to the beginning of the current physical line, not all the way to the first position of the buffer. copies the deleted material into the clipboard. The ("kill") command deletes the character under the cursor position and all subsequent characters. The cursor position doesn't change. In multiline mode, deletes only until the end of the current physical line, not the entire buffer. The material deleted by is copied into the clipboard. The ("verbatim") command places the listener's input editor into a mode in which the next character is interpreted literally and inserted into the line, even if that character is a special character such as or a command character. The two-character sequence ("extended verbatim", "super paste") enters into a verbatim insert mode, useful for entry of free-form text. It is particularly useful in multiline mode. In this mode, almost every character is inserted verbatim, including The only commands recognized are: which terminates this mode, (whether that key generates ASCII BS or DEL) and arrow key navigation. inserts a line break, which appears as such in multiline mode, or as in line mode. The command sequence may be used in multiline mode to delete the entire physical line under the cursor. Any lines below that line move up to close the gap. In line mode, the command has no effect, other than canceling select mode. The deleted line, including the terminating newline character, if it has one, is copied into the clipboard. By default, the most recent 500 lines submitted to the interactive listener are remembered in a history. This history is available for recall, making it convenient to repair mistakes, or compose new lines which are based on previous lines. Note that the history suppresses consecutive, duplicate lines. The number of lines retained may be customized using the variable. If the key is used while editing a line, the contents of the line are placed into a temporary save area. The line display is then updated to show the most recent line of history. Using will recall successively less recent lines. The key navigates in the opposite direction: from older lines to newer lines. When is invoked on the most recent history line, then the current line is restored from the temporary save area. Instead of and the commands ("previous") and ("next") may be used. If the key is pressed while a recalled history line is showing, then that line will be submitted as if it were a newly composed line. The originally edited line which had been placed in the save area is discarded. When a recalled line is showing, it may be edited. There are two important behaviors to note here. If a recalled history line is edited, and then or or a navigation command is used to show a different history line, or to restore the original current line, then the edit is made permanent: the edited line replaces its original version in the same position in the history. This feature allows corrections to be made to the history. The edit is recorded in the line's undo history as a single change; if the edited line is visited again, then a single command will revert all the edits that were made. However, if a recalled line is edited and submitted without navigating to another line, then it is submitted as a newly composed line, without replacing the original in the history. Each submitted line is entered into the history, if it is different from the most recent line already in history. This is true whether it is a freshly composed line, a recalled history line, or an edited history line. It is possible to search backwards through the history interactively for a line containing a substring. The command is used to initiate search. The command prompt is replaced with the prefix next to which a pair of empty square brackets appears, indicating that the listener is in search mode. The square brackets are the search box, enclosing the search text, which is initially empty. In search mode, characters may be typed. They accumulate inside the search box, and constitute the string to search for. The listener instantly navigates to the most recent line which contains a substring match for the search string, and places the cursor on the first character of the match. Control characters entered directly are ignored. The command be used to add a character verbatim, as in edit mode. To remove characters from the search box, Backspace can be used. The search is not repeated with the shortened search text: the same line continues to show until a character is added, at which point a new search is issued. Search mode has a "home position": a starting point for searches. The initial home position is whatever line of history is selected when search mode is initiated. Searches work backward in history from that line. If search text is edited by deleting characters and then adding new ones, the new search proceeds from the home position. The command can be used in search mode. It registers the currently showing line as the new home position, and then repeats the search using the existing search text backwards from the new position. If the search text is empty, has no effect. The command leaves search mode at any time and causes the listener to resume editing the original input at the original character position. The key accepts the result of a search and submits it as if it were a newly composed line. Navigation and editing keys may be used in search mode. A navigation or editing key immediately cancels search mode, and is processed in edit mode, using whatever line was located by the search, at the matching character position. The (Clear Screen and Refresh), as well as (Suspend to Background) commands are available in search mode. Their effects takes place without leaving search mode. Navigating to a history line manually using or (or and has the same net effect same as locating that line using search. Normally when the key is used on a recalled history line, the next time the listener is reentered, it jumps back to the newest history position where a new line is about to be composed. The alternative command sequence provides a useful alternative behavior. After the submitted line is processed, the listener doesn't jump to the newest history position. Instead, it stays in the history, advancing forward by one position to the successor of the submitted line. can be used to conveniently submit a range of lines from the history, one by one, in their original order. The equivalent command sequences and insert a word from the previous line at the cursor position. A word is defined as a sequence of non-whitespace characters, separated from other words by whitespace. By default, the last word of the previous line is inserted. Between the and the following or a decimal number can be entered. The number 1 specifies that the last word is to be inserted, 2 specifies the second last word, 3 the third word from the right and so on. Only the most recent three decimal digits are retained, so the number can range from 0 to 999. A value of 0, or a value which exceeds the number of words causes the or to do nothing. Note that "previous line" means relative to the current location in the history. If the 42nd most recent history line is currently recalled, this command takes material from the 43rd history line. The equivalent command sequences and insert an atom from the previous line at the cursor position. A line only makes atoms available if it expresses a valid TXR form, free of syntax errors. A line containing only whitespace or a comment makes no atoms available. For the purposes of this editing feature, an atom is defined as the printed representation of a Lisp atom taken from the Lisp form specified in the previous line. The line is flattened into atoms as if by the function. By default, the last atom is extracted. A numeric argument typed between the and or a can be used to select a atoms by position from the end. The number 1 specifies the last atom, 2 the second last and so on. Only the most recent three decimal digits are retained, so the number can range from 0 to 999. A value of 0, or a value which exceeds the number of words causes the or a to do nothing. Note that "previous line" has the same meaning as for the (insert previous word) command. The command sequences ("repeat") and which are equivalent, insert an entire line of history into the current buffer. By default, the previous line is inserted. A less recent line can be selected by typing a numeric argument between the and the or The immediately previous history line is numbered 1, the one before it 2 and so on. If this command is used during history navigation, it references previous lines relative to the currently recalled history line. If the Tab key is pressed while editing a line, it is interpreted as a request for completion. There is a second completion command: the sequence When completion is invoked with or the listener looks at a few of the trailing characters to the left of the cursor position to determine the applicable list of completions. Completions are determined from among the TXR Lisp symbols which have global variable, function, macro and symbolic macro bindings, as well as the static and instance slots of structures. Symbols which have operator bindings are also taken into consideration. If a package-qualified symbol is completed, then completion is restricted to that package. Keyword symbol completion is restricted to the contents of the keyword package. The namespaces which are searched for symbols are restricted according to preceding character syntax. For instance if the characters or immediately precede the prefix, then only those symbols are considered which are methods: that is, each is the static slot of at least one structure, in which that static slots holds a function. The difference between and is that Tab completion looks only for prefix matches among the eligible identifiers. Thus it is a pure completion in the sense that it suggests additional material that may follow what has been typed. If the buffer contains it will only suggest completions which can be endings for such as and It will not suggest identifiers which rewrite the prefix. By contrast, the completion suggests not only pure completions but also alternatives to the partial identifier, by looking for substring matches. For instance is a possible completion for as is If no completions are found, then the BEL character is sent to the terminal to generate a beep or a visual alert indication. The listener returns to editing mode. If completions are found, listener enters into completion selection mode. The first available completion is placed into the line as if it had been typed in. The other completions may be viewed one by one using the Tab key. (Note that the is not used, only Tab, even if completion mode had been entered via When the completions are exhausted, the original uncompleted line is shown again, and Tab can continue to be used to cycle through the completions again. In completion mode, the character acts as a command to cancel completion mode and return to editing the original uncompleted line. Any other input character causes the listener to keep the currently shown completion, and return to edit mode, where that character is immediately processed as if it had been typed in edit mode. The two character command launches an external editor to edit the current command line. The command line is stored in a temporary file first, and the editor is invoked on this file. When the editor terminates, the file is read into the editing buffer. The editor is determined from the environment variable. If this variable is unset or empty, the command does nothing. The temporary file is created in the home directory, if that can be determined. Otherwise it is created in the current working directory. If the creation of the file fails, then the command silently returns to edit mode. The home directory is determined from the environment variable in POSIX environments. On MS Windows, the variable is probed for the user's directory. If the command line contains embedded carriage returns (which denote line breaks in multiline mode) these are replaced with newline characters when written out to the file. Conversely, when the edited file is read back, its newlines are converted to carriage returns, so that multiline content is handled properly. (See the following section, Multiline Mode.) The listener provides an undo feature. The command ("old", "oops") restores the edit buffer contents and cursor position to a previous state. There is a single undo history which records up the 200 most recent edit states. However, the states are associated with history lines, so that it appears that each line has its own, independent undo history. Undoing the edits in one line has no effect on the undo history of another line. Undo also records edits for lines that have been canceled with and are not entered into the history, making it possible to recall canceled lines. The undo history is lost when TXR terminates. Undo doesn't save and restore previous contents of the clipboard buffer. There is no redo. When undo removes an edit to restore to a prior edit state, the removed edit is permanently discarded. Note that if undo is invoked on a historic line, each undo step updates that history entry instantly to the restored state, not only the visible edit buffer. This is in contrast to the way new edits work. New edits are not committed to history until navigation takes place to a different history line. Also note that when new edits are performed on a historic line and it is submitted with without navigating to another line, the undo information for those edits is retained, and belongs to the newly submitted line. The historic line hasn't actually been modified, and so it has no new undo information. However, if a historic line is edited, and then navigation takes place to a different historic line, then the undo information is committed to that line, because the modifications to the line have been placed back in the history entry. The interactive listener supports visual copy and paste operation. Text may be visually selected for copying into a clipboard or or for deletion. In visual selection mode, the actions of some editing commands are modified so that they act upon the selection instead of their usual target, or upon both the target and the selection. The command enters into visual selection mode and marks the starting point of the selection, which is considered the position immediately to the left of the current character. While in visual selection mode, it is possible to move around using the usual movement commands. The ending point of the selection tracks the movement. The selected text is displayed in reverse video. Typing again while in visual selection mode cancels the mode. Tab completion, history navigation, history search and editing in an external editor all cancel visual selection mode. By default, the selection excludes the character which lies to the right of the rightmost endpoint. Thus, the selection simply consists of the text between these two positions, whether or not they are reversed. This style of selection pairs excellently with an I-beam style cursor, and has clear semantics. The endpoints are referenced to the positions between the characters, and everything between them is selected. The selection behavior may be altered using the Boolean configuration variable This variable is by default. If it is changed to true, then the selection includes the character to the right of the rightmost endpoint, if there is such a character within the current line. This style of selection pair well with a block-shaped cursor. It creates the apparent semantics that the endpoints of the selection are characters, rather than points between characters, and that these characters are included in the selection. In visual selection, the starting point of the selection remains fixed, while the ending point tracks the movement of the cursor. The command will exchange the two points. The effect is that the cursor jumps to the opposite end of the selection. That end is now the ending point which tracks the cursor movement. The command ("yank") copies the selected text into a clipboard buffer. The previous contents of the clipboard buffer, if any, are discarded. Unlike the history, the clipboard buffer is not persisted. If TXR terminates, it is lost. If the command is invoked while a selection is in effect, then instead of deleting the character under the cursor, it deletes the selection, and copies it to the clipboard. The Delete key has the same effect. and have no effect on the clipboard when visual selection is not in effect, and they operate on just one character. The command ("quote the clipboard") inserts text from the clipboard at the current cursor position. The cursor position is updated to be immediately after the inserted text. The clipboard text remains available for further pasting. If nothing has been yet been copied to the clipboard in the current session, then this command has no effect. The command sequence ("exchange quote") exchanges the selected text with the contents of the clipboard. The selection is copied into the clipboard as if by and replaced by the previous contents of the clipboard. If the clipboard has not yet been used in the current session, If nothing has been yet been copied to the clipboard in the current session, then this command behaves like text is yanked into the clipboard, but not deleted. In visual selection mode, an editing commands may be used which insert new text, or a character may be typed in order to insert it. When this happens, the selection is first deleted and visual mode is canceled. Then the insertion takes place and visual mode is canceled. The effect is that the newly inserted text replaces the selected text. This applies to the Clipboard Paste command also. If a selection is effect when is invoked, the selected text is replaced with the clipboard buffer contents. When a selection is replaced in this manner, the contents of the clipboard are unaffected. In visual mode, it is possible to issue commands which delete text. One such command is Its special behavior in selection mode, Visual Cut, is described above. The key and also have a special behavior in select mode. If the cursor is at the rightmost endpoint of the selection, then these commands delete the selection and nothing else. If the cursor is at the leftmost endpoint of the selection, then these commands delete the selection, and take their usual effect of deleting a character also. In both cases, selection mode is canceled. The clipboard is not affected. The command for deleting the previous word, when used in visual selection mode, deletes the selection and cancels selection mode, and then deletes the word before the selection. Only the deleted selection is copied into the clipboard, not the deleted word. All other deletion commands such as simply cancel visual selection mode and take their usual effect. The listener operates in one of two modes: line mode and multiline mode. This is determined by the special variable whose default value is (multiline mode). It is possible to toggle between line mode and multiline mode using the command. In line mode, all input given to a single prompt appears to be on a single line. When the line becomes longer than the screen width, it scrolls horizontally. In line mode, carriage return characters embedded in a line are displayed as In multiline mode, when the input exceeds the screen width, it simply wraps to take up additional lines rather than scrolling horizontally. Furthermore, multiline mode not only wraps long lines of input onto multiple lines of the display, but also supports true multiline input. In multiline mode, carriage return characters embedded in input are treated as line breaks rather than being rendered as Because carriage returns are not line terminators in text files, lines which contain embedded carriage returns are correctly saved into and retrieved from the persistent history file. When is typed in multiline mode, the listener tries to determine whether the current input, taken as a whole, is an incomplete expression which requires closing punctuation for elements like compound expressions and string literals. If the input appears incomplete, then the is inserted verbatim at the current cursor position, rather than signaling that the line is being submitted for evaluation. The command sequence also has this behavior. In addition to multiline mode, the listener provides support for directly parsing input from the terminal, suitable for processing large amounts of pasted material. If the keyword is entered into the listener, it will temporarily suspend interactive editing and allow the TXR Lisp parser to read directly from standard input. The reading stops when an error occurs, or EOF is indicated by entering In direct parsing mode, each expression which is read is evaluated, but its value is not printed. However, the value of the last form evaluated is returned to the interactive listener, which prints the value and accepts it as if it as the result value of the command. Note that none of the material read from the terminal is entered into the interactive history. Only the command which triggers this parsing mode appears in the history. The command clears the screen and redraws the line being edited. This is useful when the display is disturbed by the output of some background process, or serial line noise. The ("Zzzz... (sleep)") command causes TXR to be placed into the background in a suspended, and control returned to the system shell. Bringing the suspended TXR back into the foreground is achieved with a shell job-control command such as the command in GNU Bash. When TXR is resumed, the interactive listener will redisplay the edited line and restore the previous cursor position. Making full use of this feature requires a POSIX job control shell, in the sense that without job control support in the shell, there may not be a way to restore TXR into the terminal session's foreground, causing the user to lose interactive control over that TXR instance. The command shows a summary of commands, in a four-line display which temporarily replaces the editing area. The help text is divided into several pages. dismisses the display, and returns to editing. The and keys return to the previous screen. The and commands are available, having their usual meaning of suspending and refreshing the display. Any other key advances to the next screen. Advancing from the last screen, dismisses the display, and returns to editing. Navigating to the previous screen when the first screen is being shown also dismisses the display and returns to editing. The command prints the current prompt, followed by a newline, without incrementing the prompt number. The command prints just the current prompt number, followed by a newline, without incrementing the number. In plain mode, the command enables the printing of prompts. The full prompt is printed before reading each new command line. An abbreviated prompt is printed before reading the continuation lines of an incomplete expression. The printing of prompts is automatically enabled if the input device is an interactive terminal. None of these prompt-related commands are entered into the history. When the input device isn't an interactive terminal, or if the or command-line operations are used when invoking TXR, the listener operates in plain mode. It reads input without providing any of the editing features of visual mode: no completion, history recall, selection, or copy and paste. Only the line editing features provided by the operating system are available. Prompts appear if standard input is an interactive terminal, or if explicitly enabled. There is still an incrementing counter, and the numbered variables for accessing evaluation results are established. Lines are still entered into the history, and the interactive profile is still processed, as usual. Plain mode reads whole lines of input, yet recognizes multi-line expressions. Whenever a line of input is read which represents incomplete syntax, another line of input is read and appended to that line. This repeats until the accumulated input represents complete syntax, and is then processed as a unit. Like in visual mode, each unit of input may contain multiple expressions. These are parsed as a unit and evaluated as if they were the elements of a expression. The resulting value which is printed is that of the last expression. Unless the option has been used, when the listener starts up, it looks for file called in the user's home directory, as determined by the environment variable in POSIX environments or the environment variable on MS Windows. If that variable doesn't exist, no further attempt is made to locate this file. If the file exists, it is subject to security checks. First, the is applied to its path name. The function validates that no component of the path name is a directory that is writable to another user, or a symbolic link that could be rewritten by another user. If that check passes, the file is then checked with the function which requires that other users have no read or write permission. If the checks fail, then an error message is displayed and the file is not loaded. If the file passes the security check, it is expected to be readable and to contain TXR Lisp forms, which are read and evaluated. Syntax errors encountered while reading the profile file are displayed on standard output, and any exceptions thrown that are derived from are caught and displayed. The interactive listener starts in spite of these situations. Exceptions not derived from will terminate the process. The profile file is not read by noninteractive invocations of TXR: that is, when the option isn't present. The history is maintained in a text file called in the user's home directory. Whenever the interactive listener terminates, this file is updated with the history contents stored in the listener's memory. The next time the listener starts, it first reloads the history from this file, making the most recent expressions of a previous session available for recall. The history file is maintained in a way that is somewhat robust against the loss of history arising from the situation that a user manages multiple simultaneous TXR sessions. When a session terminates, it doesn't blindly overwrite the history file, which may have already been updated with new history produced by another session. Rather, it appends new entries to the history file. New entries are those that had not been previously read from the history file, but have been newly entered into the listener. An effort is made to keep the history file trimmed to no more than twice the number of entries specified in The terminating session first makes a temporary copy of the existing history, which is trimmed to the most recent entries. New entries are then appended to this temporary file. Finally, the actual history file is replaced with this temporary file by a a rename operation. This algorithm doesn't use locking, and is therefore not robust against the situation when a two or more multiple interactive TXR sessions belonging to the same user terminate at around the same time. The home directory is determined from the contents of the environment variable in POSIX environments or on MS Windows. If this variable doesn't exist, or the user doesn't have permissions to write to this directory or to an existing history file in that directory, then the history isn't saved. It is possible to save the history without terminating the interactive session, using the command. This saves the history in the manner described above. Each invocation of only adds to the history file new input since the most recent command. When the history file is loaded, security checks take place, in exactly the same way that the file is validated. First the path of the history file is checked using the function which determines that no component of the path name can be subverted by another user, other than the superuser. If that check passes, then the file is checked using which requires that other users have no read or write permission. If the checks fail, then an error message is displayed and the history file is not loaded. A feature of the listener is visual parenthesis matching in the form of a brief forward or backward jump of the cursor. This provides a hint to the programmer, helping to prevent avoid parenthesis balancing errors. When any of the three closing characters or is inserted, the listener scans backward for the matching opening character. Likewise, if any of the three opening characters or is inserted in the middle of text, the listener scans forward for the matching closing character. If the matching character is found, the cursor jumps to that character and then returns to the original position a brief moment later. If a new character is typed during the brief time delay, the delay is immediately canceled, so as not to hinder rapid typing. This back-and-forth jump behavior also occurs when a character is erased using Backspace, and the cursor ends up immediately to the right of a parenthesis. Note that the matching is unsophisticated; it doesn't observe the lexical conventions and syntax of the TXR Lisp programming language. For instance, a closing parenthesis outside a string literal may match match an opening one inside a string literal. The listener's behavior can be influenced through values of certain global variables. The settings can be made persistent by means of setting these variables in the interactive profile file. This special variable determines how many lines of history are retained by the listener. Changing this variable from within the listener has an instant effect. If the number is reduced from its current value, history lines are immediately discarded. The default value is 500. This is a Boolean variable which indicates whether the listener is in multiline mode. The default value is Changing this variable from within the listener takes effect immediately for the next line of input. If multiline mode is toggled interactively from within the listener, the variable is updated to reflect the latest state. This happens when the command is submitted for evaluation. This Boolean variable controls the behavior of visual selection. It is by default. A visual selection is determined by endpoints, which are abstract positions understood as being between characters. When a visual selection begins, it marks an endpoint immediately to the left of a block-shaped cursor, or precisely at the in-between position of an I-beam cursor. The end of the visual selection is similarly determined from the ending cursor position. The selection consists of those characters which lie between these positions. This style of selection pairs well with an I-beam style cursor shape. If the variable is set true, then the selection also includes one more character to the right of the rightmost endpoint, if there is such a character within the current line, giving rise to the appearance that the selection is determined by the starting and ending character, and includes them. This type of selection pairs well with a block-shaped cursor. This Boolean variable controls how the listener prints the results of evaluations. It is by default. When the variable is the evaluation result of each line entered into the listener is printed using the function. Thus values are rendered in a machine-readable syntax, ensuing read/print consistency. If the variable is set true, the evaluation result of each line is printed using the function. The special variable controls whether or not a "greedy evaluation" feature is enabled in the listener. The default value is disabling the feature. Greedy evaluation means that after the listener evaluates the input expressions successfully and prints the value of the last one, it then checks whether that value is an expression that may be further subject to nontrivial evaluation. If so, it evaluates that expression, and prints the resulting value. The process is then repeated with the resulting value. It keeps repeating until evaluation throws an error, or produces a self-evaluating object. These additional evaluations are performed in such a way that all warnings are suppressed and all other exceptions are intercepted. Greedy evaluation doesn't affect the state of the listener. Only the original expression is entered into the history. Only the value of the original expression is saved in the result hash or a numbered variable. The command-line number is incremented by one. The additional evaluations are only performed for the purpose of producing useful output. The evaluations may have side effects.
      1> (set *listener-greedy-eval-p* t)
      t
      2> 'a
      a
      3> (defvar b 2)
      b
      2
      4> (defvar c '(+ 2 2))
      c
      (+ 2 2)
      4
      5> (defvar d '(list '+ 2 2))
      d
      (list '+ 2 2)
      (+ 2 2)
      4 The form produces symbol as its result value. That symbol has a variable binding as a result of that and so evaluates; that evaluation produces the contents of That object is a Lisp expression and is evaluated, producing and that is also an expression, which reduces to The object is self-evaluating, and so the greedy evaluation process stops. The special variable controls whether or the listener is operating in "auto compound expression" mode. The default value is disabling the feature. Normally, an input line can contain multiple expressions, which are treated as if they were combined into a single expression by Thus all the expressions are evaluated, and the value from the last one is printed. In auto compound mode, the behavior changes. An input line which consists of multiple expressions is turned into a compound form whose constituents are those items. Thus, for instance, the input is treated as the compound expression resulting in being calculated. When a single expression is input, it is evaluated as-is, and thus in that case auto compound expression mode makes no difference. The special variable holds a character string representing a web URL intended to point to the HTML version of this document. The initial value points to the publicly hosted document on the Internet. The user may change this to point to another location, such as a locally hosted copy of the document. This variable is used by the function. The function provides help for the library symbol If information about is available in the HTML version of this document, and is indexed, then this function causes that document to be opened using a web browser, such that the browser navigates to the appropriate section of the manual. If the argument is omitted, then the document is opened without navigating to a particular section. The base URL for the document is configured by the variable. If is successfully found, or else not specified, and successfully invokes the URL-opening mechanism, it returns Otherwise, it throws an error exception. The web browser is invoked using a system-dependent strategy. On MS Windows, the function is relied upon to open the URL. On other platforms, if the environment variable exists and is nonempty, its value is assumed to indicate the name or path of the web-browsing program which can accept the URL as an argument. If this variable doesn't exist or is empty, then searches for a system-dependent URL-opening utility, such as If this utility is not found, then falls back to searching for a browser using one of several names. If no URL-opening mechanism is identified using the above strategies, an error exception is thrown. However, if the mechanism is identified, but does not successfully dispatch the URL to a browser, there is no requirement to throw an error exception. It may appear that the function returns but has no effect. The function returns a randomly selected string containing a humorous quip, quote or witticism. The following code may be added to to produce a random quip on startup:
      (put-line (quip)) The function was introduced in TXR 244. If the is used with installations of older TXR versions, it is recommended to use the following, to avoid calling the undefined function, as well as to prevent a warning:
      (if (fboundp 'quip)
        (put-line (quip))
        (defun quip ())) On platforms with the Unix filesystem and process security model, TXR has support for executing setuid/setgid scripts, even on platforms whose operating system kernel does not honor the setuid/setgid bit on hash-bang scripts. On these systems, taking advantage of the feature requires TXR to be installed as a setuid/setgid executable. For this reason, TXR is aware when it is executed setuid and takes care to manage privileges. The following description about the handling of setuid applies to the parallel handling of setgid also. When TXR starts, early in its execution it determines whether or not is is executing setuid. If so, it temporarily drops privileges, as a precaution. This is done before processing the command-line arguments. When TXR determines that it is executing a setuid script (a file marked executable to its owner and attributed with the set-user-ID bit), it then attempts to impersonate the owner of the script file by changing to effective user ID to that owner just before executing the file. It retains the real and saved user ID. If the attempt to assume that user ID is unsuccessful, then TXR permanently drops setuid privileges before executing the script. Likewise, before executing any code other than a setuid script, TXR also drops privileges. TXR tries to honor and implement the setuid permissions on a script whether or not it is running setuid. When not running setuid, it nevertheless tries to change its effective user ID to that of the owner of the setuid script. This will succeed if it has sufficient permissions to do so. To rephrase: in order for TXR to execute a file which is setuid root, it has to be running with a root effective user ID somehow. In order to execute a file which is setuid to a non-root user, TXR has to be running effectively as root or else as that user. It doesn't matter whether these privileges are achieved effectively using the setuid mechanism, or whether TXR is running with the required user ID as its real ID. However, if TXR is running setuid, it takes special care to temporarily drop the privileges as early as possible, and eventually to drop the privileges permanently before executing any code, other that the setuid script. If the setuid script cannot be executed with the privileges it calls for, TXR also drops privileges and executes it anyway, strictly as the real user who invoked the TXR executable. What it means to drop privileges is to change the effective user ID and the saved user ID to be equal to the real user ID. On platforms where the function is available, TXR uses that function to drop privileges. On platforms where is not available, TXR tries to drop privileges using the C language function call where is the previously noted real user ID obtained from On some platforms, this only works for dropping root privileges: it overwrites the real and saved ID only if the caller is effectively root. On those platforms, this approach does not drop non-root privileges. TXR tries to detect whether this approach worked by evaluating the C language expression where is the previously noted effective user ID. In other words, it attempts to regain the dropped privilege by recovering the previous effective ID. If this attempt succeeds, TXR immediately aborts. Dropping setgid privileges is similar. Where is available it is used, otherwise an attempt is made with where is the previously noted real group ID. Then a test using is performed using the original effective group ID as This is done after dropping any setuid root user ID privilege which would allow such a test to succeed. If TXR is running both setuid and setgid, and execute a script which is setuid only, it will still drop group privileges, and vice versa: if it executed a setgid script, it will drop user privileges. For instance, if a root-owned TXR runs a setgid script which is owned by user and group-owned by group that script will run with an effective group ID of 20. The effective user ID will be that of the user who invoked the script: TXR will drop the root privilege to the original real ID of the user, and while for the setgid operation, it will change to the group ID of the script. The setuid/setgid privilege machinery in TXR does not manipulate the list of supplementary ("ancillary", in the language of POSIX) group IDs. It is unnecessary for security because the list does not change while running with setuid privilege. No group IDs are added to the list which need to be retracted when privileges are dropped. The supplementary groups also persist across the execution of a setuid/setgid script. The TXR executable image supports a general mechanism by means of which a custom program can be packaged as an apparent standalone executable. The TXR executable contains a 128 byte data area preceded by the seven-byte ASCII character sequence The 128 byte data area which follows this identifying prefix represents a null-terminated UTF-8 string. In the stock executable, this area is filled with null bytes. If the TXR executable is edited such that this area is replaced with a nonempty, null-terminated UTF-8 string, the program will, for the purposes of command-line-argument processing, treat this string as if it were the one and only command-line argument. (The original command-line arguments are still retained in the and variables). The function creates a copy of the TXR executable with a custom internal argument. Suppose that TXR is copied to an executable in the same directory called (or on an operating system which requires the suffix). Also suppose that in the same directory, there exists a file called This executable can then be edited so that the data area which follows the bytes contains the following string:
      --args|-e|(load (path-cat (dir-name txr-exe-path) "main.tl")) When the executable is invoked, it will process the above string as a single command-line argument, causing the TXR Lisp source file to be loaded. Any arguments passed to are ignored and available to via the variable. The TXR executable may require library files, depending on the functionality invoked by the program code. Library files are located relative to the installation directory, called the sysroot. The executable tries to dynamically determine the sysroot from its own location, according to the following directory structure. The executable may be renamed, it need not be called
      /path/to/sysroot/bin/txr
                   .../share/txr/stdlib/cadr.tl
                            .../stdlib/cadr.tlo
                            .../stdlib/except.tl
                            ...
                   .../share/txr/lib/... The above structure is assumed if the executable finds itself in a directory named Otherwise, if the executable finds itself in a directory not named the following structure is expected:
      /path/to/installation/txr
                        .../stdlib/cadr.tl
                        .../stdlib/cadr.tlo
                        .../stdlib/except.tl
                            ...
                        .../lib/... The directory shown above is for third-party libraries. This is the directory indicated in the default value of the special variable. The directory is not required to exist. Note that this structure had changed starting in TXR 264. Older versions of TXR, when the executable is not in a directory named expect the following structure:
      /path/to/installation/txr
                        .../share/txr/stdlib/cadr.tl
                        .../share/txr/stdlib/cadr.tlo
                        .../share/txr/stdlib/except.tl
                            ... When a custom application is deployed using a possibly renamed executable, one of the above structures should be observed: either the sysroot with a subdirectory where the executable is located, on the same level with the directory, or else the second structure in which the directory is a direct subdirectory of the executable directory. If one of these structures is not observed, the application may fail due to the failure of a library file to load. If the executable discovers that its name ends in the suffix (or else on the MS Windows platform) then the behavior is as if the command line option had been given. Similarly, if the executable finds that its name ends in (or on MS Windows) it behaves as if the option had been given. The function produces an edited copy of the TXR executable at the specified inserting as the internal argument string. In order for the copied executable to be useful, the required installation directory structure must be provided around it, as described in the previous section, Deployment Directory Structure. The return value of is unspecified. The should encode to 127 bytes of UTF-8 or less, or else it will be abruptly truncated, possibly in the middle of a UTF-8 sequence. Create a copy of TXR called which will load a file called that is located in the same directory.
      (save-exe
        "myapp"
        "--args|-e|(load (path-cat (dir-name txr-exe-path) \
        \ \"main.tl\"))") TXR had a simple, crude, built-in debugger, which was removed. New TXR versions are usually intended to be backward-compatible with prior releases in the sense that documented features will continue to work in the same way. Due to new features, new versions of TXR will supply new behaviors where old versions of TXR would have produced an error, such as a syntax error. Though, strictly speaking, this means that something is working differently in a new version, replacing an error situation with functionality is usually not considered a deviation from backward-compatibility. There is one notable deviation from this general requirement for backwards compatibility: the handling of compiled files. For pragmatic reasons, from time to time TXR may break backward compatibility, such that a newer version of TXR will not load compiled files produced by an older version. The files will have to be recompiled with the new TXR. More details are given in the section Compiled File Compatibility under the major section LISP COMPILATION. The rationale for not requiring backward compatibility support for older compiled files is that older files require the older implementation of the virtual machine which they target. In some cases the differences between the older virtual machine and new is so great that TXR would have to carry a whole separate virtual-machine implementation for the sake of the older files, which is a significant burden. When a change is introduced which is not backward compatible, TXR's option can be used to request emulation of old behavior. The option was introduced in TXR 98, and so the oldest TXR version which can be emulated is TXR 97. Side effects occur in the processing of the option. If the option is specified multiple times, the behavior is unspecified. If the environment variable exists, and its value is not en empty string, it must contain a decimal integer. Its value is taken by TXR as a request to emulate old behaviors, just like the value of the option. If the variable has incorrect contents or an out-of-range value, TXR will print an error diagnostic and exit. If both and the environment variable are supplied, the behavior is unspecified. The following version values which have a special meaning as arguments to the option, along with a description of what behaviors are affected. For each of these version values, the described behaviors are provided if is given an argument which is equal or lower. For instance selects the behaviors described below for version 105, but not those for 102.

289 Until TXR 289, the function had different semantics in the handling of the argument (now called and the argument. When the contained more indices than elements of then the replacement of elements in the main sequence would stop. No deletion of elements was performed. This behavior is restored by selecting 289 or lower compatibility. Note, however, that this breaks the ability of the macro to delete items from a sequence by The macro could do that in version 289 or older, and the behavior That behavior didn't work in version 289 or older, and is supported by the new semantics of which is capable of deleting items specified by

288 Integers and ranges callable like functions are a new feature introduced after TXR 288. The latter, callable ranges, are a breaking change; certain expressions with a range in the function position interpreted the range as a sequence. Using this compatibility value disables ranges being callable, restoring the old behaviors.

283 In TXR 283 and older versions, the parameter of the function defaults to zero, rather than

275 In TXR 275 and older versions, the FFI type operator can weaken the alignment of a type. The current behavior is that it can only increase the strictness of alignment, which mimics the type attribute found in GNU C. For instance will not have an effect, because 2 is lower than the alignment of The type operator must be used instead to specify any alignment, including lower. A compatibility value of 275 or lower restores the ability of to specify weaker alignment.

273 In TXR 273 and older versions, has a flaw, which causes it to produce an extra empty string. Because the directive in the pattern language is based on lazy strings, and depends on this function, it is affected by this issue. The extra empty string is produced because the materialized prefix of the lazy string is split on the terminator without regard for the fact that it ends in the terminator, producing an extra empty piece. For instance, if the terminator is the materialized prefix of the lazy string is and the remaining list of not-yet-materialized lazy string material is then the returned list is rather than Whenever the lazy string's is non-empty, this issue reproduces in almost all instances, because the materialized prefix, unless it is empty, is always terminated by the and so the split always produces the extra empty string. This is not a rare edge case. Compatibility values of 273 and lower restore this behavior.

272 The compatibility version value 272 restores old behaviors in the pattern language with regard to the regex and function cases of positive match variables. TXR 273, several semantic improvements took place in this area, which can break existing code. Pattern variables of the form can now invoke a vertical function against the full input, and the variable consequently to be bound to multiple lines. Previously this syntax invoked only horizontal functions or else vertical functions in a single-line horizontal mode. That behavior is restored by 272 or lower compatibility. Secondly, the function is now always invoked, whether or not the variable has a binding. The variable is then matched against the text spanned by the function to either give it a new binding or match the existing binding. The old behavior, restored by 272 or lower compatibility, is that the function is not invoked when the variable has a binding; the variable's value is instead used to match text. Lastly, a similar change took place in positive match regular expression variables of the form. Prior to 273, when a variable of this form has an existing binding, the regex is ignored, and the situation is treated as a match for the variable content. This old behavior is also restored.

265 Until TXR 265, the macro exhibited an undocumented behavior: the three-element binding expression immediately caused the form to terminate with a return value of if the form returned Neither the in the same expression, nor any subsequent binding expressions or the body of the construct, would be evaluated. Prior cleanup forms would be evaluated in reverse order, as documented. A compatibility value of 265 or less restores this behavior.

262 Selection 262 compatibility restores a wrong behavior which existed between versions 191 and 262 due to a regression. The wrong behavior is that the operator macro-expanded the replacement form, instead of associating the macro symbol with the unexpanded form. This makes a crucial difference to symbol macros which rely on expansion-time effects, such as producing a different expansion each time they are used.

258 Selecting 258 or lower compatibility causes to behave like

257 Until TXR 257, the function returned for not only lexical variables, but also for locally bound special variables, which are not lexical. The behavior is restored if 257 or older compatibility is selected.

251 Until TXR 251, the syntax was equivalent to providing little utility. A compatibility value of 251 or lower restores that behavior. The new behavior is that is equivalent to with evaluated only once, performing method dispatch.

248 Until TXR 248, the function defaulted to using equality for searching the hash table for matching values rather than the current Also, until 248, the token for denoting meta-expressions was treated with a low precedence relative to the range dot token. This led to strange results, such as parsing in a way equivalent to rather than Not is that undesirable due to the lack of symmetry, it's also inconsistent with denoting The latter is because in that case the is handled as part of the symbol token as a token, and not as a separate operator. A compatibility value of 248 or lower restores the above old behaviors of and

244 Until TXR 244, the function returned a new hash table each time it was called. The behavior is restored if 244 or older compatibility is selected.

243 Two mistakes in the pseudorandom number generator (PRNG) were discovered, affecting TXR 243 and older. Using this compatibility value, or lower, will restore the buggy behavior, allowing pseudorandom number sequences produced by those older versions can be reproduced. The PRNG is intended to be an implementation of the WELL512a PRNG described by Panneton and L'Ecuyer. The coding mistakes, however, resulted in the PRNG being an implementation of something other than WELL512a.

242 In TXR 242 and older, the instantiation of an object whose type inherits from the same supertype more than once resulted in duplicate execution of the supertype's initialization. This was a documented behavior. After 242, duplicate initialization is suppressed. For more information, see the section Duplicate Supertypes.Acompatibilityvalueof242orlowerrestores the duplicate initialization behavior.

237 Compatibility values of 237 or lower restore the destructive behavior of the and functions.

234 In TXR 234 and older versions, the exception throwing functions and did not return, regardless of the exception type. All unhandled exceptions triggered internal handling leading to unwinding and termination. The current behavior is that only exceptions lead to termination. When a non-error exception isn't intercepted by a catch or handler, the or returns normally, yielding the value If a compatibility value equal to or lower than 234 is requested, the old behavior occurs: all unhandled exceptions terminate.

231 Versions of TXR until 231 contained an undocumented feature: some library functions which are documented as having parameters that must be of string type were allowing the arguments to be symbols. For such symbolic arguments, the name of the symbol obtained from was implicitly taken as the required string value. This behavior was removed: passing symbolic arguments to library function parameters documented as strings will cause an exception to the thrown. If a compatibility value of 231 or lower is specified, however, the tolerant behavior is restored.

227 In TXR 227 and older versions, the functions and had different names, namely and respectively. If 227 or lower compatibility is selected, these functions become available under their old names in addition to their new names.

225 After TXR 225, the behavior of the operator was adjusted. Previously, a form like which contains no variable references like or generated a function similar to This was contrary to documentation, which states that should produce a variadic function which has one required argument, and which assigns that argument to the variable when invoked. The current implementation is that is equivalent to which produces the documented behavior. If 225 or lower compatibility is selected, then the old behavior of takes effect.

224 After TXR 224, the treatment of certain special structure functions has changed. Selecting 224 compatibility or lower restores that behavior. The specification given in the Special Structure Functions paragraph has always stated that special functions must be static slots, and that the behavior is unspecified if they are instance slots. The behavior of TXR 224 and earlier was that these functions worked anyway if they were instance slots; after TXR 224, they some special functions will no longer be recognized if bound to instance slots.

222 After TXR 222, the behavior of in was subject to an adjustment. Previously, if the collect body didn't bind any variables, and both required and optional variables were specified in it would still bind all of the optional ones to their default values. This was a poor behavior which violated the idea that enforces an all-or-nothing binding discipline to keep the collected lists consistent. Selecting 222 compatibility or lower restores this behavior.

215 After TXR 215, the behavior of the function changed with respect to its treatment of the variable. In cases where resolved the path by adding a suffix, was bound to the unsuffixed name, which was a documented behavior. After TXR 215, also, the behavior of the function changed. When the arguments implicate the entire string, started just returning the original string, and not making a copy. The old behavior was to always make a copy. The above old behaviors of and are restored if 215 or lower compatibility is requested. Note, however, that the restoration of the behavior in response to the compatibility option was only introduced in TXR 251. In TXR 249 and older, the compatibility value has no effect on the behavior of

202 Up to TXR 202, the function was incorrectly implemented, producing wrong results when both arguments are the same fixnum integer, or the same bignum object. The incorrect behavior is restored if 202 or earlier compatibility is requested. After 202, the behavior of the function changed with regard to symbols in the keyword package. Regardless of the flag, keywords are printed with the leading colon. Compatibility with 202 or earlier restores the behavior that when the flag is true, symbols are printed without package prefixes.

199 After TXR 199, certain global variables that had been deprecated for a long time, and no longer documented, were removed. Requesting 199 or earlier compatibility restores those variables.

190 Until TXR 190, the function neglected to perform initializations, and didn't invoke finalization on the structure object if an exception was thrown during reinitialization. Thus, contrary to documented requirements, reinitialization of a structure didn't behave like fresh construction. Also, until TXR 190, macro parameter lists implemented the requirement that a (colon keyword symbol) argument to an optional was treated as a missing argument, triggering argument-defaulting behavior. That requirement was removed; the colon symbol behaves as an ordinary value under destructuring with macro parameter lists. Moreover, until TXR 190, the symbol package didn't exist; the variable was initialized to the user package and so symbols introduced by application code were interned in the same package as the TXR Lisp library. Until TXR 190, and forms were evaluated immediately during macro expansion; in TXR 191 or later, this eager evaluation was abandoned. Unfortunately, this change introduced a regression, causing the replacement form of a to be macro-expanded at the time that form is traversed by the expander, so that the macro is associated with the expanded version of that form. This is something which had been fixed in 137. It went unnoticed until much later, after the 262 release. All the above old behaviors are restored in compatibility with version 190 or earlier. Finally, one more change after TXR 190 that is controlled by the compatibility mechanism was a critical redesign of the requirements for the behavior of the function. Version 190 compatibility causes the symbol to refer to the old implementation of

188 Until TXR 188, hash tables printed using the notation whereas hash tables simply omitted the keyword. Changes were introduced in TXR 187 which gave rise to a read/print inconsistency with printing behavior. In TXR 189, further changes were introduced to fix this inconsistency: hash tables print without any keyword indicating equality, and hash tables print as If 188 or compatibility is selected, hash tables are printed in the old way.

187 Until TXR 187, hash tables constructed by the function were based on equality by default; the keyword argument had to be specified to override this default, and the keyword didn't exist. Selecting 187 or lower compatibility restores the behavior of equality being default, and the keyword being unrecognized. This affects all functions which implicitly rely on those being : and In spite of these changes, the printed representation of hash tables continues to use the keyword to indicate hash tables based on and its absence to indicate equality. The new keyword may be used in hash literals (unless 187 compatibility is in effect, in which case it is ignored).

184 A value of 184 or lower switches to the old implementation of the and macros which was replaced starting in TXR 185. Also, this has the effect of disabling the special recognition of meta-expressions and meta-symbols in the dot position of function calls, and the macro expansion of meta-symbols in quasiliterals. This is because the old implementation implements these behaviors itself. The implication is that user code which binds custom macros to or may be affected by 184 or lower compatibility.

185 A value of 185 or lower restores the old precedence of the double dot notation for expressing ranges, relative to the referencing dot. Until TXR 185, the expression parsed as What is worse, it parsed this way even if written as Starting in TXR 186, has a lower precedence, producing the more useful and intuitive parse in other words, the range with endpoints given by and

183 A value of 183 or lower restores an inconsistent behavior in the directive and other places in the TXR pattern language where binding takes place. Prior to version 184, a string-tree match was only tried in both directions when the left-hand side of a binding (the "pattern") was a variable. For non-variable pattern terms, such as Lisp expressions or atoms, the string-tree match was tried in one direction only: a string tree arising out of the pattern could match a string atom value on the right side. A string tree is a nested list structure whose leaves are strings: a list of strings, a list of lists of strings, and so on, in any mixture. Concretely, before TXR 184, @(bind "a" ("a" "b" "c")) didn't match, but @(bind ("a" "b" "c") "a") did. However, if the variable contained then @(bind a ("a" "b" "c")) did match: an inconsistency.

177 A value of 177 or lower causes the emulation of a bug which was present in the awk macro. A range whose start and end condition matched on the same record failed to activate for that record, even though is inclusive. The behavior is incompatible with POSIX Awk.

174 A value of 174 or lower restores a previous behavior of variable substitution in the directive and in quasiliterals in both the TXR pattern language and TXR Lisp. The behavior in question is the evaluation of the element indexing or range selection modifier, exemplified by The previous behavior was that if the variable is of any type other than list, it is converted to a string (unless it already is one). The indexing then applies to the string. If it is a list then the indexing or range selection applies to the original list value, prior to conversion to text. The current behavior is that indexing and range selection is applied to the original value if that value is any sequence type which satisfies the function, otherwise to the string representation.

172 A value of 172 or lower restores a behavior of the TXR pattern matching language when matching a variable followed by a directive, such as The old behavior is that the scan for a match for the directive takes place in an environment in which a binding for has not yet been established. The new behavior is that the variable is always bound prior to the processing of the directive. During the search, it is bound to the range of text spanning between the starting position and the position being tried.

170 A value of 170 or lower disables the behavior that TXR scans standard input when no input sources are specified on the command line. Standard input must be requested explicitly using the argument. This is how it was in all versions of TXR up to 170. Some programs may behave differently because of this. Specifically, programs which do not take any arguments, and do not select an input source using the directive, or suppress the use of an input source using may now accidentally read from standard input. Until version 170, the functions and ignored negative indices in their argument (now called The new behavior is that the length of the input sequence is added to any negative index values. The resulting values are then ignored if they are still negative.

165 A value of 165 restores the following behaviors, which changed starting in 166. There was a change in Lisp evaluation support of the TXR pattern language. Specifically, Lisp argument forms were not subject to expansion prior to evaluation in these directives: and

161 Version 161 was the last version in which a bug existed in the macro. In spite of the documentation claiming that has the same syntax as the clauses of were being passed the exception symbol as the leftmost argument, followed by the exception arguments. This convention is different from clauses which do not receive the exception symbol, only the arguments. The discrepancy was corrected by making behave like as documented. Requesting compatibility with 161 or earlier restores the previous behavior of the macro.

156 After version 156, two behaviors changed in the in the macro expander for and one outright bug was fixed, and one hitherto undocumented behavior was changed and specified in the documentation at the same time. Selecting a compatibility value of 156 or less restores the previous behaviors. The bug was that single-atom case keys were undergoing evaluation. For instance would arrange for the evaluation of as a variable, rather than treating it as the symbol itself. Though the compatibility mechanism restores the behavior, applications depending on the evaluating behavior should be changed to instead use or A workaround for this bug for TXR versions 156 or older is to replace simple keys with a key list of length one, exemplified by a rewrite of the foregoing expression to Here is not evaluated. The undocumented behavior was that a matching clause which has no forms to be evaluated was producing a result value of For example previously yielded but now yields and this behavior is documented.

155 After version 155, the and functions changed semantics. Previously, these functions exhibited the flaw that under some conditions they extracted an empty token immediately following a nonempty token. This behavior was working as designed and documented, but the design was flawed, creating a major difficulty in simple tokenizing tasks when tokens may be empty strings. Requesting compatibility with version 155 or earlier restores the behavior.

154 After version 154, changes were introduced in the semantics of struct literals. Previously, the syntax denoted the construction of an instance of with as the constructor parameters, similarly to The new behavior is that is constructed using no parameters, as if by and then the slot values are assigned. This means that the values specified in the literal override any manipulations of those slots by the type's user-defined handlers. Also, after 154, methods are expected to take three arguments and are invoked for both pretty printing and regular machine-readable printing. Until 154, a struct's methods was called only when that struct was being pretty-printed, and only with two arguments; ordinary printing side-stepped the method and rendered the standard syntax featuring all instance slots.

151 After version 151, changes were implemented to the way static slots work in TXR Lisp structs. Selecting compatibility with 151 restores most of the behaviors. Until 151, each structure type had its own instance of static slots whether they were newly defined or inherited. Under the new scheme, a derived struct shares one instance of each inherited static slot with its base type. Under the old scheme, a struct inherits the static initialization functions of its bases (the argument passed in These are invoked because they are relied upon by the macro to perform the initializations of all the inherited static slots. Under the new scheme, the static initialization functions are not inherited. Only the type's own is invoked to initialize its newly defined static slots that it doesn't share with the parent. The inherited static slots simply preserve their current values they have in the base type; their values are untouched by the introduction of a derived type. The also changed semantics after version 151. The old behavior was problematic because it affected all static slots throughout the inheritance hierarchy matching the name passed in by argument. Since this function is the basis for redefining methods, its behavior broke the semantics of overriding. Selecting 151 compatibility only restores the behavior of this function and macros based on it like in the situation when it introduces a new static slot into one or more struct types, in compatibility mode it introduces the slot separately into each type without sharing, and it recurses over the entire type hierarchy, storing into all static slots which match

150 Until version 150, the function behaved in a different way from what was documented. Rather than returning the length of the match, it returned the index one past the last matching character. In the case when the starting position is zero, these values coincide; they are different if the match begins at some position inside the string. Compatibility with 150 restores the behavior. The function was also affected by this issue; however, since it returned nonsense result not corresponding to the matching text, it was repaired without backward compatibility. Also affected by version 150 compatibility are the and functions. These functions worked as documented; however, their specification changes after version 150 to a semantics which is more useful and less surprising to the programmer.

148 Up until version 148, the handlers specified in a were executed in derived-to-base order, opposite to the order of execution of handlers. Though described in terms of syntax and concepts, this is actually a change in how treats its argument. Specifying 148 or earlier compatibility provides this old behavior. Also, until version 148, the function stripped leading and trailing whitespace from a string consisting of not only spaces, tabs and newlines, but also carriage returns, vertical tabs and form feeds.

145 In versions 144 and 145, TXR opened files in text mode on Cygwin, enabling conversion between CR-LF line endings and abstract newline characters. This behavior change was retracted, so that files on Cygwin are opened without specifying text mode, causing the streams to be effectively binary. The intended "Windows native" behavior of streams being text mode is instead provided in the Windows version of TXR by the Cygnal library.

143 Until version 143, the variable didn't include the trailing slash. The function semantics changed after version 143 to be more compatible with ANSI Common Lisp. Until 143, that function removed only the global binding, leaving the dynamic rebinding of a variable intact. The operator neglected to remove the symbol's special variable mark, if the symbol was previously defined as a special variable. Also, until version 143 many more places in the TXR pattern language used bind expressions rather than Lisp expressions. The compatibility option restores these behaviors.

142 Until version 142, the TXR pattern language supported a prefix convention on data sources. Data sources beginning with the character were treated as system command pipes, and data sources beginning with indicated that a directory is to be scanned. This convention was recognized both for command-line arguments, the arguments of the directive, and of the directive, whether or not the argument was a literal or a computed value. This feature was dropped from the language after version 142. Also, until version 142, the directive recognized the name as denoting standard input, and recognized it as standard output. These behaviors were also removed; versions after 142 recognize this convention only when it appears as a command-line argument. Lastly, until version 142, the directive evaluated the argument as an expression of the TXR pattern language, requiring to be used to denote a Lisp expression. This is no longer required. All these old behaviors are provided if compatibility with 142 or earlier is requested.

139 After TXR 139, changes were implemented in the area of pseudorandom number generation. Compatibility with 139 brings back the previous seeding algorithm used by allowing the old pseudorandom sequences to be reproduced. This is only the case if the default value of 8 is used for the argument of that function (which didn't exist in 139 or earlier versions).

138 After TXR 138, the variable name lookup rules in the TXR pattern language changed for greater utility and consistency. Compatibility with 138 or later restores the previous rules under which most accesses to a TXR Lisp variable from TXR Lisp require the prefix denoting Lisp evaluation, but some do not.

137 Compatibility with TXR 137 restores the behavior of not expanding symbol macros in the dot position of a function call form. For instance if is a symbol macro, in this compatibility mode it is not recognized in a form like This preserves the behavior of code which depends on in such a form to refer to a variable that is being otherwise shadowed by the symbol macro. TXR 137 compatibility also restores a particular behavior of the global and local macro defining operators and in compatibility mode, these operators macro-expand the replacement forms of symbol macros at expansion time, and then bind the resulting expanded forms to their respective macro symbols. The forms are then potentially expanded again when the symbol macros are substituted. This wrong behavior was never implied by the documentation. The macro is also affected by this, because it is implemented in terms of Lastly, TXR 137 compatibility mode also restores another behavior of the dot position in function call forms: if the dot position of a function call form produces a sequence that is not a list, that sequence is converted to a list so that (list . "abc") produces After 137, no such treatment is applied to the value and the same form now yields

136 A request for compatibility with TXR 136 or earlier restores the old behavior of the directive, which in used to be a syntactic sugar for a directive with at the top of each block. Though semantically well-defined and working as documented, the behavior was confusing, since failed matching caused potential evaluation of multiple clauses, whereas programmers expect an if/elif/else ladder to select exactly one clause.

128 Compatibility with TXR 128 or earlier brings back the behavior that expressions in quasiliterals are evaluated according to TXR evaluation rules for quasiliterals which occur in the TXR pattern language. Similarly, expressions in blocks are treated TXR pattern language expressions.

127 In versions of TXR until 127, the functions and behaved similarly to their Common Lisp counterparts. See the Dialect Notes under these functions.

124 In TXR 124 and earlier versions, the directive didn't evaluate the argument as a Lisp expression, but as a TXR pattern language expression. Lisp expressions thus had to be delimited by The current behavior is that the argument is treated as Lisp. If the compatibility option is set to 124 or lower, the old behavior is restored. However, even without the presence of the compatibility option, if the argument is a meta-expression or meta-symbol (denotes by the prefix in front of a compound expression or symbol, respectively) it is also treated in the old way. This latter behavior is obsolescent and will eventually disappear, and the compatibility option will be the only way to get the old behavior.

123 In TXR 123 and earlier, the variable initialization forms of a or loop were evaluated outside of the scope of the implicit block. They are now inside the block. The compatibility option will restore the old behavior.

121 In TXR 121 and earlier versions, TXR Lisp expressions evaluated in the pattern language were placed in a lexical environment in which the pattern variables were visible as lexical variables. The meant that these variables could be directly captured in lexical closures. On the other hand, it meant that a Lisp function defined in a block could not access a variable established by a later It doesn't make sense for dynamically captured variables to be lexical, so the rule was changed. The backward compatibility switch will enable the old scoping behavior. Capturing the values of pattern variables in closures is possible indirectly under the new rule: simply bind new lexical variables with their values.

118 The function's name changed to after 118. The compatibility option causes to be defined also.

117 The arguments of the acquired changed after version 117. 117 compatibility brings back the old interface.

114 TXR until version 114 reported parse errors in this format:
      ./txr: (file.txr:123): syntax error The new format omits the program name prefix and parentheses. Also, the function returned an integer, obtained from the return value of the underlying C function, rather than converting that value to a Boolean. The old behavior was not documented, and 114 compatibility restores it. Lastly, prior to 115, random state objects were of type (the same symbol as the special variable name) rather than of type This is a bug whose behavior is simulated by 114 compatibility.

113 Version 113 is the last version in which the and functions returned a property list rather than a structure. Requesting 113 compatibility restores the behavior of returning a property list. However, the filesystem testing functions like will not work, because they rely on these functions returning a structure.

109 The optional trailing semicolon on hex and octal codes in the TXR pattern language was introduced in 110. The feature is disabled with 109 or lower compatibility, so that encodes rather than the current behavior of encoding Also, in 109 and earlier, newlines were allowed in word list literals and word list quasiliterals. They were treated as a word-separating space. A backslash-escaped newline, and all whitespace around it, was deleted just like in ordinary literals, and did not separate words. The old behavior is emulated.

107 Up through TXR 107, by accident, there was a function called as well as an operator by the same name. The function was renamed to Version 107 compatibility or earlier provides the function under the original name also. Also, up until this version, TXR allowed functions and macros to be defined with the same names as built-in operators, and macros. Newer versions reject this as an error. Requesting compatibility to 107 or earlier suppresses the rejection, though without introducing any requirement that redefinition will work as expected.

105 Provides the behavior that the function automatically marks a stream open on a TTY devices as a real-time stream (subject to the availability of the POSIX function). Also allows unrecognized backslash escape sequences in regular expression syntax to simply denote the escaped character literally, as was historically the case prior to TXR 106, so that for instance denotes As of TXR 106, these are diagnosed as errors.

102 Up to TXR 102, the function did not close the stream. This old behavior is emulated.

101 Up to TXR 101, the function incorrectly returned when converting the empty list to string type. This affects numerous generic sequence functions, causing their result to be instead of an empty string.

100 Up to TXR 100, the function had an undocumented behavior. When the argument was an empty string, it split the string into individual characters as if by calling This behavior changed to the currently documented behavior starting in TXR 101. Also, the arguments of the function, which introduced in TXR 91, were reversed starting in TXR 101.

99  Up to TXR 99, the substitution of TXR Lisp expressions in directives and in the quasistrings of the pattern language exhibited the buggy behavior that if the TXR Lisp expression produced a list, the list was rendered as a parenthesized representation, or the text in the empty list case. Moreover, in the case, the value of TXR Lisp expressions was not subject to filtering. Starting with TXR 100, these issues are fixed, making the behavior consistent with that of TXR Lisp quasiliterals.

97  Up to TXR 97, the error exception symbols such as were named with underscores, as in These error symbols existed: and The variable gives the version of the TXR executable. Programs can express conditional variable based on detecting the version. The variable gives the version of the installed library of TXR code accompanying the executable. It is expected that these two variables have an identical value. Any discrepancy in their value indicates an installation whose library or TXR executable were upgraded independently. Should such a situation arise in any system and cause a problem, TXR programs can be defensively coded against it with the help of these variables. Some features of the TXR library are built into the executable, whereas others are in the library directory. This aspect of library symbols isn't specified in this manual; knowing which of these two variables is relevant to a library feature requires familiarity with the implementation. Users familiar with regular expressions may not be familiar with the complement and intersection operators, which are often absent from text processing tools that support regular expressions. The following remarks are offered in the hope that they may be of some use. Regexp intersection is not essential; it may be obtained from complement and union as follows, since De Morgan's law applies to regular-expression algebra: = (The complement of the union of the complements of R1 and R2 constitutes the intersection.) This law works because the regular expression operators denote set operations in a straightforward way. A regular expression denotes a set of strings (a potentially infinite one) in a condensed way. The union of two regular expressions denotes the union of the set of texts denoted by and that denoted by Similarly denotes a set intersection, and denotes a set complement. Thus algebraic laws that apply to set operations apply to regular expressions. It's useful to keep in mind this relationship between regular expressions and sets in understanding intersection and complement. Given a finite set of strings, like the set { "abc", "def" } which corresponds to the regular expression the complement is the set which contains an infinite number of strings: it consists of all possible strings except and It includes the empty string, all strings of length 1, all strings of length 2, all strings of length 3 other than and all strings of length 4, etc. This means that a "harmless looking" expression like can actually match arbitrarily long inputs. How about matching only three-character-long strings other than or To express this, regex intersection can be used: these strings are the intersection of the set of all three-character strings, and the set of all strings which are not or The straightforward set-based reasoning leads us to this: This idiom is also called set difference, sometimes notated with a minus sign: (which is not supported in TXR regular-expression syntax). Elements which are in the set but not are those elements which are in the intersection of with the complement of This is similar to the arithmetic rule subtraction is equivalent to addition of the additive inverse. Set difference is a useful tool: it enables us to write a positive match which captures a more general set than what is intended (but one whose regular expression is far simpler than a positive match for the exact set we want), then we can intersect this over-generalized set with the complemented set of another regular expression which matches the particulars that we wish excluded. It turns out that regular expressions which do not make use of the complement or intersection operators are just as powerful as expressions that do. That is to say, with or without these operators, regular expressions can match the same sets of strings (all regular languages). This means that for a given regular expression which uses intersection and complement, it is possible to find a regular expression which doesn't use these operators, yet matches the same set of strings. But, though they exist, such equivalent regular expressions are often much more complicated, which makes them difficult to design. Such expressions do not necessarily express what it is they match; they merely capture the equivalent set. They perform a job, without making it obvious what it is they do. The use of complement and intersection leads to natural ways of expressing many kinds of matching sets, which not only demonstrate the power to carry out an operation, but also easily express the concept. For instance, using complement, we can write a straightforward regular expression which matches C language comments. A C language comment is the digraph followed by any string which does not contain the closing sequence followed by that closing sequence. Examples of valid comments are or But C comments do not nest (cannot contain comments), so that actually consists of the comment which is followed by the trailing junk Our simple characterization of the interior part of a C comment as a string which does not contain the terminating digraph makes use of the complement, and can be expressed using the complemented regular expression like this: That is to say, strings which contain are matched by the expression zero or more arbitrary characters, followed by followed by zero or more arbitrary characters. Therefore, the complement of this expression matches all other strings: those which do not contain These strings make up the inside of a C comment between the and The equivalent simple regex is quite a bit more complicated. Without complement, we must somehow write a positive match for all strings such that we avoid matching Obviously, sequences of characters other than are included: Occurrences of are also allowed, but only if followed by something other than a slash, so let's include this via union:
      ([^*]|[*][^/])*. Alas, we already have a bug in this expression. The subexpression can match since a is not a If the next character in the input is we missed a comment close. To fix the problem we revise to this:
      ([^*]|[*][^*/])* (The interior of a C language comment is any mixture of zero or more non-asterisks, or digraphs consisting of an asterisk followed by something other than a slash or another asterisk). Oops, now we have a problem again. What if two asterisks occur in a comment? They are not matched by and they are not matched by Actually, our regex must not simply match asterisk-non-asterisk digraphs, but rather sequences of one or more asterisks followed by a non-asterisk:
      ([^*]|[*]*[^*/])* This is still not right, because, for instance, it fails to match the interior of a comment which is terminated by asterisks, including the simple test cases where the comment interior is nothing but asterisks. We have no provision in our expression for this case; the expression requires all runs of asterisks to be followed by something which is not a slash or asterisk. The way to fix this is to add on a subexpression which optionally matches a run of zero or more interior asterisks before the comment close:
      ([^*]|[*]*[^*/])*[*]* Thus the semi-final regular expression is
      [/][*]([^*]|[*]*[^*/])*[*]*[*][/] (Interpretation: a C comment is an interior string enclosed in where this interior part consists of a mixture of non-asterisk characters, as well as runs of asterisk characters which are terminated by a character other than a slash, except for possibly one rightmost run of asterisks which extends to the end of the interior, touching the comment close. Phew!) One final simplification is possible: the tail part can be reduced to such that the final run of asterisks is regarded as part of an extended comment terminator which consists of one or more asterisks followed by a slash. The regular expression works, but it's cryptic; to someone who has not developed it, it isn't obvious what it is intended to match. Working out complemented matching without complement support from the language is not impossible, but it may be difficult and error-prone, possibly requiring multiple iterations of trial-and-error development involving numerous test cases, resulting in an expression that doesn't have a straightforward relationship to the original idea. The non-greedy operator is actually defined in terms of a set difference, which is in turn based on intersection and complement. The uninteresting case where the right operand is empty reduces to if there is no trailing context, the non-greedy operator matches as far as possible, possibly to the end of the input, exactly like the greedy operator. The interesting case is defined as a "syntactic sugar" which expands to the expression which means: match the longest string which is matched by but which does not contain a non-empty match for then, match This is a useful and expressive notation. With it, we can write the regular expression for matching C language comments simply like this: (match the opening sequence then match a sequence of zero or more characters non-greedily, and then the closing sequence With the non-greedy operator, we don't have to think about the interior of the comment as set of strings which excludes Though the non-greedy operator appears expressive, its apparent simplicity may be deceptive. It looks as if it works "magically" by itself; "somehow" this part "knows" only to consume enough characters so that it doesn't swallow an occurrence of the trailing context. Care must be taken that the trailing context passed to the operator really is the correct text that should be excluded by the non-greedy match. For instance, take the expression If you intend the trailing context to be merely you must be careful to write Otherwise, the trailing context is and this means that the match will consume the longest string that does not contain when in fact what was intended was to consume the longest string that does not contain The change in behavior of the operator upon modifying the trailing context is not as intuitive as that of the * operator, because the trailing context is deeply involved in its logic. On a related note, for single-character trailing contexts, it may be a good idea to use a complemented character class instead. That is to say, rather than consider The set of strings which don't contain the character a is adequately expressed by