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

  • Home
  • SEARCH
  • 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 6995923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:05:30+00:00 2026-05-27T20:05:30+00:00

The two examples Kent Dybvig gives in The Scheme Programming Language for letrec and

  • 0

The two examples Kent Dybvig gives in The Scheme Programming Language for letrec and letrec* are:

(letrec ([sum (lambda (x)
             (if (zero? x)
                 0
                 (+ x (sum (- x 1)))))])
   (sum 5))

and

(letrec* ([sum (lambda (x)
            (if (zero? x)
                    0
                (+ x (sum (- x 1)))))]
         [f (lambda () (cons n n-sum))]
         [n 15]
         [n-sum (sum n)])
  (f))

The first can also be written as a named let:

(let sum ([x 5]) 
  ((lambda (x)
                 (if (zero? x)
                     0
                     (+ x (sum (- x 1))))) x))  

and the second can be written as a let with internal defines:

(let ()
  (define sum  (lambda (x)
               (if (zero? x)
               0 
                   (+ x (sum (- x 1))))))
  (define f (lambda () (cons n n-sum)))
  (define n 15)
  (define n-sum (sum n))
  (f))

The letrec/letrec* forms don’t seem any more concise or clearer than the named let or let with internal defines forms.

Can someone show me an example where letrec/letrec* does improve the code or is necessary instead of named let or let with internal defines.

  • 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-27T20:05:31+00:00Added an answer on May 27, 2026 at 8:05 pm

    Yes, the first example can be rewritten using a named let, but note that there is no need for the lambda form in there:

    (let sum ([x 5])
      (if (zero? x)
        0
        (+ x (sum (- x 1)))))
    

    This kind of transformation is a bit misleading — it is fine to do in case you’re defining a single “looping function” (in the broad not-only-tail-recursive sense) and immediately use it on a known input. But usually, when you see examples such as the one you gave, the intention is to show the definition and use of a local function, so it’s possible to do this transformation only because it’s a for-demonstration toy example.

    Secondly, note that a named let is usually not a primitive form — the implementation strategy that practically all Scheme implementations use is to have that form expand into a letrec. It is therefore still a good idea to understand letrec if you want to understand named-lets. (And this is a fundamental feature: being able to do self-reference via a recursive scope.)

    Finally, the example that you gave with internal definitions is similar to named-lets: it is a syntactic sugar that expands into a letrec (which can be either a proper letrec or a letrec* with R5RS, and required to be a letrec* in R6RS). So in order to understand how it works, you need to understand letrec. Note also that some implementation that use strict letrec would also barf at your second example, and complain that sum is undefined. It is this syntactic sugaring that is behind the main argument for the letrec* semantics that was adopted in R6RS: many people like using internal definitions, but then there is a problem that toplevel definitions allow using previous definitions but internal definitions are less convenient in an unexpected way. With letrec*, internal definitions work like toplevel ones. (More precisely, they work like toplevel barring re-definitions, which means that they’re actually like module-toplevel definitions.)

    (Note also that (a) both Racket and Chez extend internal bodies to allow definitions and expressions to be mixed which means that the expansion is as straightforward.)

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

Sidebar

Related Questions

In The Scheme Programming Language by Kent Dybvig (4th edition) section 3.4 , he
I'm going through Kent Dybvig's The Scheme Programming Language in order to learn Scheme
In Chapter 3 of Programming Scala , the author gives two examples of for
Can someone explain why the first of the two following examples is valid and
First, two examples: // This works int foo = 43; long lFoo = foo;
The examples I can find use a two layer architecture, where the controllers directly
I have two examples that first is @OneToOne unidirectional mapping and second bidirectional. In
I have two examples on jsFiddle: http://jsfiddle.net/9uhfX/1/ http://jsfiddle.net/9uhfX/2/ Example 1 In the first one
I have seen two examples that illustrate how the client socket can receive messages
In my course notes these two examples are given. Apparently the first one is

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.