views:

37

answers:

1

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

+1  A: 

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 list works perfectly well to change a character in a char list list. All it means is that you could also use it to change an integer in an int int list.

In other words: if you put in a char list list and a char * int * int tuple, you'll get out a char list list with 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).

sepp2k
For the fun of it I put this in: val x = getsudo xs and now I get this: val aendrsudo = fn : string -> int * int * char -> char list list (and changed the xs to x in the function).Do you know of any way of forcing it to be a char list list -> int * int * char -> char list list? This is homework and they want it to have this type.Having the function be 1-indexed would mean that I start with (1,1) meaning list 1, position 1 - and that makes more sense to me than starting from line 0.
GeorgeWChubby
@George: You can just put an explicit type annotation somewhere, like `fun changesudo xs (r, s, c:char) = ...`. However there really is no need to do that, as it will work exactly the same. I doubt your instructors will mind if your function has a more general typed than was asked for.
sepp2k
Good point. Thanks muchly.
GeorgeWChubby