#lang racket (module+ test (require rackunit)) (define (stream-add s1 s2) (stream-cons (+ (stream-first s1) (stream-first s2)) (stream-add (stream-rest s1) (stream-rest s2)))) (module+ test (check-equal? (stream->list (stream-take (stream-add (in-naturals) (in-naturals)) 10)) '(0 2 4 6 8 10 12 14 16 18))) (define fib-stream (stream* 0 1 (stream-add fib-stream (stream-rest fib-stream)))) (module+ test (check-equal? (stream->list (stream-take fib-stream 10)) '(0 1 1 2 3 5 8 13 21 34))) (struct graph (nodes edges)) (define (is-hamiltonian? g node-seq) (let [(edges (graph-edges g))] (andmap (lambda (start end) (or (member (list start end) edges) (member (list end start) edges))) (take node-seq (sub1 (length node-seq))) (cdr node-seq)))) (define (find-hamiltonian-path g) (let [(perms (permutations (graph-nodes g)))] (findf (curry is-hamiltonian? g) perms))) (module+ test (define gr (graph '(1 2 3 4 5 6) '((1 2) (1 5) (2 3) (2 5) (3 4) (4 5) (4 6)))) (check-equal? (find-hamiltonian-path gr) '(3 2 1 5 4 6)) (check-equal? (find-hamiltonian-path (graph '(a b c d) '((a b) (a c) (a d)))) #f))