haskell - 在 Haskell 中运行和编译 'Hello, World!'

标签 haskell ghc

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

module Main (
main
) where 
main = putStrLn "Hello, world!"

SublimeHaskell 尝试使用
runhaskell hello.hs

返回错误
hello.o: getModificationTime: invalid argument (The system cannot find the file specified.)

我也尝试过使用
ghc --make hello.hs

有同样的错误。当我尝试在使用命令行运行之前手动编译代码时
ghc -c hello.hs

我得到一个不同的错误:
CreateDirectory ".": invalid argument (Cannot create a file when that file already exists.)

另一方面,我可以毫无问题地通过 REPL 运行程序:
ghci
GHCi, version 8.0.1: http://www.haskel.org/ghc/  :? for help
Prelude> putStrLn "Hello, world!"
Hello, world!

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

***使用 -v 编辑运行上述命令flag 给出以下结果:
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 )已被弃用。

最佳答案

GHC 要求文件与模块命名相同才能编译它。
但是,无论文件名如何,任何模块都可以加载到 GHCi 中。这就是为什么 repl 工作但编译没有。

因此,在您的情况下,您应该重命名文件 Main.hs

关于haskell - 在 Haskell 中运行和编译 'Hello, World!',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39379600/

相关文章:

haskell - 计算haskell中元组列表中值的总和

list - Haskell 回溯递归

haskell - 为什么 GHCi 不显示与 GHC 相同的错误消息

haskell - 什么是 Haskell 标准库?

haskell - GHC - Haskell 中的 Infixl 声明

haskell - 无法在 Haskell 中链接 FFI 库

Haskell:没有因使用 `==' 而产生的 (Eq a) 实例 在表达式中:(b == c)

haskell - 使用haskell过滤以 "ed"或 "ing"结尾的单词

haskell - 频繁的 GC 会阻止并行运行 Spark

haskell - Haskell 中的通用型变压器