Advertisement
PonaFly

Untitled

Feb 28th, 2019
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.72 KB | None | 0 0
  1. ;делит списки по 0 1 2
  2. (define (splitting lst n r1 r2 r3 l1 l2 l3)  
  3.   (if (and (= n l1)  (= n l2) (= n l3)) (list r1 r2 r3)    
  4.        ( cond ( (= 0 (remainder (car lst) 3)) (splitting (cdr lst) n (cons (car lst) r1) r2 r3 (+ 1 l1) l2 l3))
  5.              ( (= 1 (remainder (car lst) 3)) (splitting (cdr lst) n r1 (cons (car lst) r2) r3 l1 (+ 1 l2) l3))
  6.              ( (= 2 (remainder (car lst) 3)) (splitting (cdr lst) n r1 r2 (cons (car lst) r3) l1 l2 (+ 1 l3))))))
  7. (define (main lst N)
  8.   (define (threelist lst res)
  9.     (if (empty? (car lst)) res
  10.         (threelist (list(cdar lst) (cdadr lst) (cdaddr lst)) (cons (list(caar lst) (caadr lst) (caaddr lst)) res))))
  11.   (threelist (splitting lst N '() '() '() 0 0 0) '()))
  12.  
  13.  
  14. ; 3 task
  15.  
  16. (define (treelist tree)
  17.   (if (empty? tree) '()
  18.       (append (treelist (cadr tree)) (cons (car tree) (treelist (caddr tree))))))
  19. (define (Maxtree lst)
  20.   (define (searchmax lst mx n)
  21.     (if (empty? lst) n
  22.     (cond ((< (car lst) mx) (searchmax (cdr lst) mx n))
  23.         ((= (car lst) mx) (searchmax ( cdr lst) mx (+ 1 n)))
  24.           ((> (car lst) mx) (searchmax (cdr lst)(car lst) (= 1 n))))))
  25.   (searchmax (treelist lst)(car(treelist lst)) 0 ))
  26.  
  27.  
  28. ; 4 task
  29.  
  30. (define (tree-list tree)
  31.  (if (empty? tree) 0
  32.      (+ ( if (and (empty? ( cadr tree)) (empty? (caddr tree))) 1 0)
  33.         (tree-list (cadr tree)) (tree-list (caddr tree)))))
  34.  
  35.  
  36.  
  37. ;5 задача
  38.  (define (Twochildren tree)
  39.     (define (parent tree)
  40.        (if (empty? tree) 0
  41.    (let ((left (parent (cadr tree)))
  42.         (right (parent (caddr tree))))
  43.     (if (or (not left)
  44.             (not right)) #f
  45.     (if (> (abs (- right left)) 0) #f
  46.     (+ (max left right) 1))))))
  47.        (number? (parent tree)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement