File: wc-input1.sml

package info (click to toggle)
mlton 20100608-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 34,980 kB
  • ctags: 69,089
  • sloc: ansic: 18,421; lisp: 2,879; makefile: 1,570; sh: 1,325; pascal: 256; asm: 97
file content (35 lines) | stat: -rw-r--r-- 1,112 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(* Written by Stephen Weeks (sweeks@sweeks.com). *)

structure Main =
   struct
      fun doit n =
         let
            open TextIO
            val f = OS.FileSys.tmpName ()
            val out = openOut f
            val _ =
               output (out,
                       String.implode
                       (List.tabulate (1000000, fn i =>
                                       if i mod 10 = 0 then #"\n" else #"a")))
            val _ = closeOut out
            fun wc f =
               let
                  val ins = openIn f
                  fun loop (i: int): int =
                     case input1 ins of
                        NONE => i
                      | SOME c => loop (if c = #"\n" then i + 1 else i)
                  val n = loop 0
                  val _ = if n <> 100000 then raise Fail "bug" else ()
                  val _ = closeIn ins
               in n
               end
            val rec loop =
               fn 0 => ()
                | n => (wc f; loop (n - 1))
            val _ = loop n
            val _ = OS.FileSys.remove f
         in ()
         end
   end