Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Scheme (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Input/output=== Scheme's input and output is based on the ''port'' datatype. (R5RS sec 6.6)<ref name="r5rs"/> R5RS defines two default ports, accessible with the procedures <code>current-input-port</code> and <code>current-output-port</code>, which correspond to the Unix notions of [[Standard streams|standard input and standard output]]. Most implementations also provide <code>current-error-port</code>. [[Redirection (computing)|Redirection]] of input and standard output is supported in the standard, by standard procedures such as <code>with-input-from-file</code> and <code>with-output-to-file</code>. Most implementations provide string ports with similar redirection capabilities, enabling many normal input-output operations to be performed on string buffers instead of files, using procedures described in SRFI 6.<ref name="srfi-6">{{Cite web |last=William D Clinger |date=1999-07-01 |title=SRFI 6: Basic String Ports |url=http://srfi.schemers.org/srfi-6/srfi-6.html |access-date=2012-08-09 |publisher=The SRFI Editors, schemers.org}}</ref> The R6RS standard specifies much more sophisticated and capable port procedures and many new types of port. The following examples are written in strict R5RS Scheme. Example 1: With output defaulting to (current-output-port): <syntaxhighlight lang="Scheme"> (let ((hello0 (lambda() (display "Hello world") (newline)))) (hello0)) </syntaxhighlight> Example 2: As 1, but using optional port argument to output procedures <syntaxhighlight lang="Scheme"> (let ((hello1 (lambda (p) (display "Hello world" p) (newline p)))) (hello1 (current-output-port))) </syntaxhighlight> Example 3: As 1, but output is redirected to a newly created file <syntaxhighlight lang="Scheme"> ;; NB: with-output-to-file is an optional procedure in R5RS (let ((hello0 (lambda () (display "Hello world") (newline)))) (with-output-to-file "helloworldoutputfile" hello0)) </syntaxhighlight> Example 4: As 2, but with explicit file open and port close to send output to file <syntaxhighlight lang="Scheme"> (let ((hello1 (lambda (p) (display "Hello world" p) (newline p))) (output-port (open-output-file "helloworldoutputfile"))) (hello1 output-port) (close-output-port output-port)) </syntaxhighlight> Example 5: As 2, but with using call-with-output-file to send output to a file. <syntaxhighlight lang="Scheme"> (let ((hello1 (lambda (p) (display "Hello world" p) (newline p)))) (call-with-output-file "helloworldoutputfile" hello1)) </syntaxhighlight> Similar procedures are provided for input. R5RS Scheme provides the predicates <code>input-port?</code> and <code>output-port?</code>. For character input and output, <code>write-char</code>, <code>read-char</code>, <code>peek-char</code> and <code>char-ready?</code> are provided. For writing and reading Scheme expressions, Scheme provides <code>read</code> and <code>write</code>. On a read operation, the result returned is the end-of-file object if the input port has reached the end of the file, and this can be tested using the predicate <code>eof-object?</code>. With the standard, SRFI 28 also defines a basic formatting procedure resembling Common Lisp's <code>format</code> function, after which it is named.<ref name="srfi-28">{{Cite web |last=Scott G. Miller |date=2002-06-25 |title=SRFI 28: Basic Format Strings |url=http://srfi.schemers.org/srfi-28/srfi-28.html |publisher=The SRFI Editors, schemers.org |access-date=2012-08-09}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)