[clean-list] term reading and writing
Thomas van Noort
thomas at cs.ru.nl
Thu May 1 11:45:30 MEST 2008
Hi,
The Generics library, provided in the Clean distribution, contains two
modules which define exactly the functions you require.
HP Wei wrote:
> Newbie questions--
>
> Suppose there is a data type,
>
> :: Person = { name :: String,
> id :: (Int, String),
> flag :: Bool }
>
> Suppose a file whose name is fname.txt
> has the following content:
> { name = "a_name",
> id = (123, "an_id"),
> flag = True }
>
> -----------------------------------
> Q1: how do I 'read in' a Person from fname.txt ?
> [ i.e.
> If person is of type Person,
> person = _read_ "fname.txt"
>
> is there a built-in fx that does the above _read_ ?
> Or shall I need to write a parse-fx to do this ?
Import the module GenParse from the Generics library and use the
function parseFile,
parseFile :: File -> Maybe a | gParse{|*|} a
which requires you to derive an instance of gParse for Person,
derive gParse Person
>
> Q2: if person = {name ="xyz",
> id = (456, "id"),
> flag = False}
>
> how do I _print_ this 'person' to a text file
> fname1.txt ?
Import the module GenPrint from the Generics library and use the
function printToString,
printToString :: a -> String | gPrint{|*|} a
which requires you to derive an instance of gPrint for Person,
derive gPrint Person
The function returns a String but you can easily define a function
yourself which prints to a file,
printToFile :: a File -> File | gPrint{|*|} a
>
> *** for those who knows Erlang, Q1 and Q2 are term
> reading and writing.
>
> THanks
> HP
>
>
> _______________________________________________
> clean-list mailing list
> clean-list at science.ru.nl
> http://mailman.science.ru.nl/mailman/listinfo/clean-list
Regards,
Thomas
More information about the clean-list
mailing list