相关疑难解决方法(0)

用参数调用main函数

我从文件中读取有问题.每当我需要从文件中读取时,我都会这样做:

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的参数必须包含文件的名称.我不确定我是否理解这一点,我不知道如何继续.我会感激一些帮助.

haskell

15
推荐指数
2
解决办法
6761
查看次数

使ReadArgs 1.0使用单个参数

使用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)

我的问题是双重的:

  1. readArgs工作怎么样?
  2. 如何调整该库以允许它使用单个参数?

NB版1.1的ReadArgs消除了这个"错误"; 看评论.

haskell command-line-arguments

5
推荐指数
2
解决办法
170
查看次数

标签 统计

haskell ×2

command-line-arguments ×1