#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)))) (define fib-stream (stream* 0 1 (stream-add fib-stream (stream-rest fib-stream)))) (struct graph (nodes edges)) (define (is-ham-path? g) (let* [(edges (graph-edges g)) (edge-exists? (lambda (start end) (or (member (list start end) edges) (member (list end start) edges))))] (lambda (node-seq) (andmap edge-exists? (take node-seq (sub1 (length node-seq))) (cdr node-seq))))) (define (find-hamiltonian-path g) (let [(perms (permutations (graph-nodes g)))] (findf (is-ham-path? 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)) (define (stream-mul s1 s2) (stream-cons (* (stream-first s1) (stream-first s2)) (stream-mul (stream-rest s1) (stream-rest s2)))) (define factorial-stream (stream* 1 (stream-mul (in-naturals 1) factorial-stream))) ;; (stream-mul (in-naturals 1) (stream* 1 factorial-stream))) (define (exp-stream x) (stream-mul (stream-map (curry expt x) (in-naturals)) (stream-map (curry / 1) factorial-stream)))