Skip to content
Extraits de code Groupes Projets
Valider f9f619b2 rédigé par Nicolas Verbois's avatar Nicolas Verbois
Parcourir les fichiers

Update step_1.rkt : adding more comments and clarifying the tests

parent 7f5000cd
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
#lang r5rs
; Authors : Nicolas Verbois - 1366 1600
; Nicolas Rosar - 1443 1900
; Group F
; In this step, we've evolved the code to handle messages taking additional parameters.
; The messages 'setx and 'add are the two of those new types of messages that we implemented.
; The dispatcher function inside the point object has been renamed "self", as it is more fitting.
; The self now handles the 'setx and 'add messages, and will return the corresponding function when called.
; These functions are also defined in the object, but separately before the self function.
(define (point x y)
; The setx function will simply use "set!" to change the value of x to that of the parameter
(define (setx value)
(set! x value))
; The add function will perform an addition, for each coordinate, of the current point coordinate (x or y)
; and the parameter point corresponding coordinate ((p 'getx) or (p 'gety)).
(define (add p)
(point (+ x (p 'getx)) (+ y (p 'gety))))
(define (self m)
......@@ -10,26 +22,57 @@
((eq? m 'gety) y)
((eq? m 'type) 'point)
((eq? m 'info) (list (self 'type) (self 'getx) (self 'gety)) )
((eq? m 'setx!) setx )
((eq? m 'add) add)
(else (display "Message not understood")))) ; error ?
((eq? m 'setx!) setx) ; the new setx message
((eq? m 'add) add) ; the new add message
(else (display "Message not understood"))))
self)
(display "coucou")
; Sample code illustrating the execution of the step
; We start by creating two points, p1 and p2
(define p1 (point 1 2))
(define p2 (point 3 4))
(display (p1 'getx))
(display (p1 'getx)) ; should display : 1
(newline)
(display (p1 'gety)) ; should display : 2
(newline)
(display (p1 'type)) ; should display : point
(newline)
(display (p1 'info)) ; should display : (point 1 2)
(newline)
(display '----------)
(newline)
(display (p2 'getx)) ; should display : 3
(newline)
(display (p1 'gety))
(display (p2 'gety)) ; should display : 4
(newline)
(display (p1 'type))
(display (p2 'type)) ; should display : point
(newline)
(display (p1 'info))
(display (p2 'info)) ; should display : (point 3 4)
(newline)
(display '----------)
(newline)
; We change the value of the "x" coordinate of point p1 from 1 to 5
((p1 'setx!) 5)
(display (p1 'getx))
(display (p1 'getx)) ; should display : 5
(newline)
(display ((p1 'add) p2))
; We define a new point p3, which is the result of adding p1 and p2
(define p3 ((p1 'add) p2))
(display (p3 'info)) ; should display : (point 8 6)
(newline)
(display (p1 'foo))
(display (p1 'foo)) ; should display : Message not understood
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter