0

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

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

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

它编译并工作。然后我想切换到ByteStrings。这是我的尝试:

import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.Char8 as Bc (cons, empty,unpack)
import qualified Data.ByteString.Lazy.UTF8 as Bu (lines)
import qualified System.Posix.Env.ByteString as Bg (getArgs)

separator :: B.ByteString
separator = (Bc.cons ' ' (Bc.cons ';' (Bc.cons ' ' Bc.empty)))

getRow :: Int -> B.ByteString -> B.ByteString
getRow n = (`B.index` n) $ Bu.lines

getCol :: Int -> B.ByteString -> B.ByteString
getCol n = (`B.index` n) $ wordsWithSeparator

wordsWithSeparator :: B.ByteString -> [B.ByteString]
wordsWithSeparator str = if B.null str then [] else (takeHead separator str):(wordsWithSeparator (takeTail separator str))

takeHead :: B.ByteString -> B.ByteString -> B.ByteString
takeHead st1 st2 = if B.null st2 then B.empty else if st1 == (nHead (toInteger (B.length st1)) st2) then B.empty else B.cons (B.head st2) (takeHead st1 (B.tail st2))

takeTail :: B.ByteString -> B.ByteString -> B.ByteString
takeTail st1 st2 = if B.null st2 then B.empty else if st1 == (nHead (toInteger (B.length st1)) st2) then nTail (toInteger (B.length st1)) st2 else takeTail st1 (B.tail st2)

nTail :: Integer -> B.ByteString -> B.ByteString
nTail n str = let rec n str = if n == 0 then str else rec (n - 1) (B.tail str)
              in if (toInteger (B.length str)) < n then str else rec n str

nHead :: Integer -> B.ByteString -> B.ByteString
nHead n str = let rec n str = if n == 0 then B.empty else B.cons (B.head str)(rec (n - 1) (B.tail str))
              in if (toInteger (B.length str)) < n then str else rec n str

getValue :: B.ByteString -> B.ByteString -> B.ByteString -> B.ByteString
getValue row field = getCol (read (Bc.unpack field)) . getRow (read (Bc.unpack row))

main = do args <- Bg.getArgs
          case (map (B.fromChunks . return) args) of
                                                    (path:opt1:opt2:_) -> do src <- B.readFile (Bc.unpack path)
                                                                             B.putStrLn $ getValue opt1 opt2 src

                                                    (path:_)           -> do src <- B.readFile (Bc.unpack path)
                                                                             putStrLn $ show $ length $ Bu.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

任何提示将不胜感激。

4

3 回答 3

7
getRow n = (!!n) . lines

与之比较

getRow n = B.index . Bu.lines

在第二个版本中您根本不使用n,所以它与

getRow _ = B.index . Bu.lines

在第一个示例中,您将n其用作(!!)运算符的参数。您需要在第二个版本中执行相同的操作。

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

于 2012-04-24T23:18:32.363 回答
2

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

  1. Haskell 代码通常会为您发布的脚本编写什么脚本。
  2. 什么是有效执行所需功能的正确数据结构。

以下代码为这两个子问题提供了一个答案。它使用该text库来表示 Unicode 字符序列。此外,它利用text库的高级 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
于 2012-04-25T10:55:03.320 回答
0

我想,Yuras 已经为你解决了前两个错误。


关于第三个错误:

words' :: B.ByteString -> [B.ByteString]
words' str = if B.null str then B.empty else ...

B.empty应该[]是。B.empty :: B.ByteString,但结果应该是 type [B.ByteString]


关于第 4-7 个错误:

  • length :: [a] -> Int
  • B.length :: B.ByteString -> Int64

在这种情况下,我将更改 和 的类型签名nTailnHead使用Int64而不是Int. 如果这不起作用,我会Integer在所有Integral类型上使用toInteger,用于进行转换。


关于第 8 个错误:

的输入read必须是String. 没有办法解决这个问题。您必须将 转换B.ByteString为 aString并将其传递给read.

(顺便说一句,您确定要切换到 ByteString 而不是 Text?)


关于第 9 个(最终)错误:

args :: [Data.ByteString.ByteString](nb 是严格args :: B.ByteString字节串的列表,而不是您在其他地方使用的惰性字节串)但是出于某种原因您期望的模式匹配。

您应该以与在 a 上进行模式匹配[ByteString]相同的方式进行模式匹配[String]:它们都是列表。

将 args 转换[B.ByteString]map (B.fromChunks . return) args.

于 2012-04-25T08:24:48.810 回答