【问题标题】:Simple haskell program doesnt compile with GHCi简单的 haskell 程序不能用 GHCi 编译
【发布时间】:2012-11-20 12:06:59
【问题描述】:

考虑下面是我的代码

 import Data.Char
 import Data.List

-- square
square :: Int -> Int
square n = n * n

虽然这个方形函数看起来整洁清晰,但当我运行它时

ghci ./square.hs

返回

[1 of 1] Compiling Main             ( ./LabSheet1-solns.hs, interpreted )

./LabSheet1-solns.hs:5:1:
    parse error on input `square'
Failed, modules loaded: none.

我的解释器应该有问题。

我在 OS X 10.8.1 上运行

$ ghci --version
The Glorious Glasgow Haskell Compilation System, version 7.4.2

有什么线索吗?

【问题讨论】:

  • 假设导入在您的实际代码文件中正确缩进,这对我来说编译得很好。
  • @JaniHartikainen 为什么你会假设当给定的代码完全产生 OP 所说的错误消息时?
  • @sepp2k 因为很多人无法正确使用 SO 帖子编辑器。这就是为什么它也是评论而不是答案。

标签: haskell ghci


【解决方案1】:

您的import 语句缩进一个空格。这基本上将“一个空格”设置为文件的基本缩进级别。由于您对square 的定义没有缩进一个空格,因此会出现语法错误。

要解决此问题,请将所有行缩进一个空格,或者最好不要缩进 import 语句。

【讨论】:

    【解决方案2】:

    修正缩进:

    import Data.Char
    import Data.List
    
    -- square
    square :: Int -> Int
    square n = n * n
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多