Scheme - TD n° 4
15 mars 2000
Fonctions
(define (truc l1 l2) (cons (cons (car l1) (cdr l2)) (cons (car l2) (cdr l1))))Evaluer :
> (truc '(1 b c) '(a 2 3)) ((1 2 3) a b c) > (truc '(the voiture) '(la car)) ((the car) la voiture) > (truc '(ja ca va) '(oui es geht)) ((ja es geht) oui ca va)Faire une fonction (reverse3 l) qui inverse une liste à trois éléments.
(define (reverse3 l) (list (caddr l) (cadr l) (car l)))Faire une fonction (permut3 l) qui donne la liste des permutations d'une liste à trois éléments.
(define (permut3 l)
(let ((a (car l)) (b (cadr l)) (c (caddr l)))
(list (list a b c) (list b c a) (list c a b)
(list b a c) (list a c b) (list c b a))))
Soit les fonctions :(define (f x) (x x)) (define (g y) y)Evaluer :
> (g g) #<procedure:g> > (g f) #<procedure:f> > (f g) #<procedure:g> > (f f) ... pas de réponse !Soit la fonction :
(define (truc a b c d e f) (if (= a 0) (list a b c d e f) (truc b c d e f a)))Evaluer (Partiel 94) :
> (truc 2 1 0 8 9 3) (0 8 9 3 2 1) > (truc 2 9 7 8 5 1) ... pas de réponse !Soit la fonction :
(define (f x) (if (< x 1) 0 (f (- (log x) 1))))Evaluer :
> (f 1) 0 > (f 0) 0 > (f 100) 0
> (jeu0) 49 perdu n=15 > (jeu) 49 trop petit, 70 trop grand, 65 trop grand, 57 trop petit, n=62