Racket 将变量值输出到新文件
目前我有以下代码:Racket 将变量值输出到新文件,racket,Racket,目前我有以下代码: (define (write host code) (with-output-to-file host (lambda () (printf code)))) (let ([myself (find-system-path 'run-file)] [test (substring myself 1 3)]) (printf "~s\n" myself) (write "hello.txt" myself)) 我试图
(define (write host code)
(with-output-to-file host
(lambda ()
(printf code))))
(let ([myself (find-system-path 'run-file)]
[test (substring myself 1 3)])
(printf "~s\n" myself)
(write "hello.txt" myself))
我试图写变量“我自己”的值。很抱歉,我不是一个非常喜欢函数式语言的人
错误:
printf: contract violation
expected: string?
given: #<path:C:\Program Files\Racket\DrRacket.exe>
printf:违反合同
预期:字符串?
鉴于:#
所以我假设我必须自己把变量转换成字符串?我在这里感到困惑,我只知道诸如perl/python之类的OOP语言。问题不在于
printf
:
> (printf "~s\n" (find-system-path 'run-file))
#<path:/Users/soegaard/racketgithub/racket/racket/bin/../DrRacket.app/Contents/MacOS/DrRacket>
注意~.s
中的点
但您也可以将路径转换为字符串,然后使用子字符串
> (substring (~a (find-system-path 'run-file)) 0 20)
"/Users/soegaard/rack"
问题不在于
printf
:
> (printf "~s\n" (find-system-path 'run-file))
#<path:/Users/soegaard/racketgithub/racket/racket/bin/../DrRacket.app/Contents/MacOS/DrRacket>
注意~.s
中的点
但您也可以将路径转换为字符串,然后使用子字符串
> (substring (~a (find-system-path 'run-file)) 0 20)
"/Users/soegaard/rack"