gpt4 book ai didi

Haskell uniq -c 模拟器代码解释

转载 作者:行者123 更新时间:2023-12-01 09:06:53 25 4
gpt4 key购买 nike

这段代码对我来说似乎有点迟钝......有人愿意通过解释来帮助我吗?

uniq_c l = [ nl (tam l) i s | (s,i) <- uniq_c' l]

tam = maximum . map snd . uniq_c'

uniq_c' [] = []
uniq_c' (h:t) = let (list,rest) = span (==h) t
n = length list + 1
in (h,n) : uniq_c' rest

nl tam n line = let l = length $ show n
l_tam = length $ show tam
n' = replicate (l_tam-l) " "
in concat n' ++ show n ++ " " ++ line

最佳答案

那个代码不是很好。它不重用任何现有的 Haskell 库。这是一个替代方案。 group 函数完成了这项工作;识别重复出现。 length &&& head 获取每个这样的子列表并计算单词。最后 uniq_c 找到最大宽度并使用 printf 格式化输出。

import Control.Arrow
import Data.List
import Text.Printf

uniq :: (Eq a) => [a] -> [(Int, a)]
uniq = map (length &&& head) . group

uniq_c :: [String] -> [String]
uniq_c l =
let us = uniq l
width = length . show . maximum . map fst $ us
in map (uncurry $ printf "%*d %s" width) us

关于Haskell uniq -c 模拟器代码解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6327677/

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