Haskell从文本文件中每隔一行获取一行
我是哈斯克尔的新手。我试图读取文件中的每一行,例如,这是我的文本文件Haskell从文本文件中每隔一行获取一行,haskell,io,Haskell,Io,我是哈斯克尔的新手。我试图读取文件中的每一行,例如,这是我的文本文件 5 1 4 8 6 8 5 2 4 7 9 2 0 所以结果应该是 1 4 8 6 8 2 4 7 9 2 下面是我的代码的样子 import System.IO import Control.Monad import Data.List import Data.Function import Data.Array import Data.Char main = do file <- readFile "t
5
1 4 8 6 8
5
2 4 7 9 2
0
所以结果应该是
1 4 8 6 8
2 4 7 9 2
下面是我的代码的样子
import System.IO
import Control.Monad
import Data.List
import Data.Function
import Data.Array
import Data.Char
main = do
file <- readFile "test.txt"
print (lines file)
let ff = map ((!!) (lines file)) [1,3..(length (lines file) - 1)]
import System.IO
进口管制
导入数据。列表
导入数据。函数
导入数据。数组
导入数据.Char
main=do
filedo符号是单子上的一种语法糖。因此,您的代码在被删除时将如下所示:
readFile "test.txt" >>= \file ->
print (lines file) >>
let ff = map ((!!) (lines file)) [1,3..(length (lines file) - 1)]
in ....
现在你可以看到这个表达式是不正确的。你可以了解更多有关这方面的信息
一个简单的修复方法是只使用return()
:
但你想要的可能是:
main = do
file <- readFile "test.txt"
let ff = map ((!!) (lines file)) [1,3..(length (lines file) - 1)]
print ff
main=do
filedo符号是单子上的一种语法糖。因此,您的代码在被删除时将如下所示:
readFile "test.txt" >>= \file ->
print (lines file) >>
let ff = map ((!!) (lines file)) [1,3..(length (lines file) - 1)]
in ....
现在你可以看到这个表达式是不正确的。你可以了解更多有关这方面的信息
一个简单的修复方法是只使用return()
:
但你想要的可能是:
main = do
file <- readFile "test.txt"
let ff = map ((!!) (lines file)) [1,3..(length (lines file) - 1)]
print ff
main=do
文件另一个答案中建议的(!!)很臭
everyOther :: [a] -> [a]
everyOther = catMaybes . zipWith ($) (cycle [const Nothing, Just])
或者:
everyOther :: [a] -> [a]
everyOther (x:x':xs) = x : everyOther xs
everyOther _ = []
另一个答案中建议的(!!)很臭
everyOther :: [a] -> [a]
everyOther = catMaybes . zipWith ($) (cycle [const Nothing, Just])
或者:
everyOther :: [a] -> [a]
everyOther (x:x':xs) = x : everyOther xs
everyOther _ = []