09-26-2013 HW*

posted Sep 26, 2013, 5:46 AM by Samuel Konstantinovich   [ updated Oct 3, 2013, 5:25 AM ]
Announcements:
  • You have an exam on Friday October 4th.
  • You have a homework Monday September 30th.

Today's Goal:
 Lambda notation

Remember you can pass a function as a parameter?

Here is a function that composes 2 single parameter functions:
(define (compose f g x) (f(g x)))

We can use it on functions with single parameters such as: 
(define (sq x) (* x x))
(define (cu x) (* x x x))
(define (plus1 x) (+ x 1))
(define (minus1 x) (- x 1))

So think of this:
(compose sq plus1 2)
(compose plus1 cu 2)
(compose minus1 sq 3)


What happens if you want a function to pass as a parameter and don't need it anywhere else?




Here is a sample function
(define (f x y) (* x y) )
Equivalent Lambda Notation:
(define f (lambda (x y) (* x y) ) )

Which is basically the difference between:
(define 
     (functionName parameters) 
     (expression that defines the function) )
and the lambda version:
(define 
   functionName 
   (lambda 
       (list of parameters) 
       (expression that defines the function)  )  )

Classwork:  Write these functions in lambda notation:
a) Square  
b) Plus1
c) SumOf2
d) MeanOf3 

Answers in white:
a) (define Square (lambda (x) (* x x) ) )
b) (define Plus1 (lambda (x) (+ x 1) ) )
c) (define SumOf2 (lambda (x y) (+ x y ) ) ) 
d) (define MeanOf3 (lambda (x y z) (/ (+ x y z) 3 ) ) )





Assignment Deadline Monday 11:59pm on the homework server. (I will NOT ADD ANY PROBLEMS, I will however: fix typos... and post hints. Please read this page a few times before the deadline)
Homework Assignment 2: Use Lambda notation for this assignment.

Please attach a plain text file before you submit. No documents (ever) and no rkt files this time.

1. 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. edit: Assume a and b are integers.




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 (1 for 10's, 2 for 100's, 3 for 1000's place etc) of the number is even, false otherwise. 
examples:
(isTheNthPlaceEven? 30325, 2)  evaluates to #f
(isTheNthPlaceEven? 30325, 1)  evaluates to #t
(isTheNthPlaceEven? 66661, 0)  evaluates to #f

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

Follow the formula:
       G m1m2
F = ______
            D2
Where:

    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



SOLUTIONS:


;1 Define a quotient function without using quotient

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

;OR

(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 (expt 10 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 (expt 10 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))))

Comments