无法在Haskell中编译问候世界

时间:2019-11-27 08:58:55

标签: haskell compilation

我刚开始使用“使用Haskell编程”一书学习haskell。我要尝试做的第一个想法就是简单地从本书中编译世界示例。 我在hello.hc文件中的代码是:

main = do
    printStrLn "Hello!"

我使用命令

运行ghc
ghc hello.hc

但是我得到的只是一个错误!

hello.hc:1:1: error:
     warning: data definition has no type or storage class
     main = do
     ^

hello.hc:1:1: error:
     warning: type defaults to 'int' in declaration of 'main' [-Wimplicit-int]

hello.hc:1:8: error:
     error: expected expression before 'do'
     main = do
            ^
`gcc.exe' failed in phase `C Compiler'. (Exit code: 1)

我的Haskell平台版本为8.01,

1 个答案:

答案 0 :(得分:11)

hello.hc重命名为hello.hs并尝试

ghc hello.hs
再次

通话

ghc --help

我的编译器上的解释(省略了部分用法):

For each input file, the phase to START with is determined by the
file's suffix:

    - .lhs  literate Haskell         unlit
    - .hs   plain Haskell            ghc
    - .hc   C from the Haskell compiler  gcc
    - .c    C not from the Haskell compiler  gcc
    - .s    assembly language        as
    - other passed directly to the linker    ld

The phase at which to STOP processing is determined by a command-line
option:

    -E      stop after generating preprocessed, de-litted Haskell
             (used in conjunction with -cpp)
    -C      stop after generating C (.hc output)
    -S      stop after generating assembler (.s output)
    -c      stop after generating object files (.o output)

如果您尝试使用test.hc来编译gcc,那只是从C编译器得到的错误消息。

[nix-shell:~]$ gcc -x c -std=c99 -o test test.hc
test.hc:1:1: warning: data definition has no type or storage class
 main = do
 ^~~~
test.hc:1:1: warning: type defaults to ‘int’ in declaration of ‘main’ [-Wimplicit-int]
test.hc:1:8: error: expected expression before ‘do’
 main = do
        ^~