Open
Description
除算の小数部分を求めたくて以下の関数syoussと定数n,mを用意したのですが,
syouss n m = (n/m) - (fromIntegral (n`div`m))
n = 10
m = 3
ghciで:loadで読み込んで実行しようとするとエラーに
ghci> syouss n m
<interactive>:3:1:
No instance for (Fractional Integer) arising from a use of ‘syouss’
In the expression: syouss n m
In an equation for ‘it’: it = syouss n m
nとmの型を調べるとなぜかIntegerでした.
ghciでlet a = 10
のようにすると型はa :: Num a => aとなりましたがsyoussはまだ実行できませんでした.
ghci> let a = 10
ghci> let b = 3
ghci> :t a
a :: Num a => a
ghci> syouss a b
<interactive>:8:1:
No instance for (Show a0) arising from a use of ‘print’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Show Double -- Defined in ‘GHC.Float’
instance Show Float -- Defined in ‘GHC.Float’
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
...plus 44 others
In a stmt of an interactive GHCi command: print it
試しにghciで(a/b) - (fromIntegral (a
divb))
を直接実行すると計算できました.
ghci> (a/b) - (fromIntegral (a`div`b))
0.3333333333333335
型を見てみましたがなぜか異なっていました
ghci> :t (a/b) - (fromIntegral (a`div`b))
(a/b) - (fromIntegral (a`div`b)) :: Fractional a => a
ghci> :t syouss
syouss :: (Integral a, Fractional a) => a -> a -> a
長くなってしまいまいましたが,以上より2つの疑問点があります.
- 定数の型がファイルからの:loadとghciでのletで何故型が違うのか
- 数式
(a/b) - (fromIntegral (a
divb))
を関数定義した時と直接実行した時に何故型が変わるのか