ghc

What exactly is the kind “*” in Haskell?

我怕爱的太早我们不能终老 提交于 2020-05-20 11:14:09
问题 In Haskell, (value-level) expressions are classified into types , which can be notated with :: like so: 3 :: Int , "Hello" :: String , (+ 1) :: Num a => a -> a . Similarly, types are classified into kinds . In GHCi, you can inspect the kind of a type expression using the command :kind or :k : > :k Int Int :: * > :k Maybe Maybe :: * -> * > :k Either Either :: * -> * -> * > :k Num Num :: * -> Constraint > :k Monad Monad :: (* -> *) -> Constraint There are definitions floating around that * is

Could not find module ‘Control.Monad.State’ even though mtl is installed

时光总嘲笑我的痴心妄想 提交于 2020-05-13 06:19:12
问题 When I tried loading a module containing import Control.Monad.State I got Could not find module ‘Control.Monad.State’ Perhaps you meant Control.Monad.ST (from base-4.8.2.0) Control.Monad.ST.Safe (from base-4.8.2.0) Control.Monad.Fix (from base-4.8.2.0) Use -v to see a list of the files searched for. Failed, modules loaded: none. I installed the Haskell Platform, which has cabal and mtl installed. When I run cabal update and then cabal install mtl : Resolving dependencies... All the requested

Weird behavior of (^) in Haskell

拟墨画扇 提交于 2020-02-03 04:11:31
问题 Why does GHCi give incorrect answer below? GHCi λ> ((-20.24373193905347)^12)^2 - ((-20.24373193905347)^24) 4.503599627370496e15 Python3 >>> ((-20.24373193905347)**12)**2 - ((-20.24373193905347)**24) 0.0 UPDATE I would implement Haskell's (^) function as follows. powerXY :: Double -> Int -> Double powerXY x 0 = 1 powerXY x y | y < 0 = powerXY (1/x) (-y) | otherwise = let z = powerXY x (y `div` 2) in if odd y then z*z*x else z*z main = do let x = -20.24373193905347 print $ powerXY (powerXY x 12

Exponents defaulting to Integer

此生再无相见时 提交于 2020-01-15 09:54:30
问题 I use (^) :: (Num a, Integral b) => a -> b -> a a lot to define constant factors or sizes. Problem is that GHC complains about defaulting to Integer . Now I know why this happens ... and I know that I can "just" write (x^(y::Int)) to get rid of the warning. But that looks just "ugly". Otoh living with the warnings is also not a great option. Same thing applies for (^^) :: (Integral b, Fractional a) => a -> b -> a and (**) :: Floating a => a -> a -> a is not usable to me. Anyone has a nice

Iteratively printing every integer in a List

末鹿安然 提交于 2020-01-15 04:29:05
问题 Say I have a List of integers l = [1,2] Which I want to print to stdout . Doing print l produces [1,2] Say I want to print the list without the braces map print l produces No instance for (Show (IO ())) arising from a use of `print' Possible fix: add an instance declaration for (Show (IO ())) In a stmt of an interactive GHCi command: print it `:t print print :: Show a => a -> IO () So while I thought this would work I went ahead and tried: map putStr $ map show l Since I suspected a type