Haskell IO共享参数

ark*_*ate 1 io syntax haskell

在Haskell中,是否可以将用户输入从一个IO功能共享到另一个?

例如,如果我有:

  main = do
         putStrLn "Give me a number!"
         my_stuff <- getLine 
         let nump = read (my_stuff)::Int
         another_function nump
Run Code Online (Sandbox Code Playgroud)

其中another_function也是带有do构造的IO函数.

another_function nump = do
                          putStrLn nump
                          putStrLn "Try again!"
                          main
Run Code Online (Sandbox Code Playgroud)

使得在幻想世界的Haskell解释我有我的头感觉.但是,在现实世界中:my_stuff在another_function中未绑定; 而在main中,my_stuff需要是IO t类型,但事实并非如此.

上面的代码(很可能)对Haskellers非常冒犯,但我希望它能传达出我的目标......

我该如何解决这个问题?

npo*_*cop 5

这段代码有效.这是你想要做的吗?如果没有,你能提供不起作用的代码吗?

main = do
    putStrLn "Give me a number!"
    my_stuff <- getLine 
    let nump = read (my_stuff)::Int
    another_function nump

another_function nump = do
    putStrLn $ show nump
    putStrLn "Try again!"
    main
Run Code Online (Sandbox Code Playgroud)