Définir une fonction (max2 a b)
qui prend deux nombres et renvoie le plus grand, sans utiliser la fonction max
.
; max2: Number Number -> Number
(check-expect (max2 1 3) 3)
(check-expect (max2 5 -1) 5)
(define (max2 a b) (if (> a b) a b))
Définir une fonction (max3 a b c)
qui prend trois nombres et renvoie le plus grand, sans utiliser la fonction max
-- mais on pourra utiliser max2
définie ci-dessus.
; max3: Number Number Number -> Number
(check-expect (max3 1 5 -2) 5)
(define (max3 a b c) (max2 a (max2 b c)))
On définit une Info
comme soit:
Number
)String
)Définir une fonction (grandeur info)
telle que:
(abs n)
qui renvoie la valeur absolue de n
)si l'information est du texte, on renvoie sa taille (string-length
)
; Info is one of: ; - a Number ; - a String (check-expect (grandeur -3) 3) (check-expect (grandeur "toto") 4) (define (grandeur info) (cond [(number? info) (abs info)][(string? info) (string-length info)]))
Définir une fonction incr-list
qui prend une liste d'entiers et renvoie la même liste, où chaque élément est augmenté de 1. Par exemple, si on donne la liste (cons 1 (cons 4 '()))
, le résultat est la liste (cons 2 (cons 5 '()))
.
(check-expect (incr-list (cons 1 (cons 5 '()))) (cons 2 (cons 6 '())))
(define (incr-list li)
(cond
[(empty? li) '()]
[(cons? li) (cons (+ 1 (first li)) (incr-list (rest li)))]))
Définir une fonction count-pos
qui compte le nombre d'entiers strictements positifs (strictement supérieurs à 0) d'une liste. Par exemple, si on donne la liste (cons 4 (cons 0 (cons 8 '())))
, le résultat est le nombre 2
.
(check-expect (count-pos (cons 1 (cons 0 (cons 2 '())))) 2)
(define (count-pos li)
(cond
[(empty? li) 0]
[(cons? li) (+ (if (> (first li) 0) 1 0) (count-pos (rest li)))]))
Définir une fonction list-append
qui prend deux listes et les met bout à bout. Par exemple, (list-append (cons 1 (cons 2 '())) (cons 5 '()))
est la liste (cons 1 (cons 2 (cons 5 '())))
. (Indice: on raisonnera sur la structure du premier argument.)
(check-expect
(list-append
(cons 1 (cons 2 '()))
(cons 5 '()))
(cons 1 (cons 2 (cons 5 '()))))
(define (list-append la lb)
(cond
[(empty? la) lb]
[(cons? la) (cons (first la) (list-append (rest la) lb))]))
Question bonus: définir une fonction qui répond à la spécification suivante:
; init: Number Any -> List-of-Any
; (init n x) returns a list of length n where each element is x
(check-expect (init 0 3) '())
(check-expect (init 2 "foo") (cons "foo" (cons "foo" '())))
(define (init n x)
(cond
[(= n 0) '()]
[else (cons x (init (- n 1) x))]))
Suivre la recette de conception pour la fonction max3
du premier exercice -- je m'attends à voir chaque étape en commentaire, avec le code de la fonction à la fin.
; max3 : Number Number Number -> Number
; returns the largest of the three numbers given
(check-expect (max3 1 -3 -5) 1)
(check-expect (max3 0 1 2) 2)
(check-expect (max3 2 1 1) 1)
(define (max3 a b c) (max2 a (max2 b c)))
Expressions:
(opération argument1 argument2 argument3)
Définition et utilisation de variable:
(define nom-de-variable ...)
(define gregory (string-append "bon" " " "jour"))
gregory
Définition et utilisation de fonction:
(define (nom-de-fonction nom-argument1 nom-argument2 nom-argument-3) ...)
(define (square i) (* i i))
(square 5)
(if truc machin chouette)
(cond
[truc bidule]
[truc machine]
[else chose])
nombres
+ - * /
images
(require 2htdp/image)
(rectangle 10 20 "solid" "blue")
(circle 100 "solid" "red") square star
above beside
image-width image-height
chaînes de caractères (strings)
string-length string-append string-ith
string->number number->string
string=?
booléens
and or not
prédicats de test
image? number? string? boolean?
structures
(define-struct anim [size decr?])
(make-anim s dir)
(anim-size anim)
(anim-decr? anim)
(anim? x)
listes
'() cons first rest empty? cons?
Exemple:
; A TrafficLight is one of the following Strings:
; – "red"
; – "green"
; – "yellow"
; interpretation the three strings represent the three
; possible states that a traffic light may assume
; the state of a traffic light is represented by TrafficLight
; TrafficLight -> TrafficLight
; yields the next state given current state s
(check-expect (traffic-light-next "red") "green")
(define (traffic-light-next s)
(cond
[(string=? "red" s) "green"]
[(string=? "green" s) "yellow"]
[(string=? "yellow" s) "red"]))