Skip to content

Instantly share code, notes, and snippets.

@hoelzro
Created September 23, 2016 21:51
Show Gist options
  • Save hoelzro/b08658968f3a6b18a3af75a4cd9c3e4b to your computer and use it in GitHub Desktop.
Save hoelzro/b08658968f3a6b18a3af75a4cd9c3e4b to your computer and use it in GitHub Desktop.
#lang racket
(require racket/match)
(require racket/stream)
(define (for-each-cartesian-product lol fn)
(match lol
['() (fn '())]
[(cons hd tl)
(for [(elem hd)]
(for-each-cartesian-product tl (λ(sub-product)
(fn (cons elem sub-product)))))]))
(define count 0)
(define (in-cartesian-product . lol)
(define (prepend-to-stream-values elements full-elements substream)
(println elements)
(set! count (add1 count))
(if (stream-empty? substream)
empty-stream
(if (null? elements)
(prepend-to-stream-values full-elements full-elements (stream-rest substream))
(stream-cons (cons (first elements) (stream-first substream)) (prepend-to-stream-values (rest elements) full-elements substream)))))
(if (null? lol)
(stream-cons '() empty-stream)
(let [(head (first lol))
(tail (rest lol))]
(prepend-to-stream-values head head (apply in-cartesian-product tail)))))
(define s (in-cartesian-product '(a b c d e) '(f g h i j)))
(define num 0)
(for [(choice s)]
(println (list num count))
(set! num (add1 num)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment