-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc
executable file
·32 lines (29 loc) · 906 Bytes
/
wc
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
#!/usr/bin/env escript
%% wc -- count words, lines and characters
%%
%% BUGS
%% If Ctrl-D during a word, will not count word or curent line.
wc() ->
Input = ststd:getf(),
wc(Input, false, 0, 0, 0).
wc([], _, WC, LC, CC) ->
[LC, WC, CC];
wc([H|T], inword, WC, LC, CC) ->
case lists:member(H, [9, 10, 32]) of
true when H =:= 10 -> wc(T, false, WC+1, LC+1, CC+1);
true -> wc(T, false, WC+1, LC, CC+1);
false -> wc(T, inword, WC, LC, CC+1)
end;
wc([H|T], _, WC, LC, CC) ->
case lists:member(H, [9, 10, 32]) of
true when H =:= 10 -> wc(T, false, WC, LC+1, CC+1);
true -> wc(T, false, WC, LC, CC+1);
false -> wc(T, inword, WC, LC, CC+1)
end.
main([]) ->
wc();
main(Arg) ->
{ok, Bin} = file:read_file(Arg),
File = erlang:binary_to_list(Bin),
Ret = wc(File, false, 0, 0, 0),
ststd:putf(" ~w ~w ~w ~s~n", Ret++Arg).