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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:21:56+00:00 2026-05-22T16:21:56+00:00

I want to solve a problem in Scheme (R5RS). Here’s my sample data: (define

  • 0

I want to solve a problem in Scheme (R5RS). Here’s my sample data:

(define zipcodes '(
 (96774 ookala hawaii)
 (90001 losangeles california)
 (90263 malibu california)
 (10044 newyork newyork)
 ))

Each list element has the form (ZIP CITY STATE). I want to create a function that works like this: pass STATE as an input, and the function returns the zipcodes of this state (returns an empty list if there is no element for this state).

>(findzips 'california)
(90001 900263)

>(findzips 'berlin)
empty

I try to this by code below but it only return first value not a list

(define (findzips x)
  (find x zipcodes))
(define find
  (lambda (x zipcodes)
    (if (EQ? x (caddr (car zipcodes)))
        (list (car (car zipcodes)))
      (find x (cdr zipcodes)))))

I’m not allowed to use ! functions or let.

  • 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-22T16:21:57+00:00Added an answer on May 22, 2026 at 4:21 pm

    First, there’s a typo in the code you posted: find3 should be find.

    Let’s look at that function with proper indentation:

    (define find
      (lambda (x zipcodes)
        (if (EQ? x (caddr (car zipcodes)))    ; if the current entry matches
            (list (car (car zipcodes)))       ; then return it, in a single-element list
          (find x (cdr zipcodes)))))          ; else return the subsequent matches
    

    If the current entry matches, you don’t try to look for any subsequent match. So the list you return will never contain more than one element.

    You always need to look for subsequent elements, i.e. you always need to call (find x (cdr zipcodes)). A good way would be to call let, but your homework assignment rules that out. You can just duplicate the call. Rather than make a single-element list with the list procedure, build a list consisting of the current match followed by the list of subsequent matches. The procedure to add one element to a list is cons.

    (define find
      (lambda (x zipcodes)
        (if (EQ? x (caddr (car zipcodes)))    ; if the current entry matches
            (cons (car (car zipcodes))        ; then return it, followed by…
                  (find x (cdr zipcodes)))    ; … the subsequent matches
          (find x (cdr zipcodes)))))          ; else return the subsequent matches
    

    This isn’t finished: you’ll notice this function always throws an exception. You grind down the list, eventually reach the end… and choke because you aren’t handling the case when the list is empty. I’ll leave this as an exercise.

    Now there are other ways to write this function (in fact, better ways, but harder to understand for a beginner). They may or may not be what was intended in your assignment. Imagine you only wanted to have a single recursive call to find, and no let or any other way to store the result of calling find. Then you have no choice but to call find, but with different arguments depending on whether the element was found or not. How can you do this? (Hint: you’ll need a third argument.)

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

Sidebar

Related Questions

Theres is a little problem I want to solve with Haskell: let substitute a
I want to build my own custom log4j (network) adapter to solve my problem
Someone posted something similar but it didn't really solve the problem. I want to
I have a simple, real life problem I want to solve using an OO
I'm trying to solve pretty easy problem. I want to establish connection to 2
I want to solve x. How can I do it in Scheme? T1-F2=0 F1+T2=GM-Gm
hiere is my code, within a loop. I want to solve my closure problem.
I am new to prolog and i want to solve this problem. Suppose I
I want to solve a problem with a random failures on Visual Studio 2010
I have an optimization problem I want to solve. You have some kind of

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.