Starting from:

$24.99

Higher Order Functions_Lab 5 Solution

Objectives
• Work with higher order functions
• Work with the plot package
Activities
1. Let f and g be two functions, mapping integers to integers. Define a function dominate so that (dominate f g)outputs the smallest positive integer k for which f(k) g(k). Thus, if f(1) g(1), (dominate f g) should output 1. If f(1)≤g(1) but f(2) g(2), then (dominate f g) should output 2, etc. 2. [SICP Exercise 1.41] Define a procedure double that takes a procedure of one argument as an argument and returns a procedure that applies the original procedure twice. For example, if inc is a procedure that adds 1 to its argument, then (double inc) should be a procedure that adds 2. What value is returned by (((double (double double)) inc) 5)? 3. Sofarthissemester,wehavewrittenseveralfunctionswhichfindthenth numberinasequencewhichsatisfies some property (e.g. the nth even number). It would be helpful if we wrote a higher order function which couldtakeafunctionwhichgeneratedthesequenceandafunctionwhichtestedforthepropertyinwhich 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) Writeafunction, namedfind, whichtakes threeparameters(sequence,test, andn)andreturns the nth valueinsequenceforwhichthetestfunctionreturnstrue(#t)whenpassedavalueinsequence. (b) Testyourprogramwithbyfindingthe5th ofevennumberandthe5th ofoddnumber. Verifyyourresults with the solutions to the problem sets and in the lecture slides. (c) A Fibonacci prime is a Fibonacci number which is prime. Use your higher-order function to find the 5th Fibonacci prime. 4. Racket also provides a graphing package, named plot, with which you can graph the functions you write in SCHEME. With Racket 5.2.1, change your language to “Use the language declared in the source” and add #lang racket as the first line in your Definitions window. To plot a function, start with the following two instructions which will import the plotting functions and create graphs in a new window. (require plot) Next you can graph a function by using plot: (plot (function sin (- pi) pi #:label "sine␣x"))
1
where (- pi) and pi determine the x-axis bounds. Additional plot keywords can be used to specify additional features of the plotted functions such as a legend and x and y bounds of the graph (separate from the bounds of the function to be graphed): (plot (function sin (- pi) pi #:label "sine␣x") #:x-min -5 #:x-max 5 #:y-min -5 #:y-max 5)
Plot the following function in the interval [−5,5], using Racket’s plot capabilities: (define (f x) (+ (* (sin x) (- 3 x)) 1))

More products