Homework 1: Scheme Intro 1

Table of Contents

This assignment is to be done individually. You can talk to other people in the class, me, and any of the course staff (graders, lab assistants, prefects) for ideas and to gain assistance. You can get direct help debugging your code from me and any of the course staff, and you're welcome to show us your code during debugging or other conversations. The code that you write should be your own, and you shouldn’t directly show or share your code with other students (although you may discuss general debugging strategies with others). See the course syllabus for more details, and bring any questions to Anna.

1 Get started

You'll work either in one of the Olin CS labs (310, 304, or 308) or on your own computer. Even if you plan to primarily work in the Olin labs, I recommend getting your own computer set up for this class using the Docker approach if possible. Follow the instructions on Moodle entitled "Development setup (personal computer)" before you begin this assignment.

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.

1.2.1 Docker on your computer

If you are using Docker, you already have a folder named ProgrammingLanguages that you’ve created. That’s the one you should be using. Drag the folder with the starter files into that folder.

1.2.2 COURSES in Olin labs or on your computer

If you are not using Docker, then you'll save your work on the Courses drive to allow you to access it from the department server and in Olin.

The way to access Courses differs depending on where you are:

Once you've Mounted the Courses drive, move the folder with the starter files into your student work directory on Courses. You can do this by first downloading it above, unzipping it (double click on a Mac, or on Windows, right click on the file and choose "Extract All"), and then dragging it into your student work directory. On Courses, you should see our class, then within there StuWork, and within there, your username. You'll put the starter code folder inside the folder with your username.

(Note: Strictly speaking, if you're working on your own computer, you can skip the Courses drive part for now. However, if you're on a Mac, you'll need to use the Courses drive when we get to the portion of the class where we're working in C. Whenever you're working on the lab machines in Olin, it's important that you put your work on Courses, as otherwise, it will be deleted when you log off.)

1.3 Getting started with Scheme

1.3.1 Docker on your computer

If you are using Docker, follow the instructions with the install to start up the Docker image and use a terminal window within.

1.3.2 COURSES in Olin labs or on your computer

If you are not using Docker (i.e., you're using the Olin labs or you have installed software locally), you can either (a) create a terminal window, and navigate to the directory where you have extracted the starter code, or (b) use VS Code. If you're using VS Code, first open it up, and then:

  • On a Mac: Go to the File menu and select New Window. Drag the folder with the starter code (the one you just put onto the courses drive) onto the new window in VS Code.
  • On Windows: Press F1, and then select Remote-WSL: New Window. Use the File menu to open the folder with the starter code.

Finally, go to the Terminal menu and choose New Terminal.

Two things to note throughout the class if you're using VS Code and not 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.3 Start up Scheme [Everyone, regardless of your environment]

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 we 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 animals

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 '(eel koala okapi))

(cdr '(eel koala okapi))

(car (eel koala okapi))

Experiment with the code to try to answer the following questions (the videos may also be helpful for these!):

  • 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 Campuswire 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 koala out of the following expressions:

(koala eel cheetah okapi)
(cheetah koala)
(((cheetah) (eel) (koala) (okapi)))
(cheetah (eel) ((koala)) (((okapi))))

Here's an example of what I might write if I was trying to pick koala out of the expression (koala):

(car '(koala))

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 koala 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.

Zip up the starter folder with your modified files (i.e., your modifications to main.scm and your CollaborationsAndSources.txt) and upload it to Moodle.

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 koala, 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 koala 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 koala.

Assignment originally designed by Dave Musicant with modifications by Anna Rafferty - thanks for sharing!