我的第一个Haskell程序

今天下午写了一个Haskell的hello world,结果不能运行:

module test(main) where
import System.IO

data Shape = Circle Float Float Float | Rectangle Float Float Float Float
surface :: Shape -> Float
surface (Circle _ _ r) = pi * r ^ 2
surface (Rectangle a b c d) = (a-c) * (b-d)
main :: IO()
main = do
print $ surface $ Circle 2.2 3.0 2.5
print $ surface $ Rectangle 1 2 4 9

编译报错

ghc -o test test.hs

test.hs:1:8: parse error on input ‘test’

貌似是因为module名字首字母要大些。
改了之后,在运行。还是有问题,这次是警告。
[quote]
ghc -o test test.hs
[1 of 1] Compiling Test ( test.hs, test.o )
Warning: output was redirected with -o, but no output will be generated
because there is no Main module.
[/quote]
我比较了网上的hello world例子发现,和我的不同之处在于它的没有定义module。
去掉module那行试一下
[quote]
[1 of 1] Compiling Main ( test.hs, test.o )
Linking test ...
[/quote]
这次正常了。
运行:
[quote]
./test
19.634954
21.0
[/quote]

最终版本的代码:

import System.IO

data Shape = Circle Float Float Float | Rectangle Float Float Float Float
surface :: Shape -> Float
surface (Circle _ _ r) = pi * r ^ 2
surface (Rectangle a b c d) = (a-c) * (b-d)
main :: IO()
main = do
print $ surface $ Circle 2.2 3.0 2.5
print $ surface $ Rectangle 1 2 4 9
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值