1: #light
2: open System
3: open System.IO
4:
5: let rec line (s : StreamReader) (sawEol : bool) (acc : string) =
6: match (char.ConvertFromUtf32(s.BaseStream.ReadByte())) with
7: | "\n" when (not sawEol) -> line s true ""
8: | "\n" when sawEol -> acc
9: | str -> line s sawEol (acc + str)
10:
11: let rec binarySearch (s : StreamReader) (target : string)
12: (low : System.Int64) (high : System.Int64) =
13: let mid = Int64.shift_right (low + high) 1 in
14: let pos = s.BaseStream.Seek(mid, SeekOrigin.Begin) in
15: let cur = (line s false "") in
16: printf "Current: %s\n" cur;
17: if (high < low) then
18: Int64.zero
19: elif (String.CompareOrdinal(cur, target) > 0) then
20: binarySearch s target low (mid - Int64.one)
21: elif (String.CompareOrdinal(cur, target) < 0) then
22: binarySearch s target (mid + Int64.one) high
23: else
24: mid
25:
26: let isValidWordFromFile (filename: string) (word : string) =
27: let fileInfo = new FileInfo(filename) in
28: let size = fileInfo.Length in
29: use stream = fileInfo.OpenText() in
30: let res = (binarySearch stream word Int64.zero size) in
31: not (res = Int64.zero)
32:
33: let output = isValidWordFromFile "dictionary.txt" "entropies";;
34: printf "Found: %s\n" (any_to_string output);;
35: Console.ReadKey();;