【问题标题】:calling a function within main = do haskell [closed]在 main = do haskell 中调用函数 [关闭]
【发布时间】:2021-12-06 16:48:33
【问题描述】:

**大家好,haskell 的新手,我想知道当我尝试在 main 中调用 play_game 函数时,我得到的这个错误究竟是什么解决的。 忽略守卫的下半部分,因为我只是用随机值与他们搞乱以试图解决问题。

haskell code here

**1:https://i.stack.imgur.com/AS4IN.png

{-# LANGUAGE ScopedTypeVariables #-}

【问题讨论】:

标签: haskell functional-programming


【解决方案1】:
  1. 您在 readLn 上的语法是错误的 - 正确的是: 某事

  2. 您的主要功能是 IO GHC.Types.Any 类型,简而言之:IO a 但您的功能 play_games 返回类型为 (Num a) => a 的东西。 do 表示法的最后一个语句必须始终是返回类型...(请记住:do 表示法只是用于 () 的语法 shugar

例如:

main = do
   a <- fs
   b <- as
   pure (f a)

类似于:

fs <*> as

所以在你的例子中,一个可行的解决方案是:

main :: IO Int
main = do
    putStrLn "Please input total number of games."
    numGames <- readLn :: IO Int
    player1 <- readLn :: IO Int
    player2 <- readLn :: IO Int
    print player1
    print player2
    return $ play_games player1 player2 numGames (0,0)

play_games :: (Eq a1, Eq a2, Num a1, Num a2, Num a3) => a1 -> a2 -> p -> (a4, a3) -> a3
play_games player1 player2 numGames score
    | player1 == 0&& player2 == 1 = snd score + 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 2013-09-27
    • 2015-01-24
    • 2019-04-25
    • 2012-12-06
    • 2021-05-17
    • 2021-11-20
    相关资源
    最近更新 更多