To add to Jeffrey's answer, it looks like you meant to add items to listdoor in three steps. The confusion is about the meaning of ;
and ::
in ocaml. And you and I are both in the habit of programming by doing sequences of steps, as we did in other languages.
Everything to the right of =
is one big expression that evaluates to the function's return value. That's how values are returned from functions in ocaml.
When the expression has ;
's in it, the ;
is a delimiter between sub-expressions. Each sub-expression gets evaluated, and all the return values except for that of the last sub-expression get discarded (those sub-expressions aren't useless though - the reason for using ;
is to accumulate side-effects).
But the ::
doesn't act by side effect - cons is just a function with a return value.
So your lines countZero(doorlist,count) :: listdoor
and countOne(doorlist,count) :: listdoor
each produced a new list, but each of those lists was discarded.
If you want to keep the meaning the same, and build up a list in sequence, you could do it by storing the steps of the accumulation using let and in:
let countAll(doorlist,count) =
let step1 = [] :: countZero(doorlist,count) in
let step2 = step1 :: countOne(doorlist,count) in
let step3 = step2 :: countTwo(doorlist,count) in
step3
;;
If keeping all those intermediate names around seems less efficient than the series of concatenations you had in mind in your posting, it's because this kind of thing is more terse if you express the function in a way that resembles the verbal description you gave, instead of the sequential way that you were encouraged to come up with by other languages. If you want to collect the result of three functions into a list, you can simply do this:
let collectResults (a,b) =
[ fun1 (a,b) ; fun2 (a,b) ; fun3 (a,b) ]
;;
Do you see how the return value (everything to the right of =
) matches up with the description you gave for your function: "which is supposed to be a list containing the output of the functions"? (NB: semicolons within the square brackets delimit elements in a list - different meaning from the ;
's earlier!)
Two other minor points:
If the function you are defining doesn't call itself, there is no need to declare it 'rec'. But maybe Jeffrey's hunch is right and in fact you meant to have some recursive calls here somewhere.
There's no need to make a name for your return value (listdoor in your case). If the function's definition is very short like this one, let the definition itself be your 'label' for the return value. In the last code block, the function doesn't give a name to the list it returns.