Problem Set 1

Suggested due date: Thursday Aug 3 23:59.

Problem 1

pass <- function(mark){
  # Note: this is the same as just writing
  # mark >= 50
  # (why?)
  if(mark >= 50){
    TRUE
  }else{
    FALSE
  }
}

did_pass_all_three <- function(marks){
  pass(marks[1]) & pass(marks[2]) & pass(marks[3])
}

Please step through the following. You should follow the rules for stepping as descibed in class. On Gradescope, submit a link to your GPT-4 interaction, where ChatGPT confirms that your stepping is correct.

pass(87) # Q1(a)

did_pass_all_three(c(94, 85, 96)) # Q1(b)

did_pass_all_three(c(42, 85, 96)) # Q1(c)

Problem 2

f <- function(x){
  2*x
}

g <- function(y)
{
  5*y
}

h <- function(z){
  10*z
}

Step through the following. Make sure that at the end ChatGPT confirms that you stepped through the program correctly.

f(g(h(1))) # Q2.

Instructions for stepping through function calls

Paste all of the following into ChatGPT

Here is an example of how I would like to step through a function call in R:

my_abs <- function(x){
  if(x>=0){
     x
  }else{
     -x
  }
}

--------------
my_abs(-5)
--------------
if(-5>=0){
     -5
  }else{
     -(-5)
  }
--------------
if(FALSE){
     -5
  }else{
     5
  }
}

--------------
5



I need to trace through the following, using the same format as above. I must use eager evaluation, I must perform one step at a time, and I must not skip any steps. Here is an example of an attempt.

sq.roots <- function(A, B, C){
  disc <- B**2 - 4*A*C
  if(disc > 0){
       c((-B-sqrt(disc))/(2*A), (-B+sqrt(disc))/(2*A))
 }else if(disc == 0){
      -2/(2*A)
 }else{
      cat("ERROR")
}

sq.roots(1, 2, 1)


MY SOLUTION:
sq.roots(1, 2, 1)
-----------------
disc <- 2**2 - 4*1*1
  if(disc > 0){
       c((-2-sqrt(disc))/(2*1), (-2+sqrt(disc))/(2*1))
 }else if(disc == 0){
      -2/(2*1)
 }else{
      cat("ERROR")
}
--------------------------------------
  if(0> 0){
       c((-2-sqrt(0)/(2*1), (-2+sqrt(0))/(2*1))
 }else if(0 == 0){
      -2/(2*1)
 }else{
      cat("ERROR")
}
---------------------------------
-2/(2*1)
---------------------------------
-1

The attempt is almost right, except a step was missed: I should have evaluated 0>0 to FALSE and 0==0 to TRUE before proceeding.

Here is another exercise. Please check it line-by-line to see if the stepping is correct.

Then paste the code you are stepping through, along with your solution. Click on the “up arrow” button in the top-right corner of the ChatGPT webpage to get a link.

Your submission should contain four links to chat transcripts (one link for Q1(a), Q1(b), Q1(c), and Q(2) each). You should only submit links