Implémenter la fonction suivante:
; same-first-and-second-letter? : String -> Bool
; are the first and second letter of the input the same?
(check-expect (same-first-and-second-letter? "yacine") #false)
(check-expect (same-first-and-second-letter? "lloris") #true)
; same-first-and-second-letter? : String -> Bool
; are the first and second letter of the input the same?
(check-expect (same-first-and-second-letter? "yacine") #false)
(check-expect (same-first-and-second-letter? "lloris") #true)
(define (same-first-and-second-letter? str)
(and
(>= (string-length str) 2)
(string=? (string-ith str 0) (string-ith str 1))))
On définit deux structures de formes géométriques ("shapes") avec des couleurs.
; height: Number (hauteur)
; width: Number (largeur)
; color: String
(define-struct rect-shape [height width color])
; radius: Number (rayon)
; color: String
(define-struct circle-shape [radius color])
On appelle Shape
un valeur qui est soit un rect-shape
soit un circle-shape
; A Shape is one of the following:
; - a rect-shape
; - a circle-shape
Vous devez implémenter les deux fonctions suivantes:
; colors are represented as String
; change-color: Shape -> String -> Shape
; takes a shape and a color, and returns a modified shape of that color
(check-expect
(change-color (make-rect-shape 10 20 "blue") "red")
(make-rect-shape 10 20 "red"))
(check-expect
(change-color (make-circle-shape 10 "blue") "green")
(make-circle-shape 10 "green"))
et:
(require 2htdp/image)
; draw-shape: Shape -> Image
; converts a shape into an image
(check-expect
(draw-shape (make-rect-shape 10 20 "blue"))
(rectangle 10 20 "solid" "blue"))
(check-expect
(draw-shape (make-circle-shape 10 "blue"))
(circle 10 "solid" "blue"))
; height: Number (hauteur)
; width: Number (largeur)
; color: String
(define-struct rect-shape [height width color])
; radius: Number (rayon)
; color: String
(define-struct circle-shape [radius color])
; A Shape is one of the following:
; - a rect-shape
; - a circle-shape
; colors are represented as String
; change-color: Shape -> String -> Shape
; takes a shape and a color, and returns a modified shape of that color
(check-expect
(change-color (make-rect-shape 10 20 "blue") "red")
(make-rect-shape 10 20 "red"))
(check-expect
(change-color (make-circle-shape 10 "blue") "green")
(make-circle-shape 10 "green"))
(define (change-color shape color)
(cond
[(rect-shape? shape)
(make-rect-shape (rect-shape-height shape) (rect-shape-width shape) color)]
[(circle-shape? shape)
(make-circle-shape (circle-shape-radius shape) color)]))
(require 2htdp/image)
; draw-shape: Shape -> Image
; converts a shape into an image
(check-expect
(draw-shape (make-rect-shape 10 20 "blue"))
(rectangle 10 20 "solid" "blue"))
(check-expect
(draw-shape (make-circle-shape 10 "blue"))
(circle 10 "solid" "blue"))
(define (draw-shape shape)
(cond
[(rect-shape? shape)
(rectangle
(rect-shape-height shape)
(rect-shape-width shape)
"solid"
(rect-shape-color shape))]
[(circle-shape? shape)
(circle
(circle-shape-radius shape)
"solid"
(circle-shape-color shape))]))
; all-strictly-positive? : List Number -> Bool
; all-strictly-positive? checks that all numbers in a list
; are strictly positive? (> n 0)
(check-expect (all-strictly-positive? (cons 1 '())) #true)
(check-expect (all-strictly-positive? (cons 2 (cons -1 '()))) #false)
; all-strictly-positive? : List Number -> Bool
; all-strictly-positive? checks that all numbers in a list
; are strictly positive? (> n 0)
(check-expect (all-strictly-positive? (cons 1 '())) #true)
(check-expect (all-strictly-positive? (cons 2 (cons -1 '()))) #false)
(define (all-strictly-positive? li)
(cond
[(empty? li) #true]
[(cons? li) (and (> (first li) 0) (all-strictly-positive? (rest li)))]))
Suivre la recette de conception pour implémenter une fonction maxi
qui prend deux nombres et renvoie le plus grand des deux.
; maxi: Number Number -> Number
; prend deux nombres et renvoie le plus grand des deux
(check-expect (maxi 1 2) 2)
(check-expect (maxi -3 3) 3)
(check-expect (maxi 4 4) 4)
(define (maxi a b) (if (> a b) a b))
Expressions:
(opération argument1 argument2 ...)
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 ...) ?)
(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"]))