The procedure to subtract it's very similar to the one that adds, the fundamental difference is that here we don't have to build a new list along the way, instead we'll return a sublist of the list we're subtracting from (ls1
in this code):
; implements unary subtraction between two unary lists
; assuming that length(ls1) >= length(ls2)
; ls1: list we're subtracting from
; ls2: list we're subtracting
; returns ls1 - ls2
(define (usub ls1 ls2)
(if <???> ; is the list we're subtracting empty?
<???> ; then return the list we're subtracting from
(usub <???> <???>))) ; advance the recursion
Clearly, the trick is knowing how to advance the recursion: notice that we're reducing the size of both lists until one of them runs out, at that point we return the other - that's how we subtract. For example:
(usub '(l l l l l) '(l l))
=> '(l l l)