3
votes

I'm trying to build a list of the indices where the list minimums occur.

let rec max_index l =
let rec helper inList min builtList index = 
match inList with
| [] -> builtList
| x :: xs ->
    if (x < min) then
        helper xs x index :: builtList index + 1 //line 63
    else
        helper xs min builtList index + 1
in helper l 100000 [] 0;;

It's giving me the following error for line 63.

Error: This expression has type 'a list -> 'a list
       but an expression was expected of type 'a
       The type variable 'a occurs inside 'a list -> 'a list

An expression was expected of type 'a? I'm not sure why it's saying that. My guess it it has something to do with the index::builtList

2

2 Answers

5
votes
        helper xs x index :: builtList index + 1 //line 63
    else
        helper xs x index min index + 1

The problem you're having is that you're trying to pass a non-list to your helper function on line 65 (min) whilst trying to pass an int list to the same parameter on line 63. Try replacing min with [min] or min::[].

Edit:

After the update, the issue is that function calls are left associative and higher precedence than binary operators (see here), so helper xs x index would execute before index :: builtList and likewise helper xs x index :: builtList would execute before index + 1. To get the correct order of evaluation, you need to put parenthesis around the other function calls i.e. :: and + plus their parameters like this:

        helper xs x (index :: builtList) (index + 1) //line 63
    else
        helper xs x index min (index + 1)
3
votes

You need some parentheses. Function calling binds tighter than binary operators. So

if (x < min) then
    helper xs x (index :: builtList) (index + 1)
else
    helper xs min builtList (index + 1)