scheme format function missing from gambit

476 views Asked by At

I attempted to run a gambit scheme script that was previously run with guile. I noticed that gambit fails because it is missing the "format" function.

Is format not part of scheme?

(format #t "example(~a)=<~a>\n" i (example i))

Instead I modified my gambit script to the following.

(display (string-append "example(" (number->string i) ")=<" (number->string (example i)) ">\n"))

What am I missing here? Thanks.

2

There are 2 answers

0
ceving On

SRFI 28 is quite limited.

You can write yourself a macro, which provides a similar functionality:

(define-syntax printf
  (syntax-rules (~s ~a ~%)
    ((_ ~s arg . rest) (begin (write   arg) (printf . rest)))
    ((_ ~a arg . rest) (begin (display arg) (printf . rest)))
    ((_ ~%     . rest) (begin (newline)     (printf . rest)))
    ((_ arg    . rest) (printf ~a arg . rest))
    ((_) (void))))

This gives you a printf which understands ~a, ~s and ~%:

(let ((i 42) (example sqrt))
  (printf "example(" ~a i ")=<" ~a (example i) ">\n"))

Or even shorter:

(let ((i 42) (example sqrt))
  (printf "example(" i ")=<" (example i) ">\n"))

~~ is not necessary, because the format directives are stand-alone symbols instead of embedded strings. SRFI 28 gives you nothing more.

0
jcubic On

In Gambit you can use standard R7RS libraries and you need to import SRFI-28 that contain format function.

(import (srfi 28))

But Scheme format function as defined by SRFI-28 don't have #t argument that print to stdout like Common Lips have. First argument is always output string pattern:

(display (format "example(~a)=<~a>\n" i (example i)))
(newline)