[Haskell-cafe] Facing issue: "No instance for (Fractional a0) arising from a use of ‘it’ The type variable ‘a0’ is ambiguous"
Jack Henahan
jhenahan at me.com
Mon Sep 12 18:31:22 UTC 2016
The inferred type of findLoot is
findLoot :: (Integral a, Fractional a) => a -> [[a]] -> a
It's impossible to satisfy those constraints.
Rewriting like this does what you want, but there's likely a cleaner way
to express this algorithm.
findLoot :: (Integral a, Fractional b) => a -> [[a]] -> b
findLoot val [] = 0.0
findLoot val ((x:y:[]):xs) | val == 0 = 0.0
| val < y = ((fromIntegral val) * (fromIntegral x)) / fromIntegral y
| val > y = fromIntegral ((div val y)*x) + (findLoot (mod val y) xs)
Balraj Singh <erbalrajs at gmail.com> writes:
> Hi,
>
> I am writing a sample code below and getting an error. Please help to resolve
> it:
>
> Code:
>
> import Data.List
>
>
> findLoot val [] = 0.0
>
> findLoot val ((x:y:[]):xs) | val == 0 = 0.0
>
> | val < y = ((fromIntegral val) * (fromIntegral x))
> / fromIntegral y
>
> | val > y = ((div val y)*x) + (findLoot (mod val y)
> xs)
>
>
> Error:
>
> No instance for (Fractional a0) arising from a use of ‘it’
>
> The type variable ‘a0’ is ambiguous
>
> Note: there are several potential instances:
>
> instance HasResolution a => Fractional (Fixed a)
>
> -- Defined in ‘Data.Fixed’
>
> instance Integral a => Fractional (Ratio a)
>
> -- Defined in ‘GHC.Real’
>
> instance Fractional Double -- Defined in ‘GHC.Float’
>
> ...plus one other
>
> In the first argument of ‘print’, namely ‘it’
>
> In a stmt of an interactive GHCi command: print it
>
>
> Sample Input and Output:
>
> Input 1:-
> val = 50
> ((x:y:[]):xs) = [[120, 30], [100,50],[60, 20]]
> Output 1:-
> 180.0000
>
> Input 2:-
> val = 10
> ((x:y:[]):xs) = [[500,30]]
> Output 2:-
> 166.6667
--
Jack
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 800 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160912/4aa4044a/attachment.sig>
More information about the Haskell-Cafe
mailing list