Scheme Introductory Lab 1

Table of Contents

This assignment is to be done individually. You can talk to other people in the class, me (Dave), and any of the course staff (graders, lab assistants, teaching assistants, prefects) for ideas and to gain assistance. You can help each other debug programs, if you wish. The code that you write should be your own, however, and you shouldn't directly share your code with others. See the course syllabus for more details or just ask me if I can clarify.

1 Get started

1.1 Start up Scheme from within Repl.it

Log into Repl.it, and find the scheme-lab-username repl (where username is your GitHub username) that you created in the initial logistics assignment. If you're having trouble finding it, try to work through the logistics assignment again, and ask for help if need be.

Once you have your scheme-lab-username repl in front of you, you should see a file browser on the left, a code editor in the middle, and a terminal window on the right.

Next, start up the Scheme environment that we're using by issuing the following command in the 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 2.2.3
Copyright (C) 1995-2017 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.

1.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 types 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.

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

1.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).

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.

2 Basic Scheme primitives

2.1 Some fruit

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 '(apple orange pineapple))
(cdr '(apple orange pineapple))
(car (apple orange pineapple))

See if you can figure out for yourself the answers to these 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.

2.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 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))

2.3 A task for you to submit

Write sequences of cars and cdrs that will pick the symbol pear out of the following expressions:

(apple orange pear grapefruit)
(((apple) (orange) (pear) (grapefruit)))
(apple (orange) ((pear)) (((grapefruit))))

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. Turn in your best attempts, and copy and paste your code into the submission box in Moodle for this assignment. For this assignment only, you'll receive a grade of M (meets expectations) if you've clearly given it a solid attempt. (For assignments going forward, you'll need to have a series of tests run successfully on your code to receive an M.)