Haskell导入导出

时间:2011-06-07 04:53:48

标签: haskell

我从零到Haskell,我有以下错误无法逃脱。基本上t2正在尝试使用另一个模块add1中定义的函数t1。所以

t1.hs-----
module t1 (add1) where
add1 :: Int -> Int
add1 x = x + 1

t2.hs-----
module t2 where
import t1
   add1 2

错误始终显示为parse error on input t2'`

有什么问题?

3 个答案:

答案 0 :(得分:6)

模块名称应该是大写的,您的t2.hs文件也有一些问题,我已对其进行了修改,因此您应该能够使用runghc t2.hs运行它并看到一些输出:

t1.hs

module T1 (add1) where
add1 :: Int -> Int
add1 x = x + 1

t2.hs

module T2 where
import T1
main = do
  let x = add1 2
  putStrLn (show x)

答案 1 :(得分:4)

它必须是大写的。使t1 T1和t2 T2,它将起作用

t1.hs-----
module T1 (add1) where
add1 :: Int -> Int
add1 x = x + 1

t2.hs-----
module T2 where
import T1
   add1 2

答案 2 :(得分:1)

我想你想加载模块t2,它应该显示“3”?

这不起作用,因为您必须加载模块然后执行命令。您可以在解释器shell中加载模块t1并执行“add1 2”,也可以在t2中定义一个名为“add1 2”的新函数:

t2.hs-----
module t2 where
import t1 
   add1to2 = add1 2

现在你可以调用funktion add1to2。

托拜厄斯