2021-11-09

Nov 10 In-Class Exercise Thread.

Please post your solutions to the Nov 10 In-Class Exercise Thread.
Best,
Chris
Please post your solutions to the Nov 10 In-Class Exercise Thread. Best, Chris
2021-11-10

-- Nov 10 In-Class Exercise Thread
#lang scheme (define (ip x y) (helper x y 0)) (define (helper x y n) (if (or (null? x) (null? y)) n (helper (cdr x) (cdr y) (+ n (* (if (number? (car x)) (car x) 0) (if (number? (car y)) (car y) 0))))))
(Edited: 2021-11-10)
<nowiki> #lang scheme (define (ip x y) (helper x y 0)) (define (helper x y n) (if (or (null? x) (null? y)) n (helper (cdr x) (cdr y) (+ n (* (if (number? (car x)) (car x) 0) (if (number? (car y)) (car y) 0)))))) </nowiki>

-- Nov 10 In-Class Exercise Thread
1 ]=> (define (iner-prod-aux l1 l2 list-so-far)
  (cond ((null? l1) list-so-far)
        (else
         (+ (* (car l1) (car l2))
            (iner-prod-aux (cdr l1) (cdr l2) list-so-far)))))
(define (iner-prod l1 l2) (iner-prod-aux l1 l2 0))
Value
iner-prod-aux
1 ]=> (iner-prod '(1 2 3) '(4 5 6))
Value
iner-prod
1 ]=>
Value
32
(Edited: 2021-11-10)
1 ]=> (define (iner-prod-aux l1 l2 list-so-far) (cond ((null? l1) list-so-far) (else (+ (* (car l1) (car l2)) (iner-prod-aux (cdr l1) (cdr l2) list-so-far))))) (define (iner-prod l1 l2) (iner-prod-aux l1 l2 0)) ;Value: iner-prod-aux 1 ]=> (iner-prod '(1 2 3) '(4 5 6)) ;Value: iner-prod 1 ]=> ;Value: 32

-- Nov 10 In-Class Exercise Thread
(define (ipaux l1 l2 sum)
    (if (or (null? l1) (null? l2))
        sum ;one or both of the lists is empty, so rest of numbers are treated as 0
        (if (and (number? (car l1)) (number? (car l2)))
            (ipaux (cdr l1) (cdr l2) (+ sum (* (car l1) (car l2)))) ;both heads are numbers
            (ipaux (cdr l1) (cdr l2) sum) ;one or the other is not a number
        )
    )
) 
 
(define (ip l1 l2) (ipaux l1 l2 0))
<pre> (define (ipaux l1 l2 sum) (if (or (null? l1) (null? l2)) sum ;one or both of the lists is empty, so rest of numbers are treated as 0 (if (and (number? (car l1)) (number? (car l2))) (ipaux (cdr l1) (cdr l2) (+ sum (* (car l1) (car l2)))) ;both heads are numbers (ipaux (cdr l1) (cdr l2) sum) ;one or the other is not a number ) ) ) (define (ip l1 l2) (ipaux l1 l2 0)) </pre>

-- Nov 10 In-Class Exercise Thread
(define (ip L1 L2) (ip-aux L1 L2 0)) (define (ip-aux L1 L2 sum-so-far) (if (and (null? L1) (null? L2)) sum-so-far (ip-aux (cdr L1) (cdr L2) (add sum-so-far (product (car L1) (car L2))) ))) (define (product item-1 item-2) (cond ((eqv? #f (and (number? item-1) (number? item-2))) 0) ((eqv? #t (or (null? item-1) (null? item-2))) 0) (else (* item-1 item-2))))
<nowiki>(define (ip L1 L2) (ip-aux L1 L2 0)) (define (ip-aux L1 L2 sum-so-far) (if (and (null? L1) (null? L2)) sum-so-far (ip-aux (cdr L1) (cdr L2) (add sum-so-far (product (car L1) (car L2))) ))) (define (product item-1 item-2) (cond ((eqv? #f (and (number? item-1) (number? item-2))) 0) ((eqv? #t (or (null? item-1) (null? item-2))) 0) (else (* item-1 item-2))))</nowiki>

-- Nov 10 In-Class Exercise Thread
(define (ip-acc L1 L2 innerProduct)
	(if (or (null? L1) (null? L2)) innerProduct
                (if (and (number? (car L1)) (number? (car L2)))
		(ip-acc (cdr L1) (cdr L2) (+ innerProduct (* (car L1) (car L2)) ));
) (define (ip L1 L2) (ip-acc L1 L2 0))
(Edited: 2021-11-10)
(define (ip-acc L1 L2 innerProduct) (if (or (null? L1) (null? L2)) innerProduct (if (and (number? (car L1)) (number? (car L2))) (ip-acc (cdr L1) (cdr L2) (+ innerProduct (* (car L1) (car L2)) )); ) (define (ip L1 L2) (ip-acc L1 L2 0))

-- Nov 10 In-Class Exercise Thread
 (define (ip L1 L2)
	(define result 0)
	(print (inner-product-helper L1 L2 result)))
 (define inner-product-helper L1 L2 result
 	(if (or(null? L1) (null? L2))) result
	(define a 0)
	(define b 0)
		(if (number? a) (let a (car L1))
		(if (number? b) (let b L2))
	(+ result (* a b))
	(inner-product (cdr L1) (cdr L2) result)
(Edited: 2021-11-10)
(define (ip L1 L2) (define result 0) (print (inner-product-helper L1 L2 result))) (define inner-product-helper L1 L2 result (if (or(null? L1) (null? L2))) result (define a 0) (define b 0) (if (number? a) (let a (car L1)) (if (number? b) (let b L2)) (+ result (* a b)) (inner-product (cdr L1) (cdr L2) result)

-- Nov 10 In-Class Exercise Thread
 (define (ip a b)
   (iphelp a b 0))
 (define (iphelp a b c)
   (if (null? a) c
       (if (null? b) c
           (iphelp (cdr a) (cdr b) (+ c (* (car a) (car b))))))) 
(define (ip a b) (iphelp a b 0)) (define (iphelp a b c) (if (null? a) c (if (null? b) c (iphelp (cdr a) (cdr b) (+ c (* (car a) (car b)))))))

-- Nov 10 In-Class Exercise Thread
(define (ip-helper L1 L2 total) (if (null? L1) total (if (null? L2) total helper (cdr L1) (cdr L2) (+ total (* (car L1) (car L2)))))) (define (ip L1 L2) (ip-helper L1 L2 0))
<nowiki>(define (ip-helper L1 L2 total) (if (null? L1) total (if (null? L2) total helper (cdr L1) (cdr L2) (+ total (* (car L1) (car L2)))))) (define (ip L1 L2) (ip-helper L1 L2 0))</nowiki>

-- Nov 10 In-Class Exercise Thread
(define (ipaux l1 l2 sum)
    (if (or (null? l1) (null? l2))
        
        (if (and (number? (car l1)) (number? (car l2)))
            (ipaux (cdr l1) (cdr l2) (+ sum (* (car l1) (car l2)))) 
            (ipaux (cdr l1) (cdr l2) sum) 
        )
    )
)
 
(define (ip l1 l2) (ipaux l1 l2 0))
(define (ipaux l1 l2 sum) (if (or (null? l1) (null? l2)) (if (and (number? (car l1)) (number? (car l2))) (ipaux (cdr l1) (cdr l2) (+ sum (* (car l1) (car l2)))) (ipaux (cdr l1) (cdr l2) sum) ) ) ) (define (ip l1 l2) (ipaux l1 l2 0))
[ Next ]
X