Pauan

Untitled

Nov 29th, 2011
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.29 KB | None | 0 0
  1. (define (blank? x)
  2.   (eq? x #\.))
  3.  
  4. (define (xcar x)
  5.   (if (cons? x)
  6.         (car x)
  7.       x))
  8.  
  9. (define (xcdr x)
  10.   (if (cons? x)
  11.         (cdr x)
  12.       x))
  13.  
  14. (define (contiguous-count x top)
  15.   (local [(define (loop l r n seen)
  16.             (cond ((null? l)
  17.                     n)
  18.                   ((blank? (car l))
  19.                     (if (blank? (xcar r))
  20.                           (loop (cdr l)
  21.                                 null
  22.                                 (+ n 1 (contiguous-count (cdr x) #f))
  23.                                 #t)
  24.                         (loop (cdr l) (xcdr r) (+ n 1) #t)))
  25.                   ((and seen top (null? (cdr x)))
  26.                     (loop (cdr l) (xcdr r) 0 #f))
  27.                   (else
  28.                     (loop (cdr l) (xcdr r) n seen))))]
  29.     (loop (car x) (xcar (cdr x)) 0 #f)))
  30.  
  31. (define (contiguous? x (num 5))
  32.   (>= (contiguous-count x #t) num))
  33.  
  34.  
  35. (check-expect (contiguous? '((#\. #\. #\. #\. #\.)))
  36.               #t)
  37.  
  38. (check-expect (contiguous? '((#\. #\T #\. #\.) (#\. #\. #\. #\T) (#\T #\T #\T #\T)))
  39.               #t)
  40.  
  41. (check-expect (contiguous? '((#\T #\T #\. #\.) (#\T #\T #\. #\.) (#\T #\T #\T #\.)))
  42.               #t)
  43.  
  44. (check-expect (contiguous? '((#\. #\. #\. #\.) (#\T #\T #\. #\T) (#\T #\T #\T #\T)))
  45.               #t)
  46.  
  47. (check-expect (contiguous? '((#\. #\. #\T #\. #\. #\.)))
  48.               #f)
  49.  
  50. (check-expect (contiguous? '((#\T #\T #\. #\.) (#\. #\. #\T #\T) (#\T #\T #\T #\.)))
  51.               #f)
  52.  
  53. (check-expect (contiguous? '((#\. #\. #\. #\.) (#\T #\T #\T #\T) (#\T #\T #\T #\.)))
  54.               #f)
  55.  
  56.  
  57. (check-expect (contiguous-count '((#\. #\. #\. #\. #\.)) #t)
  58.               5)
  59.  
  60. (check-expect (contiguous-count '((#\. #\T #\. #\.) (#\. #\. #\. #\T) (#\T #\T #\T #\T)) #t)
  61.               6)
  62.  
  63. (check-expect (contiguous-count '((#\T #\T #\. #\.) (#\T #\T #\. #\.) (#\T #\T #\T #\.)) #t)
  64.               5)
  65.  
  66. (check-expect (contiguous-count '((#\. #\. #\. #\.) (#\T #\T #\. #\T) (#\T #\T #\T #\T)) #t)
  67.               5)
  68.  
  69. (check-expect (contiguous-count '((#\. #\. #\T #\. #\. #\.)) #t)
  70.               3)
  71.  
  72. (check-expect (contiguous-count '((#\T #\T #\. #\.) (#\. #\. #\T #\T) (#\T #\T #\T #\.)) #t)
  73.               2)
  74.  
  75. (check-expect (contiguous-count '((#\. #\. #\. #\.) (#\T #\T #\T #\T) (#\T #\T #\T #\.)) #t)
  76.               4)
Advertisement
Add Comment
Please, Sign In to add comment