2
我有這樣的代碼在函數內部副作用... -> IO()
這段代碼如何進入無限循環?又名如何在Haskell中實現一個計數器?
let index_player = (-1)
dronePositionByPlayer <- replicateM nb_players $ do
let index_player = index_player + 1
dronePositions <- replicateM nb_drones $ do
input_line <- getLine
let input = words input_line
let dx = read (input!!0) :: Int
let dy = read (input!!1) :: Int
let dronePosition = DronePosition (Position dx dy) index_player
hPutStrLn stderr $ "position = " ++ show dronePosition
return (dronePosition)
return (dronePositions)
當我執行它,在解析的輸入數據(這是包含每個的x和y位置的幾行)我對參考標準此跟蹤錯誤輸出:
position = DronePosition (Position 703 892) Answer: <<loop>>
很明顯,它可以讀取第一的位置,但隨後它會在無限循環,可能是想顯示的字段index_player
。
DronePosition
和Position
是簡單的代數數據類型:
data Position = Position Int Int deriving Show
data DronePosition = DronePosition Position Drone deriving Show
是形成不良的在我的代碼是什麼?