I’m trying to declare a function that will let me change a number in a char list list (I’m still working on the sudoku game from earlier).
changesudo : char list list -> int * int * char -> char list list
I need to be able to call changesudo xs (r,s,c) where xs is the char list list, r is the list, s is the position in xs and c is the char.
This is what I have:
fun changesudo xs (r,s,c) =
let
val g = hd (List.take (List.drop
(xs , (r-1)) , 1));
val h = (List.take(g , s-1)) @ [c]
@ List.drop(g , s);
in
(List.take (xs , (r-1)) @ [h] @ List.drop(xs , r))
end;
and this is a ‘a list list -> int * int * ‘a -> ‘a list list – so I’m almost there.
How do I fix it?
I get the char list list with this function
There’s nothing to fix. The type you’ve got is more general than the type you aimed for, but that’s not a problem.
The type
'a list list -> int * int * 'a -> 'a list listworks perfectly well to change a character in achar list list. All it means is that you could also use it to change an integer in anint int list.In other words: if you put in a
char list listand achar * int * inttuple, you’ll get out achar list listwith the char at the given position replaced, so it works exactly like you want.One word of caution: I don’t know whether it’s intentional or not, but your function is 1-indexed, i.e. the first item in the first list is at position
(1,1)not(0,0).