Assignment 1: Programming in Plait
Objectives:
- Become comfortable with using the Plait features for defining and calling functions, conditionals, arithmetic, and defining datatypes.
- Know how to translate a written problem statement into a Plait program
- Learn how to test your code and write specifications
Resources:
- The Plait tutorial and documentation page: https://docs.racket-lang.org/plait/
- How to Design Programs: https://htdp.org/
Parameters:
- Due date: Friday January 12 at 11:59PM
- Upload a
code.rkt
file with your solution to Gradescope. - the first line in
code.rkt
should be#lang plait
. - Ask questions on Piazza or during Office Hours.
- Perform all the work for this project by yourself. You may discuss with your classmates, but you must write your own code.
Setup
- Install DrRacket and Plait by following the instructions at https://docs.racket-lang.org/plait/getting-started.html
- We are using DrRacket version 8.11.1 for the autograder. We recommend that you use this version.
Exercise 1
Implement a function my-max
which takes as input two numbers as input and outputs whichever one is greater. Do not use the built-in max
function.
Example usage:
> (my-max 10 3)
10
- Include in your solution at least three tests for your function.
Exercise 2
Define a function plural
that takes a string as input and returns a string. If the given string ends in “y”, the function should return the input with the “y” replaced by “ies”. Otherwise, the result should be the same as the given string with “s” added to the end.
Example usage:
> (plural "baby")
"babies"
> (plural "fish")
"fishs"
Your solution can make use of the following string manipulation built-in functions (see the Plait documentation here):
substring
string-append
string-length
equal?
Include in your solution at least three tests for your function.
Exercise 3
Use the following type definition (add it to your program):
(define-type Light
(bulb [watts-per-hour : Number] [technology : Symbol])
(candle [inches : Number]))
Implement the function electricity-usage
, which takes a Light
as input and produces the number of watts of electricity that the light uses in 24 hours. Assume candles use zero watts. Your function should use type-case
with bulb
and candle
cases.
Example usage:
> (electricity-usage (bulb 10 'modern))
240
> (electricity-usage (candle 1.1))
0
Exercise 4
Implement the function use-for-one-hour
, which takes a Light
as input and produces another Light
that represents the given light source after it is used for another hour. Assume that a candle burns one inch per hour (unless it is already gone), and assume that a light bulb is the same after one hour of use.
Example usage:
> (use-for-one-hour (bulb 100 'halogen))
(bulb 100 'halogen)
> (use-for-one-hour (candle 1))
(candle 0)
Note that floating point precision will not always do what you expect. For instance, the following is acceptable:
> (use-for-one-hour (candle 1.1))
(candle 0.10000000000000009)
You should try to include at least 3 tests for your function.