# Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 1.10 KB  |  hits: 18  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. search through nested list in scheme to find a number
2. ((1 2) (2 3) (3 4) (3 5) (4 5))
3.
4. (1 2)
5.
6. > (flatten '((8) 4 ((7 4) 5) ((())) (((6))) 7 2 ()))
7. (8 4 7 4 5 6 7 2)
8.
9. (define data '((1 2)(2 3)(3 4)(3 5)(4 5)))
10. (solution data 3)
11. -> '((3 4) (3 5))
12.
13. (define data '((1 2)(2 3)(3 4)(3 5)(4 5)))
14. (solution data 1)
15. -> '((1 2))
16.
17. (define (check-pair num p)
18.   (cond
19.     [(= (first p) num) #t]
20.     [else #f]))
21.
22. (define (check-pair num)
23.   (lambda (p)
24.     (cond
25.       [(= (first p) num) #t]
26.       [else #f])))
27.
28. (define (check-pair num)
29.   (lambda (p)
30.     (cond
31.       [(= (first p) num) #t]
32.       [else #f])))
33.
34. (define (solution list num)
35.   (local
36.     ((define check-pair-by-num (check-pair num)))
37.     (filter check-pair-by-num list)))
38.
39. (define data '((1 2)(2 3)(3 4)(3 5)(4 5)))
40.
41. (solution data 1)
42.
43. (define (solve lst num)
44.       (cond
45.         [(null? lst) null]
46.         [(cons? (first lst)) (solve (first lst) num)]
47.         [(member num lst) (cons lst (solve (rest lst) num))]
48.         [#t (solve (rest lst) num)]))
49.
50. (find contains-1? '((1 2)(2 3)(3 4)(5 6)))
51.
52. (member 1 '(1 2)) => #t
53. (member 1 '(3 4)) => #f