A student wrote this code; the idea is to type createBoard(2, 3)
to produce [[0, 0], [0, 0], [0, 0]]
. (This is just a first draft to get the structure right!)
let createBoard: (int, int) => list(list(int)) =
(width, height) => {
let rec createList: (int, 'a) => list('a) =
(length, content) =>
switch (length) {
| 0 => []
| _ => [content, ...createList(length - 1, content)]
};
createList(width, createList(height, 0));
};
In VSCode, this generates an error in the 2nd-to-last line, saying that createList(height, 0)
is of type list(int)
, but int
was expected. That seemed odd to me, and I dug in a little deeper. When I write
let createBoard: (int, int) => list(list(int)) =
(width, height) => {
let rec createList: (int, 'a) => list('a) =
(length, content) =>
switch (length) {
| 0 => []
| _ => [content, ...createList(length - 1, content)]
};
//let k = createList(height, 0);
let t = createList;
[]
};
everything compiles fine, and I get a warning about t
being unused, but hovering over it tells me that it has type (int, 'a) => list('a)
, which is comforting.
But when I uncomment that previous line:
let createBoard: (int, int) => list(list(int)) =
(width, height) => {
let rec createList: (int, 'a) => list('a) =
(length, content) =>
switch (length) {
| 0 => []
| _ => [content, ...createList(length - 1, content)]
};
let k = createList(height, 0);
let t = createList;
[]
};
hovering over t
shows it’s of type (int, int) => list(int)
.
I’m not sure how that can be the case — invoking a procedure should not change its type, if I understand correctly. I’ve written the corresponding code in ReScript, and the error persists.
Any insights?