F # count the number of times different words appear in an English article

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.