I have a list of lists, for example [[1; 2; 3]; [2]; [3; 4; 5; 6]; [7; eight; 9; 10] I want to place them in Hashtbl, where the key is the length of the list and the value is the list of lists containing all the sublists of this length.
So, for the example above, the hash will look like this
Key Value 1 [[2]] 3 [[1;2;3]] 4 [[3;4;5;6];[7;8;9;10]]
In addition, I am also trying to track the length of the longest list, and this number is what the function returns
The code that does this is as follows.
let hashify lst = let hash = Hashtbl.create 123456 in let rec collector curmax lst = match lst with [] -> curmax | h::t -> let len = (List.length h) in (if ((Hashtbl.mem hash len)=true) then ( let v = (Hashtbl.find hash len) in Hashtbl.add hash len v@ [h] ) (* Line 660 *) else ( Hashtbl.add hash len [h])); (collector (max len curmax) t) in collector 0 lst ;;
Now when I do this, I get the following error for the code above
File "all_code.ml", line 600, characters 50-72: Error: This expression has type unit but an expression was expected of type 'a list
Why Ocaml requires a return type of "list" and how to fix it. thanks in advance Puneet
source share