Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 切换到bytestring_Haskell_Bytestring - Fatal编程技术网

Haskell 切换到bytestring

Haskell 切换到bytestring,haskell,bytestring,Haskell,Bytestring,编辑:我听从了Yuras和Dave4420的建议,谢谢。我仍然有一些错误。更新了问题。最后,我将使用梅尔西的版本谢谢,但我仍然想找到我的错误 我有一个简单的脚本,如下所示: import System.Environment getRow :: Int -> String -> String getRow n = (!!n) . lines getField :: Int -> String -> String getField n = (!!n) . words'

编辑:我听从了Yuras和Dave4420的建议,谢谢。我仍然有一些错误。更新了问题。最后,我将使用梅尔西的版本谢谢,但我仍然想找到我的错误

我有一个简单的脚本,如下所示:

import System.Environment

getRow :: Int -> String -> String
getRow n = (!!n) . lines

getField :: Int -> String -> String
getField n = (!!n) . words'

words' :: String -> [String]
words' str = case str of
                        [] -> []
                        _ -> (takeHead " ; " str) : (words' (takeTail " ; " str))

takeHead :: String -> String -> String
takeHead st1 st2 = case st2 of
                                [] -> []
                                _ -> if st1 == (nHead (length st1) st2) then [] else (head st2):(takeHead st1 (tail st2))

takeTail :: String -> String -> String
takeTail st1 st2 = case st2 of
                                [] -> []
                                _ -> if st1 == (nHead (length st1) st2) then nTail (length st1) st2 else takeTail st1 (tail st2)

nTail :: Int -> String -> String
nTail n str = let rec n str = if n == 0 then str else rec (n - 1) (tail str)
              in if (length str) < n then str else rec n str

nHead :: Int -> String -> String
nHead n str = let rec n str = if n == 0 then [] else (head str):(rec (n - 1) (tail str))
              in if (length str) < n then str else rec n str

getValue :: String -> String -> String -> String
getValue row field src = getField (read field) $ getRow (read row) src

main :: IO ()
main = do
    args <- getArgs
    case args of
        (path: opt1: opt2: _) -> do
            src <- readFile path
            putStrLn $ getValue opt1 opt2 src
        (path: _) -> do
            src <- readFile path
            putStrLn $ show $ length $ lines src
它不起作用。我无法调试它。以下是GHC告诉我的:

BETA_getlow2.hs:10:23:
    Couldn't match expected type `GHC.Int.Int64' with actual type `Int'
    In the second argument of `B.index', namely `n'
    In the expression: (`B.index` n)
    In the expression: (`B.index` n) $ Bu.lines

BETA_getlow2.hs:13:23:
    Couldn't match expected type `GHC.Int.Int64' with actual type `Int'
    In the second argument of `B.index', namely `n'
    In the expression: (`B.index` n)
    In the expression: (`B.index` n) $ wordsWithSeparator
任何提示都将不胜感激

getRow n = (!!n) . lines
比照

getRow n = B.index . Bu.lines
在第二个版本中,您根本不使用n,因此它与

getRow _ = B.index . Bu.lines
在第一个示例中,您使用n作为!!操作人员在第二个版本中也需要这样做


看起来这不是代码中的唯一问题,但我希望这是一个很好的起点

我想,尤拉斯已经为你解决了前两个错误

关于第三个错误:

words' :: B.ByteString -> [B.ByteString]
words' str = if B.null str then B.empty else ...
B.empty应为[]。B.empty::B.ByteString,但结果应具有类型[B.ByteString]

关于第4-7个错误:

长度::[a]->Int B.length::B.ByteString->Int64 在本例中,我会将nTail和nHead的类型签名更改为使用Int64而不是Int。如果这不起作用,我会在所有整数类型上使用Integer,使用toInteger进行转换

关于第八个错误:

要读取的输入必须是字符串。那是绕不过去的。您必须将B.ByteString转换为字符串,并将其传递给read

顺便问一下,您确定要切换到ByteString而不是文本吗

关于第9个最终错误:

args::[Data.ByteString.ByteString]n.b.严格的ByteString列表,不是您在其他地方使用的惰性ByteString,而是您出于某种原因期望args::b.ByteString的模式匹配

您应该像对[String]进行模式匹配一样对[ByteString]进行模式匹配:它们都是列表


使用map B.fromChunks将args转换为[B.ByteString]类型。返回参数。

我冒昧地将以下两个子问题解释为您的原始问题

对于您发布的脚本,您通常会编写什么Haskell代码。 有效执行所需功能的正确数据结构是什么。 下面的代码给出了这两个子问题的一个答案。它使用库来表示Unicode字符序列。此外,它还利用文本库的高级API来实现所需的功能。这使得代码更容易掌握,从而避免了在实现低级函数时可能出现的错误

{-# LANGUAGE OverloadedStrings #-}

import qualified Data.Text    as T
import qualified Data.Text.IO as T

import System.Environment (getArgs)

type Table a = [[a]]

-- | Split a text value into a text table.
toTable :: T.Text -> Table T.Text
toTable = map (T.splitOn " ; ") . T.lines

-- | Retrieve a cell from a table.
cell :: Int -> Int -> Table a -> a
cell row col = (!! col) . (!! row)

main :: IO ()
main = do
    (path:rest) <- getArgs
    src <- T.readFile path
    case rest of
        row : col : _ -> T.putStrLn $ cell (read row) (read col) $ toTable src
        _             -> putStrLn $ show $ length $ T.lines src

您可以将代码内联粘贴,它将高亮显示。只需确保前后各留一行空白,每行代码至少缩进四个空格即可。好像我不能给分。。。
{-# LANGUAGE OverloadedStrings #-}

import qualified Data.Text    as T
import qualified Data.Text.IO as T

import System.Environment (getArgs)

type Table a = [[a]]

-- | Split a text value into a text table.
toTable :: T.Text -> Table T.Text
toTable = map (T.splitOn " ; ") . T.lines

-- | Retrieve a cell from a table.
cell :: Int -> Int -> Table a -> a
cell row col = (!! col) . (!! row)

main :: IO ()
main = do
    (path:rest) <- getArgs
    src <- T.readFile path
    case rest of
        row : col : _ -> T.putStrLn $ cell (read row) (read col) $ toTable src
        _             -> putStrLn $ show $ length $ T.lines src