0

単項数 ( lll = 3 または llll = 4) を含む 2 つのリストを減算する関数を作成しています。

したがって、これを行う必要があります。

~(usub '(l l l l) '(l l))
(l l)

現在、それらを加算する関数はありますが、減算する関数はありません。追加に近いと思いましたが、わかりません。ヘルプ?

(define (uadd ls1 ls2)
  (if (null? ls1) ls2
    (cons (car ls1) (uadd (cdr ls1) ls2))))
4

1 に答える 1

0

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)
于 2013-04-24T16:09:58.447 に答える