1
countSequences :: Int -> Int -> Integer

countSequences 0 m = 0
countSequences m 0 = 0
countSequences (n) (m) =  if (n <= (m+1)) then (truncate((cee (m+1) (n) (0))) + truncate((countSequences (fromIntegral (n-1)) (fromIntegral (m))))) 
else truncate(countSequences (fromIntegral (n-1)) (fromIntegral (m)))

factorial :: Float -> Float
factorial 0 = 1
factorial 1 = 1
factorial x = x * factorial(x-1)

cee :: Float -> Float -> Float -> Float

cee x y z = if (x==y) then ((1) / (factorial ((x+z)-(y)))) else ((x) * (cee (x-1) (y) (z+1)))

i cant really understand why this error keep coming up .. the truncate is supposed to convert the type from Float to Integer so ..

2 Answers 2

5

The error is:

Couldn't match expected type `Float' with actual type `Int'
In the first argument of `(+)', namely `m'
In the first argument of `cee', namely `(m + 1)'
In the first argument of `truncate', namely
  `((cee (m + 1) (n) (0)))'

You see, the problem is you passing an Int to the function cee.

Here, I cleaned up the code for you:

countSequences :: Int -> Int -> Integer

countSequences 0 m = 0
countSequences m 0 = 0
countSequences n m = 
  if n <= m+1
  then truncate (cee (fromIntegral (m+1)) (fromIntegral n) 0) +
       countSequences (n-1) m
  else countSequences (n-1) m

factorial :: Float -> Float
factorial 0 = 1
factorial 1 = 1
factorial x = x * factorial (x-1)

cee :: Float -> Float -> Float -> Float

cee x y z =
  if (x==y)
  then 1 / factorial (x+z-y)
  else x * cee (x-1) y (z+1)
Sign up to request clarification or add additional context in comments.

3 Comments

fromIntegral m+1 should probably be fromIntegral m + 1 or fromIntegral (m+1), since it's such a common mistake to not realise that application binds tightest :)
@ehird: you're right. I got lazy and just used the convenient fact that (fromIntegral x) + 1 == fromIntegral (x+1). I'll edit the answer.
Technically, for sufficiently pathological x you could get an overflow pre-conversion, which would probably result in different results since Float doesn't overflow in the same way... but yes, close enough for all reasonable values :)
2

m is of type Int (per your type signature for countSequences: hence, so is m + 1. However, your function cee expects a Float, so the type checker righteously complains.

Furthermore, you will need a couple of more fixes to make this type check. Here's a version that passes the checker:

countSequences :: Int -> Int -> Integer
countSequences 0 m = 0
countSequences m 0 = 0
countSequences n m =
  if   n <=  m + 1
  then truncate $
         cee (fromIntegral (m+1)) (fromIntegral n) 0 +
         fromIntegral (countSequences (n-1) m)
  else countSequences (n-1) m

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.