summaryrefslogtreecommitdiff
path: root/miralib/manual/31/3
blob: 1746682c494715914975e0780263e74d34fb8328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
_R_e_a_d_i_n_g_ _w_i_t_h_ _i_n_t_e_r_p_r_e_t_a_t_i_o_n_ _(_`_r_e_a_d_v_a_l_s_'_ _a_n_d_ _`_$_+_'_)

There is a function _r_e_a_d_v_a_l_s 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, _r_e_a_d_v_a_l_s 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 _s_h_o_w
 (i) _r_e_a_d_v_a_l_s 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 _r_e_a_d_v_a_l_s might be used in a script
	x :: [num]
	x = readvals "data"
The file `data' should contain expressions of type num (one per line).

The _r_e_a_d_v_a_l_s function provides  Miranda  with  a  simple  form  of  data
persistence - data can be written to a file (e.g. using `_s_h_o_w') and read
back using _r_e_a_d_v_a_l_s 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 `_s_h_o_w'.

Data of abstract type can be written to file using _s_h_o_w  and  read  back
with  _r_e_a_d_v_a_l_s - 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 _r_e_a_d_v_a_l_s  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.