Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

I want to code in Scheme

Name: Anonymous 2013-03-21 17:48

But I don't really know how to write idiomatic code. Is anyone here willing to rewrite some of my python code, so I can see how it's done?

Also, what is the code-tag on this board?

Name: Anonymous 2013-03-22 15:49

>>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(n2) 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.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List