Hello,
Given a list of numbers, say, (1 3 6 10 0)
, how do you compute differences (xi - xi-1), provided that you have x-1 = 0 ?
(the result in this example should be (1 2 3 4 -10)
)
I've found this solution to be correct:
(define (pairwise-2 f init l) (first (foldl (λ (x acc-data) (let ([result-list (first acc-data)] [prev-x (second acc-data)]) (list (append result-list (list(f x prev-x))) x))) (list empty 0) l))) (pairwise-2 - 0 '(1 3 6 10 0)) ;; => (1 2 3 4 -10)
However, I think there should be more elegant though no less flexible solution. It's just ugly.
I'm new to functional programming and would like to hear any suggestions on the code.
Thanks.