我是 Haskell 的新手,并试图摆弄一些我在现实世界中经常遇到的测试用例。假设我有包含以下内容的文本文件“foo.txt”:
45.4 34.3 377.8
33.2 98.4 456.7
99.1 44.2 395.3
我正在尝试产生输出
[[45.4,34.3,377.8],[33.2,98.4,456.7],[99.1,44.2,395.3]]
我的代码在下面,但我在输出中得到了一些虚假的“LPS”......不确定它代表什么。
import qualified Data.ByteString.Lazy.Char8 as BStr
import qualified Data.Map as Map
readDatafile = (map (BStr.words) . BStr.lines)
testFunc path = do
contents <- BStr.readFile path
print (readDatafile contents)
当用testFunc "foo.txt"调用时,输出是
[[LPS ["45.4"],LPS ["34.3"],LPS ["377.8"]],[LPS ["33.2"],LPS ["98.4"],LPS ["456.7"]],[LPS ["99.1"],LPS ["44.2"],LPS ["395.3"]]]
任何帮助表示赞赏!谢谢。PS:使用 ByteString,因为这将在未来用于大量文件。
编辑:
我也很困惑为什么输出列表按上面的方式分组(每个数字都绑定在 [] 中),而在 ghci 中,下面的行给出了不同的排列。
*Main> (map words . lines) "45.4 34.3 377.8\n33.2 98.4 456.7\n99.1 44.2 395.3"
[["45.4","34.3","377.8"],["33.2","98.4","456.7"],["99.1","44.2","395.3"]]