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.