Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3: \"Lazy Evaluation\" (2.5 marks) For this question, you will consider the foll

ID: 3748318 • Letter: 3

Question

3: "Lazy Evaluation" (2.5 marks)
For this question, you will consider the following collection of Haskell function definitions
and then evaluate whichever of the expressions (of the following page) corresponds to the
2nd LAST DIGIT of your student number.

puzzle :: Int -> Int -> Int
puzzle a b = a * b
enigma :: Int -> Int -> Int
enigma a b = a - b
secret :: Int -> Int -> Int
secret a b = b - a

If the 2nd LAST DIGIT of your student number is 1, 2, or 3, then trace the evaluation of:
enigma (secret (puzzle 1 3) 5) 7
If the 2nd LAST DIGIT of your student number is 4, 5, or 6, then trace the evaluation of:
puzzle (enigma (secret 2 4) 6) 8
If the 2nd LAST DIGIT of your student number is 7, 8, 9, or 0, then trace the evaluation of:
secret (puzzle (enigma 3 5) 7) 9
To receive full marks for this question you must SHOW (in the correct order) EVERY REDUCIBLE EXPRESSION REPLACEMENT performed by Haskell during the evaluation.
n.b., Not counting the initial expression, each of the above expressions can be reduced to an integer final answer using exactly 6 steps. This includes the steps required for evaluating the arithmetic expression. SHOW EVERY STEP and do not hesitate to create and execute the program from the previous page in order to help you confirm your final answer

Explanation / Answer


Haskell function

puzzle :: Int -> Int -> Int // puzzle function takes two integer inputs and gives one integer output
puzzle a b = a*b // a and b are inputs and a*b is output
engima :: Int -> Int -> Int // engima function takes two integer inputs and gives one integer output
engima a b = a-b // a and b are inputs and a-b is output
secret :: Int -> Int -> Int // secret function takes two integer inputs and gives one integer output
secret a b = b-a // a and b are inputs and b-a is output

1) if the 2nd Last Digit of student number is 1,2, or 3 then evaluation is done by
engima(secret(puzzle 1 3)5)7

-> let's student number's 2nd last digit is 1
so using
engima(secret(puzzle 1 3)5)7
a b
-> puzzle 1 3 gives a*b
= 1*3 = 3 // puzzle a b a*b
-> engima(secret 3 5) 7
-> secret 3 5 gives b-a
= 5-3 = 2 // secret a b b-a
-> engima 2 7 // gives a-b
= 2-7 = -5

2) if the 2nd Last Digit of student number is 4,5, or 6 then evaluation is done by
puzzle (enigma (secret 2 4) 6) 8

-> let's student number's 2nd last digit is 4
so using
puzzle (enigma (secret 2 4) 6) 8
  
-> first evaluating innermost function secret
a b
-> secret(2 4) //inp : a b out : b-a
= 4-2 = 2
-> puzzle( engima 2 6) 8 // replacing secret function by 2
-> engima 2 6 // inp: a b out : a-b
= 2-6 = -4
-> puzzle -4 8 // inp: a b out : a*b
= -4*8 = -32

3) if the 2nd Last Digit of student number is 4,5, or 6 then evaluation is done by
secret(puzzle(engima 3 5) 7) 9
  
-> let's student number's 2nd last digit is 4
so using
secret(puzzle(engima 3 5) 7) 9
-> first evaluating innermost function engima
a b
-> engima 3 5
= 3-5 = -2
-> secret(puzzle -2 7) 9
-> puzzle -2 7
= -2*7 = -14
-> secret -14 9
= 9-(-14) = 23

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote