0

I am new to haskell and am trying to learn why certain things do not compile in haskell. For example, I get the following error:

 • Couldn't match expected type ‘Int -> Float’
                  with actual type ‘Float’
    • Possible cause: ‘(/)’ is applied to too many arguments
      In the expression: (fromIntegral x :: Float) / 100.0
      In an equation for ‘percent’:
          percent x = (fromIntegral x :: Float) / 100.0

For this code:

percent :: Int -> Int -> Float
percent x =  (fromIntegral x :: Float) * 100.0

Or something like this:

    percent :: Int -> Int -> Float
    percent x =   a * 100.0
        where a = fromIntegral x :: Float

While, this method works, that I found on this post, that is slightly more complex as it divides two ints before it multiplies the 100? It follows a similar methodology as the ones I coded as well that involve only one int multiplied by 100. The second one I coded, I modeled after the code seen below. I'm not confused as to why the above do not work, however, the one below that is doing a similar thing works.

percent :: Int -> Int -> Float
percent x y =   100 * ( a / b )
  where a = fromIntegral x :: Float
        b = fromIntegral y :: Float

I would appreciate the assistance as I am new to Haskell and still trying to learn how it operates.

Brooke
  • 109
  • 1
  • 2
  • 12

1 Answers1

4

Your type says the function takes two Int arguments:

percent :: Int -> Int -> Float

But your implementation takes only one:

percent x = {- ... -}

Change one or the other of these.

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
  • Thanks for the assistance. I am still trying to grasp how to define the types. Your description was very clear. – Brooke Feb 17 '18 at 05:17