apexsquirt

TP4 LIFAP2

Mar 18th, 2020
1,443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 4.71 KB | None | 0 0
  1. #lang racket
  2.  
  3. (define iverson
  4.   (lambda (b)
  5.     (if b 1 0)))
  6.  
  7. (define max
  8.   (lambda (a b) ; NxN->N
  9.     (if (>= a b) a b)))
  10.  
  11.  
  12. (define ev
  13.   (lambda (arbre)
  14.     (iverson (even? (valeur arbre)))))
  15.  
  16. (define Caar
  17.   (lambda (x)
  18.     (if (null? x)
  19.         0
  20.         (if (null? (car x))
  21.             0
  22.             (caar x)))))
  23.  
  24. (define Cadar
  25.   (lambda (x)
  26.     (if (null? x)
  27.         0
  28.         (if (null? (car x))
  29.             0
  30.             (if (null? (cdar x))
  31.                 0
  32.                 (cadar x))))))
  33.  
  34.  
  35. ; RECURSIVITE PROFONDE
  36.  
  37. (define nbsup0
  38.   (lambda (L) ; lists |-> N
  39.     (if (null? L)
  40.         0
  41.         (cond ((list? (car L)) (+ (nbsup0 (car L)) (nbsup0 (cdr L))))
  42.               ((number? (car L)) (+ (iverson (> (car L) 0)) (nbsup0 (cdr L))))
  43.               (else (nbsup0 (cdr L)))))))
  44.  
  45. (define occ-prof
  46.   (lambda (e L) ; symbols*lists |-> N
  47.     (if (null? L)
  48.         0
  49.         (cond ((list? (car L)) (+ (occ-prof e (car L)) (occ-prof e (cdr L))))
  50.               ((symbol? (car L)) (+ (iverson (equal? (car L) e)) (occ-prof e (cdr L))))
  51.               (else (occ-prof e (cdr L)))))))
  52.  
  53. (define absliste
  54.   (lambda (L) ; lists |-> lists
  55.     (if (null? L)
  56.         '()
  57.         (cond ((list? (car L)) (append (absliste (car L)) (absliste (cdr L))))
  58.               ((number? (car L)) (cons (abs (car L)) (absliste (cdr L))))
  59.               (else (cons (car L) (absliste (cdr L))))))))
  60.  
  61. (define profondeur
  62.   (lambda (L) ; lists |-> N
  63.     (if (null? L)
  64.         1
  65.         (cond ((list? (car L)) (max (+ 1 (profondeur (car L)))
  66.                                     (profondeur (cdr L))))
  67.               (else (profondeur (cdr L)))))))
  68.  
  69. ; ARBRES
  70.  
  71. ; PRIMITIVES SUR LES ARBRES
  72.  
  73. (define arbre-vide?
  74.   (lambda (arbre) ; trees |-> bool
  75.     (null? arbre)))
  76.  
  77. (define valeur
  78.   (lambda (arbre) ; trees |-> ?
  79.     (car arbre)))
  80.  
  81. (define fils-g
  82.   (lambda (arbre) ; trees |-> ?
  83.     (cadr arbre)))
  84.  
  85. (define fils-d
  86.   (lambda (arbre) ; trees |-> mixed
  87.     (caddr arbre)))
  88.  
  89. (define cons-binaire
  90.   (lambda (a1 a2 a3) ; mixed^3 |-> trees
  91.     (list a1 a2 a3)))
  92.  
  93. (define arbre-vide
  94.   (lambda ()
  95.     '()))
  96.  
  97. (define arbre=?
  98.   (lambda (a1 a2)
  99.     (equal? a1 a2)))
  100.  
  101. (define arbre?
  102.   (lambda (m) ; mixed? |-> bool
  103.     (cond ((null? m) #f)
  104.           ((not (list? (cdr m))) #f)
  105.           ((null? (cdr m)) #f)
  106.           ((not (list? (cddr m))) #f)
  107.           ((null? (cddr m)) #f)
  108.           ((not (list? (cdddr m))) #f)
  109.           (else (null? (cdddr m))))))
  110.  
  111. (define feuille?
  112.   (lambda (m) ; mixed? |-> bool
  113.     (if (arbre? m)
  114.         (and (null? (cadr m)) (null? (caddr m)))
  115.         #f)))
  116.  
  117. ; FONCTIONS SUR LES ARBRES
  118.  
  119. (define nb-noeuds
  120.   (lambda (tree)
  121.     (cond ((arbre-vide? tree) 0)
  122.           ((feuille? tree) 1)
  123.           (else (+ 1
  124.                    (nb-noeuds (fils-g tree))
  125.                    (nb-noeuds (fils-d tree)))))))
  126.  
  127. (define nb-feuilles
  128.   (lambda (tree)
  129.     (cond ((arbre-vide? tree) 0)
  130.           ((feuille? tree) 1)
  131.           (else (+ (nb-feuilles (fils-g tree))
  132.                    (nb-feuilles (fils-d tree)))))))
  133.  
  134. (define fois2
  135.   (lambda (tree)
  136.     (if (null? tree)
  137.         (arbre-vide)
  138.         (cons-binaire (* 2 (valeur tree))
  139.                       (fois2 (fils-g tree))
  140.                       (fois2 (fils-d tree))))))
  141.  
  142. (define feuilles
  143.   (lambda (tree)
  144.     (cond ((arbre-vide? tree) '())
  145.           ((feuille? tree) (cons (valeur tree) '()))
  146.           (else (append (feuilles (fils-g tree))
  147.                         (feuilles (fils-d tree)))))))
  148.  
  149. (define miroir
  150.   (lambda (tree)
  151.     (if (arbre-vide? tree)
  152.         (arbre-vide)
  153.         (cons-binaire (valeur tree)
  154.                       (miroir (fils-d tree))
  155.                       (miroir (fils-g tree))))))
  156.  
  157. (define pair-impair
  158.   (lambda (arbre)
  159.     (if (arbre-vide? arbre)
  160.         (arbre-vide)
  161.         (if (feuille? arbre)
  162.             (cons-binaire (list (ev arbre)
  163.                                 (- 1 (ev arbre)))
  164.                           '()
  165.                           '())
  166.             (cons-binaire (list (+ (ev arbre)
  167.                                    (Caar (pair-impair (fils-g arbre)))
  168.                                    (Caar (pair-impair (fils-d arbre))))
  169.                                 (+ (- 1 (ev arbre))
  170.                                    (Cadar (pair-impair (fils-g arbre)))
  171.                                    (Cadar (pair-impair (fils-d arbre)))))
  172.                           (pair-impair (fils-g arbre))
  173.                           (pair-impair (fils-d arbre)))))))
  174.  
  175. (define a1 '(3 (2 (1 () ()) ()) (7 (5 (4 () ()) (6 () ())) (9 () ()))))
  176. (define a2 '(3 (2 () (1 () ()) ) (7 (5 (4 () ()) (6 () ())) (9 () ()))))
  177. (define a3 '(3 (2 (1 () ()) (0 () ())) (7 (5 (4 () ()) (6 () ())) (9 () ()))))
  178. (define a4 '())
Advertisement
Add Comment
Please, Sign In to add comment