2017-10-04 11 views
1

はなぜリストを返す機能をHaskellのことができないと間違っている何

partin a = [floor a, a-floor a] 

エラー:

<interactive>:342:1: error: 
    • Ambiguous type variable ‘a0’ arising from a use of ‘print’ 
     prevents the constraint ‘(Show a0)’ from being solved. 
     Probable fix: use a type annotation to specify what ‘a0’ should be. 
     These potential instances exist: 
     instance Show Ordering -- Defined in ‘GHC.Show’ 
     instance Show Integer -- Defined in ‘GHC.Show’ 
     instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’ 
     ...plus 22 others 
     ...plus 16 instances involving out-of-scope types 
     (use -fprint-potential-instances to see them all) 
    • In a stmt of an interactive GHCi command: print it 
+4

これは、何かを印刷することを話していますが、何かを印刷しようとしているかはわかりません。あなたがやっていることの残りの部分を示してください。 – dfeuer

+0

実際に私が "part a = a - floor a"をするときにも同じエラーが出ます。コードはこの機能でのみ構成されています –

+3

GHCiプロンプトで、「対話型GHCiコマンドのstmt: 'print it'」のように、「part something」とタイプしました。 – HTNW

答えて

7

は、私はあなたのものの完全な範囲を見ることなく、完全な答えを与えることはできませんしかし、ここには、ほぼ確実に関与している明確な問題があります。あなたはfloorの種類は、あなたがインスタンスであることをaの種類を強制している、あなたはa - floor aを使用しているので(-)のタイプは

(-) :: Num a => a -> a -> a 

ある

floor :: (RealFrac a, Integral b) => a -> b 

ある

partin a = [floor a, a-floor a] 

を書きますの両方RealFracクラスIntegralクラス。しかし、標準ライブラリにはそのような型はありません(それほど意味がありません)。その結果、GHCは非常に限定されたデフォルトのコレクションからあなたのタイプを選択することはできません。物事はあなたが

partin a = [fromIntegral (floor a), a - fromIntegral (floor a :: Int)] 

を使用する場合は、より良い多くのことをうまくしかし、あなたは異なるの二つの成分に数を分割しようとしているので、それは本当に、ここリストを持つようにあまり意味がありませんのでご注意かもしれませんタイプ。

partin a = (floor a, a - fromIntegral (floor a :: Int)) 
関連する問題