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)))))
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
already does the re-binding of the current-port for you (as you tried in your reconstruction).See the Racket docs for it here.