简体   繁体   中英

Finding the same elements in two list in racket

Assume (list 'red 'blue 'green 'yellow) and (list 'black 'red 'orange 'green) , then it should produce 2 since there are 2 same elements. I only know how to find the same elements in the exact same place as follows:

(define (helper l1 l2)
  (cond
    [(equal? l1) empty]
    [(equal? l2) empty]
    [(equal? (first l1) (first l2)) true]
    [else (rest l1) (rest l2)]))

Help please. :)

In case you're not doing this as a homework exercise, here's Óscar López' code using Racket's set library .

#lang racket

(require racket/set)

(define number-same (compose1 length set-intersect))

(number-same '(red blue green yellow)
             '(black red orange green))

this my code for finding the same elements in two list on racket

(define (cek x y)
 (cond
 [(null? y) 0]
 [(eq? x (car y)) (+ 1 (cek x (cdr y)))]
 [else (cek x (cdr y))]
 )
)
(define (sifat x y)
 (cond
 [(null? x) 0]
 [else (+ (cek (car x) y) (sifat (cdr x) y))]
 )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM