How to take intersection of pairs from two lists in scheme?

前端 未结 2 1694
走了就别回头了
走了就别回头了 2021-01-24 00:18

I am using this script from The little schemer, to get intersection of two sets. But I am getting unbound identifier error at \'member?\', can anyone please tell what\'s wrong w

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-24 00:43

    My intersection solution:

    #lang racket
    (define (intersect set1 set2)
      (cond [(empty? set1) '()]
            [(empty? set2) '()]
    
            [(= (caar set1) (caar set2)) (cons (list (caar set1)
                                                     (+ (cadar set1)
                                                        (cadar set2)))
                                               (intersect (cdr set1) (cdr set2)))]
            [(< (caar set1) (caar set2)) (intersect (cdr set1) set2)]
            [else (intersect set1 (cdr set2))]))
    

提交回复
热议问题