2017-10-19 1 views
0

私は迷路ジェネレータを作成しており、迷路を印刷して視覚化したいと考えています。私は壁のタイプと、それらの壁のランダムな迷路を生成する関数を持っています。私はprettyPrint(weightedGrid 10 10)を呼び出すときモナドタイプを表示可能にする

import qualified Data.Graph.Inductive as Graph 
import   Data.Graph.Inductive (Gr, prettyPrint) 
data WeightedWall = WeightedWall (Int, Int, Int) Orientation deriving (Eq) 
weightedGrid :: MonadRandom m => Int -> Int -> Gr() (m WeightedWall) 

はしかし、私はこのエラーを取得する:

Ambiguous type variable ‘m0’ arising from a use of ‘prettyPrint’ 
    prevents the constraint ‘(Show 
           (m0 WeightedWall))’ from being solved. 
    Probable fix: use a type annotation to specify what ‘m0’ should be. 

私はこの問題を解決するために私のコードで何をしないのですか?

+3

MonadRandom'あなたは 'M'になりたいです何 '? – 4castle

+0

私はハスケルを初めて熟知しており、何をすべきか分からない。 weightedGridのランダム性を注入する関数は、それが役立つならControl.Monad.RandomのgetRandomRです。 – chronologos

+0

'm'のいくつかの値は[' Rand'](http://hackage.haskell.org/package/MonadRandom-0.5.1/docs/Control-Monad-Trans-Random-Lazy.html#t:ランド)または「IO」。コンパイラは、 'weightedGrid'を呼び出した結果に型の注釈を与えない限り、どちらを使うべきかを知りません。 – 4castle

答えて

1

あなたはプリティプリンタが型を持っていることになるでしょう:

prettyPrint :: WeightedWall -> String 

その後、あなたは、あなたのMonadRandomインスタンスからWeightedWallを摘み取るprettyPrintに渡し、その後、IOモナドでStringを印刷する必要があります。

getRandomRの機能は、MonadRandomのtypeclassのメンバーであるため、使用しているMonadRandomインスタンスを教えてください。私はIOと仮定しようとしています。それは、1つの石で2羽の鳥を殺すからです(無作為の源と印刷)。次のようにあなたのmain機能はなります:

main :: IO() 
main = do 
    ww <- weightedGrid 10 10 -- pluck weighted wall from MonadRandom instance IO 
    putStrLn $ prettyPrint ww 
0

を私がやってしまった:

pp :: WeightedWall -> String 
pp (WeightedWall (a, b, c) _) = show a ++ " " ++ show b ++ " " ++ show c 

main :: IO() 
main = do 
    ww <- mapM Data.Graph.Inductive.edgeLabel $ labEdges (weightedGrid 10 10) 
    forM_ (map pp ww) putStrLn 
関連する問題