## 5.7 Unit: library

This unit contains basic Scheme definitions. This unit is used by default, unless the program is compiled with the `-explicit-use` option.

### 5.7.1 Arithmetic

[procedure] (add1 N)
[procedure] (sub1 N)
  Adds/subtracts 1 from `N`.

[procedure] (bitwise-and N1 ...)
[procedure] (bitwise-ior N1 ...)
[procedure] (bitwise-xor N1 ...)
[procedure] (bitwise-not N)
[procedure] (arithmetic-shift N1 N2)
  Binary fixnum operations. `arithmetic-shift` shifts the argument `N1` by `N2` bits to the left. If `N2` is negative, than `N1` is shifted to the right.

[procedure] (fixnum? X)
  Returns `#t` if `X` is a fixnum, or `#f` otherwise.

[procedure] (fx+ N1 N2)
[procedure] (fx- N1 N2)
[procedure] (fx* N1 N2)
[procedure] (fx/ N1 N2)
[procedure] (fxmod N1 N2)
[procedure] (fxneg N)
[procedure] (fxmin N1 N2)
[procedure] (fxmax N1 N2)
[procedure] (fx= N1 N2)
[procedure] (fx> N1 N2)
[procedure] (fx< N1 N2)
[procedure] (fx>= N1 N2)
[procedure] (fx<= N1 N2)
  Arithmetic fixnum operations. These procedures do not check their arguments, so non-fixnum parameters will result in incorrect results. `fxneg` negates its argument.

[procedure] (signum N)
  Returns `1` if `N` is positive, `-1` if `N` is negative or `0` if `N` is zero.

[procedure] (vector-resize VECTOR N [INIT])
  Creates and returns a new vector with the contents of `VECTOR` and length `N`. If `N` is greater than the original length of `VECTOR`, then all additional items are initialized to `INIT`. If `INIT` is not specified, the contents are initialized to some unspecified value.

### 5.7.2 Input/Output

[procedure] (current-error-port [PORT])
  Returns default error output port. If `PORT` is given, then that port is selected as the new current error output port.

[procedure] (end-of-file)
  Returns the end-of-file object.

[procedure] (flush-output [PORT])
  Write buffered output to the given output-port. `PORT` defaults to the value of `(current-output-port)`.

[procedure] (port-name PORT)
  Fetch filename from `PORT`. This returns the filename that was used to open this file. Returns a special tag string, enclosed into parentheses for non-file ports.

[procedure] (port-position PORT)
  Returns the current position of `PORT` as two values: row and column number. If the port does not support such an operation an error is signaled. This procedure is currently only available for input ports.

[procedure] (set-port-name! PORT STRING)
  Sets the name of `PORT` to `STRING`.

### 5.7.3 Files

[procedure] (delete-file STRING)
  Deletes the file with the pathname `STRING`. If the file does not exist, an error is signaled.

[procedure] (file-exists? STRING)
  Returns `#t` if a file with the given pathname exists, or `#f` otherwise.

[variable] pathname-directory-separator
  Contains the directory-separator character for pathnames on this platform.

[variable] pathname-extension-separator
  Contains the extension-separator character for pathnames on this platform.

[procedure] (rename-file OLD NEW)
  Renames the file or directory with the pathname `OLD` to `NEW`. If the operation does not succeed, an error is signaled.

### 5.7.4 String ports

[procedure] (get-output-string PORT)
  Returns accumulated output of a port created with `(open-output-string)`.

[procedure] (open-input-string STRING)
  Returns a port for reading from `STRING`.

[procedure] (open-output-string)
  Returns a port for accumulating output in a string.

### 5.7.5 Feature identifiers

[procedure] (features)
  Returns a list of all registered features that will be accepted as valid feature-identifiers by `cond-expand`.

[procedure] (register-feature! FEATURE ...)
  Register one or more features that will be accepted as valid feature-identifiers by `cond-expand`. `FEATURE ...` may be a keyword, string or symbol.

[procedure] (unregister-feature! FEATURE ...)
  Unregisters the specified feature-identifiers. `FEATURE ...` may be a keyword, string or symbol.

### 5.7.6 Keywords

Keywords are special symbols prefixed with `#:` that evaluate to themselves. Procedures can use keywords to accept optional named parameters in addition to normal required parameters. Assignment to and bindings of keyword symbols is not allowed. The parameter `keyword-style` and the compiler/interpreter option `-keyword-style` can be used to allow an additional keyword syntax, either compatible to Common LISP, or to DSSSL.

[procedure] (get-keyword KEYWORD ARGLIST [THUNK])
  Returns the argument from `ARGLIST` specified under the keyword `KEYWORD`. If the keyword is not found, then the zero-argument procedure `THUNK` is invoked and the result value is returned. If `THUNK` is not given, `#f` is returned.

      (define (increase x . args)
        (+ x (get-keyword #:amount args (lambda () 1))) )
      (increase 123)                                      ==> 124
      (increase 123 #:amount 10)                          ==> 133

  Note: the `KEYWORD` may actually be any kind of object.

[procedure] (keyword? X)
  Returns `#t` if `X` is a keyword symbol, or `#f` otherwise.

[procedure] (keyword`->`string KEYWORD)
  Transforms `KEYWORD` into a string.

[procedure] (string`->`keyword STRING)
  Returns a keyword with the name `STRING`.

### 5.7.7 Exceptions

Chicken implements the (currently withdrawn) SRFI-12 exception system. For more information, see the SRFI-12 Document 12

[syntax] (condition-case EXPRESSION CLAUSE ...)
  Evaluates `EXPRESSION` and handles any exceptions that are covered by `CLAUSE ...`, where `CLAUSE` should be of the following form:

      CLAUSE = ([VARIABLE] (KIND ...) BODY ...)

  If provided, `VARIABLE` will be bound to the signalled exception object. `BODY ...` is executed when the exception is a property- or composite condition with the kinds given `KIND ...` (unevaluated). If no clause applies, the exception is re-signalled in the same dynamic context as the `condition-case` form.

      (define (check thunk)
        (condition-case (thunk)
          [(exn file) (print "file error")]
          [(exn) (print "other error")]
          [var () (print "something else")] ) )

      (check (lambda () (open-input-file "")))   ; -> "file error"
      (check (lambda () some-unbound-variable))  ; -> "othererror"
      (check (lambda () (signal 99)))            ; -> "something else"

      (condition-case some-unbound-variable
        [(exn file) (print "ignored)] )      ; -> signals error

All error-conditions signalled by the system are of kind `exn`. The following composite conditions are additionally defined:

(exn type)
  Signalled on type-mismatch errors, for example when an argument of the wrong type is passed to a builtin procedure.

(exn arithmetic)
  Signalled on arithmetic errors, like division by zero.

(exn i/o)
  Signalled on input/output errors.

(exn i/o file)
  Signalled on file-related errors.

(exn i/o net)
  Signalled on network errors.

(exn runtime)
  Signalled on low-level runtime-system error-situations, like running out of memory.

(exn bounds)
  Signalled on errors caused by accessing non-existent elements of a collection.

Notes:

- All error-exceptions (of the kind `exn`) are non-continuable.

- Error-exceptions of the `exn` kind have additional `arguments` and `location` properties that contain the arguments passed to the error-handler and the name of the procedure where the error occurred (if available).

- When the `posix` unit is available and used, then a user-interrupt (`signal/int`) signals an exception of the kind `user-interrupt`.

### 5.7.8 Miscellaneous

[procedure] (argv)
  Return a list of all supplied command-line arguments. The first item in the list is a string containing the name of the executing program. The other items are the arguments passed to the application. This list is freshly created on every invocation of `(argv)`. It depends on the host-shell whether arguments are expanded ('globbed') or not.

[procedure] (build-platform)
  Returns a symbol specifying the toolset which has been used for building the executing system, which is one of the following:

      djgpp
      cygwin
      msvc
      mingw32
      gnu
      metrowerks
      unknown

[procedure] (call/cc PROCEDURE)
  An alias for `call-with-current-continuation`.

[procedure] (char-name SYMBOL-OR-CHAR [CHAR])
  This procedure can be used to inquire about character names or to define new ones. With a single argument the behavior is as follows: If `SYMBOL-OR-CHAR` is a symbol, then `char-name` returns the character with this name, or `#f` if no character is defined under this name. If `SYMBOL-OR-CHAR` is a character, then the name of the character is returned as a symbol, or `#f` if the character has no associated name.

  If the optional argument `CHAR` is provided, then `SYMBOL-OR-CHAR` should be a symbol that will be the new name of the given character. If multiple names designate the same character, then the `write` will use the character name that was defined last.

      (char-name 'space)                  ==> #\space(char-name #\space)                 ==> space
      (char-name 'bell)                   ==> #f
      (char-name (integer->char 7))       ==> #f
      (char-name 'bell (integer->char 7))
      (char-name 'bell)                   ==> #\bell(char->integer (char-name 'bell))   ==> 7

[procedure] (chicken-version)
  Returns a string containing the version number of the Chicken runtime system.

[procedure] (cpu-time)
  Returns the used CPU time of the current process in milliseconds as two values: the time spent in user code, and the time spent in system code. On platforms where user and system time can not be differentiated, system time will be always be 0.

[procedure] (current-milliseconds)
  Returns the number of milliseconds since process- or machine startup.

[procedure] (current-seconds)
  Returns the number of seconds since midnight, Jan. 1, 1970.

[procedure] (enable-interrupts)
[procedure] (disable-interrupts)
  Enables/disables processing of timer-interrupts and interrupts caused by signals.

      (disable-interrupts)
      (disable-interrupts)
      (enable-interrupts)
      ; <interrupts still disabled - call enable-interrupts once more>

[procedure] (andmap PROC LIST1 ...)
  Repeatedly calls `PROC` with arguments taken from `LIST1 ...`. If any invocation should return `#f`, the result of `andmap` is `#f`. If all invocations return a true result, then the result of `andmap` is `#t`.

[procedure] (errno)
  Returns the error code of the last system call.

[procedure] (error STRING EXP ...)
  Prints error message, writes all extra arguments to the value of `(current-error-port)` and invokes the current value of `(error-handler)`. This conforms to SRFI-23 13.

[procedure] (exit [CODE])
  Exit the running process and return exit-code, which defaults to 0 (Invokes `exit-handler`).

[procedure] (gc [FLAG])
  Invokes a garbage-collection and returns the number of free bytes in the heap. The flag specifies whether a minor (`#f`) or major (`#t`) GC is to be triggered. If no argument is given, `#t` is assumed. When the argument is `#t`, all pending finalizers are executed.

[procedure] (gensym [STRING-OR-SYMBOL])
  Returns a newly created uninterned symbol. If an argument is provided, the new symbol is prefixed with that argument.

[procedure] (getenv STRING)
  Returns the value of the environment variable `STRING` or `#f` if that variable is not defined.

[procedure] (machine-type)
  Returns a symbol specifying the processor on which this process is currently running, which is one of the following:

      alpha
      mips
      hppa
      ultrasparc
      sparc
      ppc
      x86
      unknown

[procedure] (make-parameter VALUE [GUARD])
  Returns a procedure that accepts zero or one argument. Invoking the procedure with zero arguments returns `VALUE`. Invoking the procedure with one argument changes its value to the value of that argument (subsequent invocations with zero parameters return the new value). `GUARD` should be a procedure of a single argument. Any new values of the parameter (even the initial value) are passed to this procedure. The guard procedure should check the value and/or convert it to an appropriate form.

[procedure] (ormap PROC LIST1 ...)
  Repeatedly calls `PROC` with arguments taken from `LIST1 ...`. If any invocation should return a value different from `#f`, then this value is returned as the result of `ormap`. If all invocations return #f, then the result of `ormap` is `#f`.

[procedure] (port? X)
  Returns `#t` if `X` is a port object or `#f` otherwise.

[procedure] (print EXP1 ...)
  Outputs the arguments `EXP1, ...` using `display` and writes a newline character to the port that is the value of `(current-output-port)`.

[procedure] (print* EXP1 ...)
  Similar to `print`, but does not output a terminating newline character.

[procedure] (print-error-message EXN [PORT])
  Prints an appropriate error message to `PORT` (which defaults to the value of `(current-error-port)` for the object `EXN`. `EXN` may be a condition, a string or any other object.

[procedure] (reset)
  Reset program (Invokes `reset-handler`).

[procedure] (set-finalizer! X PROC)
  Registers a procedure of one argument `PROC`, that will be called as soon as the non-immediate data object `X` is about to be garbage-collected (with that object as its argument). Note that the finalizer will not be called when interrupts are disabled.

[procedure] (set-gc-report! FLAG)
  Print statistics after every GC, depending on `FLAG`. A value of `#t` shows statistics after every major GC. A true value different from `#t` shows statistics after every minor GC. `#f` switches statistics off.

[procedure] (software-type)
  Returns a symbol specifying the operating system on which this process is currently running, which is one of the following:

      msdos
      windows
      unix
      macos
      unknown

[procedure] (software-version)
  Returns a symbol specifying the operating system version on which this process is currently running, which is one of the following:

      linux
      freebsd
      netbsd
      openbsd
      macosx
      hpux
      solaris
      sunos
      unknown

[procedure] (string`->`uninterned-symbol STRING)
  Returns a newly created, unique symbol with the name `STRING`.

[procedure] (system STRING)
  Execute shell command. The functionality offered by this procedure depends on the capabilities of the host shell.

[procedure] (vector-copy! VECTOR1 VECTOR2 [COUNT])
  Copies contents of `VECTOR1` into `VECTOR2`. If the argument `COUNT` is given, it specifies the maximal number of elements to be copied. If not given, the minimum of the lengths of the argument vectors is copied.

[procedure] (void)
  Returns an unspecified value.

12 http://srfi.schemers.org/srfi-12/srfi-12.html

13 http://srfi.schemers.org/srfi-23/srfi-23.html