[project @ 2000-08-17 11:24:49 by rrt]
[ghc-hetmet.git] / ghc / tests / programs / waugh_neural / ReadLists.lhs
1 ReadLists
2 Written by Sam Waugh
3 Date Started : 10th September 1992
4 Last Modified: 10th November 1992
5
6 This module allows the reading of lists of values from a string
7 of the one type seperated by white space.
8
9 Thanks to Paul Hudak for suggestions concerning getVals.
10
11 > module ReadLists (readWhiteList, readNumBools) where
12
13
14 readWhiteList reads a white-spaced list from a given string
15
16 > readWhiteList :: (Read a) => String -> [a]
17 > readWhiteList = getVals reads
18
19
20 readNumBools reads a list of white-spaced boolean values from a given
21 string.  Booleans in a string are represented as 1's and 0's.
22
23 > readNumBools :: String -> [Bool]
24 > readNumBools = getVals readBool
25
26 > readBool :: ReadS Bool
27 > readBool []     = []
28 > readBool (x:xs) = [(x == '1', xs)]
29
30
31 getVals (base function) takes a string, s, and a reading function, readVal,
32 and repeatedly applies readVal to s while removing whitespace
33
34 > getVals :: ReadS a -> String -> [a]
35 > getVals readVal s = case readVal (stripWhite s) of
36 >                       []       -> []
37 >                       (x,s'):_ -> x : getVals readVal s'
38
39
40 stripWhite removes white space from the front of a string
41
42 > stripWhite :: String -> String
43 > stripWhite = dropWhile (`elem` " \t\n")