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 8239817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:16:25+00:00 2026-06-07T20:16:25+00:00

I don’t understand what the differences are between (sorry for the contrived example): (define

  • 0

I don’t understand what the differences are between (sorry for the contrived example):

(define average
  (lambda (elems)

   (define length
     (lambda (xs)
      (if (null? xs)
          0
          (+ 1 (length (cdr xs))))))

   (define sum
     (lambda (xs)
      (if (null? xs)
          0
          (+ (car xs) (sum (cdr xs))))))

   (define total (sum elems))

   (define count (length elems))

   (/ total count)))

and

(define average
  (lambda (elems)
   (letrec ((length
              (lambda (xs)
               (if (null? xs)
                   0
                   (+ 1 (length (cdr xs))))))

            (sum
              (lambda (xs)
               (if (null? xs)
                   0
                   (+ (car xs) (sum (cdr xs))))))

            (total (sum elems))

            (count (length elems)))

     (/ total count))))

As far as I can tell, they both create a new scope, and in that scope create 4 local variables that refer to each other and to themselves, and evaluate and return a body.

Am I missing something here, or is letrec synonymous with scoped defines?

I know this may be implementation dependent; I’m trying to get an understanding of the fundamentals of Lisps.

  • 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-06-07T20:16:27+00:00Added an answer on June 7, 2026 at 8:16 pm

    You are correct that there are parallels between the define and letrec versions of your code. However, the devil is in the details. In R5RS, internal define has letrec semantics. In R6RS, internal define has letrec* semantics.

    What’s the difference? Your code has actually just highlighted this difference. As Zhehao’s answer mentions, your definition of total and count inside the same letrec as the length and sum is incorrect: length and sum are not guaranteed to be bound by the time you’re evaluating the values of (length elems) and (sum elems), since the binding of those variables is not guaranteed to be left-to-right.

    letrec* is similar to letrec, but with a left-to-right guarantee. So if you changed your letrec to letrec*, it’d be okay.

    Now, back to my initial comment: because R5RS’s internal define uses letrec semantics, even your define version of the code would be incorrect under an R5RS implementation, but it would be okay under an R6RS implementation, which has letrec* semantics.

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

Sidebar

Related Questions

Don't think that I'm mad, I understand how php works! That being said. I
don't understand: in my controller: @json = User.all.to_gmaps4rails do |user| \Title\: \#{user.email}\ end in
Don't understand, if Data.Map is and [] is. I found this out while wondering
Don't quite understand determinism in the context of concurrency and parallelism in Haskell. Some
Don't freak out about the length of my post, it's pretty simple, I just
Don't understand why #include <Header.h> is not compiling while #include Header.h is compiling with
Don't think this is a repost, difficult to search for the word between because
Don't quite understand why this copy constructor is not invoked when I build with
I don't understand why it is happening. Here is my form: <%= form_tag controller:
don't know if the title describes anything about what I'm trying to say but

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.