chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Working with ports


From: John Cowan
Subject: Re: [Chicken-users] Working with ports
Date: Mon, 21 Oct 2013 16:03:13 -0400
User-agent: Mutt/1.5.20 (2009-06-14)

Loïc Faure-Lacroix scripsit:

> Being new to scheme, I hardly understand in what are ports superior
> than having a file handle and accessing files using a file descriptor.

Scheme ports are more general-purpose than Posix file handles or fds;
they allow I/O to and from strings, byte vectors, and arbitrary
data structures if you provide constructors, accessors, and mutators
for them.  Thus, if you create a string input port, you can read
characters, lines, or Scheme datums from the string that underlies the port.

> I then wrote this macro:
> 
> (define-syntax with-output-file

This is already available as a procedure `with-output-to-file`, so yhou
just need to wrap that.

> I'm not sure if I'm doing it right. Usually, I'd write a function that
> receive a port as a parameter explicitly and read/write directly to the
> port. In this case, it is not clear that a user would have to capture
> the output   by rebinding the current-output-port. But the function
> that does the work is independent of the port that can be used. I'm
> wondering if this is a good way to write code in scheme.

Most standard Scheme I/O procedures accept a port, and default to either
standard-input-port or standard-output-port if it is not supplied.

-- 
A rose by any other name                            John Cowan
may smell as sweet,                                 http://www.ccil.org/~cowan
but if you called it an onion                       address@hidden
you'd get cooks very confused.          --RMS



reply via email to

[Prev in Thread] Current Thread [Next in Thread]