Count the number of times that different words appear in an English article. Use F # To write and play, which is purely entertaining! Hey
Use a binary tree to traverse the output in the middle order; call. Net's file. readalltext to read the file
Module Node
// Binary tree; node: node; empty: Empty node; type tree = empty | node of tree * string * int * tree
// Traverse letrec printtree in the middle order t =
Match T
| Empty-> printfn ""
| Node (L, Data, num, R) as N->
Printtree L
Printfn "% s --- the number of times is ----- % d" Data num
Printtree R
// Recursively insert a node. If the node is empty, the new node is inserted. If the node data is larger than the node data, the right child node is inserted. If the node data is smaller than the node data, the left node is inserted.
// If the value is equal to, add num to the node, indicating that this word appears again. letrec insert1 = Function
| X, N, empty-> node (empty, X, 1, empty)
| X, N, node (L, Data, num, R)->
If x <data then node (insert (x, 1, L), data, num, R)
Elif x> data then node (L, Data, num, insert (x, 1, R ))
Else node (L, Data, num + 1, R)
Open System
Open System. Io
[<Entrypoint>]
Let main (ARGs: String []) =
Let reader = file. readalltext ("D: \ s.txt ")
// Split the content in the file by space to obtain the string array let strings = reader. Split ('')
// Define variable root letmutable root = node. Empty
// Traverse all words and construct a binary tree for S in strings do
Root <-node. insert1 (s, 1, root)
// Returns the node. printtree Root
0