Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

These are questions that should be solved using the coding language scheme. I have tried to make these images the best quality as possible. 1.

These are questions that should be solved using the coding language scheme. I have tried to make these images the best quality as possible.

image text in transcribedimage text in transcribedimage text in transcribed

1. As a warm-up, write a recursive function (harmonic n) that calculates the nth harmonic number | Hn =i+ + + +; You can use your (harmonic n) function from Problem set 3 if you would rather not write this from scratch. 2. Recall the example code from the lecture slides which introduced higher-order functions (func- tions that take other functions as parameters). You may notice that the base case in the code below has been changed to n=1. (define (sum f n) (if (= n 1) (f 1) (+ (f n) (sum f ( n 1))))) This code computes f(1) + f(2) + ... + f(n) with f and n passed as parameters. Use sum to write a new function (harm-sum k) that calculates the harmonic numbers, using sum to define harm-sum by passing it a function harm-term that can calculate the nth term in the harmonic series. Use your new function to compute a few harmonic numbers, and use your old function to verify that your answers are correct. 3. As you noticed, the sum function in the course lecture slides sums from 0 to n and the function in question 2 sums from 1 to n. (a) Write a new, more general, summation function named g-sum, which takes three parame- ters, f - the function, a - the starting index of the summation and b the ending index of the summation. For a given function f, and integers a and b, g-sum(s, a,b) = { $(1) i= a (b) Test your generalized version of sum with the harmonic numbers in question 2. You should be able to reuse harm-term. (c) We can define a geometric series of the negative powers of 2: c it+ +... Use g-sum to define a function (geom-series-np2 n) to calculate the sum of the first n+1 elements of this series. As part of this, define a function (geometric-term k) that calculates the (k + 1)th term in this series, that is, te (d) It is possible to use an unnamed function that is, a lambda form) as a parameter when- ever a function is expected. Use an unnamed function with g-sum to define a function (convergent-series a b), which calculates the sum of the inverse-squares from a to b, that is convergent-series(a,b) =) 4. So far this semester, we have written several functions which find the nth number in a sequence which satisfies some property (e.g. the nth even number). It would be helpful if we wrote a higher order function which could take a function which generated the sequence and a function which tested for the property in which we are interested as well as the integer n and returned the nth value in that sequence which satisfied the property (i.e. the test function returns true when passed that value). (a) Write a function, named find, which takes three parameters (sequence, a function; test, a function and n, a positive integer) and returns the nth value in sequence for which the test function returns true (#t) when passed a value in sequence. (sequence k) should return the kith element of the sequence. (test x) should return #t if the property holds for 2, #f otherwise. (b) Test your program with by finding the 15th even number and the 15th odd number. Verify your results. (c) A Fibonacci prime is a Fibonacci number which is prime. Use your higher-order function to find the 5th Fibonacci prime. Remember the "smooth" code from the lecture slides. 5. The composition of two functions f and g, denoted fog, is the function defined as (fg)(x) = f(g(2) (a) Write a Scheme function (comp f g) that returns the function fog. Once comp is defined you would see the following behavior: > (define (double x) (* 2 x)) > (define (add-one x) (+ I 1)) > (define com (comp add-one double)) > (com 3) A00 > ((comp double add-one) 3) (b) Use comp to define the pos-cos function: cos(2) if cos (2) > 0 pos-cos(1) = 1 -cos() if cosc) (define (double x) (* 2 x)) > (define (add-one x) (+ I 1)) > (define com (comp add-one double)) > (com 3) A00 > ((comp double add-one) 3) (b) Use comp to define the pos-cos function: cos(2) if cos (2) > 0 pos-cos(1) = 1 -cos() if cosc)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial Databases 2nd Symposium Ssd 91 Zurich Switzerland August 1991 Proceedings Lncs 525

Authors: Oliver Gunther ,Hans-Jorg Schek

1st Edition

3540544143, 978-3540544142

More Books

Students also viewed these Databases questions

Question

Explain walter's model of dividend policy.

Answered: 1 week ago