0

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. :)

Óscar López
  • 232,561
  • 37
  • 312
  • 386
user3457749
  • 893
  • 1
  • 6
  • 10

2 Answers2

3

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))
kayleefrye
  • 85
  • 1
  • 6
  • This answer is incorrect, test it: throws an error. See my answer for a correct implementation that fixes the mistakes. – Óscar López Jun 15 '14 at 20:22
  • @ÓscarLópez Works fine here. Are you using the most recent Racket version? Lists [are sets](http://docs.racket-lang.org/reference/sets.html) BTW. – kayleefrye Jun 15 '14 at 23:29
  • Hmmm, indeed this seems to be a version-related issue, I was using v5.3.6. In previous releases of Racket you had to explicitly convert the input lists into sets, before calling `set-intersect` on them. Oh well, at least I can say that my solution is backward-compatible :P – Óscar López Jun 15 '14 at 23:46
-1

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))]
 )
)