Homework 1: Scheme Intro 1
Table of Contents
This assignment is to be done individually. You can talk to other people in the class to come up with ideas and get help with general concepts, but you should not show others your code, nor see others' code. You can get direct help debugging from Anna and any of the course staff (graders, lab assistants, prefects).
1. Get started
If you haven't completed the course Development setup yet, please do that before continuing.
1.1. Download the starter files
1.2. Finding a spot for your work
You’ll need a folder (also known as a directory) to store your code. You shouldn’t do all of this right in your home folder or on your desktop, as it will get really messy with a lot of files.
You already have a folder named ProgrammingLanguages that you created as part of the Docker setup. Drag the folder with the starter files into the ProgrammingLanguages folder.
1.3. Getting started with Scheme
First, you'll launch Docker in VSCode. Open the ProgrammingLanguages folder in VSCode (File->Open Folder). You may see a popup in the lower right corner prompting you to reopen the folder in the Docker container. If so, click through to do so. Otherwise, click the >< icon in the lower left corner of VSCode. This will open a dropdown menu where you can select "Reopen in Container".
If you run into any problems launching Docker, refer back to the Development Setup instructions on Moodle or reach out to Anna or Mike Tie for help.
Two things to note throughout the class when using VS Code and
Docker: it's important that you open a folder, not
a specific file, and it's important to make the new terminal after you
open the folder, not before. That allows the terminal to start in the
folder (aka directory) where your code and files are. If you ever want
to check the current directory in the terminal, type pwd in the
terminal and then
hit return. (pwd stands for "print working directory.")
1.3.1. Start up Scheme
Open a new terminal window in VSCode (see the top-level menu, Terminal -> New Terminal).
You'll start up the Scheme environment that we're using by issuing the
following command in that new terminal window. Make sure to include the dot and the slash before the word scheme.
That's because you're actually running a script that I created for you to start things up.
./scheme
You should see something like this:
GNU Guile 3.0.7 Copyright (C) 1995-2021 Free Software Foundation, Inc. Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'. This program is free software, and you are welcome to redistribute it under certain conditions; type `,show c' for details. Enter `,help' for help. scheme@(guile-user)>
This shows that you are at the Guile interactive prompt. (Guile is the name of the particular Scheme implementation that we'll be using.) This interactive prompt is much like working with Python interactively. You can type in commands, and see what they do.
If this doesn't work and you see a "Permission denied" error, it means that the permissions have for some reason been changed. That's okay - we can fix it! We want to give executable permissions to that scheme script. In the terminal, type:
chmod a+x scheme
That says to give executable (x) permissions to all (a) for the
file scheme. If you ever get this error in the future, now you know
how to fix it!
2. Call a function
At the Guile prompt, type
(+ 3 5)
In Scheme, + is a function.
To call a function in Scheme, you place the name of the function and its arguments, separated by spaces,
inside parentheses. This takes a little getting used to! In most programming languages, function calls
look something like this:
function(arg1, arg2, arg3)
In Scheme, function calls look like this:
(function arg1 arg2 arg3)
So the above code that you typed in should add 3 to 5, and so you should see output that looks like:
$1 = 8
This means that the return value from the above expression is 8, and Guile is assigning it to a
history variable called $1. You can ignore these history variables; they're not especially useful for us.
3. Play around a little
Back at the regular Scheme prompt, change your program in some way. For example, re-type it as
(+ 3 7)
Experiment with using the up and down arrow keys, which let you move backwards and forwards through your command history.
4. The Guile error prompt
If you make a mistake and type in some invalid syntax, Guile Scheme presents a sub-prompt indicating that you've had an error. Perhaps this has already happened to you. The error prompt looks looks like this:
Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue. scheme@(guile-user) [1]>
The key way to notice that you're there is somewhat subtle: it's the
[1] that appears in the prompt. It means that you're 1 level deep
into the error system. We'll play with the debugging system more later
on, but for now, when you see this, get out of by typing ,q (don't
miss the comma in front).
Let's try it, just so you can see how it works. At the Guile prompt, just type x, and hit enter.
You should see an error about an unbound variable, and then you'll find yourself at the error prompt.
Type ,q to return to the main Guile prompt.
Notice that if you type ,q at the main Guile prompt, it will exit Guile and you'll end up back at the
regular bash prompt. If that happens, type ./scheme again to start it up.
5. Basic Scheme primitives
5.1. Some vegetables
At the Guile prompt, enter the following, one at a time. You can copy and paste if you like, but pay careful attention to what you're pasting so you can figure out what's going on. (The last one should cause an error.)
(car '(artichoke broccoli carrot)) (cdr '(artichoke broccoli carrot)) (car (artichoke broccoli carrot))
Experiment with the code to try to answer the following questions:
- What do car and cdr do?
- Why is the single quote necessary? What does it do?
- The last line of the above should cause an error: why?
The error caused by the last line should land you at a Guile error prompt, that looks like this:
Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue. scheme@(guile-user) [1]>
Again, remember that you know you're at an error prompt whenever you see a number in brackets,
like the [1] in there. Type ,q to get out of it.
5.2. Some more things to try
Enter each of the following lines of Scheme code, one-by-one, into Guile and observe the output. See if you can figure out for yourself what's going on. Assemble a list of questions for posting on Moodle or discussing in class when this assignment is complete.
;; This is a comment, by the way! (cons 'x '(1 2)) (cons '(1 5) '(2 3)) (append '(1) '(2 3)) (append '(1 5) '(2 3)) (list '1 '2 '3 '(4 5)) (length '(plato socrates aristotle)) (reverse '(plato socrates aristotle)) (member 'socrates '(plato socrates aristotle)) (member 'raphael '(plato socrates aristotle))
6. A task for you to submit
Write sequences of cars and cdrs that will pick the symbol broccoli out of the following expressions:
(artichoke broccoli fennel carrot) (broccoli carrot) (((fennel) (artichoke broccoli) (carrot))) (fennel (artichoke radish) (broccoli) (((carrot))))
Here's an example of what I might write if I was trying to pick broccoli out of the expression (broccoli):
(car '(broccoli))
It's ok if you have trouble getting this to work: the key aspect to this assignment is to make the effort before the deadline so that you can ask questions.
7. What to hand in
In the (initially empty) file main.scm, paste in your code to pick
out broccoli from each expression. You should end up with four lines
in main.scm, one for each of the four expressions above.
Additionally, create a file named CollaborationsAndSources.txt within
the starter folder. In this file, you should describe (a) your
collaborations with anyone on the assignment and (b) your use of any
outside sources on the assignment. For example, I might say something
like "I worked through a few related examples on the board with Grace
Hopper to better understand car and cdr, and then I wrote my code for
the examples in the assignment on my own. I talked with Alan Turing
about the assignment to help him understand list. I didn't refer to any
outside sources except the Dybvig book." With every assignment you
submit, you'll need to include a file named
CollaborationsAndSources.txt that has these two parts. Your file
should be specific about your collaborations and sources - it
shouldn't be empty or just list names of people.
Create the zip file that you'll submit to Gradescope by running the script ./zipitup.
If you get an error about file permissions, first run chmod a+x ./zipitup.
For this
assignment only, you'll earn a grade of M (meets expectations; 3) if
you've clearly given it a solid attempt, even if you didn't always get
the broccoli, and if you have CollaborationsAndSources.txt. For assignments going
forward, you'll need to have a series of tests run successfully on
your code to earn an M. You'll earn an E (exemplary; 4) if
your four lines of code always pick out broccoli and you have
CollaborationsAndSources.txt. The graders will copy and paste your
code from main.scm into Scheme to confirm that they correctly get
the broccoli.
Assignment originally designed by Dave Musicant - thanks for sharing!