From 54956dacfbb02481fec49798ee81c14b9edbd3e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20N=C4=9Bme=C4=8Dek?= Date: Thu, 20 Mar 2025 17:58:34 +0100 Subject: [PATCH] Add a simpler solution A solution using `ormap` --- labs/lab05.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/labs/lab05.md b/labs/lab05.md index 684f02c..1e673f5 100644 --- a/labs/lab05.md +++ b/labs/lab05.md @@ -127,7 +127,8 @@ Now we can apply the above function to all permutations. The function `(check-pa permutations of nodes and filter those which form a path. If there is a permutation being a path simultaneously, we have a Hamiltonian path. Otherwise, we return `#f`. ::: details Solution: `find-hamiltonian-path` -```racket +::: code-group +```racket [filter] (define (find-hamiltonian-path g) (define perms (permutations (graph-nodes g))) (let ([paths (filter identity (map (check-path g) perms))]) @@ -135,6 +136,12 @@ simultaneously, we have a Hamiltonian path. Otherwise, we return `#f`. #f (car paths)))) ``` +```racket [ormap] +(define (find-hamiltonian-path g) + (define perms (permutations (graph-nodes g))) + (ormap (check-path g) perms)) +``` +::: If you are curious, try to use the function `in-permutations` to compute the `perms` lazily and compare the perfromance of the two implementations on a larger graph.