summaryrefslogtreecommitdiff
path: root/1415/fp2/week1/mart/SimpleFileIO.icl
diff options
context:
space:
mode:
authorCamil Staps2016-02-02 19:24:50 +0100
committerCamil Staps2016-02-02 19:24:50 +0100
commita7d7542dc646a5fd124ef71e71ce260889f1701b (patch)
tree04ed89503bbb3cc9933273a1326a53ca724c3492 /1415/fp2/week1/mart/SimpleFileIO.icl
parentweek6 camil: working positioning of lines by putting empties at left and righ... (diff)
Moved to 1415 directoryHEADmaster
Diffstat (limited to '1415/fp2/week1/mart/SimpleFileIO.icl')
-rw-r--r--1415/fp2/week1/mart/SimpleFileIO.icl39
1 files changed, 39 insertions, 0 deletions
diff --git a/1415/fp2/week1/mart/SimpleFileIO.icl b/1415/fp2/week1/mart/SimpleFileIO.icl
new file mode 100644
index 0000000..b2a483a
--- /dev/null
+++ b/1415/fp2/week1/mart/SimpleFileIO.icl
@@ -0,0 +1,39 @@
+implementation module SimpleFileIO
+
+import StdEnv, StdFile, StdOverloaded, StdMaybe
+
+// 1.
+readFile :: String *env -> (Maybe String, *env) | FileSystem env
+readFile s env
+# (ss, env) = readLines s env
+| ss == Nothing = (Nothing, env)
+| otherwise = (Just (foldl (+++) "" (fromJust ss)), env)
+
+writeFile :: String String *env -> (Bool, *env) | FileSystem env
+writeFile fn s env
+# (ok, outfile, env) = fopen fn FWriteText env
+| not ok = (False, env)
+# outfile = fwrites s outfile
+# (ok, env) = fclose outfile env
+| otherwise = (ok, env)
+
+// 2.
+readLines :: String *env -> (Maybe [String],*env) | FileSystem env
+readLines s env
+# (ok, infile, env) = sfopen s FReadText env
+| not ok = (Nothing, env)
+| otherwise = (Just (fst (readLines` infile)), env)
+where
+ readLines` :: File -> ([String], File)
+ readLines` file
+ | sfend file = ([], file)
+ # (line, file) = sfreadline file
+ # (ss, file) = readLines` file
+ | otherwise = ([line : ss], file)
+
+writeLines :: String [String] *env -> (Bool, *env) | FileSystem env
+writeLines fn ss env = writeFile fn (foldl (+++) "" [s +++ "\n" \\ s <- ss]) env
+
+// 3.
+//mapFile :: String String (a -> b) *env -> (Bool, *env) | FileSystem env & ... a & ... b
+