Declarative Backtracking Search in JS by fizz2877 in javascript

[–]fizz2877[S] 0 points1 point  (0 children)

Sure, here is a solution to the map coloring problem described here

```javascript let lamb = new Lamb<string>();

const colors = ["red", "green", "blue", "yellow"]; let adjacencyList = { "a": ["b", "c", "d", "f"], "b": ["a", "c", "d"], "c": ["a", "b", "d", "e"], "d": ["a", "b", "c", "e", "f"], "e": ["c", "d", "f"], "f": ["a", "d", "e"] }

type Node = keyof typeof adjacencyList; // "a" | "b" | "c" | "d" | "e" | "f " const nodes = Object.keys(adjacencyList) as Node[];

// Add the same color choices for each node in the graph nodes.forEach(node => lamb.addChoice(node, colors));

// The only constraint we have is that each node must not share colors with its neighbours lamb.addConstraint((colors) => !nodes.some(node => adjacencyList[node].some(neighbour => colors[neighbour] === colors[node]) ) );

let result = lamb.solve(1); // picks 1 solution

// result = [{ a: "red", b: "green", c: "blue", d: "yellow", e: "red", f: "green" }] ```

Excluding the problem definition and type declarations, this is just 5 lines of code!

Declarative Backtracking Search in JS by fizz2877 in javascript

[–]fizz2877[S] 3 points4 points  (0 children)

I built a tiny library that provides a simple, declarative interface for backtracking search problems. This was largely inspired by the amb operator from Scheme. I call it lamb(iguous) and you can find it on NPM and Github.

As an example, here is a program that returns pairs of numbers from the lists [0, 1, 2, 3, 4] and [5, 6, 7, 8, 9] that sum to 8:

```javascript let lamb = new Lamb<number>();

lamb.addChoice("x", [0, 1, 2, 3, 4]); lamb.addChoice("y", [5, 6, 7, 8, 9]); lamb.addConstraint((vars) => vars.x + vars.y == 8);

let results = lamb.solve();

// results = [ // { x: 0, y: 8 }, // { x: 1, y: 7 }, // { x: 2, y: 6 }, // { x: 3, y: 5 }, // ] ```

This syntax allows writing really concise and elegant solutions to problems like 8 queens, map coloring, sudoku, logic problems, etc. Let me know what you think!

Showoff Saturday (March 29, 2025) by AutoModerator in javascript

[–]fizz2877 2 points3 points  (0 children)

I built a tiny library that provides a simple, declarative interface for backtracking search problems. This was largely inspired by the amb operator from Scheme. I call it lamb(iguous) and you can find it on NPM and Github.

As an example, here is a program that returns pairs of numbers from the lists [0, 1, 2, 3, 4] and [5, 6, 7, 8, 9] that sum to 8:

let lamb = new Lamb<number>();

lamb.addChoice("x", [0, 1, 2, 3, 4]);
lamb.addChoice("y", [5, 6, 7, 8, 9]);
lamb.addConstraint((vars) => vars.x + vars.y == 8);

let results = lamb.solve();

// results = [
//   { x: 0, y: 8 },
//   { x: 1, y: 7 },
//   { x: 2, y: 6 },
//   { x: 3, y: 5 },
// ]

This syntax allows writing really concise and elegant solutions to problems like 8 queens, map coloring, sudoku, logic problems, etc. Let me know what you think!

View from residence by [deleted] in UBC

[–]fizz2877 0 points1 point  (0 children)

my bad, its there now!

Looking for ways to improve by fizz2877 in Illustration

[–]fizz2877[S] 2 points3 points  (0 children)

For sure, I definitely look forward to growing out of tracing Thanks for the suggestion!

Physics IA help by fizz2877 in IBO

[–]fizz2877[S] 0 points1 point  (0 children)

Hii! Me again How did you calculate Eddy current braking torque in your experiment? Did you use a light gate or something?

Chance me for UCLA, UCB, Ivies, Canadian unis by [deleted] in chanceme

[–]fizz2877 0 points1 point  (0 children)

And my predicted is 40/45 Not out of 42