In: Computer Science
Please answer this two part question. Thank you!
For this assignment you must write the following functions in Scheme:
2.1 Write a recursive function called eval-poly
that takes a list of numbers representing the coefficients of a
polynomial and a value for ? and evaluates the polynomial for the
given value of ?. The list of coefficients should start with the
term of lowest degree and end with the term of highest degree. If
any term of intermediate degree is missing from the polynomial it
should have a coefficient of zero. For example, the polynomial
?3+4?2+2 would be represented by the list '(2 0 4 1). Hint: the
polynomial above can be rewritten as 2+?⋅(0+?⋅(4+?⋅1))
> (eval-poly '() 0)
0
> (eval-poly '(5) 0)
5
> (eval-poly '(4 3) 2)
10
> (eval-poly '(2 7 1) 3)
32
2.2 Write a tail-recursive version of the
previous problem called eval-poly-tail. It should call a helper
function called eval-poly-tail-helper that uses tail recursion to
keep a running sum of the terms evaluated so far. You might want to
use the expt function to take a number to a power.
> (eval-poly-tail '() 0)
0
> (eval-poly-tail '(5) 0)
5
> (eval-poly-tail '(4 3) 2)
10
> (eval-poly-tail '(2 7 1) 3)
32
I have implemented the above problem using python.
1) Code
Output
2) Using Tail Recurssion
Code
Output