I need to write a function in Scheme (Racket) that takes two not necessarily equal
length lists and return a list which each element is the sum of the elements of the same
index from the two lists. if the lists’ length is not equal the shorter one should append to itself until it reaches the size of the longer one. for example:
=> (addLists '(1 2 3 4) '(1 2))
(2 4 4 6)
=> (addLists '(1 2 3 4) '(1 2 3 4 5))
(2 4 6 8 6)
Until now I was able to write a function that completes this for equal length lists, but the problem lies with how do I increase the length of the shorter list within this function (or with a helper function that gets a list and a size and extends it properly).
(define (sumListPairs lst1 lst2)
(if (null? lst1) null
(cons (+ (car lst1) (car lst2))
(sumListPairs (cdr lst1) (cdr lst2)))))
Will appreciate any help, Thank you.
This can be done, but since I have the impression that this is a homework exercise, I’ll only give a hint. First, introduce a named-
letto do the actual recursion. Then, expand the number of cases in the recursion from two to four.Now, fill in the
...parts. Note that you have access to all ofl1,l2,lst1andlst2. (You might want to refactor a bit after you’re done, since the cases will be quite similar.)