notes

2017-09-26 HW09

posted by Samuel Konstantinovich   [ updated ]

SOLUTIONS TO Recent HW:
(define (isBetween a b n)
                    (or
                     (and (> n a) (< n b))
                     (and (< n a) (> n b))))
;alternate
;                   (and (> n (min a b)) (< n (max a b)))))

;alternate 2 which is similar to the 1st one
;but you were told not to use this 
;                   (or 
;                     (< a n b)
;                     (> a n b))))



(define (xor a b)
              (and (or a b)(not (and a b))))
;alternate:   (not (equal? a b)))) ;this is not using boolean logic however
;There are only 4 test cases:
(xor #true #true)
(xor #true #false)
(xor #false #true)
(xor #false #false)

(define  (alarmTime day isVacation)
  (if isVacation
      ;vacation
      (if (or (= day 0)(= day 6 )) 
          12 ;weekend
          10);weekday
      ;not vacation
      (if (or (= day 0)(= day 6 ))
          10 ;weekend
          7)));weekday
;Sample test cases:
(alarmTime 0 #true)
(alarmTime 2 #true)

(define(gradeFix n)
  (cond
    ((>= n 90)n)
    ((> n 86)88)
    ((> n 82)85)
    (else 50)))


Lab Should be doable in class!
-place as a comment in the homework 9 slot when you finish
-If not done in class finish at home. 
-These are much easier than the homework due Thursday.

You have 6 functions to write (many do not use if/cond)
If your code works for ONLY my test cases it is wrong. It must work as described, for all test cases. You must come up with your own test cases to convince yourself that it works. 

a) Write a function (or35 n) that accepts a non-negative integer and returns a boolean. 
-Return true if the given non-negative number is a multiple of 3 or a multiple of 5, false otherwise
(or35 6)  → #true
(or35 10) → #true
(or35 8)  → #false

b) Write a function (old35 n) that accepts a non-negative integer and returns a boolean. 
-Return true if the given non-negative number is a multiple of 3 or 5, but not both, false otherwise
(old35 6)  → #true
(old35 10) → #true
(old35 15) → #false


c) Write a function (more20 n) that accepts a non-negative integer and returns a boolean. 
-Return true if the given non-negative number is 1 or 2 more than a multiple of 20, false otherwise
(more20 20) → #false
(more20 21) → #true
(more20 22) → #true

d) Write a function (less20 n) that accepts a non-negative integer and returns a boolean. 
-Return true if the given non-negative number is 1 or 2 less than a multiple of 20, false otherwise.
(less20 18) → #true
(less20 19) → #true
(less20 20) → #false
(less20 21) → #false

Write a function (nearTen n) that accepts a non-negative integer and returns a boolean. 
-Given a non-negative number, return true if num is within 2 of a multiple of 10, false otherwise.
(nearTen 12) → #true
(nearTen 17) → #false
(nearTen 19) → #true


Write a function (categorize n) that accepts a non-negative integer and returns one of several categories:
-> "strange"  when n is a multiple of 3 or 5 but not both. 
-> "charm"    when n is a multiple of both 3 and 5.
-> "down"     when n is within 2 of a multiple of 10. This has lower priority than "strange" or "charm".
-> "up"         for all other numbers.
Do not re-write code that you have previously created. Use functions that you have previously defined.
(categorize 15) → "charm"
(categorize 20) → "strange"
(categorize 30)  "charm"
(categorize 19) → "down"
(categorize 13) → "up"

 

2017-09-25 HW08

posted Sep 25, 2017, 5:59 AM by Samuel Konstantinovich   [ updated ]

Do Now: 
(in notebooks)
0. While working, log in and lock the screen.
1. Write a function (checkSign x) , assume x is a real number.
When x is positive return 1 
When x is negative return 2
When x is neutral return 3
examples:
(checkSign 4.5) -> 1
(checkSign -3) -> 2
(checkSign 0) -> 3

2. Create a (myQuotient a b) function that does the same thing as  (quotient a b). 
-You may use any tools EXCEPT for the quotient function. 
-Assume a and b are positive integers. This means you don't have to test it on negative values, or decimals. (Discuss with neighbors!)
(myQuotient 10 3 ) -> 3
(myQuotient 10 4 ) -> 2
(myQuotient 9 3 ) -> 3

Goal: if vs cond, a cleaner way to make multiple optioins.

Classwork:
1. Rewrite checksign using cond.
2. Rewrite gradefix using cond.
3. 

;Using many if statements connected together
;starts to make the code go across the screen:

(define (f g)
  (if (> g 99)
     "OK"
     (if (> g 90)
        "Whats wrong n00b?"
        (if (> g 80)
            "At least I will feed you..."
            "No soup for you!")))

;Scheme has a better way to do this!

(cond
   ( <clause> )
   ( <clause> )
   ( <clause> )
...
   ( <clause> )
)

;Each clause has 2 parts which can be either:
a value (no parens) or an expression (with parens)
boolean    value   
(> x 2)    5
#T         (+ x 1)
else is the same as #T, and should be placed last 
if you want a 'catch all' case.

(cond
   (  ( > x 10)  2 )   
   (  ( > x 20)  3 )
   (  ( < x 0)  4 )
)

when x is 11 you get 2
when x is 19 you get 2
when x is 21 you get 2 because the 1st line is true,
and it doen't get past it.
when x is -11 you get 4
when x is 5 you get NOTHING!!!! this is really bad

(cond
   (  ( > x 20)  3 ) ; now we fixed the 3 never showing up
   (  ( > x 10)  2 )
   (  ( < x 0)  4 )
   (  #true  5 )  ; now we fixed the no-result from happening
   ;because #true will always happen no matter what
)

when x is 11 you get 2
when x is 19 you get 2
when x is 21 you get 3
when x is -11 you get 4
when x is 5 you get 5

Example of cond in a function:

(define (condDemo x) (cond ( (< x 10) 2 ) ( (< x 7) 3 ) ( (> x 20) 4 ) ( else 99)));else is the same result                ;as using #true

;we can put booleans into a function directly!

(define (condDemo2 x b) (cond ( (< x 10) 2 ) ;less than 10 ( #false 3 ) ;this never happens ( (> x 20) 4 ) ;more than 20
( b 4 ) ;when b is true and between 10 and 20 ( #true 99) ;default value for all other cases.
))

(condDemo2 15 #true)
(condDemo2 15 #false)


HW08: Due Thursday 8am on HW server. 
Submit a text file with header.
This is a challenging problem set, it does not give many easy examples to build up. The exam won't be this tricky. You have multiple days to think about the problems and work on them.

 (1 Test case reminder, then 2 problem solving parts each with a and b)
0. For both questions and both parts of each question
You must discuss a set of test cases with your neighbors. Test your code with it.
The tests should be enough to convince everyone that your program works!
Include your test cases (commented out, along with your results commented out)

1. Leap Year 
Write a function (LeapYear year) that returns true when the year given is a leap year, false otherwise.
"There is a leap year every year whose number is perfectly divisible by four 
- except for years which are both divisible by 100 and not divisible by 400."
***The second part of the rule affects century years. 
For example: the century years 1600 and 2000 are leap years, 
                       but the century years 1700, 1800, and 1900 are not.

a. Try with if/cond *easier*
b. (LeapYearShort year)  Same problem just  use boolean logic, no if cond (but you need and/or/not) *trickier*

http://www.infoplease.com/spot/leapyear2.html 
This is why we have this complex system for the century years.
2. Military time. 
24 hour time is useful. It prevents you from getting ready for school at 6pm thinking it is 6am. 
Also, there is no such thing as 12am or 12pm, only 12noon, 12midnight. 

 midnight        ->  0:00
 1 am to 12 noon ->  1:00-12:00
 1 pm to 11 pm    -> 13:00-23:00

 Minutes are not changed at all in military time. 

a) Write a function (to24hourtime hour ampm)
It takes an integer hour that is from 1 to 12 inclusive, and an integer ampm that is either 0 or 1, Where 0 represents am, and 1 represents pm. It returns the correct hour as an integer.
Even though 12am and 12pm don't really exist, we will designate 12am as midnight for this problem.
(to24hourtime 12 0) -> 0 ;  this is midnight for our purposes.
(to24hourtime 12 1) -> 12 ; this is noon for our purposes.
(to24hourtime 1 0) -> 1
(to24hourtime 1 1) -> 13
(also you can google military time to learn the conversion rules)
After you write this using cond at first. Try to write it using a mathematical expression!
b) *Challenge! Do your best! If you get stuck think about it later*
(to24hourtimeShort hour ampm) -> same problem using no logic operators or decisions. Use math operations!
Do not use:
if   cond   and  or  not


2017-09-20 HW07 EXAM

posted Sep 20, 2017, 6:22 AM by Samuel Konstantinovich   [ updated Sep 20, 2017, 11:14 PM ]

Goal: Multiple choices!


NOTE: Exam next Friday!

Do Now:

1. Write a function that accepts three side lengths of a triangle a b and c, that assumes c is the longest side. It returns true if that triangle is a right triangle, false otherwise. (Does not require if statements)
(pythCheck 3 4 5)  is #true
(pythCheck 5 4 3)  is #false
(pythCheck 3 4 6)  is #false
After writing it out , run drRacket and test at least one other Pythagorean triples!

Note: You probably cannot check triangles with irrational side measures because of rounding errors.(like a  1 1  sqrt(2) triangle)
 
2. Discuss how can you make (isRight a b c) that uses pythCheck such that it does not assume anything about the longest side?
Write this function! (returns true if the 3 values given are valid lengths of a right triangle. Assume a,b,c are all integers)


Classwork:

Rubber ducky!

We can return words too!
(if (> x 3)
    "Big" 
    "Small")

would show you a value either: (Quotes show up when typing text as a value in computer languages, and when you print it in Scheme)
"Big"
OR
"Small"
but never both.

Branching an if statement:

An if statement is an expression so you can put an if inside of an if:
(if  you_have_cake_ingredients ;outer if
     (if  you_can_bake
          (go_bake_a_cake)
          (go_learn_to_bake) ) ;close the inner if
    (go_buy_cake_stuff)  ) ;close the outer if

Try to make a function: 
(gee x)
- negative numbers turn positive
- zero becomes -1
- positive numbers become double their value.
(gee -3)  is 3
(gee -4) is 4
(gee 0) is -1
(gee 3) is 6
(gee 1.5) is 3.0


Try to make a function:
(calcGrade n) Where n is your grade, you get different results:
>= 90 : "Pass"
From 70 up to but not including 90: "Fail"
<70 : "Disown"



LAB/HW07: Monday 8am on the HW server: (As a comment)
This is a normal-sized assignment, but due to the holiday, if you have any plans that would make you unable to complete it by Monday, please provide a reason when you submit it. 

Directions: Write the following functions in Scheme, including the test cases you used to verify they work:
1.
(isBetween a b n)  where a b and n are all numbers.
    -Return #true when n is strictly between (not equal) a and b.
    -Return  #false otherwise
    -Note: a can be smaller larger or equal to b.
    -Restriction: Scheme allows you to say (< a b n)  to represent  a < b < n, but you should avoid this construct and use your knowledge of compound inequalities!

2.
(xor a b) where a and b are boolean values. This is similar to the or function, but is the exclusive or.

Since it is a boolean function  you can use #true and #false as arguments, and you can only use and/or/not, to operate on the parameters.

Returns: 
    #true when a and b have different truth values.
    #false when they are both the same.
e.g.
(xor #true #true) -> #false
(xor #true #false) -> #true

Restriction: Do this using the three operations:
      and, or, not 
     (There is another easy way to do it but that avoids the heart of the problem.)



3. 
Write a function (alarmTime day isVacation)
Given a day encoded as:
0=Sun, 1=Mon, 2=Tue, ...6=Sat, 
and a boolean isVacation indicating if we are on vacation, return an integer indicating when the alarm clock should ring. 
-Weekdays, the alarm should be 7
-Weekend it should be 10 
-Unless we are on vacation (isVacation is #true) 
Vacation mode changes the times to be:
   -Weekdays should be 10
   -weekends should be 12.

4. Write a function (gradeFix n) such that:
n >=90  return the same number back
n > 86 and n< 90 return an 88
n >82 and n<=86 return an 85
All other cases return a 50.

2017-09-19 HW06

posted Sep 19, 2017, 6:22 AM by Samuel Konstantinovich   [ updated Sep 19, 2017, 6:56 AM ]

Goal: Decision making in scheme (conditionals)

Do Now:
On paper: Write the scheme functions:

(myMaxOf3 a b c) -> do not use the built in max function.

(isMultipleOf x y) that returns true when x is a multiple of y, false otherwise.


MyMax Solutions:
(define (myMax a b)
  (+
   (/ (abs (- a b)) 2)
   (/ (+ a b) 2)))

(define (midpoint a b)
  (/ (+ a b) 2))

(define (dist a b)
  (abs (- a b)))

(define (myMaxAlt a b)
  (+
   (midpoint a b)
   (/ (dist a b) 2)))




Classwork:

If statements:
(if <boolean expression>
  <expression1>
  <expression2>)

When the boolean expression is true, the entire if evaluates to expression1.
When it is false, the entire if evaluates to expression2.
Example1:
(if (> 10 2)
    (+ 1 2)
    (- 2 3))

a) First evaluate the boolean 
(if (> 10 2)
    (+ 1 2)
    (- 2 3))
b) choose which statement to pick
(if #T
    (+ 1 2)
    (- 2 3))
c) Replace the entire if with just that statement
(+ 1 2)
d) evaluate that statement
3

Example2:
(if (= (remainder x 2) 0)
    (> x 0)
    (< x 0))

;assume x is 5:
a) First evaluate the boolean   (= (remainder 5 2) 0) -> (= 1 0) -> #F
(if (= (remainder x 2) 0)
    (> x 0)
    (< x 0))


b) choose which statement to pick
(if #F
    (> x 0)
    (< x 0))
c) Replace the entire if with just that statement
(< x 0)  which is (< 5 0)
d) evaluate that statement
#F



If statements can go inside functions as well:

(define (foo x)
      (if (> x 3)
          1
          2))
(foo 1) is 2
(foo 2) is 2 
(foo 3) is 2
(foo 4) is 1
(foo 5) is 1

(define (bar x y)
      (if (and (> x 0) (> y 0))
          10
          20))

(bar 1 2) is 10
(bar -1 2) is 20
(bar -3 -4) is 20
(bar 4 -9) is 20
(bar 12 5) is 10

YOUR TURN TO WRITE CODE!

Go To SchemingBat
http://bert.stuy.edu/pbrooks/SchemingBat/scheming.py

Lab + HW6
Choose: Simple decisions and do the 1st 5 problems (up to three-part function) 
a. Write and save your work in DrRacket, because the website does not save it for you!
b. BEFORE you submit to the website, come up with 4 test cases.
c. Test your work in drRacket, then paste into the website to verify that you are correct.
d. Submit all 5 problems in the HW06 slot as a comment.

2017-09-18 HW05

posted Sep 18, 2017, 6:25 AM by Samuel Konstantinovich   [ updated Sep 18, 2017, 10:27 AM ]

Do Now:
Write a function 
(midPoint A B) that returns the value of the number halfway between numbers A and B. 
e.g.
(midPoint 1 2) -> 1.5
(midPoint -1 3) -> 1

Challenge Problem:

How might you be able to calculate the maximum of two numerical values values using only:

+ , - , *, / , abs


If you use the method on 5 and 12 the result of the expression should be 12.

If you use the method on 12 and 5 the result of the expression should be 12.

If you use the method on 5 and -12 the result of the expression should be 5.

If you use the method on -2 and 2 the result of the expression should be 2.


Classwork In groups of two:
-Draw a number line and sketch out what happens when you perform operations, this is more helpful than typing random things. Label your diagram with points A and B on the number line.
-Think about it in terms of math:
a. Is there a way to calculate the point halfway between A and B?
b. Is there a way to calculate the distance between A and B?
c. Does it matter if A or B is larger in the prior calculations? If it does, can you modify the calculation so that it doesn't matter?
d. Label the diagram with how you can calculate each portion, then write a formula for the max of A and B as a regular math formula.
e. NOW if you did all of that, you have figured out what you want to write as a scheme function (myMax A B) -> since max is already defined we are writing the function with the prefix that suggests you wrote it, and it is not the build in function.

Goal Booleans.

Depending on which scheme language you chose, True and False are written as:
#T   
#F
or even as:
#true
#false

You can generate booleans from numerical values using comparisons:
(< 3 3)
(<= 3 3)
(= 3 4)
(= 3 3.0)
(> 4 3)

You can use the built in functions: and,or,not  they operate on booleans.
(and #F #T)
(or #F #T)
(not (and #T #F))

You can write a function that returns a boolean as well:
(define (isGreaterThan10 x) (> x 10))
(isGreaterThan10 3)
(isGreaterThan10 13)
(isGreaterThan10 10)

LAB! Submit as HW05 as a comment on the homework server.
1. Define a function that returns true (evaluates to true) when the number is positive, false otherwise.
(this is just a simple test if you can write a boolean function. Normally you just write the boolean out.)
(define (isPositive x)
  (???))

2. Define a function that evaluates to true when a number is even, false otherwise. 
(define (isEven x)
  (???))

3. Define a function that evaluates to true when the parameter is a teen number. Teens: 13,14,15,16,17,18,19
(isTeen x)
(isTeen 12) -> #false
(isTeen 13) -> #true
(isTeen 114) ->#false

4. Define a function that evaluates to true when the sum of two numerical parameters is a teen, false otherwise. 
(isTeenSum x y)



2017-09-15

posted Sep 15, 2017, 6:18 AM by Samuel Konstantinovich

Do Now:

Log in, then lock your screen. (winkey-L or top right menu with switch user)


In your notebook:

    Write a function (SumOfSquares a b) that takes numeric inputs a, b and returns the sum of their squares.

e.g.,

    (SumOfSquares 0 0) → 0

    (SumOfSquares 1 2) → 5

    (SumOfSquares 4 5) → 41


Goal: Explore more functions, get comfortable poking around

Go Over Get Ones/Tens/Nth Digit

Classwork:

1.Test your sumOfSquares solution.

2. You are now tasked with figuring out some more information about how some built in functions work.

For each function or functions a-e you must figure out via experimentation and testing (not looking it up)

    i. How many parameters can it take? (don’t just guess, test a few things)

    ii What is the rule for the how the function works? (Cover all cases including when they behave differently when you change the number of parameters. Write answers in English, not code.)

a. Absolute value

b. maximum

c. rounding

d. +,-

e. *,/


As you test, write down your findings for each function on paper, so as to convince me you tested and know what is going on.



Challenge problem (Think about this for later)

How might you be able to calculate the maximum of two numerical values using only:

+

-

*

/

abs


If you use the method on 5 and 12 the result of the expression should be 12.

If you use the method on 12 and 5 the result of the expression should be 12.

If you use the method on 5 and -12 the result of the expression should be 5.

If you use the method on -2 and 2 the result of the expression should be 2.

Hint:  Draw a number line and sketch out what happens when you perform operations, this is more helpful than typing random things.

2017-09-14 HW04

posted Sep 14, 2017, 6:16 AM by Samuel Konstantinovich   [ updated Sep 14, 2017, 7:09 AM ]

DO NOW!

1. Before you have internet access you need to set your proxy settings.
Log in.
Click the Ubuntu circle, type network and click on the network icon.
Go to the proxy tab and select manual

-add the following to TWO lines of the proxy setup tab: http / https 
Proxy:   149.89.1.30  
port: 3128  (the right empty box with +- buttons)


Here is a site which we will use for some scheme practice.
-Click on simple calculations to get a reduced list of problems.
-Test your code will run your own test cases.
-Submit code will run the built in test cases, and tell you if you are correct.

2. Complete as a warm up: 
Increment
Mean-2
Sum2

HOW TO CODE:
-You should NOT write anything on the computer until you have an idea of the solution! Solving problems does not require any computer, the computer is used to help you fix your syntax, or help you realize your solution is not working. (This isn't important when you are just translating a formula)
-You should use paper to jot down ideas, and help you organize your problem solving.
-You should feel free to come up with ideas using your neighbors as support. 
-You should write and test your code in drracket and then paste into the website! (This is much easier than typing into the website directly.)


3. LAB
Remember your tools so far:
+,-,/,*,quotient,remainder, sqrt, define
There is also an exponent function!
(expt a b)   evaluates to: a to the b power.
(expt 3 2)  is 3 squared.
(expt 2 3) is 2 cubed.

This is a lab, that you will complete at home (all labs will just be labeled as Homework)
-Go to SchemingBat
Under simple calculations:
a-Cicle Area
b-Distance
c-getOnesDigit  (This is your first problem to solve. It is not just a formula. Discuss with a neighbor, think out loud with each other!)
d-getTensDigit
e-getNthDigit
Under All exercises:
f-Newtons Gravity
g-discriminant
h-Quadratic Solver Plus

HW04
Submit your lab as a comment in HW04 on the HW server. 
(Slot will be there by the end of the day)
Include your Functions AND any test cases you used to help you code.



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.***
 

2017-09-12 HW02

posted Sep 12, 2017, 6:19 AM by Samuel Konstantinovich   [ updated Sep 12, 2017, 6:25 AM ]

Do Now:
Convert to Scheme notation:
a)  8 + 3 * 2 + 1 
c)  9 - 1 / 2 + 3 * 2
b)   7 - 4
     (5 * 2)2

operators:
+
-
*
/
quotient - the integer portion of the mathematical quotient.
remainder - the leftover portion of the original number when dividing.

Evaluate:
a) (quotient (remainder 12 11) (remainder 5 3) )
b) ( / (- (* 3 2) 5) (* (- 3 2) 5) )



Login:
username: stuy.edu username
password: OSIS

Running DrRacket on the school computers.
1. To open a terminal (shortcut  Ctrl-Alt-T)
Click the ubuntu menu is the top left corner (the funny circle)
type terminal
click the terminal icon

2. In the terminal type 
drracket  
Then press enter


Changing your password:
1) Open the Terminal application on your computer.
2) At the $ prompt type:
Answer yes
It will ask for your password, the cursor 
will not move as you enter it, that is normal.
3) At the $ prompt type:
$ passwd
Follow the instructions to change your password.
4) Close Terminal and log out the computer.



This header is to be included on all submitted assignments
;Last name, First Name
;STUY ID
;MKS21 Period<x>
;YYYY-MM-DD
;HW<x>

e.g.
;Smith, David
;1234
;MKS21 Period4
;2015-09-18
;HW04

HOMEWORK!

HW02:


Answer the following questions, submit a plain text document on the homework server.

1. Convert the infix expression to scheme notation.
 a. ( 5 - 4 * 2 ) / ( 10 - 5 ) + 3
 b.   2 +  3 ( 4 - 2) / ( 1 - 3 / 5 ) 
 c.   4 + 3 * 2 
      ----------   -    1
        5 - 10    

2. Evaluate the expressions (show the steps you took line by line) You may verify your result using DrRacket.
 a. (* (+ 4 -7 9) (/ 15 5) (- 3 -1) )
 b. (/ (- 20 -4)   (* -3 (* 2 -1)))
 c.  (+ 
     (remainder 10 7) 
     (remainder 7 10) 
     (* 
       (quotient 24 10) 
       (remainder 24 10)))




2017-09-11 HW

posted Sep 11, 2017, 5:56 AM by Samuel Konstantinovich



Goal: Infix, prefix, and postfix

Infix notation:
<value1> <operator> <value2>
examples:  
2 + 3  
5 * 2

Prefix notation
<operator> <value1> <value2>
examples:  
+ 2 3     
* 5 2

Postfix notation
<value> <value> <operator>
examples:  
2 3 +      
5 2 *

Scheme notation
Prefix with required the parenthesis: before the operator and after the final operand:
(<operator>  <value1> <value2>)

Some operatiors suppor more than 2 values:
 (<operator>  <value1> <value2> ... <valueN>)

Converting 
converting from infix to prefix requires you to know the order of operations. 
2+ 3 * 4    ->  (+ 2 (* 3 4))
2 * 3 + 4  ->  (+ (*2 3) 4)

Think about where the parenthesis could go for infix to help you convert:
2 * 3 + 4   is really (2 * 3) + 4  which converts more easily.

Be careful when converting... because Scheme has no order of operations, but infix does. You must pay attention to this.

If you want a mechanical way of doing this here:
(4+5) / 6 * 2        Start with this tricky example
(((4 + 5) / 6) * 2)  Add more parenthesis to remind yourself of the order of operations
(* (/ (+4 5) 6) 2)   Now move the operators to the beginning of the set of parenthesis that contains them.


Dr Racket:

Running it
Using it to:
  -evaluate a single line
  -evaluate multiple things
  -Save files

Homework : (Self-assessment so you know you got everything so far)
Test DrRacket at home. 
Test the examples from class in DrRacket,  by evaluating them in the program, and then tracing through it using the stepper.
Make sure the way you evaluate an expression is the same way that DrRacket traces through it. This is open ended... the people that aren't 100% confident should make a few extra attempts at correcting their way of thinking. If you are confident, you should verify that you are correct.

1-10 of 11