【发布时间】:2016-08-03 12:16:04
【问题描述】:
我是 haskell 的新手,我想对字符串列表进行排序。例如,我的变量ff 中有一个列表,其中包含三个字符串["1 8 8 5 6", "1 4 2 3", "5 4 9 7 9 9"],我想对它们进行排序,所以我的结果应该看起来像["1 5 6 8 8", "1 2 3 4", "4 5 7 9 9 9"] 这是我完美运行的代码
import System.IO
import Control.Monad
import Data.List
import Data.Function
import Data.Array
import Data.Char
sortNumeric = sortBy (compare `on` (read :: String -> Int))
wordsWhen :: (Char -> Bool) -> String -> [String]
wordsWhen p s = case dropWhile p s of
"" -> []
s' -> w : wordsWhen p s''
where (w, s'') = break p s'
main = do
file <- readFile "test.txt"
let ff = map ((!!) (lines file)) [1,3..(length (lines file) - 1)]
let splitString = wordsWhen (==' ') (ff!!0)
let sortedResult = sortNumeric (splitString)
print sortedResult
问题在于这一行let splitString = wordsWhen (==' ') (ff!!0) 我总是得到列表的第一个元素,所以只有第一个元素被排序。如何传递列表的所有值?这是我尝试做的let splitString = wordsWhen (==' ') (ff!![0..(length(ff)-1)]) 不幸的是这不起作用。任何想法如何解决这个问题?
【问题讨论】:
标签: list sorting haskell indexing