[racket] Question

From: Rüdiger Asche (rac at ruediger-asche.de)
Date: Wed Sep 5 16:15:37 EDT 2012

Interesting. How does the pattern matcher do that? I tried comparing two solutions in terms of performance (see below, group 1&4 (naive approach) against 2&3 (matching approach)), and aside from the fact that I get somewhat inconsistent results (when I run the test suite several times after one another, I would expect comparable result every time but don't seem to accomplish those even though calling collect-garbage should get me a "clean state" every time), it appears that neither #2 or #3 (#3 being taken from the docs) appears to be significantly worse than #1 or #4 even though the pattern matching and substitution process is expectedly rather sophisticated (and thus more resource consuming)?

P.S. #4 only added for the sake of comparison between the list and '(,...) implementations.

(define (switch-one-and-three1 ls)
    (list (caddr ls)(cadr ls)(car ls)))


(define (switch-one-and-three2 ls)
  (match ls
    [`(,one ,two ,three) `(,three ,two ,one)]))

(define (switch-one-and-three3 ls)
  (match ls
    [(list a b c) (list c b a)]))

(define (switch-one-and-three4 ls)
    `(,(caddr ls) ,(cadr ls) ,(car ls)))
 
 
 
(define iterate (lambda (ct thunk) 
                      (if (zero? ct) 
                         #t (begin (thunk) (iterate (- ct 1) thunk)))))

(define (thunk1) (switch-one-and-three1 '(a b c)))

(define (thunk2) (switch-one-and-three2 '(a b c)))

(define (thunk3) (switch-one-and-three3 '(a b c)))

(define (thunk4) (switch-one-and-three4 '(a b c)))


(collect-garbage)       
       
(time (iterate 10000000 thunk1))

(collect-garbage) 

(time (iterate 10000000 thunk2))

(collect-garbage) 

(time (iterate 10000000 thunk3))

(collect-garbage) 

(time (iterate 10000000 thunk4))


  ----- Original Message ----- 
  From: Matthias Felleisen 
  To: Ashley Fowler 
  Cc: users at racket-lang.org 
  Sent: Wednesday, September 05, 2012 8:29 PM
  Subject: Re: [racket] Question


  #lang racket


  (require rackunit)


  ;; [List X Y Z] -> [List Z Y X]
  ;; switch items one and three in a 3-element list 


  (module+ test 
    (check-equal? (switch-one-and-three1 '(a b c)) '(c b a))
    (check-equal? (switch-one-and-three1 '(1 2 3)) '(3 2 1)))


  (define (switch-one-and-three1 ls)
    (match ls
      [`(,one ,two ,three) `(,three ,two ,one)]))


  That's 50$ -- Matthias




  On Sep 5, 2012, at 2:26 PM, Ashley Fowler wrote:


    Can anybody help me with this problem? I have an idea but would like some suggestions on how to start. The problem is below...


Write a procedure (switch-one-and-three1 LS) that takes a list of 
exactly three items as its argument and returns the list with the first
and third items switched around. You may assume the input list contains 
exactly three items____________________
     Racket Users list:
     http://lists.racket-lang.org/users





------------------------------------------------------------------------------


  ____________________
    Racket Users list:
    http://lists.racket-lang.org/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120905/5bae8f9a/attachment.html>

Posted on the users mailing list.