首页 文章

在Scheme中检查对象是否为“listdiff”

提问于
浏览
1

listdiff是一对谁的车是L,其cdr是eq?到L,或到(cdr L),或到(cdr(cdr L)))等.listdiff的cdr不必是列表;它可能是任何物体 . listdiff D表示(cd D)之前的(车辆D)的前缀 . 例如,假设ils是不正确的列表(e i o u.y) . 然后(cons ils)返回一个空的listdiff,(cons ils(cdr(cdr ils)))返回一个listdiff,其元素与列表(ae)相同,并且(cons(cdr ils)'y)返回一个listdiff与(eiou)相同的元素 . 相反,(cons'()ils)和(consil(追加'(a e i o u)'y))都不会返回listdiff .

我想在Racket上创建以下过程:

(listdiff?obj)如果obj是listdiff,则返回#t,否则返回#f .

任何人都可以给我指示这样做吗?

1 回答

  • 0

    这是你可以使用的骨架 .

    (define (listdiff? pair)
      (define l (car pair))      ; l
      (define needle (cdr pair)) ; needle is what to look for in l
      (define (search haystack)
        ;; #t when haystack looks the same as needle
        ;; #f if haystackk is the empty list
        ;; otherwise recurse to (cdr haystack)
        )
    
      (search l))
    

相关问题