【发布时间】:2014-11-10 16:03:17
【问题描述】:
我在 Haskell 中有这些功能:
type World = [String]
look :: World -> IO World
look w = do
putStrLn $ "You are here " ++ show w
return w
test w = do
words <- fmap words getLine
case words of
("quit":_) -> return ()
("look":_) -> do w' <- area w'; describe w'
("remove":item:_) -> do w' <- removeItem item w; loop w'
_ -> do putStrLn "Wrong input"; test w
area::String->String
area place =
return place
describe:: String ->String
describe place =
case place of
"usa" -> putStrLn "It is usa"
"uk" -> putStrLn "It is uk"
"aur" -> putStrLn "It is aus"
main = do
let world0 = ["ht", "alt"]
let place = ["usa"]
area place
print place
test world0
排队
("look":_) -> do w' <- area w'; describe w'
我想调用“区域”函数来返回在 main 中输入的地点,并调用 describe 函数来返回该地点的描述。我该怎么做?
【问题讨论】:
-
这是一个用 Haskell 编写的简单冒险游戏,可能有助于程序组织:link
-
不应该
area和describe分别具有String -> IO String和String -> IO ()类型吗?你在这些函数中使用了return和putStrLn,所以它们必须返回一个单子值
标签: haskell