CS 115 – Dubai – Fall 2012
Assignment 04
Due: Thursday, October 18, 2012 at 11:59 p.m.
• Do not use reverse or member in any of your solutions.
• Since you are using Beginning Student Scheme, you may not use list abbreviations on this assignment.
• You may want to include defined constants to help reduce the writing for the examples and tests.
• For this and all subsequent assignments, you are expected to use the design recipe when writing functions from
scratch. Be sure to follow all the steps of the design recipe, including the definition of constants and helper
functions, that include the design recipe, where appropriate.
• Do not copy the purpose directly from the assignment description. The purpose should be written in your own
words and include reference to the parameter names of your functions.
• The solutions you submit must be entirely your own work. Do not look up either full or partial solutions on the
Internet or in printed sources.
• Do not send any code files by email to any course staff. It will not be accepted by course staff as an assignment
submission. Course staff will not debug code emailed to them.
• Test data for all questions will always meet the stated assumptions for consumed values.
• Read each question carefully for restrictions.
• Read the course Web page for more information on assignment policies and how to organize and submit your
work. Follow the instructions in the style guide. Specifically, your solutions should be placed in files a04qY.rkt,
where Y is a value from 1 to 3.
• Download the interface file from the course Web page.
Language level: Beginning Student Coverage: Module 5 (simple lists)
1. Using structural recursion, write a function called calc-sqrt that consumes a list of numbers (lon) and produces a list of elements, where each element in the list produced is one of:
• the positive square root of the number in lon, where the number in lon is a non-negative integer and its square root is also an integer
• the symbol ‘irrational, where the number in lon is a positive integer, but its square root is a non-integer
• the symbol ‘imaginary for all negative numbers in lon
In the case where the element is positive non-integers, there is no matching entry in the list that is produced. The list produced contains the matching values in the same relative order as the original list.
For example,
(calc-sqrt (cons 4 (cons 0 (cons 1.3 (cons 5 (cons -1 (cons -1.3 empty))))))
(cons 2 (cons 0 (cons ‘irrational (cons ‘imaginary (cons ‘imaginary empty)))))
2. Using structural recursion, write a function called string->ascii that consumes a string (phrase), and produces a list of natural numbers representing ASCII codes of characters of phrase, that matches the characters in the order they appear in the string. For example,
(string->ascii “Hi!”) produces (cons 72 (cons 105 (cons 33 empty)))
If phrase is the empty string (“”), the function should produce the empty list. The built-in function char->integer consumes a character and produces an integer in the range 0 … 255 and matching the
—-PAGE—-
CS 115 – Dubai – Fall 2012
Assignment 04
Due: Thursday, October 18, 2012 at 11:59 p.m.
ASCII code value of the character consumed. This function may be helpful.
3. The terms of a geometric sequence starting at value !, with common ratio ! would be: !, ! ∙ !, ! ∙ !!, ! ∙ !!, …
Neither ! nor ! may have the value 0. For example, a geometric sequence starting at 4 with a common ratio of ! would be:
4,2,1,! ,! ,! ,…
Using structural recursion, write a function called geo-seq? that consumes a list of numbers and produces true when the numbers in the list (in their current order) form a geometric sequence and false otherwise. An empty list should produce false. A list with one or two elements should produce true, as long as neither of the elements are 0. Hint, think carefully how you can determine if any two adjacent values in the list are part of a geometric sequence.
—-PAGE—-