我从文件中读取有问题.每当我需要从文件中读取时,我都会这样做:
main = do x <- readFile "/tmp/foo.txt"
putStr x
Run Code Online (Sandbox Code Playgroud)
但是现在我希望这条道路成为一个论点,所以我尝试了下面的内容
main s = do x <- readFile s
putStr x
Run Code Online (Sandbox Code Playgroud)
它不起作用.我看到以下错误:
Couldn't match expected type `IO t0'
with actual type `FilePath -> IO ()'
In the expression: main
When checking the type of the function `main'
Run Code Online (Sandbox Code Playgroud)
我的作业是编写一个程序,程序必须包含一个main函数(因为它将被编译),而call的参数必须包含文件的名称.我不确定我是否理解这一点,我不知道如何继续.我会感激一些帮助.
使用ReadArgs包,它似乎不支持单参数情况.
{-# LANGUAGE ScopedTypeVariables #-}
import ReadArgs (readArgs)
main = do
(foo :: Int) <- readArgs
print foo
Run Code Online (Sandbox Code Playgroud)
错误是(使用版本1.0时):
No instance for (ReadArgs.ArgumentTuple Int)
arising from a use of `readArgs'
Run Code Online (Sandbox Code Playgroud)
我的问题是双重的:
readArgs工作怎么样?NB版1.1的ReadArgs消除了这个"错误"; 看评论.