Understanding a Haskell List (print sqrt for a list item)

I have GHCi, version 7.8.3. I would like to calculate the sum of sqrt elements that are divisible by 10.

If I write [ x | x <- [10..100], x `mod` 10 == 0]or sum [sqrt x | x <- [10..100]]correctly.

But if I write sum [ sqrt x | x <- [10..100], x `mod` 10 == 0]when an error is displayed:

'<interactive>:39:1:
    No instance for (Show t0) arising from a use of ‘print’
    The type variable ‘t0’ 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 23 others
    In a stmt of an interactive GHCi command: print it'

How to change the command, the correct program?

+4
source share
1 answer

- , mod Integral a => a, sqrt Floating a => a. , GHC , , , GHCi, - . , GHCi print, show, - , . show, Integral Floating, .

typecheck, mod sqrt. fromIntegral sqrt:

sum [sqrt $ fromIntegral x | x <- [10..100], x `mod` 10 == 0]
+5
source

All Articles