Question

In: Computer Science

Hello. Please answer the following two-part question in Scheme. Not Python, not any form of C,...

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.

Solutions

Expert Solution

  • 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


Related Solutions

Hello. Please answer the following two-part question in Scheme. Not Python, not any form of C,...
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...
Hello. Please answer the following two-part question in Scheme. Not Python, not any form of C,...
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. 3.1 Write a recursive function called split that takes a list and returns a list containing two lists, each of which has roughly half the items in the original list. The easiest way to do this is...
Hello. Please answer the following question in Scheme. Not Python, not any form of C, but...
Hello. Please answer the following 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. 1.2 Write a function called countdown that takes a positive integer and uses a do expression to display a sequence of numbers counting down to 1, each on its own line, then displaying the string "Blastoff!". > (countdown...
Hello. Please answer the following question in Scheme. Not Python, not any form of C, but...
Hello. Please answer the following 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. 4 Write a recursive function called mergesort that sorts a list by doing the following: (a) Use split to split the list into two roughly equal-sized partitions. (b) Recursively sort both partitions. (c) Use merge to merge the...
Hello there , can any one please find an answer for this question Q. a single...
Hello there , can any one please find an answer for this question Q. a single lossless dielectric slab of a thickness “d” and permittivity “ε2” is inserted between two regions. the first region is the free space and the second one is a lossless dielectric material of permittivity “ε3”. a uniform plane wave propagating in y- direction and polarized in z-direction is normally incident on the first region. derive an analytical expression for the total reflection coefficient and the...
Hello, please answer this question Compare and contrast the two major causes of hemolytic disease of...
Hello, please answer this question Compare and contrast the two major causes of hemolytic disease of the newborns.
Hello, I'm confused on part 'C' on how to graph the answer I received in part...
Hello, I'm confused on part 'C' on how to graph the answer I received in part 'B' B. A steam boiler is required as part of the design of a new plant. The types of fuel that can be used to ignite the boiler are natural gas, fuel oil, and coal. The cost of installation including all required controls is $40,000 for natural gas, $50,000 for fuel oil, and $120,000 for coal. In addition, the annual cost of fuel oil...
HELLO, PLEASE ANSWER THE FOLLOWING QUESTION. IT WAS ASSIGNED WITH 2 PARTS. THANKS 42A) A company...
HELLO, PLEASE ANSWER THE FOLLOWING QUESTION. IT WAS ASSIGNED WITH 2 PARTS. THANKS 42A) A company uses the periodic inventory system and had the following activity during the current monthly period. November 1: Beginning inventory 112 Units @ $20 November 5: Purchased 112 Units @ $22 November 8: Purchased 62 Units @ $23 November 16: Sold 174 Units @ $105 November 19: Purchased 75 Units @ $25 Using the weighted-average inventory method, the company's ending inventory would be 42B. A...
Please answer this two part question. Thank you! For this assignment you must write the following...
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...
Please answer the following question in a paragraph form address the issue of the stakeholder perspective...
Please answer the following question in a paragraph form address the issue of the stakeholder perspective and provide recommendations on how this industry should proceed in regards to self-driving trucks. 1. Apply the Three Levels of Stakeholder framework, from Business Ethics A Managerial Approach. book to this case. Discuss how the stakeholders involved should consider this innovation based on this framework. 2. Include recommendations on how decisions that regard the use of innovations such as the self-driving truck have on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT