我是 Haskell 新手 :)。我想从 IO 中迭代读取一个元素,如果存在,则从列表中删除该元素。在其他情况下,重复读取直到输入的元素在列表中。我有以下代码:
import Data.List (delete)
main = do
let initialDieRoll = [23,45,98,34]
strength <- askStrength
let dieRollWithoutStrength = removeStrengthFromDieRollLoop strength initialDieRoll
print dieRollWithoutStrength
removeStrengthFromDieRollLoop :: Int -> [Int] -> [Int]
removeStrengthFromDieRollLoop = removeStrengthFromDieRoll
removeStrengthFromDieRoll :: Int -> [Int] -> [Int]
removeStrengthFromDieRoll strength dieRoll = do
if strength `elem` dieRoll then
delete strength dieRoll
else do
"Please, choose an element from the initial die roll"
newStrength <- askStrength
removeStrengthFromDieRollLoop askStrength dieRoll
askStrength :: IO Int
askStrength = do
putStr "Strength : "
readLn::IO Int
但是,当我请求新的强度时,出现以下错误:
Couldn't match type ‘IO’ with ‘[]’
Expected: [Int]
Actual: IO Int
In a stmt of a 'do' block: newStrength <- askStrength
In the expression:
do "Please, choose an element from the initial die roll"
newStrength <- askStrength
removeStrengthFromDieRollLoop askStrength dieRoll
In a stmt of a 'do' block:
if strength `elem` dieRoll then
delete st
最好的处理方法是什么?如何修复错误?如果值不在初始列表中,每次请求值循环的最佳方法是什么?谢谢。