Reading with interpretation (`readvals' and `$+')
There is a function readvals which takes a string representing a UNIX
pathname, and returns a list of values found in the file of that name.
The values may be represented by arbitrary Miranda expressions, written
one per line. Blank lines, and Miranda style comments (||...) are
ignored. If the input file appears to be a teletype, readvals reacts to
syntactically incorrect or wrongly typed data by prompting the user to
repeat the line, and such bad values are omitted from the result list.
If the input file does not appear to be a teletype, bad data causes
readvals to abort with an error message.
Note that, similarly to show
(i) readvals is a reserved word, not an identifier.
(ii) the context in which it is used must be such as to determine its
type monomorphically. Extra type specifications may be needed in the
script to meet this condition.
Here is a simple example of how readvals might be used in a script
x :: [num]
x = readvals "data"
The file `data' should contain expressions of type num (one per line).
The readvals function provides Miranda with a simple form of data
persistence - data can be written to a file (e.g. using `show') and read
back using readvals in a later session. Data objects saved in this way
must of course be finite. Notice also that if you wish to save data
containing functions, you will have to set up some special arrangement,
since such data cannot be written out using `show'.
Data of abstract type can be written to file using show and read back
with readvals - provided that an appropriate show-function was included
in the signature of the abstract type (see manual section on abstract
types).
Finally note that $+ behaves exactly like an application of readvals to
the name of the file to which the standard input is connected. For
example
sum $+
read a sequence of numeric expressions from the keyboard (one per line)
up to the next control-D, and then returns their sum.