Haskell: errors when trying to define function with two arguments

Viewed 348

I'm trying to define a simple function that turns a list of single integers into one larger integer. For example, given the list [1,5,2,0] it would return 1520. To do that in base 10 I used:

calc_nr [] = 0
calc_nr (a:y) = a * 10^(length y) + (calc_nr y)

Now, I wanted to extend that to different bases, which can be done by altering the base 10 power in the expression to the desired base. For that, I thought of taking in another argument, b, and replacing the base 10 power for a base b power.
However, I'm getting some errors when I try to do that. Writing:

calc_nr b [] = 0
calc_nr b (a:y) = a * b^(length y) + (calc_nr y)

Gives me the error:

* Occurs check: cannot construct the infinite type: t ~ [t]
  Expected type: [t] -> t
    Actual type: t -> [t] -> t
* Relevant bindings include
    calc_nr :: [t] -> t (bound at calc_nr.hs:39:1)

I'm new to Haskell, so maybe this is quite a stupid mistake, but any help would be really appreciated!

1 Answers
Related