0

I am brand-new to Scheme (using Dr. Racket), and this is a homework assignment, so please no outright answers. I've been trying to find a solution online, but have not found one that is satisfactory.

I am writing a procedure that will take one argument - a list of pairs. I then need to find the difference between each pair of two numbers using abs, then add up the differences for all pairs.

My code is below.

(define example2 '((2 -7) (-4 -20) (7 7) (-13 2)))

(define get-difference      
  (lambda (lyst)
    (apply plus (abs(list-ref lyst 0)) (abs(list-ref lyst 1)))))

(define total-error
  (lambda list-of-pairs
    (apply plus (map get-difference list-of-pairs)))) 

I am having trouble with two things (I think): Accessing each pair of data through the whole list Using abs appropriately for each pair in the list.

I can use get-difference if I pass it two numbers,

(define get-difference      
  (lambda (x y)
    (+ (abs x) (abs y))))

but I am trying to pass a list to it instead. From another assignment, I used a get-num function, and it worked correctly - however, this approach did not seem to work for my issue:

(define get-num
  (lambda (lyst)
    (list-ref lyst 1)))

(define average-grade
  (lambda (list-of-grades)
    (/ (apply plus (map get-num list-of-grades)) (length list-of-grades)))) 

Any hints or suggestions are appreciated. Thank you!

EDIT: I forgot to mention - I am not allowed to use recursion or looping.

2
  • 2
    seems like a problem well suited to be solved via folding Commented Feb 3, 2015 at 17:46
  • Thanks for the suggestion! We haven't talked about folding in class, so I can't use it. I will, however, look into it for future use. Commented Feb 3, 2015 at 18:20

1 Answer 1

1

For starters, I think the get-difference function is incorrectly defined. Shouldn't it be like this instead?

(define get-difference      
  (lambda (x y)
    (abs (- x y))))

Back to the main problem. You're on the right track, using apply, map, list-ref will work, but you're combining them in the wrong way. Try this:

(define total-error
  (lambda (list-of-pairs)
    (apply + (map (lambda (pair) ; each element in the list is a pair
                    (get-difference (list-ref pair 0)
                                    (list-ref pair 1)))
                  list-of-pairs))))

Alternatively, we can do the difference-then-addition in a single step, using foldl:

(define total-error
  (lambda (list-of-pairs)
    (foldl (lambda (pair sum)
             (+ (get-difference (list-ref pair 0)
                                (list-ref pair 1))
                sum))
           0
           list-of-pairs)))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I have been confused about where to use apply and map correctly. So, just to make sure I'm clear, list-of-pairs is the argument being passed in to map, and the get difference function is called on each pair (which is passed to lambda as the parameter). I appreciate your help!
@AbigailB that's right. And after map has finished, apply + adds all the differences

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.