danbaron
28-05-2011, 06:51
Below, there are four function definitions.
Function g takes 6 parameters.
Function h takes 3 parameters.
Function time-g takes 6 parameters, and times function g.
Function time-h takes 3 parameters, and times function h.
' code ---------------------------------------------------------------------------------------------------------------------
#lang racket
(define (g a b c d e f)
(+ (expt a b) (expt c d) (expt e f)))
(define (h a b c)
(expt a (expt b c)))
(define (time-g a b c d e f)
(let ((t1 (current-milliseconds)))
(let ((result (g a b c d e f)))
(let ((t2 (current-milliseconds)))
(let ((tt (/ (- t2 t1) 1000.0)))
tt)))))
(define (time-h a b c)
(let ((t1 (current-milliseconds)))
(let ((result (h a b c)))
(let ((t2 (current-milliseconds)))
(let ((tt (/ (- t2 t1) 1000.0)))
tt)))))
' REPL interactions --------------------------------------------------------------------------------------------------------
Welcome to DrRacket, version 5.1 [3m].
Language: racket.
> (time-g 34565 67656 37916 34854 377252 76301)
1.159
> (time-h 7 7 7)
1.162
>
Is there a way to have just one time function, that will work for both g and h?
Yes.
Instead of passing the individual parameters to the functions, you pass the functions lists which contain the parameters.
:idea:
' code ---------------------------------------------------------------------------------------------------------------------
#lang racket
(define (g l)
(define a (first l))
(define b (second l))
(define c (third l))
(define d (fourth l))
(define e (fifth l))
(define f (sixth l))
(+ (expt a b) (expt c d) (expt e f)))
(define (h l)
(define a (first l))
(define b (second l))
(define c (third l))
(expt a (expt b c)))
(define (time fn l)
(let ((t1 (current-milliseconds)))
(let ((result (fn l)))
(let ((t2 (current-milliseconds)))
(let ((tt (/ (- t2 t1) 1000.0)))
tt)))))
' REPL interactions --------------------------------------------------------------------------------------------------------
Welcome to DrRacket, version 5.1 [3m].
Language: racket.
> (time g (list 34565 67656 37916 34854 377252 76301))
0.965
> (time h (list 7 7 7))
1.209
>
Function g takes 6 parameters.
Function h takes 3 parameters.
Function time-g takes 6 parameters, and times function g.
Function time-h takes 3 parameters, and times function h.
' code ---------------------------------------------------------------------------------------------------------------------
#lang racket
(define (g a b c d e f)
(+ (expt a b) (expt c d) (expt e f)))
(define (h a b c)
(expt a (expt b c)))
(define (time-g a b c d e f)
(let ((t1 (current-milliseconds)))
(let ((result (g a b c d e f)))
(let ((t2 (current-milliseconds)))
(let ((tt (/ (- t2 t1) 1000.0)))
tt)))))
(define (time-h a b c)
(let ((t1 (current-milliseconds)))
(let ((result (h a b c)))
(let ((t2 (current-milliseconds)))
(let ((tt (/ (- t2 t1) 1000.0)))
tt)))))
' REPL interactions --------------------------------------------------------------------------------------------------------
Welcome to DrRacket, version 5.1 [3m].
Language: racket.
> (time-g 34565 67656 37916 34854 377252 76301)
1.159
> (time-h 7 7 7)
1.162
>
Is there a way to have just one time function, that will work for both g and h?
Yes.
Instead of passing the individual parameters to the functions, you pass the functions lists which contain the parameters.
:idea:
' code ---------------------------------------------------------------------------------------------------------------------
#lang racket
(define (g l)
(define a (first l))
(define b (second l))
(define c (third l))
(define d (fourth l))
(define e (fifth l))
(define f (sixth l))
(+ (expt a b) (expt c d) (expt e f)))
(define (h l)
(define a (first l))
(define b (second l))
(define c (third l))
(expt a (expt b c)))
(define (time fn l)
(let ((t1 (current-milliseconds)))
(let ((result (fn l)))
(let ((t2 (current-milliseconds)))
(let ((tt (/ (- t2 t1) 1000.0)))
tt)))))
' REPL interactions --------------------------------------------------------------------------------------------------------
Welcome to DrRacket, version 5.1 [3m].
Language: racket.
> (time g (list 34565 67656 37916 34854 377252 76301))
0.965
> (time h (list 7 7 7))
1.209
>