Advertisement
zaquest

cpt_lab5

Apr 23rd, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.57 KB | None | 0 0
  1. #!/usr/bin/env racket
  2. #lang racket
  3.  
  4. ; n=10 - размерность матрицы
  5.  
  6. (define nth list-ref)
  7.  
  8. (define (diag-maxs-vec matrix)
  9.     (define (iter i vec)
  10.       (if (= i 19)
  11.         vec
  12.         (iter (add1 i)
  13.               (cons (apply max (diag matrix i)) vec))))
  14.     (iter 0 '()))
  15.  
  16. (define (diag matrix n)
  17.   (let ((beg (diag-beg n))
  18.         (end (coord-move-diag (diag-end n))))
  19.     (define (iter p d)
  20.       (if (equal? p end)
  21.         d
  22.         (iter (coord-move-diag p) (cons (matrix-elem matrix p) d))))
  23.     (iter beg '())))
  24.  
  25. (define (diag-beg n)
  26.   (make-coord (max (- 9 n) 0)
  27.               (max (- n 9) 0)))
  28.  
  29. (define (diag-end n)
  30.   (make-coord (min (- 18 n) 9)
  31.               (min n 9)))
  32.  
  33. (define (matrix-elem matrix coord)
  34.   (nth (nth matrix
  35.             (coord-i coord))
  36.        (coord-j coord)))
  37.  
  38. (define (make-coord i j)
  39.   (cons i j))
  40.  
  41. (define (coord-i c)
  42.   (car c))
  43.  
  44. (define (coord-j c)
  45.   (cdr c))
  46.  
  47. (define (coord-move-diag c)
  48.   (make-coord (add1 (coord-i c)) (add1 (coord-j c))))
  49.  
  50.  
  51. (define matrix (list (list 1 2 3 4 5 6 7 8 9 10)
  52.                      (list 1 2 3 4 5 6 7 8 9 10)
  53.                      (list 1 2 3 4 5 6 7 8 9 10)
  54.                      (list 1 2 3 4 5 6 7 8 9 10)
  55.                      (list 1 2 3 4 5 6 7 8 9 10)
  56.                      (list 1 2 3 4 5 6 7 8 9 10)
  57.                      (list 1 2 3 4 5 6 7 8 9 10)
  58.                      (list 1 2 3 4 5 6 7 8 9 10)
  59.                      (list 1 2 3 4 5 6 7 8 9 10)
  60.                      (list 1 2 3 4 5 6 7 8 9 10)))
  61.  
  62. (diag-maxs-vec matrix)
  63. (length (diag-maxs-vec matrix))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement