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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:04:44+00:00 2026-05-21T15:04:44+00:00

For example, I want to check if an element is in a list. The

  • 0

For example,

I want to check if an element is in a list. The algorithm is straightforward, let’s do it in C++

bool element_of( const std::vector<int>& lst, int elem ) {
    for( int i( 0 ), ie = lst.size(); i < ie; ++i ) 
        if( elem == lst[i] )
            return true;
    return false;
}

Since Scheme don’t let me use single if statement, I can’t do something similar to the C++ code above. Then I came up with a temporary variable, namely result. result will have initial value of #f, next I recursively call the function to check the next item in the list i.e. cdr lst … So my question is, does the variable which created with let restore its initial value each time it enters a new function call or its value stays the same until the last call?

On the other hand, using fold, my solution was,

(define (element-of x lst)
  (fold (lambda (elem result)
          (if (eq? elem x) (or result #t) result)) 
        #f
        lst))

Thanks,

  • 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-21T15:04:45+00:00Added an answer on May 21, 2026 at 3:04 pm

    Each Let call creates a new set of variables in the environment that the main body of the Let is being evaluted in. The Let syntax is a “syntactic sugar” for a lambda being evaluated with arguments passed to it that have been evaluted. For instance

    (let ((a (func object))
          (b (func object2)))
         (cons a b))
    

    is the same as writing

    ((lambda (a b) (cons a b)) (func object) (func object2))
    

    So you can see that in the Let syntax, the arguments are first evaluated, and then the body is evaluated, and the definitions of a and b are utilized in the local environment scope. So if you recursively call Let, each time you enter the body of the Let call, you are evaluating the body in a new environment (because the body is inside a newly defined lambda), and the definition of the arguments defined in the local Let scope will be different (they are actually new variables in a nested environment setup by the new lambda, not simply variables that have been mutated or “re-defined” like you would find in a C++ loop).

    Another way of saying this is that you’re variables will be like the local scope variables in a C++ recursive function … for each function’s stack-frame, the locally scoped variables will have their own definition, and their own memory location … they are not mutated variables like you might see in a loop that re-uses the same memory variables in the local scope.

    Hope this helps,

    Jason

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

Sidebar

Related Questions

I have two List's which I want to check for corresponding numbers. for example
I want to check a particular website from various locations. For example, I see
I have created a separate class (let's call it class2.cs for example) and want
I want to check if an item from a list equals to any one
what I basically need is to check every element of a list and if
I want to check whether an element exists in the whole page or not.
example: I want to see if array[5] holds a value or is empty.
For example I want to be able to programatically hit a line of code
For example: I want: if file1 exists: CLEAN_SRC = *.h file3 else CLEAN_SRC =
for example : I want to remove all highlighted tags alt text http://shup.com/Shup/299976/110220132930-My-Desktop.png

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.