|
|
1.1 root 1: (* lex.sml *)
2:
3: signature LEX =
4: sig
5: val words: string -> string list
6: end
7:
8: structure Lex1 : LEX =
9: struct
10:
11: fun separator " " = true
12: | separator "\t" = true
13: | separator "\n" = true
14: | separator _ = false
15:
16: fun words s =
17: let fun getword(w,[]) = [implode(rev w)]
18: | getword(w,c::rest) =
19: if separator(c)
20: then implode(rev w) :: skip rest
21: else getword(c::w,rest)
22: and skip [] = []
23: | skip(c::rest) =
24: if separator c
25: then skip rest
26: else getword([c],rest)
27: in skip(explode s)
28: end
29:
30: end (* Lex1 *)
31:
32: structure Lex2 : LEX =
33: struct
34:
35: fun separator " " = true
36: | separator "\t" = true
37: | separator "\n" = true
38: | separator _ = false
39:
40: fun words(s: string) =
41: let val len = length s
42: fun skip n =
43: let fun getword m =
44: if m>=len orelse separator(substring(s,m,1))
45: then substring(s,n,(m-n))::skip(m+1)
46: else getword(m+1)
47: in if n>=len
48: then []
49: else if separator(substring(s,n,1))
50: then skip(n+1)
51: else getword(n+1)
52: end
53: in skip 0
54: end
55:
56: end (* Lex2 *)
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.