Fix mk_starts and second_last in knights benchmarks.

mk_starts was not yielding enough values. It's originally a translation of a double list comprehension in Haskell, which cannot simply be translated to a map2. The latter combine elements two by two, but the former works through all possible permutations.
This commit is contained in:
KtorZ 2024-07-16 14:48:51 +02:00
parent 65afb11546
commit 5b0a1716b5
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 17 additions and 22 deletions

View File

@ -44,14 +44,20 @@ fn root(size: Int) -> Queue<(Int, ChessSet)> {
queue.append_all_front(queue.new(), mk_starts(size))
}
fn mk_starts(sze: Int) -> List<(Int, ChessSet)> {
let x_list = interval(1, sze)
let y_list = interval(1, sze)
fn mk_starts(size: Int) -> List<(Int, ChessSet)> {
let it = interval(1, size)
let l =
list.flat_map(
it,
fn(x) { list.map(it, fn(y) { start_tour((x, y), size) }) },
)
let l = x_list |> list.map2(y_list, fn(a, b) { start_tour((a, b), sze) })
let length = list.length(l)
list.repeat(1 - length, length) |> list.zip(l)
expect length == size * size
list.zip(list.repeat(1 - length, length), l)
}
fn interval(a: Int, b: Int) -> List<Int> {

View File

@ -33,32 +33,21 @@ pub fn last_piece(board: ChessSet) -> Tile {
pub fn delete_first(board: ChessSet) -> ChessSet {
let ChessSet { move_number, visited, .. } = board
expect Some(deleted_first) = list.init(visited)
expect Some(new_visited) = list.init(visited)
ChessSet {
..board,
move_number: move_number - 1,
start: second_last(visited),
visited: deleted_first,
visited: new_visited,
}
}
fn second_last(visited: List<a>) -> Option<a> {
when visited is {
[] -> None
[_, ..rest] -> {
let value = second_last(rest)
if value == None {
if builtin.null_list(rest) {
None
} else {
Some(builtin.head_list(visited))
}
} else {
value
}
}
when list.reverse(visited) is {
[] -> fail
[_] -> None
[_, a, ..] -> Some(a)
}
}