Haskell完整的文本文件索引器

diz*_*9er 1 java recursion haskell

在早些时候看到关于文本文件索引的帖子后,我开始好奇,因为我正在学习使用Haskell(这不太顺利),如果它可以在Haskell中轻松实现相同的目标: 我如何创建Java来自文件内容的字符串?

该课程的目标是读取文件,按字母顺序排列文件中的单词,并显示这些单词出现的行号.

Haskell中的函数是否能够以类似的方式构建?

Pio*_*Miś 5

这是一个可以完成您需要的繁重工作的功能:

import Data.List

sortWords :: String -> [(Int,String)]
sortWords contents = sortBy (\(_,w1) (_,w2) -> compare w1 w2) ys
  where xs = zip [1..] (lines contents)
        ys = concatMap (\(n,line) -> zip (repeat n) (words line)) xs
Run Code Online (Sandbox Code Playgroud)

剩下要做的唯一事情就是编写一些简单的IO代码来读取文件并很好地打印结果.如果您在以下输入上运行此功能:

Here are some test
lines to see if this works...
What: about? punctuation!
Run Code Online (Sandbox Code Playgroud)

你会得到这个:

(1,"Here")
(3,"What:")
(3,"about?")
(1,"are")
(2,"if")
(2,"lines")
(3,"punctuation!")
(2,"see")
(1,"some")
(1,"test")
(2,"this")
(2,"to")
(2,"works...")
Run Code Online (Sandbox Code Playgroud)


And*_*ewC 5

这是一个Haskell解决方案

import Data.List (sortBy)
import Data.Function (on)

numberWordsSorted :: String -> [(String,Int)]
numberWordsSorted  = sortBy (compare `on` fst)
                   . concat 
                   . zipWith (\n xs -> [(x,n)|x<- xs]) [1..]   -- add line numbers
                   . map words
                   . lines

main = fmap numberWordsSorted (readFile "example.txt") >>= mapM_ print
Run Code Online (Sandbox Code Playgroud)

如果你example.txt用内容运行它

the quick brown fox
jumps over the lazy dog
Run Code Online (Sandbox Code Playgroud)

你得到

("brown",1)
("dog",2)
("fox",1)
("jumps",2)
("lazy",2)
("over",2)
("quick",1)
("the",1)
("the",2)
Run Code Online (Sandbox Code Playgroud)

Data.Map如果你更愿意看到the [1,2]而不是两行输出,你应该使用而不是成对.