Skip to content

Instantly share code, notes, and snippets.

@samth
Created December 6, 2019 17:09
Show Gist options
  • Save samth/e21c0c9e2e8de7c64400b2ea6363d200 to your computer and use it in GitHub Desktop.
Save samth/e21c0c9e2e8de7c64400b2ea6363d200 to your computer and use it in GitHub Desktop.
#lang racket
(define (matrix m n)
(if (= 1 m)
(for/list ((a (in-range 1 (+ n 1))))
(list a))
(let ((downstream (matrix (- m 1) n)))
(for*/list ((a (in-range 1 (+ n 1)))
(b (in-list downstream)))
(cons a b)))))
(length (matrix 10 6))
;#lang racket
(define null '())
(define (matrix m n)
(if (= 1 m)
(let loop ([a 1])
(if (= a (+ n 1))
null
(cons (list a) (loop (+ a 1)))))
;(for/list ((a (in-range 1 (+ n 1))))
; (list a))
(let ((downstream (matrix (- m 1) n)))
(let loop ([a 1])
(if (= a (+ n 1))
null
(append (map (lambda (b) (cons a b)) downstream)
(loop (+ a 1))))))))
;(for*/list ((a (in-range 1 (+ n 1)))
; (b (in-list downstream)))
; (cons a b)))))
(display (length (matrix 10 6)))
(newline)
[samth@huor:/tmp/guile-2.9.5 plt] time ./meta/guile -s ~/m.scm
60466176
real 0m12.291s
user 0m31.301s
sys 0m5.041s
[samth@huor:/tmp/guile-2.9.5 plt] time ./meta/guile -s ~/m.scm
60466176
real 0m12.383s
user 0m26.433s
sys 0m4.695s
[samth@huor:/tmp/guile-2.9.5 plt] time racketcs ~/m.rkt
60466176
real 0m11.185s
user 0m10.204s
sys 0m0.981s
[samth@huor:/tmp/guile-2.9.5 plt] time racketcs ~/m.rkt
60466176
real 0m11.197s
user 0m10.233s
sys 0m0.964s
[samth@huor:/tmp/guile-2.9.5 plt] time r ~/m.rkt
60466176
real 0m17.153s
user 0m14.234s
sys 0m2.927s
[samth@huor:/tmp/guile-2.9.5 plt] time r ~/m.rkt
60466176
real 0m17.340s
user 0m14.604s
sys 0m2.745s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment