myid
V2EX  ›  Haskell

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

  •  
  •   myid · Feb 28, 2016 · 5704 views
    This topic created in 3726 days ago, the information mentioned may be changed or developed.
    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
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4238 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:56 · PVG 08:56 · LAX 17:56 · JFK 20:56
    ♥ Do have faith in what you're doing.