<div dir="ltr">Thanks Jack it works.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 13, 2016 at 12:01 AM, Jack Henahan <span dir="ltr"><<a href="mailto:jhenahan@me.com" target="_blank">jhenahan@me.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
The inferred type of findLoot is<br>
<br>
findLoot :: (Integral a, Fractional a) => a -> [[a]] -> a<br>
<br>
It's impossible to satisfy those constraints.<br>
<br>
Rewriting like this does what you want, but there's likely a cleaner way<br>
to express this algorithm.<br>
<br>
findLoot :: (Integral a, Fractional b) => a -> [[a]] -> b<br>
<span class="">findLoot val [] = 0.0<br>
findLoot val ((x:y:[]):xs) | val == 0 = 0.0<br>
| val < y = ((fromIntegral val) * (fromIntegral x)) / fromIntegral y<br>
</span> | val > y = fromIntegral ((div val y)*x) + (findLoot (mod val y) xs)<br>
<div><div class="h5"><br>
<br>
<br>
Balraj Singh <<a href="mailto:erbalrajs@gmail.com">erbalrajs@gmail.com</a>> writes:<br>
<br>
> Hi,<br>
><br>
> I am writing a sample code below and getting an error. Please help to resolve<br>
> it:<br>
><br>
> Code:<br>
><br>
> import Data.List<br>
><br>
><br>
> findLoot val [] = 0.0<br>
><br>
> findLoot val ((x:y:[]):xs) | val == 0 = 0.0<br>
><br>
> | val < y = ((fromIntegral val) * (fromIntegral x))<br>
> / fromIntegral y<br>
><br>
> | val > y = ((div val y)*x) + (findLoot (mod val y)<br>
> xs)<br>
><br>
><br>
> Error:<br>
><br>
> No instance for (Fractional a0) arising from a use of ‘it’<br>
><br>
> The type variable ‘a0’ is ambiguous<br>
><br>
> Note: there are several potential instances:<br>
><br>
> instance HasResolution a => Fractional (Fixed a)<br>
><br>
> -- Defined in ‘Data.Fixed’<br>
><br>
> instance Integral a => Fractional (Ratio a)<br>
><br>
> -- Defined in ‘GHC.Real’<br>
><br>
> instance Fractional Double -- Defined in ‘GHC.Float’<br>
><br>
> ...plus one other<br>
><br>
> In the first argument of ‘print’, namely ‘it’<br>
><br>
> In a stmt of an interactive GHCi command: print it<br>
><br>
><br>
> Sample Input and Output:<br>
><br>
> Input 1:-<br>
> val = 50<br>
> ((x:y:[]):xs) = [[120, 30], [100,50],[60, 20]]<br>
> Output 1:-<br>
> 180.0000<br>
><br>
> Input 2:-<br>
> val = 10<br>
> ((x:y:[]):xs) = [[500,30]]<br>
> Output 2:-<br>
> 166.6667<br>
<br>
<br>
</div></div>--<br>
Jack<br>
</blockquote></div><br></div>