You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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> :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
Prelude> :t \a b -> (a/b) - (fromIntegral (a`div`b))
\a b -> (a/b) - (fromIntegral (a`div`b))
:: (Integral a, Fractional a) => a -> a -> a
で
*Main> :t syouss
syouss :: (Integral a, Fractional a) => a -> a -> a
と同じです。
そして、 Int の値が実行できない問題はこっちでも発生します。
*Main> (\a b -> (a/b) - (fromIntegral (a`div`b))) (10 :: Int) (3 :: Int)
<interactive>:17:12:
No instance for (Fractional Int) arising from a use of ‘/’
In the first argument of ‘(-)’, namely ‘(a / b)’
In the expression: (a / b) - (fromIntegral (a `div` b))
In the expression: \ a b -> (a / b) - (fromIntegral (a `div` b))
これはどうしてかと言えば Int だと Fractional に明示的に変換しないといけないからです。
除算の小数部分を求めたくて以下の関数syoussと定数n,mを用意したのですが,
ghciで:loadで読み込んで実行しようとするとエラーに
nとmの型を調べるとなぜかIntegerでした.
ghciで
let a = 10
のようにすると型はa :: Num a => aとなりましたがsyoussはまだ実行できませんでした.試しにghciで
(a/b) - (fromIntegral (a
divb))
を直接実行すると計算できました.型を見てみましたがなぜか異なっていました
長くなってしまいまいましたが,以上より2つの疑問点があります.
(a/b) - (fromIntegral (a
divb))
を関数定義した時と直接実行した時に何故型が変わるのかThe text was updated successfully, but these errors were encountered: