(* -*- mode: sml -*-
 * $Id: spellcheck.smlnj,v 1.2 2001/07/09 00:25:28 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *)

structure Test : sig
    val main : (string * string list) -> OS.Process.status
end = struct

open TextIO;

fun chop str = String.substring (str, 0, (String.size str) - 1);

fun spellcheck () =
    let
    val dict = HashTable.mkTable (HashString.hashString, op =) (40000, (Fail "not found"))
    in
    let val din = openIn "Usr.Dict.Words"
        fun init_dict din dict =
        ( HashTable.insert dict (chop (inputLine din), 1);
          init_dict din dict )
    in
        init_dict din dict
    end handle EndOfFile => ();

    let fun check_words dict =
        let val word = chop (inputLine stdIn) in
            case HashTable.find dict word of
            SOME _ => ()
              | NONE => (print word ; print "\n");
            check_words dict
        end
    in
        check_words dict
    end handle EndOfFile => ()
    end

fun atoi s = case Int.fromString s of SOME num => num | NONE => 0;

fun main(name, args) = 
    let
    val arg = hd(args @ ["1"])
    val num = atoi arg
    in
    spellcheck ();
    OS.Process.success
    end
end

val _ = SMLofNJ.exportFn("spellcheck", Test.main);