程序代写代做代考 interpreter Haskell Lab 1: Hello World CIS 252: Introduction to Computer Science

Lab 1: Hello World CIS 252: Introduction to Computer Science

This lab will introduce you to some of the key tools we will be using in this
course. In many labs, you’re allowed to work together with a partner. However,

THIS LAB MUST BE A SOLO EFFORT.

Important: Hold on to this lab write-up: you may find it useful as a reference
throughout the semester.

* Read this writeup very carefully, lest you go astray.

Part 1: Getting set up

1. Find the lab room: CST 1-231 . Be sure to know your SU NetID and pass-
word.

2. Find an empty machine and wake it up.

3. Type your NetID and then your password into the login screen.

4. At the upper left-hand corner of the screen, select the Go menu and then
choose Utilities . Double-click on the Terminal application, and you’ll un-
lock a portal to Unix.

5. You should see a terminal window. You can adjust the size of the window,
the font, and other settings via the application’s Preferences item (which
is available frm the Terminal menu in the upper lefthand corner of the
screen).

* An important caveat: Any Preference settings you change will be
stored to the local disk of the computer you’re currently working on. If
you later work at a different computer, you will likely encounter the origi-
nal default settings (which you are of course free to adjust as you like).

Part 2: Looking around in Unix

Both Unix and Haskell are case sensitive (i.e., capitalization matters), so make
sure you type things exactly as stated throughout the rest of this writeup.

1. Type to the terminal prompt:

ls -F ←↩

(The “←↩ ” means “hit return.” Also, be careful: the first character is the letter
l (as in “lion”), not the digit 1 (“one”).)
You’ll see the names Desktop/, Documents/, and several others. These
are files and directories in your home directory (a.k.a, home folder). The
command “ls” stands for list directory, and the “-F” is a specific option for
how the directory listing should be formatted:

An ending / indicates a directory, * indicates an executable file, and @
indicates a link to another file. These symbols (/, *, and @) are merely
decorations and not actually part of the file names.

2. Now type to the terminal prompt:

ls ←↩

You should see the same names as before, but without the trailing / and *
characters that the “-F” provided previously.

3. * This series of steps is extremely important, so please read carefully. If
you have any questions, please ask for help.

(a) Type to the terminal prompt:
ls /Volumes ←↩

You’ll see a few names, including one numeric name (it may be 01
or 03 or something else entirely). REMEMBER this number; better yet,
write it down.
* If more than one numeric name shows up, please ask for help to
identify the one you need to remember.

(b) Type to the terminal prompt:
cd ←↩
rm SUAD ←↩

(c) Type the following to the terminal prompt, but replace XX with the
numeric name you saw in the previous step:

ln -s /Volumes/XX SUAD ←↩

4. Type to the terminal prompt:

cd SUAD ←↩
ls ←↩

Page 1 Spring 2017

Lab 1: Hello World CIS 252: Introduction to Computer Science

The command “cd SUAD” takes you to the SUAD directory (“cd” stands
for change directory), and the command ls again lists the contents of the
current directory.

Here, you’re apt to see files that look familiar: SUAD is now a link to your
SU Active Directory, which you’ve likely accessed previously from various
machines across campus.

5. Type to the terminal prompt:

mkdir cis252 ←↩
cd cis252 ←↩
pwd ←↩

The command “mkdir cis252” makes a new directory cis252 for the
files for this course. The command “cd cis252” takes you to the cis252
directory that we just created. The “pwd” (for print working directory) tells
you the name of the current directory you are in.

6. Now type to the terminal prompt:

ls ←↩

You’ll see that there are no files in this directory: we’ll change that soon.

7. Type to the terminal prompt:

cd ←↩
pwd ←↩

The command “cd” takes you back to your home directory, and “pwd”
tells you the name of your current directory.

8. Now let’s go to the cis252 directory again to do some work. Type to the
terminal prompt:

cd SUAD/cis252 ←↩

I strongly encourage you to use this directory throughout the semester for
all of your CIS 252 files. At a minimum, make sure that you always save
files within your SUAD subdirectory: that is the only way to ensure that
you’ll be able to access them from another machine.

Part 3: Starting ghci

1. For something a bit more interesting, type to the terminal prompt:

ghci ←↩

You will see something like:

GHCi, version 8.0.1: http://www.haskell.org/ghc/ 😕 for help
Prelude>

You’ve started up the Haskell interpreter, which is the environment in
which we’ll run/interpret Haskell programs:

2. To the “Prelude>” prompt, type:

3+4 ←↩

You should see 7 as the answer.

3. Now type to the “Prelude>” prompt:

😕 ←↩
:q ←↩

The “:?” gives you the various options you can set in the ghci interpreter
(most of which we’ll never use!), and the “:q” quits ghci.

Part 4: Starting emacs

1. Now the fun starts. Type to the Unix terminal prompt:

touch simple.hs ←↩
open -a Aquamacs simple.hs ←↩

These commands (i) create a new (empty) file named simple.hs and then
(ii) start up the emacs text editor to edit that file. The “.hs” is the exten-
sion for Haskell files.

2. Click on the emacs window and type into that window:

Page 2 Spring 2017

Lab 1: Hello World CIS 252: Introduction to Computer Science

milesToKm :: Float -> Float ←↩
milesToKm m = m*1.609344 ←↩
←↩
poundsToKg :: Float -> Float ←↩
poundsToKg p = p*0.45359237 ←↩

The arrow keys move you around the text, and delete does its usual thing.

3. * Read this step completely before trying it out! Type to emacs
C-x C-s

“C-x” means hold the control-key and the x-key at the same time, and
“C-s” means hold the control-key and the s-key at the same time. This
action saves the file.

[Alternatively, go to the File menu, and then select Save .]

4. Now type to emacs:

C-c C-b

Note that “C-c” means hold the control-key and the c-key at the same time,
and by now you can guess what “C-b” means.

[Alternatively, go the Haskell menu and select Start Interpreter .]

The emacs window should split in half with a version of ghci starting up
in the new half. Your cursor should be in this new sub-window.

5. Type to ghci:

:load simple ←↩
milesToKm 10 ←↩
poundsToKg 2.206 ←↩

If you’ve done things correctly, you should get two results: the first is the
number of kilometers in 10 miles, and the second is the number of kilo-
grams in 2.206 pounds.

Assuming things went as planned, you have successfully written a tiny
Haskell program, loaded it into the interpreter ghci, and run the thing.

6. Now type to emacs: C-x C-c and answer yes to the questions it asks you.
The emacs window should go away.

Part 5: Printing

The Haskell file you just created (i.e., simple.hs) is a simple text file, which
can be printed from any system in whatever way the system supports printing
plain text files.

For now, probably the easiest way to print your Haskell files is to do it inside of
Emacs. Having just quit Emacs, we will have to start it up again.

1. At the Unix terminal prompt, type:

open -a Aquamacs ←↩

You will likely see the Emacs startup screen (something you didn’t see ear-
lier, because we had instead specified a specific file to edit).

2. To load the file simple.hs, type to emacs

C-x C-f

[Alternatively, go to the File menu and select Load File .]

When prompted for the file name, type the following:

/̃SUAD/cis252/simple.hs

3. Once the file has been loaded, go to the File menu and select Print Buffer .

4. A printer dialogue box should open. You now have multiple options:

Option #1: Select the printer LCS-VC-PRQ-01-LCS-CST1231 (it will
likely be your only option).

Usually you’ll then click Print , but for now click Cancel . (If you
did click Print , then your Haskell program should be on its way to
the printer in the corner of the room; go grab it.)

Option #2: Select the PDF button in the lower left of the dialogue box,
and then select Save as PDF .
Make sure you save it to your SUAD/cis252 directory. You’ll then
be able to access/print this file from any machine on campus.

5. Quit emacs.

Page 3 Spring 2017

Lab 1: Hello World CIS 252: Introduction to Computer Science

Part 6: Your turn

In the previous parts, we walked you through various tasks using the Terminal
window, the text editor emacs, and the Haskell interpreter ghci. Here you get to
carry out a similar set of tasks on your own. If you can’t remember a command,
refer back to the appropriate section.

* Before starting, make sure that you’re in your /̃SUAD/cis252 directory,
and save all your files there.

1. Create a new (empty) file called lab1.hs and start up emacs to edit it.

2. In emacs, insert the following text into lab1.hs:

— Name: fill in your name
— Email: fill in your email address
— Section: fill in your 252 lecture section (LSB, CST, or SOM)

timesPlus :: Integer -> Integer -> (Integer, Integer)
timesPlus x y = (x*y, x+y)

Save this file.

3. Start up ghci inside of emacs, load the file lab1.hs into ghci, and then
evaluate:

timesPlus 47 1000

If you’ve done things correctly, you should get a pair of integers as a result.

4. Save the *haskell* buffer (i.e., the buffer containing ghci) under the
name lab1-transcript.txt.

5. Print out the two files lab1.hs and lab1-transcript.txt.

6. This lab is due by noon on Friday, January 20:

(a) The last page of this writeup is a disclosure cover sheet. Fill it out
completely.

(b) Staple this disclosure cover sheet on top of the two files you just
printed out.

(c) Choose one of the following two ways to submit your lab:

i. Hand it in to a TA or an instructor during the lab section.
ii. Place it in the appropriate CIS 252 bin located near CST 4-226.

* Make sure you’ve included your name!

Part 7: Logging off P P DON’T FORGET THIS PART P P

1. Click on the apple in the upper lefthand corner of the screen.

2. Select Log Out .

3. In the little dialog box that pops up, click on Log Out .

Page 4 Spring 2017

Posted in Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *