Starting from:

$35

Project 4 Solution

Goal: To learn how to use functional programming language to solve a problem.




Description:




In this assignment, you are required to write functions specified below in Scheme, one of the functional programming languages.




 
Write a non-recursive Scheme function area(r) that given the radius r of a circle, computes its area as 3.14 * r * r.




 
Write a recursive Scheme function power(A, B) that takes two integer parameters, A and B, and returns A raised to the B power. A must be a positive value, but B maybe a negative value.




 
Write a recursive Scheme function countZero(list) that returns the number of value zeros in a given simple list of numbers. For example, (countZero ‘(1 0 2 3 0)) should return 2.




 
Write a recursive Scheme function reverse(list) that returns the reverse of its simple list parameter. For example, (reverse ‘(1 2 3 a b)) should return ‘(b a 3 2 1).




 
Write a recursive Scheme function palindrome(list) that returns true if the simple list reads the same forward and backward; otherwise returns false. For example, (palindrome ‘(a 1 b 1 a)) returns true, while (palindrome ‘(a b)) returns false.




 
Write a recursive Scheme function merge(firstNameList, lastNamelist) that receives a simple list of first names and a simple list of last names, and merges the two lists into one list consisting of (firstName lastName) pairs. Display the result list. For example, if the inputs are (John Kim Kate George), (Davidson Hunter Johnson Olson), the output should be ( (John Davidson) (Kim Hunter) (Kate Johnson) (George Olson) ).




Note: A simple list is a list such that none of its element is a list. For example, ‘(1 2 3) is a simple list, while ‘((1 2) 3) is not.







To avoid typing long lists at the command line, I suggest you create a few sample arguments in files:




 
(load "my_lists")




 
(countZero list1)




where the file my_lists contains the definition

(define list1 '(1 9 0 12 0 30 50 0))




Important: you are required to use only the functional features of Scheme; functions with an exclamation point in their names (e.g. set!) and input/output mechanisms other than load and the regular read-eval-print loop are not allowed. (You may find imperative features useful for debugging. That’s ok, but get them out of your code before you hand anything in.)




Requirement:




 
All functions should be put within the same source file.




 
The signature of each function (including function name and order of parameters) must match the given signature.

 
No need to check the type of each parameter. We always assume for each function call, the right type of values are passed to these function parameters.




IDE: Dr. Racket




Dr. Racket is the IDE we are going to use. The software is free and can be downloaded from: http://racket-lang.org/download/. It supports different platforms such as Windows, Mac, or Linux. You can also use Dr. Racket on shemp.cs.mtsu.edu. Here is how it looks like:



































































The top window is the editor, and bottom half is the command line window. After completing coding, click the Run button on the menu bar, and if there is no syntax error, functions defined in the code can be invoked from the command line window. The following pictures gives an example: after typing (list-sum '(1 2 3 4 5 6 7 8)) and press ENTER, the function is called and result is diplayed.
































































Project Tips

The detailed manual of Racket can be found at:




http://docs.racket-lang.org/reference/index.html




You don’t need read through the whole reference, just find information you need such as how to define/invoke functions, how to manipulate lists, and how to use conditional statements.




Some useful functions are provided below with a brief description:




 
(list? listvalue )

returns true (#t) if listvalue is a list value; otherwise false (#f)




 
(null? listvalue)

returns true if listvalue is an empty list, otherwise false




 
(equal? value1 value2)

returns true if value1 is equal to value2, otherwise false




 
( value1 value2)

returns true if value1 greater than value2, otherwise false. (similar for )




 
(- value1 value2)

returns the result of value1 – value2 (similar for +)




 
(length listvalue)

returns the number of elements in the listvalue.




 
(list-tail listvalue pos)

returns the list after the first pos elements of listvalue.




 
(drop-right listvalue pos)

returns a fresh list whose elements are the prefix of listvalue, dropping its pos-length tail.




 
(list-ref listvalue pos)

Returns the element of listvalue at position pos, where the list’s first element is position 0.




 
(not boolvalue)

Returns true if boolvalue is false; otherwise false.




For detailed information on list operations, please refer to the page below:

http://docs.racket-lang.org/reference/pairs.html#%28part._.List_.Operations%29




How to submit?




 
Copy the rubric4.doc and your source program to the project4 folder in your individual repository. Edit the file to put your name.




 
Commit the whole project4 folder to the server. Make sure the folder contains your source program.




 
Any commit of the project after the deadline is considered as cheating. If this happens, the latest version before the deadline will be graded, and you may receive up to 50 points deduction.




 
No hard copy needed

More products