2
votes
heist :: (Num n) => [n] -> [n] -> n -> n
-- heist [] [] _ = 0
heist w v maxw = func w v i j where
    i = length w
    j = maxw  
func :: (Num n) => [n] -> [n] -> n -> n -> n 
func _ _ 0 0 = 0

the above code is giving me:

Heist.hs:15:27:
    Could not deduce (n ~ Int)
    from the context (Num n)
      bound by the type signature for
                 heist :: Num n => [n] -> [n] -> n -> n
      at Heist.hs:(15,1)-(17,16)
      `n' is a rigid type variable bound by
          the type signature for heist :: Num n => [n] -> [n] -> n -> n
          at Heist.hs:15:1
    In the third argument of `func', namely `i'
    In the expression: func w v i j
    In an equation for `heist':
        heist w v maxw
          = func w v i j
          where
              i = length w
              j = maxw

Why is that happening?

I am wrapping my head around Haskell type system inch by inch

2

2 Answers

7
votes

length returns an Int; use i = Data.List.genericLength w or i = fromIntegral (length w).

8
votes

length always returns an Int. By passing i to func you're saying that n should be Int, but heist wants n to be generic, hence the type error.