Haskell中的Filename参数

时间:2016-11-30 16:32:56

标签: haskell

这是怎么回事:

module Main where

import System.Environment (getArgs)
import qualified Data.ByteString.Char8 as B
import Data.ByteString.Char8 (ByteString)

type Field = ByteString
type Row = [Field]
type CSV = [Row]

main :: IO ()
main = do
    contents <- B.readFile "test.csv"
    print (parseCSV contents)

-- used with "./myprogram" to read "test.csv"

但如果我用"test.csv"中的命令行参数替换main,那就不行了:

-- ... same as before ...

main :: IO ()
main = do
    contents <- B.readFile $ head getArgs
    print (parseCSV contents)

-- used with "./myprogram test.csv" to read "test.csv"

对于后者,我在编译时遇到了这个错误:

csv.hs:20:35: error:
    • Couldn't match expected type ‘[FilePath]’
                  with actual type ‘IO [String]’
    • In the first argument of ‘head’, namely ‘getArgs’
      In the second argument of ‘($)’, namely ‘head getArgs’
      In a stmt of a 'do' block: contents <- B.readFile $ head getArgs

编辑 - 我最初省略了代码的一部分,我认为没有必要理解这个问题。现在已经纠正了。

1 个答案:

答案 0 :(得分:4)

请记住getArgs :: IO [String]也是monadic,因此您需要先在 public void OnNavigatingFrom(FirstFloor.ModernUI.Windows.Navigation.NavigatingCancelEventArgs e) { TaskEx.Run(() => { //Replace Sleep call by the async command execution System.Threading.Thread.Sleep(5000); }).Wait();} 块中提取它,然后才能找到它的头部:

do