我是Haskell的新手,當我嘗試配置我碼。我明白main()中的所有指令都需要是IO(),並且發生錯誤是因爲我使用的函數之一(在Graphics.Gloss.Interface.IO.Animate中)沒有返回IO()。我想用光澤包顯示遺傳算法的結果。 這裏是我的代碼:未能與實際類型'(控制器 - > IO()) - > IO()'預期類型'IO()'
module Main where
import Prelude as P
import Control.Monad.Random as Rand
import Data.Functor
import Data.IORef
import Graphics.Gloss.Interface.IO.Animate
import Graphics.Solution
import Graphics.Plot
import Args
import Task
import Parsing
import Genetic
import Control.Concurrent.Async
import Control.Concurrent.STM.TChan
import Control.Monad.STM
import Control.Arrow (first)
main :: IO()
main = do
args <- parseOptions
opts <- loadEvolOptions (evolOptionsFileName args)
gen <- newStdGen
[email protected](Task _ twrs _ _) <- loadTask (inputFileName args) (fitnessFuncFileName args)
chan <- newTChanIO
asolution <- async $ solve chan gen opts task
dataRef <- newIORef []
finalSolutionRef <- newIORef Nothing
animateIO mode white $ const $ do
mfinsol <- readIORef finalSolutionRef
case mfinsol of
Just solution -> do
samples <- readIORef dataRef
return $ solutionPicture task solution (fitnessPlot samples)
Nothing -> do
msolution <- poll asolution
case msolution of
Nothing -> do
mv <- atomically $ tryReadTChan chan
case mv of
Nothing -> return()
Just v -> modifyIORef dataRef (++[v])
samples <- readIORef dataRef
return $ fitnessPlot samples
Just esol -> case esol of
Left e -> fail $ show e
Right solution -> do
saveResult (outputFileName args) (filterTowers solution twrs)
writeIORef finalSolutionRef (Just solution)
samples <- readIORef dataRef
return $ solutionPicture task solution (fitnessPlot samples)
where mode = InWindow "test_genetic_al" (1280, 1024) (10, 10)
fitnessPlot ds = translate (-300) (-200) $ scale 600 600 $ plot "generation" "fitness" $ first fromIntegral <$> ds
這是我的了:
Couldn't match expected type ‘IO()’
with actual type ‘(Controller -> IO()) -> IO()’
In a stmt of a 'do' block:
animateIO mode white
$ const
$ do { mfinsol <- readIORef finalSolutionRef;
case mfinsol of {
Just solution -> do { ... }
Nothing -> do { ... } } }
In the expression:
do { args <- parseOptions;
opts <- loadEvolOptions (evolOptionsFileName args);
gen <- newStdGen;
[email protected](Task _ twrs _ _) <- loadTask
(inputFileName args) (fitnessFuncFileName args);
.... }
In an equation for ‘main’:
main
= do { args <- parseOptions;
opts <- loadEvolOptions (evolOptionsFileName args);
gen <- newStdGen;
.... }
where
mode = InWindow "test_genetic_al" (1280, 1024) (10, 10)
fitnessPlot ds
= translate (- 300) (- 200)
$ scale 600 600
$ plot "generation" "fitness" $ first fromIntegral <$> ds
我已經在谷歌搜索我的問題和StackOverflow上了這麼多次,但仍然無法找到一個解決方案錯誤。請幫幫我。
P/S:這是Graphics.Gloss引導線:https://hackage.haskell.org/package/gloss-1.11.1.1/docs/Graphics-Gloss-Interface-IO-Animate.html
爲我愚蠢的問題再次對不起,以後我給Lazersmoke的建議(你可以在評論區見下文),我得到了另一個錯誤與我要求的錯誤非常類似:
我改了行:animateIO模式白色$常量$千萬
分爲:animateIO模式白(_ - >收益率())$常量$做
Couldn't match type ‘Picture’ with ‘()’
Expected type: Controller -> IO()
Actual type: Controller -> IO Picture
In the second argument of ‘($)’, namely
‘const
$ do { mfinsol <- readIORef finalSolutionRef;
case mfinsol of {
Just solution -> do { ... }
Nothing -> do { ... } } }’
In a stmt of a 'do' block:
animateIO mode white (\ _ -> return())
$ const
$ do { mfinsol <- readIORef finalSolutionRef;
case mfinsol of {
Just solution -> do { ... }
Nothing -> do { ... } } }
In the expression:
do { args <- parseOptions;
opts <- loadEvolOptions (evolOptionsFileName args);
gen <- newStdGen;
[email protected](Task _ twrs _ _) <- loadTask
(inputFileName args) (fitnessFuncFileName args);
.... }
錯誤只是意味着你忘了提供一個參數,在這種情況下,'Controller - > IO()'函數。 – ocramz
那麼我該如何解決這個問題,請問你能告訴我方式嗎? –
他所說的,即「回撥顯示控制器」。正如你在你鏈接的文檔中看到的那樣。你需要類似'animateIO mode white(\ _ - > return())$ const $ do'來滿足類型,儘管你可能需要根據你的意圖提供一個實際的回調,以使它正常工作。 – Lazersmoke