Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Programming Language Expert

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) ... )))

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M92263939

Have any Question?


Related Questions in Programming Language

Question 1 what is a computer program what is structured

Question: 1. What is a Computer program? What is structured programming? 2. What is modular programming? Why we use it? 3. Please evaluate Sin (x) by infinite series. Then write an algorithm to implement it with up to th ...

Extend the adworks applicationi add dialogs to allow the

Extend the AdWorks application I. Add Dialogs to allow the user to Add, Edit, Read and Delete a Customer and refresh the view accordingly. 1. The user should be able to select a specific customer from the DataGrid and cl ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Question 1 what is hadoop explaining hadoop 2 what is

Question: 1. What is Hadoop (Explaining Hadoop) ? 2. What is HDFS? 3. What is YARN (Yet Another Resource Negotiator)? The response must be typed, single spaced, must be in times new roman font (size 12) and must follow t ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Php amp session managment assignment -this assignment looks

PHP & SESSION MANAGMENT ASSIGNMENT - This assignment looks at using PHP for creating cookies and session management. Class Exercise - Web Project: Member Registration/Login This exercise will cover adding data connectivi ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As