2017-09-13 HW03

posted Sep 13, 2017, 6:13 AM by Samuel Konstantinovich   [ updated Sep 14, 2017, 5:50 AM ]
Goal: Finding fun in functions.

New built in function:

(sqrt x)
examples:
(sqrt 9) evaluates to 3.0
(sqrt 100) evaluates to 10.0


Math notation of functions:
We can define functions like in math, the math notation is as follows:
f(x) = x + 1  
f is the name of the function
(x) is the parameter
x+1 is what to do with the parameters

or

sum(a,b) = a + b
sum is the name of the function
a and b are parameters
a + b is what to do with the function

SCHEME notation of functions:

(define  (functionName parameters) (???) )

(define (f x) (+ x 1)  ) 
; this is like f(x) = x + 1


(define (add x y) (+ x y) )
;this is like add(x,y) = x + y 

;Try to make it readable:
(define (function parameters)
 (what_to_do))
;Break up what_to_do into new lines as well if needed.

(define (add x y)
      (+ x y))

Once you define a function, you can use it in all future functions in the same file. This is how we break up problems into manageable chunks!


PRACTICE
;1.Write a function:
;   foo(x) = 2*x + 5




;2. Write a square function! 
;-We will call it sq.
;-FIRST decide: How many parameters does this function need?



;3. More parameters
;Let us try to write a distance function that takes
;two sets of coordinates (x1,y1) and (x2,y2)


(define (dist x1 y1 x2 y2)  
    ;Write this part.
)

Come up with test cases to see if your function works



Homework!

HW03
- Read the rest of the How To CS at stuy document
- Re-read academic dishonesty section (you were supposed to read it but many people didn't)

- Write:  *No header required on this since it is a comment*
1. What are some policies you didn't realize?
2. Create a scheme function (ctof f) that takes a floating point number f that is assumed to be Fahrenheit and converts it to Celsius. Check your function using test values. This is a simple formula to google if you do not remember it. 

(edit typo)
***Submit this as a COMMENT on the homework server HW03 slot.***
 
Comments