
Thanks Jack it works.
On Tue, Sep 13, 2016 at 12:01 AM, Jack Henahan
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
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