(diagram) Check if there is an item in one list in the second list with do cicle

How do we check in the case diagram if the element of the first list is in the second?

+3
source share
1 answer

The do cycle in the racket has an interesting structure:

(do ([id init-expr step-expr-maybe] ...)
    (stop?-expr finish-expr ...)
  expr ...)

The documentation for r5rs gives an example:

(let ((x '(1 3 5 7 9)))
  (do ((x x (cdr x))
       (sum 0 (+ sum (car x))))
      ((null? x) sum)))

This operator returns 25, the sum of the elements in the loop. xin a loop, do is initialized to xin let, and then iteratively sets to cdritself each time through the loop. suminitialized to 0 and accumulates the value car xevery time through. A stop condition is when the iteration variable is empty and the return value is the sum.

, , . . - . , , ( ):

(define (find5 lst)
  (do ([x lst (rest x)]
       [found #f (or found (eq? 5 (first x)))])
    ((null? x) found)))

sum, or found. , first rest car cdr , . , .

(find5 '(1 2 3 4 6))

#f, . :

(find5 '(1 2 3 4 5 6))

#t.

do ?

+5

All Articles