How to write to a file in tinyscheme?

ぃ、小莉子 提交于 2019-12-11 02:25:54

问题


Scheme implementation : tinyscheme

Here is my try:

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

Ceates biophilia.c with following content:

Error: ( : 26) not enough arguments

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

回答1:


You are calling with-output-to-file incorrectly.

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

So call it like:

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

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

See the Racket docs for it here.



来源:https://stackoverflow.com/questions/10908660/how-to-write-to-a-file-in-tinyscheme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!