Problem Set 1: Getting Started
Assigned: Tuesday January 19, 2016
Due: Tuesday January 26, 2016, midnight
Points: 6
This is a warm-up assignment that is intended to help you
with your system setup and with breaking the ice with coding in OCaml.
There are two parts: system setup then 4 simple coding problems.
System Setup
Acknowledgement: The system setup that we are using in this course is adapted
from Cornell's CS3110 course. Many thanks to Ben Greenman!
We've pre-loaded a Linux virtual machine image with all of the
software you will need in this course as well as two popular text
editors and development environments. Download, install, and launch
the virtual machine:
- Download and install Virtual Box for your operating system:
https://www.virtualbox.org/wiki/Downloads
- Download the cs1101 virtual machine image:
cs1101VM. This is really huge (3GB!) file so it will take some time to download.
- Import the virtual machine:
- Run VirtualBox.
- Select File => Import Appliance.
- Select Open Appliance, and choose the .ova file you just
downloaded. Then select continue and then select import. This
step might take some time.
- Run the virtual machine:
- Select cs1101VM from the list.
- Click Start.
After a bit of a startup work,
you should eventually see a linux desktop window adorned with camels
walking on a beach. This will be your programming environment for CS1101.
You'll learn to control the environment using typed commands in a
command-shell.
Before we start working with the VM, lets return to the host desktop
for a minute and reclaim our 3GB of our harddrive by deleting
the CS1101F15.ova file that we downloaded --- we won't be
needing it. You can drag it to the Trash and then delete it by
emptying the trash.
Returning to the VM, select the Camel desktop. To get started, use
the controls found in the lower left-hand corner of the screen to
perform these tasks:
- Launch the Chromium web browser. Navigate to the course home
page for the current semester and modify the settings to make
our course home page to be the default home page for the browser.
- Open a terminal window. Type ls -l. This shows you
the files in your home directory vagrant. You should
see a folder named Muller1. The Muller part is
my surname and the 1 part refers to the problem set
number. For each problem set in this course, you'll create a
folder that holds all the files that are part of your solution
to the problem set. First, we'll rename the folder.
The terminal window provides access to a unix command shell.
Type
> mv Muller1 YourSurname1
where YourSurname is your surname. By the end of the semester, you
should have YourSurname1, YourSurname2, etc., one for each
problem set.
- Open a Sublime Text window. You'll find it under
Programming via the icon at the lower left:
Under File and then Open navigate to the folder
that you just renamed and open the file problemset1.ml.
Exiting the Virtual Machine
When you are done with your work it is important to shut down the
virtual machine. This involves two steps.
- Saving the machine state
- Exiting Virtual Box
Problems
The YourSurname1 folder contains three files:
assert.ml,
Makefile and
problemset1.ml.
For now, your only concern is the problemset1.ml file, you do not need
to modify the other two. Open this file with Sublime Text. It contains
stub definitions for each of the names (i.e., helloWorld, golden,
hypotenuse and yellowCircle). As you will eventually see, we'll
specify these definitions along the lines of:
val helloWorld : unit -> string
val golden : unit -> float
val hypotenuse : float -> float -> float
val yellowCircle : unit -> unit
Your task in this problem set is to modify these stub definitions so that
they work correctly. Scroll down to line 43:
let helloWorld () : string = failwith "Problem 1 not implemented"
Your job is to replace everything from failwith on, to something that
would yield the string "Hello World!". (It's super easy!)
Finally to some actual coding!
The four problems are specified in the problemset1.ml file. Roughly
speaking, they are the following.
- Write an OCaml function val helloWorld : unit -> string.
The helloWorld function accepts no arguments and simply returns
the string "Hello World!".
- The golden
ratio, aka $\phi$, is computed by
$$\phi = {1 + \sqrt{5} \over 2}, $$
Write an OCaml function val golden : unit -> float that accepts no
arguments and returns the golden ratio. The library function sqrt can
be used in computing the result.
# let phi = golden();;
val phi : float = 1.618033988749895
- Write an OCaml function val hypotenuse : float -> float ->
float that accepts a two floating point numbers base
and height giving the base and height of a right-triangle and
uses the Pythagorean Theorem to return the length of the hypotenuse.
- The function yellowCircle should use the Universe
library to create picture with a large yellow circle in the display.
The current definition is:
let width = 600
let height = width
let radius = (float width) /. 2.
let draw ignore = failwith "Problem 4 not implemented"
let yellowCircle() : unit =
big_bang ()
~name:"Yellow Circle"
~to_draw:draw
~width:width
~height:height
Modify the definition to draw a yellow circle as specified.
The draw function will need to use the Image.circle
function from the Image Library. A quick inspection of the documentation
for that function:
val circle : float -> ?fill:bool -> ?outline_size:float -> Color.t -> t
circle radius color : creates a circle of radius with color.
The outline width is outline_size point. Filled with color if
fill is true.
shows that the Image.circle function requires a radius and
a color. For the color, you can use Color.yellow.
Try one function at a time. Once you've got the first one working you're
going to want to try it out. If you retrieve the terminal window, you can
type
> make
to compile your code. If that produced no errors, you can then type:
> ./problemset1
to try running it to see if it works. Once the first one is working,
move on to the second one.
Once you have all of your functions working, exit Sublime Text. Then
compress the YourSurname1 folder and upload it to the
appropriate folder on the course Canvas website.
|