【问题标题】:How do I make the recursive function work?如何使递归函数工作?
【发布时间】:2015-05-23 17:53:09
【问题描述】:

我这里有一些简单的haskell 代码两个骰子并总结它们的值。我想利用这个价值在垄断板上采取行动。我决定为此使用递归函数,并为第一步使用另一个函数。问题是我声明递归函数采用 IO Int 类型参数并返回相同类型的结果。我该如何解决这个问题。 注意:我是 Haskell 的新手。

import System.Random

firstMove :: IO Int
firstMove = do
  start   <- randomRIO (0, 0)
  current <- sumOfTwoDices
  return (start + current)

makeAmove :: IO Int -> IO Int
makeAmove firstMove = firstMove
makeAmove pos = do
   step <- sumOfTwoDices
   return (pos + step)

rollDice :: IO Int
rollDice = randomRIO (1, 6)

sumOfTwoDices :: IO Int
sumOfTwoDices = do
    dice1 <- rollDice
    dice2 <- rollDice
    return (dice1 + dice2)

即使我指定它接收 IO Int 类型,它也将 pos 视为 Int。帮助表示赞赏

【问题讨论】:

  • 这是整个代码

标签: haskell


【解决方案1】:

错误实际上是posIO Int,即使+ 要求它的两个参数都是Ints。您可以使用fmap 解除对pos 的添加:

makeAmove :: IO Int -> IO Int
makeAmove pos = do
   step <- sumOfTwoDices
   fmap (+ step) pos

【讨论】:

  • Hmm.. 我似乎收到警告,提示模式匹配在您的 sn-p 的第二行重叠。我应该担心吗?除此之外它很好
  • @user3402719 - 你没有在我的代码中得到它,但是在你原来的makeAmove firstMove = firstMove 中是的,我认为你不想要的总是匹配的。目前尚不清楚您要在那里做什么。
【解决方案2】:

只是为了让你得到一些东西工作我把它清理了一下:

import System.Random

firstMove :: IO Int
firstMove = do
  -- randomRIO (0,0) will allways be 0 so start=0
  let start = 0
  current <- sumOfTwoDices
  return (start+current)

makeAmove :: IO Int -> IO Int
makeAmove pos = do
   cur  <- pos
   step <- sumOfTwoDices
   return (cur+step)

rollDice :: IO Int
rollDice = randomRIO (1,6)

sumOfTwoDices :: IO Int
sumOfTwoDices = do
    dice1 <- rollDice
    dice2 <- rollDice
    return $ dice1+dice2

但正如我在上一个问题中告诉你的那样 - 你不应该在 IO monad 中做所有事情 - 重写它看起来像这样:

module Monoploy where

import System.Random

type Position = Int
type Steps = Int

start :: Position
start = 0

end :: Position
end = 79 -- please note: I don't know the number of tiles on monopoly so the 79 (80 positions) will obvious be wrong ;)

-- moves around the board / wraps back to start
move :: Steps -> Position -> Position
move s p = (p+s) `mod` (end+1)

randomMove :: Position -> IO Position
randomMove pos = do
  steps <- sumOfTwoDices
  return $ move steps pos

rollDice :: IO Steps
rollDice = randomRIO (1,6)

sumOfTwoDices :: IO Int
sumOfTwoDices = do
  dice1 <- rollDice
  dice2 <- rollDice
  return $ dice1+dice2

sampleMoveFromStart :: IO Position
sampleMoveFromStart = randomMove start

【讨论】:

    猜你喜欢
    • 2012-02-24
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2019-03-05
    相关资源
    最近更新 更多