Checksum Scheme

Well, my friend and I tried several times to do this and refused to go to the mentor, because everything they do gives us answers, and this does not help us learn anything.

Currently, I can get the code to do one of two things, return the amount of the list or return the list itself, and we need to get it so that it returns the list and the last digit of the amount. Here are two things that we still have:

(define (add-checksum ls) (cond [(null? ls) 0] [else (+ (car ls) (add-checksum (cdr ls)))])) (define (add-checksum-helper ls) (cond [(null? ls) 0] [else (cons (car ls) (add-checksum-helper (cdr ls)))])) 

The first set of codes adds and returns the amount. The second returns a list plus a .0 , where should the last digit of the sum go? Can anybody help us? Thanks!

EDIT:

 (define (checksum ls) (append ls ((cdr (add-checksum ls))))) (define (add-checksum ls) (cond [(null? ls) 0] [else (+ (car ls) (add-checksum (cdr ls)))])) 

Return:

 (checksum '(4 5 6 7 8)) ((4 5 6 7 8) . 30) 

We need to return (4 5 6 7 8 0) instead of (.30) . We donโ€™t know how easy it is to get 0 . if we do (cdr 30) , we get a list not paired. Any suggestions now?

+4
source share
2 answers

The add-checksum procedure is fine, but you can get the same result using the apply and + procedure in the original list.

For the second part: is it necessary to store the checksum in the last position? it would be much easier to keep it in the first position, cons result of calling add-checksum with the original list.

Finally, if you need to extract the last digit of a number, try something like this with an n positive integer:

 (remainder n 10) 

For instance:

 (remainder 28 10) > 8 
+1
source

It seems like an easy way to do this is to use your two separate functions as helpers for the final function; which calculates the sum that puts this number at the end.

Perhaps your task requires you to go through the list only once and calculate it on the way down, which is a somewhat fictitious requirement, however, since the asymptotic complexity is the same in both cases.

0
source

Source: https://habr.com/ru/post/1436253/


All Articles