无限循环在哪里?

huangapple go评论54阅读模式
英文:

Where ist the infinity loop?

问题

有人愿意告诉我这个函数有什么问题吗?

这个复杂的函数有问题:

buildBoard :: String -> Board
buildBoard xs = buildBoard' ("+" ++ xs ++ "+") []

buildBoard' :: String -> [Cell] -> Board
buildBoard' [] _ = [[]]
buildBoard' xs result
  | head xs == ',' && xs !! 1 == ',' = buildBoard' (tail xs) (Empty : result)
  | head xs == 'w' =
      buildBoard' (drop (getFirstNonNumber 0) xs) (Piece White intConversion : result)
  | head xs == 'b' =
      buildBoard' (drop (getFirstNonNumber 0) xs) (Piece White intConversion : result)
  | head xs == '/' && xs !! 1 == ',' = buildBoard' (tail xs) (Empty : result)
  | head xs == ',' && xs !! 1 == '/' = buildBoard' (tail xs) (Empty : result)
  | head xs == '+' && xs !! 1 == ',' = buildBoard' (tail xs) (Empty : result)
  | head xs == '+' && xs !! 1 `elem` ['b', 'w'] = buildBoard' (tail xs) result
  | head xs == ',' && xs !! 1 == '+' = buildBoard2 (reverse (Empty : result))
  | head xs == '+' = buildBoard2 (reverse result)
  | otherwise = buildBoard2 (reverse result)
  where intConversion = read [xs !! 1 .. xs !! (getFirstNonNumber 0 - 1)] :: Int
        getFirstNonNumber num = if xs !! num `elem` [',', '/'] then num else getFirstNonNumber (num + 1)

separate :: Int -> [a] -> [[a]]
separate _ [] = []
separate n l
  | n > 0 = take n l : separate n (drop n l)
  | otherwise = []

buildBoard2 :: [Cell] -> Board
buildBoard2 = separate 9

一些关于Cell、Board等的额外信息:

data Player = Black | White deriving Show
data Cell = Piece Player Int | Empty deriving Show
data Pos = Pos { col :: Char, row :: Int } deriving Show
type Board = [[Cell]]
英文:

somebody care to tell me where the problem with this function is?

Something is wrong with this complex:

buildBoard :: String -> Board
buildBoard xs = buildBoard' ("+" ++ xs ++ "+") []
buildBoard' :: String -> [Cell] -> Board
buildBoard' [] _ = [[]] 
buildBoard' xs result
|head xs == ',' && xs!!1 == ',' = buildBoard' (tail xs) (Empty:result)
|head xs == 'w' =
buildBoard' (drop (getFirstNonNumber 0) xs) (Piece White intConversion : result)
|head xs == 'b' =
buildBoard' (drop (getFirstNonNumber 0) xs) (Piece White intConversion : result)
|head xs == '/' && xs!!1 == ',' = buildBoard' (tail xs) (Empty:result)
|head xs == ',' && xs!!1 == '/' = buildBoard' (tail xs) (Empty:result)
|head xs == '+' && xs!!1 == ',' = buildBoard' (tail xs) (Empty:result)
|head xs == '+' && xs!!1 `elem` ['b','w'] = buildBoard' (tail xs) result
|head xs == ',' && xs!!1 == '+' = buildBoard2 (reverse (Empty:result))
|head xs == '+' = buildBoard2 (reverse result)
|otherwise = buildBoard2 (reverse result)
where intConversion = read [xs!!1..xs!!(getFirstNonNumber 0 -1)] :: Int
getFirstNonNumber num = if xs!!num `elem` [',','/']  then num else getFirstNonNumber num+1
separate :: Int -> [a] -> [[a]]
separate _ [] = []
separate n l
| n > 0 = take n l : separate n (drop n l)
| otherwise = []
buildBoard2 :: [Cell] -> Board
buildBoard2 = separate 9

some additional info about what Cell, Board etc is:

data Player = Black | White deriving Show
data Cell = Piece Player Int | Empty deriving Show
data Pos = Pos { col :: Char, row :: Int } deriving Show
type Board = [[Cell]]

答案1

得分: 5

这里至少有一个:

getFirstNonNumber num = if ... then num else getFirstNonNumber (num+1)

你几乎可以肯定最后一次调用应该是:

getFirstNonNumber (num+1)
英文:

Here, at least, is one:

getFirstNonNumber num = if ... then num else getFirstNonNumber num+1

You almost certainly meant that last call to be:

getFirstNonNumber (num+1)

huangapple
  • 本文由 发表于 2023年2月6日 21:55:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定