>>27
``In Racket'':
(define (cartesian-product . lists)
(if (empty? lists)
(list '())
(append-map (lambda (element)
(map (curry cons element)
(apply cartesian-product (rest lists))))
(first lists))))
(define (cartesian-product . lists)
(if (empty? lists)
(list '())
(append-map (lambda (element)
(map (curry cons element)
(apply cartesian-product (rest lists))))
(first lists))))
(define (map-indexed f list)
(map f (range (length list)) list))
(map-indexed cons (cartesian-product numops jnumtypes))
It would have been a bit shorter using list comprehensions (racket's library syntax gives you the cartesian product for free), but I guessed that you would have preferred it this way, as it uses higher order functions directly.
The logic is O(n
2) complexity on both this and your Python version, though. You are building a cartesian product after all.
Clojure has more list manipulation functions too, by the way.