Scheme 如何返回包含过程的已计算参数的字符串?
使用Racket,我想设计一个过程,它将一个二进制操作和两个任意类型的参数a&b作为输入,对参数进行二进制操作求值,并返回一个描述求值结果的字符串。例如,如果我的过程被称为foo,那么Scheme 如何返回包含过程的已计算参数的字符串?,scheme,racket,Scheme,Racket,使用Racket,我想设计一个过程,它将一个二进制操作和两个任意类型的参数a&b作为输入,对参数进行二进制操作求值,并返回一个描述求值结果的字符串。例如,如果我的过程被称为foo,那么 (foo + 1 2) (foo cons 'bar empty) 会回来吗 The + of 1 and 2 is: 3 The cons of 'bar and empty is: (bar) 我的代码看起来像 (define (foo op a b) (let ((result (op a b)
(foo + 1 2)
(foo cons 'bar empty)
会回来吗
The + of 1 and 2 is: 3
The cons of 'bar and empty is: (bar)
我的代码看起来像
(define (foo op a b)
(let ((result (op a b)))
(display
(string-append
"The "
(arg->string op)
" of "
(arg->string a)
" and "
(arg->string b)
" is: "
(arg->string result)
))))
是否可以构造一些操作arg->string来执行我想要的操作(即,在转换为字符串之前计算其参数?)简单地构造一个字符串或在过程体中引用参数将返回文本结果,即“op”或“op”而不是“+”或“+”
感谢您的帮助。这是我想到的最简单的解决方案:
(define (foo op a b)
(printf "The ~s of ~s and ~s is: ~s\n"
(object-name op) a b (op a b)))
看看这是否适合您:
(foo + 1 2)
> The + of 1 and 2 is: 3
(foo cons 'bar empty)
> The cons of bar and () is: (bar)
@这本书是个好消息!就在您接受我的回答之后,我找到了一种显示过程名称的方法,它完全符合您的要求。看看我最新的答案!:)