V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
myid
V2EX  ›  Haskell

贴 Haskell 代码,解一道小题。给任意字符串,找出长度最长的数字串。请随意扔砖头。 http://www.v2ex.com/t/259360#reply18

  •  
  •   myid · 2016-02-28 20:59:18 +08:00 · 4606 次点击
    这是一个创建于 2950 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import Data.Char (isDigit)

    longestInts :: String -> [Int]
    longestInts xs = map (digitsToInt) $ longestLength $ extractDigits xs

    extractDigits :: String -> [String]
    extractDigits [] = []
    extractDigits xs
    | null $ dropWhile (not.isDigit) xs = []
    | otherwise = (takeWhile (isDigit) $ dropWhile (not.isDigit) xs) : (extractDigits $ dropWhile (isDigit) $ dropWhile (not.isDigit) xs)

    longestLength :: [[a]] -> [[a]]
    longestLength xs = filter (\x -> length x == (maximum $ map (length) xs)) xs
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3035 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:49 · PVG 20:49 · LAX 05:49 · JFK 08:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.