Define the fibonacci function fib as usual

Assignment Help Programming Languages
Reference no: EM131455199

Programming Languages Concepts Assignment: Memoization in Scheme

Submission: Please submit your a DrRacket file via Canvas.

1. In this task, we define a function called fib mem, which is the memoized version of the Fibonacci function, which takes an integer n as input and returns the n-th number in the Fibonacci sequence.

• Define the Fibonacci function fib as usual.

• Define the bind and lookup functions for association lists, as we discussed in class. Recall that an association list in Scheme is just a list of pairs and each pair contains a key and a value.

- (bind k v al) adds a new entry (k,v) to the beginning of association list al.
- (lookup k al) returns the value for key k in al if there is an entry for k and returns #f otherwise.

• Define a global variable al for the association list used in fib mem. (define al '())

• Finally, define fib mem. When given n, it checks whether there is an entry for n in al. If there is, it returns the value in the entry; if not, it invokes (fib n), adds the entry (n, (fib n)) in the association list, and returns (fib n).
Notes:

- To distinguish the two cases in fib mem, add the following display command for the case when the input n is in the current association list. It displays the string on screen.

(display ‘‘memoization hit \n'')

- To add an entry to al, you will have to use set! to modify the global variable al. This has the side effect of modifying al so that it is visible to the next invocation of fib mem.

- You will also need to use the sequencing construct in Scheme. In particular, (begin e1 e2) evaluates e1 (which usually has some side effect) and then evaluates e2; the value of e2 becomes the value of (begin e1 e2). For example,

(begin
(display ‘‘memoization hit \n'') (+ 1 2))

The example displays the message and returns 3.

2. (Automatic memoization, 3 points). In this task, we write a func- tion build mem that takes an input function and returns a function that is the memoized version of the input function. That is (build mem fib) should return a function behaving the same as fib mem. For sim- plicity, you can assume the input function takes exactly one parameter. As before, the returned function should display the message "memo- ization hit" when given an input that is already in the association list.

Hint: the function returned by build mem should have its own asso- ciation list, which cannot be defined as a global variable. Introduce a local variable instead. That is, the function build mem should be defined in the following way:

(define (build_mem f) (let ((al '()))
(lambda (n) ... )))

Reference no: EM131455199

Questions Cloud

Discuss the training needs assessment : Explain your training and development strategy for your chosen organization. Evaluate your analysis of training and development needs.
What is the advantage of hybrid electrical vehicle : What is the advantage of Hybrid Electrical Vehicle? And the effects of Hybrid Electrical Vehicle - Why should I write an Annotated Bibliography?
Check the given events are independent or not : Suppose that we have two everts. A and B, with P(A) = 50. P(B) = .60, and P(A ? B) = .40.
How was the use of technology demonstrated in project : How was the use of technology demonstrated in this podcast? Identify uses of information technology to connect clients to online resources.
Define the fibonacci function fib as usual : Define the Fibonacci function fib as usual. Recall that an association list in Scheme is just a list of pairs and each pair contains a key and a value.
Address the following in a well-integrated analysis : Conduct reading and research on wage and salary pay systems in private-sector organizations.
Identify a human services organization : Identify a human services organization-for example, a mental health provider or a foster care provider-and describe the services it provides.
Find the independent events : Assume that we have two events. A and B. that are mutually exclusive. Assume further that we know P(A) = .30 and P(B). .40.
Necessary in today work environment : Discuss your opinion regarding whether unions are still relevant and necessary in today's work environment.

Reviews

Write a Review

Programming Languages Questions & Answers

  Program that will allow a user to input list of their family

Problem Statement: Design a program that will allow a user to Input a list of their family members along with their age and state where they reside. Determine and output the average age of their family and output the names of anyone who lives in T..

  Summarizes information about the tune

An iTunes entry in music library is a descriptor that summarizes information about the tune that it describes. (It is not the actual tune, which is contained in a large music data file.)

  Create block using a loop to determine number of items

Create a block using a loop that will determine the number of items that can be purchased based on the price of the item and the total amount available to spend.

  Write the code to update every other element

Write the code to update every other element within the array with a lowercase x. The output should appear like the following: PRINTING CONTENTS OF ARRAY and adding x to every other element A x C x E x G x I x K x M x O x Q x S x U x W x Y x.

  Write a program to calculating sales tax on a sale

Write a program to Calculating sales tax on a sale. Converting from Fahrenheit to Celsius. Figuring a total bulk sale price based on price per unit and number of units.

  Describe how to create users for sales organization unit

Describe how you would create users for sales organization unit and how you can set up user groups in this particular situation (global, universal, domain local).

  Write a program that is passed a virtual address

Write a program that is passed a virtual address (in decimal) via the attached txt file and have it output the page number and offset for the given address in the command line.

  Design function to accept two integer values

Design function named max that accepts two integer values as arguments and returns the value that is the greater of the two. for example if 7 and 12 are passed as arguments to the function the function should return 12.

  Write application to inputs ten numbers from user

Write the application to inputs ten numbers from user, each number can be between 10 and 100, inclusive. As each number is read in determine if it is number already entered.

  Write a definition for the following terms

Write a definition for the following terms

  1 what were the key learnings surprises realized during

1. what were the key learnings surprises realized during the implementation of the first phase of outsourcing to

  Write program asks user to enter temperature

Write a program that asks the user to enter a temperature, and then shows all the substances that will freeze at that temperature and all the substances that will boil at that temperature.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd