Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List (方案)如何减去一元数列表?_List_Scheme - Fatal编程技术网

List (方案)如何减去一元数列表?

List (方案)如何减去一元数列表?,list,scheme,List,Scheme,我在做一个函数,它减去两个包含一元数(l=3或l=4)的列表 因此,它需要这样做: ~(usub '(l l l l) '(l l)) (l l) 现在,我有一个将它们相加的函数,但没有一个将它们相减的函数。我想这将是接近添加一个,但我不能想出它。帮忙 (define (uadd ls1 ls2) (if (null? ls1) ls2 (cons (car ls1) (uadd (cdr ls1) ls2)))) 减法的过程与加法的过程非常相似,根本的区别在于我们不必在此过程中

我在做一个函数,它减去两个包含一元数(l=3或l=4)的列表

因此,它需要这样做:

~(usub '(l l l l) '(l l))
(l l)
现在,我有一个将它们相加的函数,但没有一个将它们相减的函数。我想这将是接近添加一个,但我不能想出它。帮忙

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

减法的过程与加法的过程非常相似,根本的区别在于我们不必在此过程中构建一个新的列表,而是返回要减法的列表的子列表(
ls1
):


(制造清单(-(长度ls1)(长度ls2))'l)
?@Ankur:答案还是一样的;p(有点讽刺)我只是让一切都与uadd相反(cons(car ls2)(usub ls1(cdr ls2)),但它返回的“l”比它应该的少一个。这不起作用。首先,不需要
cons
在这里,我们是在“破坏”而不是“构建”一个列表
 ; 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
(usub '(l l l l l) '(l l))
=> '(l l l)