cs245 -- hw2 -- Go: Names, Scopes and Bindings
Throughout this assignment, all programming should be done in Go. In addition, all printing should be done using
fmt.Printf rather than fmt.Println (or just println). For the String() method (see below) use fmt.Sprintf.
The programs written for this assignment may be quite short (perhaps less than 20 lines of code, each).
Nonetheless
they should be properly commented.
In some cases below, it may be that the question is answered by providing a program that does not compile. This
is OK, but in such cases you need to carefully explain why not compiling proves your point.
Part 1 -- Static and Dynamic Scoping
Write a program -- or two -- that that conclusively proves whether Go uses static or dynamic scoping. (Yes, I know
that Go uses static scoping; you program should "prove" that.)
Associated with this part shoud be a textual description of your program(s) describing exactly why and how they
prove the form of scoping used by Go. The discussion is worth at least as much as the actual programs.
Part 2 -- Stack space
I assert that, in Go, the number of items that can be put on the stack is dependent on the size of the items. Write
a very simple recursive function to determine the max stack height achievable by go. Then progressively modify this
program and determine the effect of your changes on the stack depth.
Two suggestions:
- the easiest way to progressively modify your program is to simply add variables to the recursive routine.
- Strings written to stdout are not buffered in Go (they may be buffered by the OS). So all of your prints
should appear regardless of how the program dies.
In addition to writing this code answer these questions:
- Based on your analysis how much stack space is available, by default on your machine. Is this value
consistent as
you change the amount of memory used by the recursive function? (This will involve a some math, some
estimation, and will definitely require knowing the number of bytes used by variables. So I recommend using
types like int16 or float64.
- One quick way to add lots of space used within a function in Go is with arrays. Would this trick work in
Java? Explain.
Part 3 -- functions and Closures
Write a function that returns an incrementer function. For example, when passed a 3, the function should return a
function that on successive calls would return 0,3,6,9,..... Specifically, your code might look like the
following(but with better names)
f := makeIncrementer(3)
fmt.Println(f())
fmt.Println(f())
which would output
0
3
When passed 7, the incrementer funcion should return
0,7,14,21,... Importantly the incrementer functions must be independent of each other. For instance, given a
3-incrementer a 7-incrementer, then alternating call to the two functions would yeild 0,0,3,7,6,14,9,21,...
You program should be able to have an unbounded number of these iterators.
In addition to writing this code, write a testing function that demonstrates the use of your iterator, its
unboundedness, and any other special properties it might have.
Finally, on a Linux machine, collect a "script" file for the execution of your testing code. One way to collect a
script
file is the following:
- UNIX> script XXX
Replace XXX with the name of a file you want to make. That is, at a unix prompt
enter the word script followed by the name of a file you wish to create.
- You should get back a response like "Script stated, file is XXX"
- Execute your Go program, perhaps multiple times to illustrate what you want to illustrate. Everything that
is going to the screen will also go into the file
- UNIX> exit
- You should get back a response like "Script done, file is XXX"
- Check the contents of the file
UNIX> cat XXX
This also works on a mac, I do not think there is a similar capability from the command line in Windows. There are
lots of other ways to do somethign similar.
Electronic Submissions
Your submission will be handed in using the submit script.
If you write your program on computers other than those in the lab, be aware that your program will be
graded based on how it runs on the
department’s Linux server, not how it runs on your computer. The most likely problem is not
submitting everything or hard coding file locations that are not correct on the Linux servers.
Make a README
Once you have finished coding (and fully commenting your code) make a README file. This file should follow the
format of this sample README. This is your opportunity to tell me what went well or
poorly, what is still broken and why, etc. I will read, and often respond to, everthing you write in the README.
The easiest place to write your readme is within VSC. Make a file just like a standard java file but name it
README.txt then just write in it. You should start by copying the sample readme.
Submit
If you developed this program on your own computer, you should be able to use the systems set up in lab to help
get files to Linux. If you did not get SFTP set up within VSC then do the following:
- Open a terminal window on your computer (a windows powershell or a Mac terminal)
- Connect to a linux lab machine. A full list of possibly available lab machines is at Machine list. I will assume you are using
165.106.10.186 -- you can use any machine. To connect:
ssh YOURNAME@165.106.10.186
or
ssh lab186
Enter your Unix password if/when prompted
- On the linux computer:
cd cs245
mkdir a1
You can change a1 to whatever you like, the directions below assume a1.
- Leave this terminal window open and connected to Linux, you will use it again in a couple of steps
- Within VSC open a terminal by going to the Terminal menu and selecting "new terminal"
- In the Terminal inside of VSC you should be in the directory containing your program. Enter
scp * YOURNAME@165.106.10.186:cs245/a1
- You should get messages on screen showing that each file in this directory has been copied. Make sure you
get all of the files you want to submit.
- Confirm that the copy was successful. Back in the Terminal you opened on the Linux machines (in the first
steps)
ls a1
This should show a list of of the files copied in your scp command.
- When all of the files are in the a1 directory and you are still in the cs245 directory
/home/gtowell/bin/submit -c 245 -p 1 -d a1
This says to submit for project 1 (-p) everything in the directory a1 (-d) for the class
245 (-c). You should see listing of all the files you submitted and a message that says "success".
- You can submit multiple times. I will grade only the last submission -- unless you tell me otherwise. The
submission process attaches a timestamp so I know when you submitted (down to the second). The closest
submission I have ever received to the deadline is 7 seconds.
If the submission deadline is approaching and you cannot get this process to work, send email
gtowell245@cs.brynmawr.edu with all of your files attached. I will accept this for assignment 1 ONLY.
The submission should include the following items:
- README:
- This file should follow the format of this sample README
-
- Source files
- All of them
- Question answers
- If separate from readme.
- Data files used:
- Be sure to include any non-standard data files uses. (Rare)
-
DO NOT INCLUDE:
- Data files that are read from the class site. Do include any of your own data files.
Again: Once you have everything you want to submit in the a1 directory within /home/YOU/cs245/
- Go to the directory /home/YOU/cs245
- Enter /home/gtowell/bin/submit -c 245 -p 1 -d a1
If this worked you will get a message with the word "success". If you cannot achieve success and the deadline is
approaching, send me email. We can set up a meeting to work out your problems. The email will establish that you
intended to submit. Once you send the email, do not change the files that you were trying to submit.