开发者

Haskell import and export

I am from zero to Haskell, and I have the followin开发者_如何学运维g error that can't get away. Basically t2 is trying to use the function add1 defined in the other module t1. So

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

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

The error always says parse error on inputt2'`

What's the problem?


Module names should be uppercase, your t2.hs file also has some issues, I've modified it so you should be able to run it with runghc t2.hs and see some output:

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)


It needs to be uppercase. Make t1 T1, and t2 T2, and it would work

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

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


I guess you want to load module t2 and it should display "3"?

This doesnt work, cause you have to load a module and then execute a command. Either you can load module t1 and execute "add1 2" in your interpreter shell or you can define a new function in t2 which calls "add1 2":

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

Now you can call the funktion add1to2.

Tobias

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜