gambit 中缺少方案格式功能

问题描述 投票:0回答:2

我尝试运行一个先前使用 guile 运行的策略方案脚本。我注意到策略失败了,因为它缺少“格式”功能。

格式不是方案的一部分吗?

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

相反,我将我的策略脚本修改为以下内容。

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

我在这里缺少什么?谢谢。

scheme guile gambit
2个回答
4
投票

在Gambit中你可以使用标准的R7RS库,并且需要导入包含格式化函数的SRFI-28。

(import (srfi 28))

但是 SRFI-28 定义的方案格式函数没有像 Common Lips 那样打印到标准输出的

#t
参数。第一个参数始终是输出字符串模式:

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

0
投票

SRFI 28 相当有限。

您可以自己编写一个宏,它提供类似的功能:

(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))))

这为您提供了一个

printf
,它可以理解
~a
~s
~%

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

或更短:

(let ((i 42) (example sqrt))
  (printf "example(" i ")=<" (example i) ">\n"))
© www.soinside.com 2019 - 2024. All rights reserved.