Skip to content

Instantly share code, notes, and snippets.

@bitmappergit
Last active November 25, 2019 23:03
Show Gist options
  • Save bitmappergit/cb8d00eecf7edcbfdd193fc8e0585aca to your computer and use it in GitHub Desktop.
Save bitmappergit/cb8d00eecf7edcbfdd193fc8e0585aca to your computer and use it in GitHub Desktop.
datatype result = T | F | N
fun isWs (SOME x) = (case (Char.isSpace x) of true => T | false => F)
| isWs (NONE) = N
fun isNl (SOME #"\n") = T
| isNl (SOME _) = F
| isNl (NONE) = N
fun wc stream = let
fun tmp (a, b, c, q) = let
val input = TextIO.input1 stream
in
case (isWs input, isNl input, q) of
(F, F, T) => tmp (a, b+1, c+1, F)
| (F, F, F) => tmp (a, b, c+1, q)
| (T, T, F) => tmp (a+1, b, c+1, T)
| (T, T, T) => tmp (a+1, b, c+1, T)
| (T, F, F) => tmp (a, b, c+1, T)
| (T, F, T) => tmp (a, b, c+1, q)
| (_, _, _) => [a, b+1, c]
end
in tmp (0, 0, 0, F) end
fun pr x file =
(map (fn y => (print y; print " "))
(map Int.toString x);
map print [file, "\n"])
val _ = OS.Process.exit let
val fileName = hd (CommandLine.arguments ())
val file = TextIO.openIn fileName
val _ = pr (wc file) fileName
in OS.Process.success end
@bitmappergit
Copy link
Author

Screen Shot 2019-11-24 at 11 19 27 AM

prefix for gnu wc is gwc as I'm on macOS, everyone else was using linux systems (as far as I can tell?) which default to gnu wc, whereas macOS defaults to BSD wc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment