Skip to content

Instantly share code, notes, and snippets.

@SuzanneSoy
Forked from hoelzro/cartesian.rkt
Created September 23, 2016 22:27
Show Gist options
  • Save SuzanneSoy/d495a361527594af78a4136251e55852 to your computer and use it in GitHub Desktop.
Save SuzanneSoy/d495a361527594af78a4136251e55852 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 (in-cartesian-product '(0 1) '(0 1) '(0 1) '(0 1) '(0 1)
'(0 1) '(0 1) '(0 1) '(0 1) '(0 1)
'(0 1) '(0 1) '(0 1) '(0 1) '(0 1)
'(0 1) '(0 1) '(0 1) '(0 1) '(0 1)
'(0 1) '(0 1) '(0 1) '(0 1) '(0 1)
'(0 1) '(0 1) '(0 1) '(0 1) '(0 1)))]
(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