4.4  Macros and procedures implemented in the interpreter

The interpreter contains the definitions of the library units extras, srfi-1, srfi-4, srfi-18, srfi-37, format, regex, script-utils and syntax-case. If available, the units posix, chicken-setup and tcp are also included.

Additional macros and procedures available in the interpreter are:

[syntax] (advise NAME MODE PROC)
Modifies the behavior of the procedures named NAME, according to MODE:

before
Call the procedure PROC before every invocation of NAME, with the same arguments.

after
Call the procedure PROC with the result value(s) of NAME.

around
Call the procedure PROC with the arguments passed to NAME. Additionally the (original) value of NAME is passed as the first argument to PROC.

Only the PROC argument is evaluated. Note that multiple pieces of advice on the same procedure are allowed.

>>> (define (fac n)
      (if (zero? n) 1 (* n (fac (sub1 n)))))
>>> (define count 0)
>>> (advise fac before (lambda _ (set! count (add1 count))))
>>> (fac 10)                                                 ==> 3628800
>>> count                                                    ==> 11
>>> (advise fac around
      (let ((i 0))
        (define (indent)
          (do ((i i (sub1 i)))
              ((zero? i))
            (write-char #\space)))
      (lambda (f n)
        (indent)
        (print "fac: " n)
        (set! i (add1 i))
        (let ((x (f n)))
          (set! i (sub1 i))
          (indent)
          (print "-> " x)
          x))))
>>> (fac 3)
fac: 3
 fac: 2
  fac: 1
   fac: 0
   -> 1
  -> 1
 -> 2
-> 6                                                         ==> 6
>>> count                   ==> 15
>>> (set! count 0)
>>> (unadvise fac)
>>> (fac 10)                                                 ==> 3628800
>>> count           ==> 0

[syntax] (unadvise NAME ...)
Removes all pieces of advice from the procedures NAME ... and restores their original behavior.

[syntax] (trace NAME ...)
Switches tracing on for the procedures with the given names.

>>> (fac 10)                       ==> 3628800
>>> (trace fac)
>>> (fac 3)
|(fac 3)
| (fac 2)
|  (fac 1)
|   (fac 0)
|   fac -> 1 
|  fac -> 1 
| fac -> 2 
|fac -> 6                          ==> 6
>>> (untrace fac)
>>> (fac 3)                        ==> 6

[syntax] (untrace NAME ...)
Switches tracing of the given procedures off.

[procedure] ($ [INDEX])
Returns entry number INDEX in the history list.

[procedure] (& [INDEX])
Returns result(s) of entry number INDEX in the history list.