summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-09-24 20:39:15 -0400
committersiddharth <s@ricketyspace.net>2021-09-24 20:39:43 -0400
commitee5c19327b177e4582491ab165343e852dfbe1d9 (patch)
treedcbe812ebb5f0908a2923a28b4ba5d05c946e278
parent1f0681506de2eb3742889808ad51e68f8975bd96 (diff)
ex: do ex 2.46
-rw-r--r--ex/2/46.rkt51
1 files changed, 51 insertions, 0 deletions
diff --git a/ex/2/46.rkt b/ex/2/46.rkt
new file mode 100644
index 0000000..6f0e33a
--- /dev/null
+++ b/ex/2/46.rkt
@@ -0,0 +1,51 @@
+;;;; License: CC0-1.0
+
+#lang racket
+
+(provide make-vect
+ xcor-vect
+ ycor-vect
+ add-vect
+ sub-vect
+ scale-vect)
+
+(define (make-vect x y)
+ (cons x y))
+
+(define (xcor-vect a)
+ (car a))
+
+(define (ycor-vect a)
+ (cdr a))
+
+(define (add-vect a b)
+ (let ((x (+ (xcor-vect a) (xcor-vect b)))
+ (y (+ (ycor-vect a) (ycor-vect b))))
+ (make-vect x y)))
+
+(define (sub-vect a b)
+ (let ((x (- (xcor-vect a) (xcor-vect b)))
+ (y (- (ycor-vect a) (ycor-vect b))))
+ (make-vect x y)) )
+
+(define (scale-vect s a)
+ (let ((x (* s (xcor-vect a)))
+ (y (* s (ycor-vect a))))
+ (make-vect x y)))
+
+;;; Racket REPL
+;;;
+;;; 46.rkt> (define a (make-vect 8 16))
+;;; 46.rkt> (define b (make-vect 32 64))
+;;; 46.rkt> (xcor-vect a)
+;;; 8
+;;; 46.rkt> (ycor-vect a)
+;;; 16
+;;; 46.rkt> (add-vect a b)
+;;; '(40 . 80)
+;;; 46.rkt> (sub-vect b a)
+;;; '(24 . 48)
+;;; 46.rkt> (scale-vect 2 a)
+;;; '(16 . 32)
+;;; 46.rkt> (scale-vect 2 b)
+;;; '(64 . 128)