tags:

views:

83

answers:

1

I'm writing a recursive function in Ocaml that's supposed to count the number of items in an integer list (Yes I know there's a List.length function but I'm trying to do it myself). However the Ocaml compiler/interpreter forces me to use alpha list all the time.

So is it wrong to say that, when a function accepts a list as a parameter, the type of that list must always be alpha? Thanks

EDIT: the reason why it's inconvenient for me to use alpha lists is because i can't compare the head of the alpha list with an integer value due to type-matching complaints

+5  A: 

Easy:

 let length (lst : int list) = ...

I'm a bit confused that a comparison is tripping you up; a comparison with an integer should just constrain 'a to be int. For example, in

let length lst = match lst with
  | x :: xs when x = 0 -> ...

lst would have type int list.

Chris Conway
Might need to add a 'rec'.
grettke
I'm only upvoting once because you didn't use the phrase "principal type", which would have been in context and which OCaml left on the side of the road a long time ago anyway.
Pascal Cuoq