Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 383389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:16:41+00:00 2026-05-12T15:16:41+00:00

Let f transform one value to another, then I’m writing a function that repeats

  • 0

Let f transform one value to another, then I’m writing a function that repeats the transformation n times.

I have come up with two different ways:

  • One is the obvious way that
    literally applies the function n
    times, so repeat(f, 4) means x →
    f(f(f(f(x))))
  • The other way is inspired from the
    fast method for powering, which means
    dividing the problem into two
    problems that are half as large
    whenever n is even. So repeat(f, 4)
    means x → g(g(x)) where g(x) =
    f(f(x))

At first I thought the second method wouldn’t improve efficiency that much. At the end of the day, we would still need to apply f n times, wouldn’t we? In the above example, g would still be translated into f o f without any further simplification, right?

However, when I tried out the methods, the latter method was noticeable faster.


;; computes the composite of two functions
(define (compose f g)
  (lambda (x) (f (g x))))

;; identify function
(define (id x) x)

;; repeats the application of a function, naive way
(define (repeat1 f n)
  (define (iter k acc)
    (if (= k 0)
        acc
        (iter (- k 1) (compose f acc))))
  (iter n id))

;; repeats the application of a function, divide n conquer way
(define (repeat2 f n)
  (define (iter f k acc)
    (cond ((= k 0) acc)
          ((even? k) (iter (compose f f) (/ k 2) acc))
          (else (iter f (- k 1) (compose f acc)))))
  (iter f n id))

;; increment function used for testing
(define (inc x) (+ x 1))

In fact, ((repeat2 inc 1000000) 0) was much faster than ((repeat1 inc 1000000) 0). My question is in what aspect was the second method more efficient than the first? Did re-using the same function object preserves storage and reduces the time spent for creating new objects?

After all, the application has to be repeated n times, or saying it another way, x→((x+1)+1) cannot be automatically reduced to x→(x+2), right?

I’m running on DrScheme 4.2.1.

Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:16:41+00:00Added an answer on May 12, 2026 at 3:16 pm

    You’re right that both versions do the same number of calls to inc — but there’s more
    overhead than that in your code. Specifically, the first version creates N closures, whereas
    the second one creates only log(N) closures — and if the closure creation is most of the work
    then you’ll see a big difference in performance.

    There are three things that you can use to see this in more details:

    1. Use DrScheme’s time special form to measure the speed. In addition to the time that it
      took to perform some computation, it will also tell you how much time was spent in GC.
      You will see that the first version is doing some GC work, while the second doesn’t.
      (Well, it does, but it’s so little, that it will probably not show.)

    2. Your inc function is doing so little, that you’re measuring only the looping overhead.
      For example, when I use this bad version:

      (define (slow-inc x)
        (define (plus1 x)
          (/ (if (< (random 10) 5)
               (* (+ x 1) 2)
               (+ (* x 2) 2))
             2))
        (- (plus1 (plus1 (plus1 x))) 2))
      

      the difference between the two uses drops from a factor of ~11 to 1.6.

    3. Finally, try this version out:

      (define (repeat3 f n)
        (lambda (x)
          (define (iter n x)
            (if (zero? n) x (iter (sub1 n) (f x))))
          (iter n x)))
      

      It doesn’t do any compositions, and it works in roughly
      the same speed as your second version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say i have this json data. How to transform the tags to a
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have a scenario where I have a Service that is responsible
I have a MovieClip that consists of multiple layers, one of those layers is
So let's say i have one input like this: <input id=test multiple=true type=file name=image_name[]
Let's say that I have a set of relations that looks like this: relations
Let's say I have 3 point clouds: first that has 3 points {x1,y1,z1}, {x2,y2,z2},
I need to transform one XML document into another using XSLT (for now from
I have a function where I want to transform a list of floats into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.