创建一个使用SML读取文件的列表
我试图创建一个读取文本文件的列表,例如,我有一个这样的文本文件创建一个使用SML读取文件的列表,sml,polyml,Sml,Polyml,我试图创建一个读取文本文件的列表,例如,我有一个这样的文本文件“1 5 12 9 2 6”,我想创建一个这样的列表[1,5,12,9,2,6]使用SML可以将此任务划分为几个子问题: 将文件读入字符串可以通过 type filepath = string (* filepath -> string *) fun readFile filePath = let val fd = TextIO.openIn filePath val s = TextIO.inputA
“1 5 12 9 2 6”
,我想创建一个这样的列表[1,5,12,9,2,6]
使用SML可以将此任务划分为几个子问题:
type filepath = string
(* filepath -> string *)
fun readFile filePath =
let val fd = TextIO.openIn filePath
val s = TextIO.inputAll fd
val _ = TextIO.closeIn fd
in s end
(* string -> string list *)
fun split s =
String.tokens Char.isSpace s
(* 'a option list -> 'a list option *)
fun sequence (SOME x :: rest) = Option.map (fn xs => x :: xs) (sequence rest)
| sequence (NONE :: _) = NONE
| sequence [] = SOME []
fun convert ss = sequence (List.map Int.fromString ss)
看type filepath = string
(* filepath -> string *)
fun readFile filePath =
let val fd = TextIO.openIn filePath
val s = TextIO.inputAll fd
val _ = TextIO.closeIn fd
in s end
(* string -> string list *)
fun split s =
String.tokens Char.isSpace s
(* 'a option list -> 'a list option *)
fun sequence (SOME x :: rest) = Option.map (fn xs => x :: xs) (sequence rest)
| sequence (NONE :: _) = NONE
| sequence [] = SOME []
fun convert ss = sequence (List.map Int.fromString ss)
看type filepath = string
(* filepath -> string *)
fun readFile filePath =
let val fd = TextIO.openIn filePath
val s = TextIO.inputAll fd
val _ = TextIO.closeIn fd
in s end
(* string -> string list *)
fun split s =
String.tokens Char.isSpace s
(* 'a option list -> 'a list option *)
fun sequence (SOME x :: rest) = Option.map (fn xs => x :: xs) (sequence rest)
| sequence (NONE :: _) = NONE
| sequence [] = SOME []
fun convert ss = sequence (List.map Int.fromString ss)
由于使用Int.fromString
将任何一个字符串转换为整数可能会失败并生成NONE
,List.map Int.fromString
将生成“Int选项列表”而不是“Int列表”。此“int option”列表可转换为可选的“int list”,即删除所有“int option”中的部分
,但如果只有一个无
,则整个结果将被丢弃并变为无
。这给出了最后一种类型“int list option”(要么NONE
要么SOME[1,2,…]
)
看看哪一个对这种递归有用(* filepath -> int list *)
fun readIntegers filePath =
convert (split (readFile filePath))
- 文件系统错误将导致
引发readIntegers
异常Io
- 文件中的字符串
将被解释为负5~5
- 字符串
将产生故障(-5
)NONE
- 字符串
将产生数字123(123a
有点过于宽容)Int.toString