HW4 + Solutions

posted Sep 19, 2012, 6:25 PM by Samuel Konstantinovich   [ updated Oct 8, 2012, 10:30 PM ]
  • The homework server will be updated with students that were moved around or added this weekend. 
  • You have an exam on Friday September 28th.

Assignment Deadline Tuesday 11:59pm on the homework server.
Homework Assignment 4: Use Lambda notation for this assignment.

1. (EDIT: Changed question) 
Create a (myQuotient a b) function that does the same thing as  (quotient a b). You may use any tools EXCEPT for the existing quotient function. 

2. Write a function called isTheNthPlaceEven? that accepts two parameters x and column. Assume x is an integer, and column is an integer that must be a positive power of ten. The function returns true when that column (10, 100, 1000s place etc) of the number is even, false otherwise. 
(isTheNthPlaceEven? 30325, 100)  evaluates to #f
(isTheNthPlaceEven? 30325, 10)  evaluates to #t
(isTheNthPlaceEven? 66616, 10)  evaluates to #f

3. Write a function called gravity that calculates the gravitational force between two objects.
(gravity m1 m2 D)
Examples: (EDIT: fixed rounding)
(gravity 100000 50000 2) evaluates to 0.0834125
(gravity 25000 95000 3) evaluates to 0.017609305555555555

Follow the formula:
       G m1m2
F = ______

    G = gravitational constant 6.673×10 ^ -11 

    m1 = mass of the first object

    m2 = mass of the second object

    D = the distance between the two masses

;Sample solutions

;1 Define a quotient function without using quotient

(define myQuotient (lambda (a b) (/ (- a (remainder a b))b)))


(define myQuotient (lambda (a b)(-(/ a b)(/(remainder a b)b))))

;2 Is the Nth place even

;by defining a 2nd function

(define getNthPlace (lambda (x place)(remainder (quotient x place)10)))

(define isTheNthPlaceEven? (lambda (x place) (even? (getNthPlace x place))))

;OR writing it as one function

(define isTheNthPlaceEven? (lambda (x place) (even? (remainder (quotient x place)10))))

;3 Gravity is rad...

;Here are 3 ways to define G:

(define G (* 6.673 0.00000000001))

(define G (* 6.673 (expt 10 -11)))

(define G 6.673e-011)

;The function itself:

(define gravity (lambda (m1 m2 D)(/(* m1 m2 G)(* D D))))