如何在tinyscheme中写入文件? [英] How to write to a file in tinyscheme?

查看:83
本文介绍了如何在tinyscheme中写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方案实施:tinyscheme

Scheme implementation : tinyscheme

这是我的尝试:

(with-output-to-file "biophilia.c"
  (lambda (output-port)
    (write "Hello" output-port)))

通过以下内容创建biophilia.c:

Ceates biophilia.c with following content:

错误:(:26)参数不足

我在这里做错了什么?如何修理?

What am I doing wrong here? how to repair it?

(define (with-output-to-file s p)
     (let ((outport (open-output-file s)))
          (if (eq? outport #f)
               #f
               (let ((prev-outport (current-output-port)))
                    (set-output-port outport)
                    (let ((res (p)))
                         (close-output-port outport)
                         (set-output-port prev-outport)
                         res)))))

推荐答案

您在错误地呼叫with-output-to-file.

第二个参数是一个thunk,而不是一个需要port参数的过程.

The second argument is a thunk, and not a procedure expecting a port argument.

所以这样称呼它:

(with-output-to-file "biophilia.c"
  (lambda ()
    (write "Hello")))

with-output-to-file已经为您进行了当前端口的重新绑定(如您在重建中所尝试的那样).

with-output-to-file already does the re-binding of the current-port for you (as you tried in your reconstruction).

请参见Racket文档

See the Racket docs for it here.

这篇关于如何在tinyscheme中写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆