In: Computer Science
Hello. Please answer the following two-part question in Scheme. Not Python, not any form of C, but in the language Scheme. If you do not know Scheme, please do not answer the question. I've had to upload it multiple times now. Thank you.
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
Edit: If you can't answer my question then move along to allow someone who can to do so. Thanks.
SCHEME SPLIT
(define split
(lambda (L)
(if (null? L)
'()
(list (first L) (second L)))))
(define first
(lambda (L)
(cond ((null? L) '())
((null? (cdr L)) L)
(else (cons (car L)
(first (cdr (cdr L))))))))
(define second
(lambda (L)
(cond ((null? L) '())
((null? (cdr L)) '())
(else (cons (car (cdr L))
(second (cdr (cdr L))))))))
SCHEME Merge
(define (merge-sort 1 gt?)
(define (merge left right)
(cond
((null? left)
right)
((null? right)
left)
((gt? (car left) (car right))
(cons (car right)
(merge left (cdr right))))
(else
(cons (car left)
(merge (cdr left) right)))))
(define (take l n)
(if (zero? n)
(list)
(cons (car l)
(take (cdr l) (- n 1)))))
(let ((half (quotient (length l) 2)))
(if (zero? half)
l
(merge (merge-sort
(take l half) gt?)
(merge-sort (list-tail l half) gt? )))))
Refer this answer for following question