In: Computer Science
IN SCHEME
Write a function subtract which takes a list of numbers as a parameter and returns a list of the differences. So, a list containing the second minus the first, third minus the second, etc.
(subtract '(3 4 10 14 5))
'(1 6 4 -9)
CODE:
(define (subtract list)
(cond ((null? list) '()) ;;If list is empty returns null list
((null? (cdr list)) '()) ;;If only one element in the list return
empty list
(else (cons (- (cadr list) (car list)) (subtract (cdr list))))))
;;else add (second elelemnt - first element) to list and
continue
Snapshot of Code and Output: