From ee5c19327b177e4582491ab165343e852dfbe1d9 Mon Sep 17 00:00:00 2001 From: siddharth Date: Fri, 24 Sep 2021 20:39:15 -0400 Subject: ex: do ex 2.46 --- ex/2/46.rkt | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 ex/2/46.rkt (limited to 'ex/2') 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) -- cgit v1.2.3