Please write function in Racket language.
Write a recursive Racket function "sum-diff" that takes two lists of integers that are the same length and evaluates to an integer. The resulting integer should be the sum of the absolute value of the differences between each pair of integers with the same index in the two lists. For example (sum-diff '(-1+2+3)'(123)) should evaluate to 12 because the absolute value of the differences between (-1 and 1), (-2 and 2) and (-3 and 3) are 2,4, and 6, added together equals 12.
(define sumdiff 0) ;; Defining variable sumdiff and initiation to zero
(define (calcdiffsum list1 list2) ;; Defining calcdiffsum function and accepting two arguments as list
(cond [(or (empty? list1)
(empty? list2)) empty] ;;return as empty if list1 or list 2 value becomes empty
[else (cons
(set! sumdiff (+ sumdiff (- (first list2)
(first list1)))) ;; Subtracting the list 2 first value and list 1 first value & adding that to sumdiff value
(calcdiffsum (rest list1) (rest list2)))])) ;; Calling the function as recursion with removed first values. It will continues until the list becomes empty
(calcdiffsum '(-1 -2 -3) '(1 2 3))
(writeln sumdiff)
Get Answers For Free
Most questions answered within 1 hours.