gpt4 book ai didi

list - Haskell - 打印元组列表

转载 作者:行者123 更新时间:2023-12-02 08:27:51 25 4
gpt4 key购买 nike

我有一个元组列表,格式如下:

[(String, Int)]

我如何打印它以显示如下:

String : Int
String : Int
String : Int
...

我是 Haskell 的新手,所以请尽可能说清楚。谢谢!

更新:这是我的程序代码现在的样子:

main = do  
putStrLn "********* Haskell word frequency counter *********"
putStrLn ""
conts <- readFile "text.txt"
let lowConts = map toLower conts
let counted = countAllWords (lowConts)
let sorted = sortTuples (counted)
let reversed = reverse sorted
putStrLn "Word : Count"
mapM_ (printTuple) reversed

-- Counts all the words.
countAllWords :: String -> [(String, Int)]
countAllWords fileContents = wordsCount (toWords (noPunc fileContents))

-- Splits words and removes linking words.
toWords :: String -> [String]
toWords s = filter (\w -> w `notElem` ["and","the","for"]) (words s)

-- Remove punctuation from text String.
noPunc :: String -> String
noPunc xs = [ x | x <- xs, not (x `elem` ",.?!-:;\"\'") ]

-- Counts, how often each string in the given list appears.
wordsCount :: [String] -> [(String, Int)]
wordsCount xs = map (\xs -> (head xs, length xs)) . group . sort $ xs

-- Sort list in order of occurrences.
sortTuples :: [(String, Int)] -> [(String, Int)]
sortTuples sort = sortBy (comparing snd) sort


printTuple :: Show a => [(String, a)] -> IO ()
printTuple xs = forM_ xs (putStrLn . formatOne)

formatOne :: Show a => (String, a) -> String
formatOne (s,i) = s ++ " : " ++ show i

它向我返回了这个错误:

fileToText.hs:18:28:
Couldn't match type ‘(String, Int)’ with ‘[(String, a0)]’
Expected type: [[(String, a0)]]
Actual type: [(String, Int)]
In the second argument of ‘mapM_’, namely ‘reversed’
In a stmt of a 'do' block: mapM_ (printTuple) reversed

感谢您的帮助!

最佳答案

让我们从格式化一项开始:

formatOne :: Show a => (String, a) -> String
formatOne (s,i) = s ++ " : " ++ show i

现在您可以将此函数(例如)与 Control.Monad 中的 forM_ 一起使用,将其打印到屏幕上,如下所示(forM_因为我们想在 IO-Monad 中 - 因为我们要使用 putStrLn):

Prelude> let test = [("X1",4), ("X2",5)]
Prelude> import Control.Monad (forM_)
Prelude Control.Monad> forM_ test (putStrLn . formatOne)
X1 : 4
X2 : 5

在一个文件中你可以像这样使用它:

import Control.Monad (forM_)

printTuples :: Show a => [(String, a)] -> IO ()
printTuples xs = forM_ xs (putStrLn . formatOne)

formatOne :: Show a => (String, a) -> String
formatOne (s,i) = s ++ " : " ++ show i

编译文件

总的来说,这是一个至少可以编译的代码版本(没有文本文件无法测试它;))

import Control.Monad (forM_)
import Data.Char (toLower)
import Data.List (sort, sortBy, group)
import Data.Ord (comparing)

main :: IO ()
main = do
putStrLn "********* Haskell word frequency counter *********"
putStrLn ""
conts <- readFile "text.txt"
let lowConts = map toLower conts
let counted = countAllWords lowConts
let sorted = sortTuples counted
let reversed = reverse sorted
putStrLn "Word : Count"
printTuples reversed

-- Counts all the words.
countAllWords :: String -> [(String, Int)]
countAllWords = wordsCount . toWords . noPunc

-- Splits words and removes linking words.
toWords :: String -> [String]
toWords = filter (\w -> w `notElem` ["and","the","for"]) . words

-- Remove punctuation from text String.
noPunc :: String -> String
noPunc xs = [ x | x <- xs, x `notElem` ",.?!-:;\"\'" ]

-- Counts, how often each string in the given list appears.
wordsCount :: [String] -> [(String, Int)]
wordsCount = map (\xs -> (head xs, length xs)) . group . sort

-- Sort list in order of occurrences.
sortTuples :: [(String, Int)] -> [(String, Int)]
sortTuples = sortBy $ comparing snd

-- print one tuple per line separated by " : "
printTuples :: Show a => [(String, a)] -> IO ()
printTuples = mapM_ (putStrLn . formatTuple)
where formatTuple (s,i) = s ++ " : " ++ show i

我还删除了编译器警告并对其进行了 HLINT(但跳过了 Control.Arrow 内容 - 我认为 head &&& length 在这里不是更具可读性的选项)

关于list - Haskell - 打印元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30388543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com