I/O


 

 

4.11 I/O

TBoS p.113-121, SD

The basic read and write operations in Shen are constructed on top of two primitives of Kλ called 'read-byte' and 'write-byte' which take a stream as an argument. If no stream is given then these functions take the standard input and the standard output as default arguments. All other I/O system functions are built from these two primitives. These include

Type Functions Comments
Input read-byte, read,
lineread, input,
input+
(note all of the preceding take a stream as an optional argument which if not specified becomes the standard input)
Input read-file,
read-file-as-bytelist,
read-file-as-string
Output write-byte, pr (note all of the preceding take a stream as an optional argument which if not specified becomes the standard output)
Output nl, output, print, write-to-file

The functions are covered in the glossary of functions. In brief

Function Description
input takes the user input and evaluates it.
input+ takes a type and a stream as an input and type checks the user input according to the type. If a type error is found, an exception is raised otherwise behaves as input.
lineread reads the user input and when the input is terminated by a new line, returns the list of parsable tokens of that input.
nl takes a non-negative integer n and prints n new lines. If none is given prints a single new line.
output takes the same input as make-string and returns the same output but as a side-effect prints the resulting string.
pr takes a string and a stream and prints the string to the stream.
print prints any object returning that object.
read takes the user input and parses it without evaluating the result.
read-byte takes a stream and reads the first byte off it. If the stream is empty returns -1.
read-file takes a file name (string) and returns the list of parsable tokens in it.
read-file-as-bytelist takes a file name (string) and returns the list of bytes in it.
read-file-as-string takes a file name (string) and returns the contents of it as a string.
write-to-file takes a file name f (string) and an expression e and writes the normal form of e to f overwriting any contents. If e is a string, then the contents of e are written to f without the outer quotes.

In the examples below, the user input is in red.

(lineread)
1 2 3
[1 2 3]

(pr "hello world")
hello world"hello world"

(input)
(* 7 8)
56

(read)
(* 7 8)
[* 7 8]

(input+ number)
a
a is not of type number

(read-file "factorial.shen")
[define factorial 0 -> 1 X -> [* X [factorial [- X 1]]]]
1. Introduction

2. License

3. History

4. The Core Language

4.1 Base Types
4.1.1 Symbols
4.1.2 Strings
4.1.3 Numbers
4.2 Function Applications
4.3 The Top Level
4.4 Arithmetic
4.5 Comments

4.6 Sequences

4.6.1 Lists
4.6.2 Tuples
4.6.3 Vectors

4.7 lambda and let
4.8 Global Assignments
4.9 Higher Order Functions
4.10 Lazy Evaluation
4.11 I/O
4.12 Loading Files
4.13 Streams
4.14 Exceptions
4.15 Hashing
4.16 Property Lists
4.17 Eval

5 Defining Functions

5.1 Partial Functions
5.2 List Handling Functions
5.3 String Handling Functions
5.4 Tuple Handling Functions
5.5 Vector Handling Functions
5.6 Guards
5.7 Backtracking
5.8 Writing in Kλ
5.9 Macros

6. Packages

7. Shen-YACC

7.1 Recognisor Generator
7.2 Semantic Actions

8. Shen Prolog

8.1 Sample Programs

9. Types

9.1 Types and Constructors
9.2 Functions and Types
9.3 Synonyms

10 Sequent Calculus

10.1 Recursive Types

10.2 Exotic Types

10.2.1 Dependent Types
10.2.2 Negative Types
10.2.3 Subtypes
10.2.4 The Type of All Sets

11 Glossary of Functions

12 The Syntax of Shen

Built by Shen Technology (c) Mark Tarver, September 2021