[clean-list] Re: Advice on style, please
alanh@dcs.kcl.ac.uk
alanh@dcs.kcl.ac.uk
Mon, 17 Dec 2001 10:09:19 +0000 (GMT)
Many thanks to Richard and Fergus, particularly to Fergus for real help
with my C code. Please excuse my blunders. They were committed late
in the evening, when I should have been in bed.
Please erase that example. It is a defamation of the Clean language.
The problem can be solved substantially better, e.g. as below.
Regards
Alan H.
module copy
import StdEnv
import ArgEnv
Start :: *World -> *World
Start w0
| argc <> 3 = snd (fclose (console1 <<< "Usage:\nCopy <input file> <output file>\n") w1)
| not fiOK = snd (fclose (console1 <<< ("Can't open file " +++ argv.[1] +++ "\n")) w2)
| not foOK = snd (fclose (console1 <<< ("Can't open file " +++ argv.[2] +++ "\n")) w3)
| not ccOK = snd (fclose (console1 <<< "failed.\n") w4)
| otherwise = snd (fclose console1 w4)
where
(console1, w1) = stdio w0
argv = getCommandLine
argc = size argv
(fiOK, fi0, w2) = fopen argv.[1] FReadText w1
(foOK, fo0, w3) = fopen argv.[2] FWriteText w2
(ccOK, fi1, fo1) = cc (fi0,fo0)
w4 = (snd (fclose fo1 (snd (fclose fi1 w3))))
cc :: (*File, *File) -> (Bool, *File, *File)
cc (fi0,fo0)
| atEnd = (True, fia, fo0)
| not ok = (False, fi1, fo0)
| err = (False, fi1, fo1)
| otherwise = cc (fi1,fo1)
where
(atEnd, fia) = fend fi0
(ok, c, fi1) = freadc fia
(err,fo1) = ferror (fwritec c fo0)