dilator
01-16-2005, 04:14 PM
Hello all I am a newbie to SML and this forum I only started programming in SML about a week ago. I am trying to write a program to solve all the possible solutions to the 8 queens problem but I keep on getting this exception:
uncaught exception nonexhaustive match failure
raised at: stdIn:783.35
every time I run my program.
Here is my code:
fun updateList(x::xs, 1, col) = col::xs
|updateList(x::xs, row, col) = x::updateList(xs, row-1, col)
and
printList(nil) = print("\n")
|printList(x::xs) = (
print(Int.toString(x)^" ");
printList(xs))
and
content(x::xs, 1) = x
|content(x::xs, subscript) = content(xs, subscript-1)
and
loop1(board, col, size, row, count) = if col < size then (br := updateList(!board, row, col); if valid(!board, 0, row) then EightQ(!board, size, row+1, count) else loop1(br, col+1, size, row, count)) else print("oheoje\n\n\n")
and
valid(board, ctr, row) = if ctr < row then (if (content(board, ctr) = content(board, row)) orelse Int.abs((content(board, row) - content(board, ctr))) = (row - ctr) then false else valid(board, ctr+1, row)) else true
and
EightQ(board, size, row, count) = if row = size then (print("Solution is "); printList(!br)) else loop1(br, 0, size, row, count);
I think my problem is in function loop1 where I have br := updateList(!board, row, col); but I don't know how to fix it. So please if someone can help me out I am completely lost and confused.
ps. I run the program by calling EightQ(!br, 8, 0, count); where count is initialized to 0.
uncaught exception nonexhaustive match failure
raised at: stdIn:783.35
every time I run my program.
Here is my code:
fun updateList(x::xs, 1, col) = col::xs
|updateList(x::xs, row, col) = x::updateList(xs, row-1, col)
and
printList(nil) = print("\n")
|printList(x::xs) = (
print(Int.toString(x)^" ");
printList(xs))
and
content(x::xs, 1) = x
|content(x::xs, subscript) = content(xs, subscript-1)
and
loop1(board, col, size, row, count) = if col < size then (br := updateList(!board, row, col); if valid(!board, 0, row) then EightQ(!board, size, row+1, count) else loop1(br, col+1, size, row, count)) else print("oheoje\n\n\n")
and
valid(board, ctr, row) = if ctr < row then (if (content(board, ctr) = content(board, row)) orelse Int.abs((content(board, row) - content(board, ctr))) = (row - ctr) then false else valid(board, ctr+1, row)) else true
and
EightQ(board, size, row, count) = if row = size then (print("Solution is "); printList(!br)) else loop1(br, 0, size, row, count);
I think my problem is in function loop1 where I have br := updateList(!board, row, col); but I don't know how to fix it. So please if someone can help me out I am completely lost and confused.
ps. I run the program by calling EightQ(!br, 8, 0, count); where count is initialized to 0.