Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- HW4 #5 A
- ScalarProductDoLoop.rkt by Ryan Liszewski
- Calculates the scalar product of the given 2 vectors using a do loop
- produces an error message when the vectors have different lengths or is empty
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|#
- #lang racket
- (define scalar-product
- (lambda (x y)
- (cond((= (vector-length x) (vector-length y))
- (let ((z 0))
- (do ((i 0 (add1 i)))
- ((>= i (vector-length x)) (display z))
- (set! z
- (+ z (* (vector-ref x i) (vector-ref y i)))))))
- (else (display "Error: different sizes of vectors!")))))
- #|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- HW4 #5 B
- ScalarProductRecursive.rkt by Ryan Liszewski
- Calculates the scalar product of the given 2 vectors using recursion
- produces an error message when the vectors have different lengths
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|#
- #lang racket
- (define scalar-product
- (lambda (x y)
- ;If they equal the same size
- (cond ((= (vector-length x) (vector-length y))
- ;FInd scalar product
- (recur (vector->list x) (vector->list y)))
- ;else the size are not the same
- (else (display "Error: different sizes of vectors!")))))
- ;Calls function recursively
- (define recur
- (lambda (list1 list2)
- (cond ((null? (cdr list1)) (* (car list1) (car list2)))
- (else (+ (* (car list1) (car list2))
- (recur (cdr list1) (cdr list2)))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement