带有文件名和列表的SML函数 [英] SML function that takes a filename and a list

查看:116
本文介绍了带有文件名和列表的SML函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个函数,该函数以文件名作为字符串和字符对列表。此功能必须打开命名的文件,读取文件的内容,并将字符回显到屏幕上。在对的第一个位置出现的任何字符都必须作为在对的第二个位置出现的字符回显。例如,诸如fileSubst inputFile [(## a,# b),(# b,# z)]的调用将回显inputFile的内容,并替换所有出现的字符a

I want to write a function that takes a filename as a string and a list of pairs of characters. This function must open the named file, read the contents of the file, and echo the characters to the screen. Any occurrence of a character in the first position of a pair must be echoed as the character in the second position of the pair. For example, an invocation such as fileSubst "inputFile" [(#"a", #"b"), (#"b", #"z")] will echo the contents of inputFile with all occurrences of the character a replaced by the character b and all occurrences of the character b replaced by character z.

    fun fileSubst (fileName : string, []) = nil
  | fileSubst (fileName, (a,b)::cs) = let 
    val stream = TextIO.openIn fileName    
     TextIO.input1 = fileSubst (fileName,cs) in if isSome a 
    then print(Char.toString(a)) else TextIO.print end ;


推荐答案

如@molbdnilo所建议,将其拆分为几个函数在其他情况下可能会有用。您可以将其划分为一个将文件内容读取为字符串的函数,

As @molbdnilo suggest, split this into several functions that might be useful in other contexts. You could divide this into one function that reads the file's content into a string,

fun readFile fname =
    let val fd = TextIO.openIn fname
        val contents = TextIO.inputAll fd
        val _ = TextIO.closeIn fd
    in contents end

一个用于在字符对列表中搜索正确映射的函数,

a function that searches for the right mapping in a list of pairs of characters,

fun lookup [] needle = NONE
  | lookup ((key,value)::haystack) needle =
    if needle = key then SOME value else lookup haystack needle

此函数的变体,返回键作为默认值,

a variation of this function that returns the key as a default value,

fun lookupDefault haystack needle =
    Option.getOpt (lookup haystack needle, needle)

以及一个映射每个字符的函数

and a function that maps each character in a string to another one,

fun subst mapping = String.map (lookupDefault mapping)

然后,您可以将它们组合成一个可以在文件上运行的文件。我不确定我是否认为函数调用 fileSubst foo.txt 可以读取文件的内容,执行替换打印结果显示到屏幕上。为什么它不写回文件还是将其作为字符串返回?

You can then compose those to get one that operates on files. I'm not sure I would assume that a function call fileSubst "foo.txt" might read the file's content, perform the substitution and print the result to screen. Why wouldn't it write it back to file or return it as a string?

由于文件内容可以做很多事情,因此您也可以一个更通用的文件处理功能,

Since there are so many things you can do with a file's content, you could also make a more generic file handling function,

fun withFile fname f =
    let val fd = TextIO.openIn fname
        val contents = TextIO.inputAll fd
        val _ = TextIO.closeIn fd
    in f contents end

然后,例如,

- withFile "helloworld.txt" (print o subst [(#"l", #"L")]);
HeLLo, WorLd!

通过创建通用的函数,您不必在库中填充高度专业的函数(可能无论如何都不会重复使用。

By making generically useful functions you don't have to populate your library with highly specialized functions that are likely not going to be re-used much anyways.

这篇关于带有文件名和列表的SML函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆