首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行并编译“Hello,World!”在Haskell

运行并编译“Hello,World!”在Haskell
EN

Stack Overflow用户
提问于 2016-09-08 05:55:44
回答 1查看 2.9K关注 0票数 8

我是Haskell的新手,在运行以下代码时遇到问题:

代码语言:javascript
复制
module Main (
main
) where 
main = putStrLn "Hello, world!"

SublimeHaskell尝试使用以下命令编译和运行上面的代码

代码语言:javascript
复制
runhaskell hello.hs

,它返回错误

代码语言:javascript
复制
hello.o: getModificationTime: invalid argument (The system cannot find the file specified.)

我还尝试使用以下命令运行它

代码语言:javascript
复制
ghc --make hello.hs

带着同样的错误。当我尝试在使用命令行运行之前手动编译代码时

代码语言:javascript
复制
ghc -c hello.hs

我得到一个不同的错误:

代码语言:javascript
复制
CreateDirectory ".": invalid argument (Cannot create a file when that file already exists.)

另一方面,我可以通过REPL运行程序而不会出现问题:

代码语言:javascript
复制
ghci
GHCi, version 8.0.1: http://www.haskel.org/ghc/  :? for help
Prelude> putStrLn "Hello, world!"
Hello, world!

如果相关,我使用的是Windows7,并且我使用Haskell平台安装程序安装了GHC。

*edit使用-v标志运行上述命令会产生以下结果:

代码语言:javascript
复制
ghc -v hello.hs

Glasgow Haskell Compiler, Version 8.0.1, stage 2 booted by GHC version     7.10.2
Using binary package database: C:\Program Files\Haskell      Platform\8.0.1\lib\package.conf.d\package.cache
loading package database C:\Program Files\Haskell    Platform\8.0.1\lib\package.conf.d
wired-in package ghc-prim mapped to ghc-prim-0.5.0.0
wired-in package integer-gmp mapped to integer-gmp-1.0.0.1
wired-in package base mapped to base-4.9.0.0
wired-in package rts mapped to rts
wired-in package template-haskell mapped to template-haskell-2.11.0.0
wired-in package ghc mapped to ghc-8.0.1
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags:
loading package database C:\Program Files\Haskell     Platform\8.0.1\lib\package.conf.d
wired-in package ghc-prim mapped to ghc-prim-0.5.0.0
wired-in package integer-gmp mapped to integer-gmp-1.0.0.1
wired-in package base mapped to base-4.9.0.0
wired-in package rts mapped to rts-1.0
wired-in package template-haskell mapped to template-haskell-2.11.0.0
wired-in package ghc mapped to ghc-8.0.1
wired-in package dph-seq not found.
wired-in package dph-par not found.
*** Chasing dependencies:
Chasing modules from: *hello.hs
*** Deleting temp files:
Deleting:
*** Deleting temp dirs:
Deleting:
hello.o: getModificationTime: invalid argument (The system cannot find the file specified.)

ghc -c -v hello.hs

Glasgow Haskell Compiler, Version 8.0.1, stage 2 booted by GHC version 7.10.2
Using binary package database: C:\Program Files\Haskell     Platform\8.0.1\lib\package.conf.d\package.cache
loading package database C:\Program Files\Haskell    Platform\8.0.1\lib\package.conf.d
wired-in package ghc-prim mapped to ghc-prim-0.5.0.0
wired-in package integer-gmp mapped to integer-gmp-1.0.0.1
wired-in package base mapped to base-4.9.0.0
wired-in package rts mapped to rts
wired-in package template-haskell mapped to template-haskell-2.11.0.0
wired-in package ghc mapped to ghc-8.0.1
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags:
*** Checking old interface for Main:
*** Parser [Main]:
!!! Parser [Main]: finished in 0.00 milliseconds, allocated 0.067 megabytes
*** Renamer/typechecker [Main]:
!!! Renamer/typechecker [Main]: finished in 46.80 milliseconds, allocated  15.720 megabytes
*** Desugar [Main]:
Result size of Desugar (after optimization)
= {terms: 13, types: 6, coercions: 0}
!!! Desugar [Main]: finished in 0.00 milliseconds, allocated 0.204 megabytes
*** Simplifier [Main]:
Result size of Simplifier iteration=1
= {terms: 17, types: 8, coercions: 0}
Result size of Simplifier = {terms: 17, types: 8, coercions: 0}
!!! Simplifier [Main]: finished in 0.00 milliseconds, allocated 0.171  megabytes
*** CoreTidy [Main]:
Result size of Tidy Core = {terms: 17, types: 8, coercions: 0}
!!! CoreTidy [Main]: finished in 0.00 milliseconds, allocated 2.558  megabytes
*** Deleting temp files:
Deleting:
*** Deleting temp dirs:
Deleting:
CreateDirectory ".": invalid argument (Cannot create a file when that file already exists.)

环顾四周,似乎列出的两个未找到的包(dph-seqdph-par)已被弃用。

EN

回答 1

Stack Overflow用户

发布于 2017-08-19 10:33:43

GHC要求文件的名称与模块的名称相同,以便对其进行编译。但是,无论文件名如何,任何模块都可以加载到GHCi中。这就是为什么repl可以工作,而编译不能。

因此,在您的示例中,应将文件重命名为Main.hs

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39379600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档