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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:26:24+00:00 2026-06-12T17:26:24+00:00

define a procedure ‘reduce-per-key’ which a procedure reducef and a list of associations in

  • 0

“define a procedure ‘reduce-per-key’ which a procedure reducef and a list of associations in which each key is paired with a list. The output is a list of the same structure except that each key is now associated with the result of applying reducef to its associated list”

I’ve already written ‘map-per-key’ and ‘group-by-key’ :

(define (map-per-key mapf lls)
  (cond
    [(null? lls) '()]
    [else (append (mapf (car lls))(map-per-key mapf (cdr lls)))]))

(define (addval kv lls)
  (cond
    [(null? lls) (list (list (car kv)(cdr kv)))]
    [(eq? (caar lls) (car kv)) 
     (cons (list (car kv) (cons (cadr kv) (cadar lls)))(cdr lls))]
    [else (cons (car lls)(addval kv (cdr lls)))]))

(define (group-by-key lls)
  (cond
    [(null? lls) '()]
    [else (addval (car lls) (group-by-key (cdr lls)))]))

how would I write the next step, ‘reduce-per-key’ ? I’m also having trouble determining if it calls for two arguments or three.

so far, I’ve come up with:

(define (reduce-per-key reducef lls)
  (let loop ((val (car lls))
             (lls (cdr lls)))
    (if (null? lls) val
        (loop (reducef val (car lls)) (cdr lls)))))

however, with a test case such as:

(reduce-per-key
   (lambda (kv) (list (car kv) (length (cadr kv))))
   (group-by-key
     (map-per-key (lambda (kv) (list kv kv kv)) xs)))

I receive an incorrect argument count, but when I try to write it with three arguments, I also receive this error. Anyone know what I’m doing wrong?

  • 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-12T17:26:26+00:00Added an answer on June 12, 2026 at 5:26 pm

    Your solution is a lot more complicated than it needs to be, and has several errors. In fact, the correct answer is simple enough to make unnecessary the definition of new helper procedures. Try working on this skeleton of a solution, just fill-in the blanks:

    (define (reduce-per-key reducef lls)
      (if (null? lls)        ; If the association list is empty, we're done
          <???>              ; and we can return the empty list.
          (cons (cons <???>  ; Otherwise, build a new association with the same key 
                      <???>) ; and the result of mapping `reducef` on the key's value
                (reduce-per-key <???> <???>)))) ; pass reducef, advance the recursion
    

    Remember that there’s a built-in procedure for mapping a function over a list. Test it like this:

    (reduce-per-key (lambda (x) (* x x))
                    '((x 1 2) (y 3) (z 4 5 6)))
    
    > '((x 1 4) (y 9) (z 16 25 36))
    

    Notice that each association is composed of a key (the car part) and a list as its value (the cdr part). For example:

    (define an-association '(x 3 6 9))
    (car an-association)
    > 'x       ; the key
    (cdr an-association)
    > '(3 6 9) ; the value, it's a list
    

    As a final thought, the name reduce-per-key is a bit misleading, map-per-key would be a lot more appropriate as this procedure can be easily expressed using map … but that’s left as an exercise for the reader.

    UPDATE:

    Now that you’ve found a solution, I can suggest a more concise alternative using map:

    (define (reduce-per-key reducef lls)
      (map (lambda (e) (cons (car e) (map reducef (cdr e))))
           lls))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define stored procedure parameters which do not allow null arguments. alt
Using inequality operators, I have to define a procedure weekend which takes a string
I am learning Scheme and want to write a recursive procedure which output to
Define a procedure, same_structure, that takes two inputs. It should output True if the
I want to define record with procedure or function. Can you help with syntax?
In Chapter 16 of The Seasoned Schemer, the authors define a recursive procedure depth,
I defined the Scheme procedure to return another procedure with 2 parameters : (define
I have a stored procedure which accepts a User defined table type called SeasonTable
I have a stored procedure in which i want to create a user defined
Is the order in which parameters are calculated before a procedure is called defined

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.