2016-09-26 17 views
0

該函數將實數的列表保存到文件:SML:如何寫在文件兩個清單ML

fun writeReal (real, filename) = 
    let val fd = TextIO.openOut filename 
     val _ = map (fn i => TextIO.output (fd, Real.toString i^"\r\n")) real 
     val _ = TextIO.closeOut fd 
    in() end 

調用函數:

writeReal ([1.0,2.0,3.0,4.0], "hello.txt") 

將以下內容寫入文件hello.txt的

1.0 
2.0 
3.0 
4.0 

如果我有兩個表,一個包含實數的列表,另一個是單詞列表,我如何讀取和寫入這兩個列表到文件中?例如:

writeReal ([1.0,2.0,3.0,4.0], [one, two, three, four] , "hello.txt") 

應該寫下面的文件hello.txt的

1.0 one 
2.0 two 
3.0 three 
4.0 four 

回答

1

首先,這裏是一些一般性的反饋:

  1. 這是很好的習慣attribute your sources,在這種情況下會提供一些上下文。

  2. 一個函數可以將多個實數寫入一個文件,也許應該叫做writeReals(複數)。

  3. 由於您丟棄了List.map的結果,請考慮使用List.app

如果我有兩個表,一個包含實數列表,文字的另一列表,我怎麼讀,寫兩個列表的文件嗎?

  • 我會建議你實際上寫對的列表,而不是對列表。成對的列表,例如[(1.0, "Hello"), (2.0, "World")],將始終有相同數量的實數和字符串。一對列表,例如([1.0, 2.0, 3.0], ["Hello", "World"])不一定會那樣。

    如果你被迫用一對名單的工作,你可以將它們轉換成使用內置的庫函數ListPair.zip像這樣對的列表:

    - ListPair.zip ([1.0, 2.0, 3.0], ["Hello", "World"]); 
    > val it = [(1.0, "Hello"), (2.0, "World")] : (real * string) list 
    
  • 寫對的列表,這裏有一些提示:

    1. 寫一個函數,pairToString : (real * string) -> string是一對轉換成可以寫入文件中的一行。例如,pairToString (1.0, "Hello")應產生字符串"1.0 Hello\r\n"

    2. 使用上面的框架功能應用此功能:

      fun writeRealStringPairs (pairs, filename) = 
          let val fd = TextIO.openOut filename 
           val _ = List.app ... pairs 
           val _ = TextIO.closeOut fd 
          in() end 
      

      ,你的東西明智的替代...

  • 讀對的列表,這裏有一些提示:

    1. 寫一個函數pairFromString : string -> (real * string)從文件中的行轉換成一對。例如,pairFromString "1.0 Hello\r\n"應該產生這對(1.0, "Hello")

      這個功能是所謂的解析器,並不是完全微不足道的寫。結合例如功能String.tokens,您可能會玩得很開心。 Char.isSpaceReal.fromString。請注意,Real.fromString實際上返回實際選項的值(如果它失敗),因此您需要使用例如

      case Real.fromString word1 of 
          NONE => raise Fail ("Couldn't parse as number: "^word1) 
          | SOME r => r 
      
    2. 將此函數應用於文件中的每一行,例如,通過這樣做:

      fun isLinebreak c = c = #"\r" orelse c = #"\n" 
      fun readRealStringPairs filename = 
          let val fd = TextIO.openIn filename 
           val content = TextIO.inputAll fd 
           val _ = TextIO.closeIn fd 
           val lines = String.tokens isLinebreak content 
           val pairs = List.map ... lines 
          in pairs end 
      

      你在哪裏用合理的東西代替...