gpt4 book ai didi

haskell - 可以在不使用可变变量的情况下对其进行编码吗?

转载 作者:行者123 更新时间:2023-12-01 07:51:26 25 4
gpt4 key购买 nike

考虑以下(非常乏味的)游戏:
- 玩家 A 想一个介于 1 到 100 之间的数字。
- 玩家 B 可以尝试 5 次猜数字。玩家 A 会以“太大”、“太小”或“正确”来回应每个猜测。

我想在 Haskell 中模拟这个,这当然是微不足道的。不过,为了让事情变得有趣,我想以玩家 B 不能“作弊”的方式编写代码。这意味着两件事:
- 不允许玩家 B 代码看到密码的正确值。玩家 A 代码为玩家 B 代码提供了一个用于检查其猜测的函数。
- Player B 代码调用该函数的次数不得超过五次。不知何故,播放器 A 代码必须记录函数被调用的次数。

这在具有私有(private)可变变量的 OO 语言中很容易实现。

在 Haskell 中,我使用 IORef 对其进行编码以记录调用次数。没关系,我认为我的解决方案是正确的。但我的问题是:

“这可以在没有 IORef 或类似的情况下在 Haskell 中完成吗?有没有我错过的纯功能解决方案?”

这是我的 Haskell 代码:

import Data.IORef (newIORef, readIORef, writeIORef)
import System.Random (randomRIO)

lowest = 1
highest = 100
maxtries = 5

playerA :: IO (Int -> IO (Maybe Ordering))
playerA = do
secret <- randomRIO (lowest, highest)
print ("Secret", secret)
tries <- newIORef maxtries
return $ \ guess -> do
t <- readIORef tries
if t == 0 then
return Nothing
else do
writeIORef tries $ t - 1
return $ Just $ guess `compare` secret

playerB :: (Int -> IO (Maybe Ordering)) -> Int -> Int -> IO ()
playerB guessfunc lowbound highbound = do
let guess = (lowbound + highbound) `div` 2
response <- guessfunc guess
print (lowbound, highbound, guess, response)
case response of
Just GT -> playerB guessfunc lowbound (guess - 1)
Just LT -> playerB guessfunc (guess + 1) highbound
Just EQ -> putStrLn "Player B wins"
Nothing -> putStrLn "Player B loses"

main :: IO ()
main = do
guessfunc <- playerA
playerB guessfunc lowest highest

最佳答案

IO 中无需执行此操作,您不妨使用纯状态单子(monad):

import Control.Monad.Trans.State
import System.Random (randomRIO)

maxtries = 5

playerA :: IO (Int -> State Int (Maybe Ordering))
playerA = do
secret <- randomRIO (1,100)
return $ \guess -> state $ \tries
-> if tries < maxtries then (Just $ guess`compare`secret, tries+1)
else (Nothing, tries)

playerB :: Monad m => (Int -> m (Maybe Ordering)) -> Int -> Int -> m (Maybe Int)
playerB guessfunc lowbound highbound = do
let guess = (lowbound + highbound)`div`2
response <- guessfunc guess
case response of
Just GT -> playerB guessfunc lowbound (guess - 1)
Just LT -> playerB guessfunc (guess + 1) highbound
Just EQ -> return $ Just guess
Nothing -> return Nothing

main = do
pa <- playerA
print . (`evalState`0) $ playerB pa 1 100

请注意 playerB不知道 monad 是 State Int .这很重要,因此她不能通过在调用 guessfunc 之间操纵状态变量来作弊。 .

关于haskell - 可以在不使用可变变量的情况下对其进行编码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48422051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com