英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论