Now, if there are more than one the same concept library, then you always want to benchmark, don't you? So I made the following script to do:
(import (time) (match) (rename (match match) (match m:match)) (srfi :1)) (define (count-pair lis) (let loop ((i 0) (lis lis)) (match lis (((a . d) rest ...) (loop (+ i 1) rest)) ((x rest ...) (loop i rest)) (() i)))) (define (m:count-pair lis) (let loop ((i 0) (lis lis)) (m:match lis (`((,a . ,d) . ,rest) (loop (+ i 1) rest)) (`(,x . ,rest) (loop i rest)) (x i)))) (define lis (list-tabulate 50000 (lambda (i) (if (zero? (mod i 5)) (iota (mod i 100)) 'x)))) (time (count-pair lis)) (time (m:count-pair lis))It seems the library doesn't have variable length list match with
...so using pair notation. (This means it can't distinguish pair and list but not sure if it really can't match variable length...)
And I made the library name
(match match)for my own sake.
The benchmark result was like this:
$ ~/projects sash -L. -S.sld match-bench.scm ;; (count-pair lis) ;; 14.316742 real 14.304623 user 0.003922 sys ;; (m:count-pair lis) ;; 0.009591 real 0.005592 user 0.003994 sysBoom! Wow! It's pretty fast! For this type of very simple pattern match which I believe most of the cases, this match library can be a good alternative.