Advertisement
Guest User

sudoku.sml

a guest
Jul 14th, 2011
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. local
  2. (* an exception we hopefully don't need *)
  3. exception Error of string;
  4.  
  5. (* isinList == check if char is in char list *)
  6. fun isinList (c:char) ([]:char list) = false
  7. | isinList (c:char) ((clh::clt):char list) = if clh = c then true else isinList c clt;
  8.  
  9. (* checkList == check if a row or column has only the following character set 1,2,3,4,5,6,7,8,9 *)
  10. fun checkList ([]) ([]) = true
  11. | checkList (l::ls) ((h::tl):char list) = if l = h then checkList (ls) (tl) else if (isinList l tl) then checkList (l::ls) (tl@[h]) else false
  12. | checkList _ _ = false;
  13.  
  14. (* makeColumn == make char list list of the Columns *)
  15.  
  16. fun makeColumn ([[], [], [], [], [], [], [], [], []]:char list list) (accl:char list list) = accl
  17. | makeColumn ([(h1::cl1):char list, (h2::cl2):char list, (h3::cl3):char list, (h4::cl4):char list, (h5::cl5):char list, (h6::cl6):char list, (h7::cl7):char list, (h8::cl8):char list, (h9::cl9):char list]:char list list) (accl:char list list) =
  18. makeColumn ([cl1:char list, cl2:char list, cl3:char list, cl4, cl5, cl6, cl7, cl8, cl9]) ([h1, h2, h3, h4, h5, h6, h7, h8, h9]::accl)
  19. | makeColumn (_) (_) = raise Error "Error in makeColumn";
  20. in
  21. (* rscheck : char list list -> bool *)
  22.  
  23. fun rscheck ([]:char list list) = true
  24. | rscheck ([cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9]:char list list) = if
  25. (checkList(cl1) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  26. (checkList(cl2) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  27. (checkList(cl3) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  28. (checkList(cl4) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  29. (checkList(cl5) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  30. (checkList(cl6) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  31. (checkList(cl7) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  32. (checkList(cl8) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  33. (checkList(cl9) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) then case (makeColumn([cl1,cl2,cl3,cl4,cl5,cl6,cl7,cl8,cl9]) ([])) of ([clc1, clc2, clc3, clc4, clc5, clc6, clc7, clc8, clc9]):char list list
  34. => if
  35. (checkList(clc1) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  36. (checkList(clc2) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  37. (checkList(clc3) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  38. (checkList(clc4) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  39. (checkList(clc5) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  40. (checkList(clc6) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  41. (checkList(clc7) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  42. (checkList(clc8) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) andalso
  43. (checkList(clc9) ([#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"])) then true else false
  44. | _ => raise Error "Error in return of makeColumn"
  45. else false;
  46. end;
  47. (* Testcase1 => true *)
  48. val test1 =[[#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"],
  49. [#"9",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8"],
  50. [#"8",#"9",#"1",#"2",#"3",#"4",#"5",#"6",#"7"],
  51. [#"7",#"8",#"9",#"1",#"2",#"3",#"4",#"5",#"6"],
  52. [#"6",#"7",#"8",#"9",#"1",#"2",#"3",#"4",#"5"],
  53. [#"5",#"6",#"7",#"8",#"9",#"1",#"2",#"3",#"4"],
  54. [#"4",#"5",#"6",#"7",#"8",#"9",#"1",#"2",#"3"],
  55. [#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"1",#"2"],
  56. [#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"1"]];
  57. rscheck (test1);
  58.  
  59. (* Testcase2 => false *)
  60. val test2 =[[#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9"],
  61. [#"9",#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8"],
  62. [#"8",#"9",#"1",#"2",#"3",#"4",#"5",#"6",#"7"],
  63. [#"7",#"8",#"9",#"1",#"2",#"3",#"4",#"5",#"6"],
  64. [#"6",#"7",#"8",#"9",#"1",#"2",#"3",#"4",#"5"],
  65. [#"5",#"6",#"7",#"8",#"9",#"1",#"2",#"3",#"4"],
  66. [#"4",#"5",#"6",#"7",#"8",#"9",#"1",#"2",#"3"],
  67. [#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"1",#"2"],
  68. [#"3",#"2",#"4",#"5",#"6",#"7",#"8",#"9",#"1"]];
  69. rscheck (test2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement