CS 3721 Lab 01 Solutions: Understanding Scheme Language in DrScheme, Lab Reports of Programming Languages

Solutions to the first lab of cs 3721: programming languages, focusing on the scheme language using drscheme. It covers interacting with the environment, creating atoms, building lists, using scheme syntax, declaring variables, and manipulating lists using cons, car, and cdr. Students will learn how to enter and save code, understand scheme's prefix notation, and work with variables and lists.

Typology: Lab Reports

Pre 2010

Uploaded on 07/30/2009

koofers-user-ytm
koofers-user-ytm 🇺🇸

4

(1)

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 3721: Programming Languages Lab
Lab #01 Solutions: Basic Scheme: learn a new language.
First, start the DrScheme programming environment. You can download various versions of
DrScheme
from http://www.drscheme.org or use the environment installed on the depart-
ment Linux machines (by running \
drscheme
"). The web site, http://www.drscheme.org,
has comprehensive documentation both about
DrScheme
and about the
Scheme
language
itself. Various reference material for Scheme and Dr. Scheme can be found at
http://download.plt-scheme.org/doc/; the Revised
5
Report on the Algorithmic Language
Scheme is the ocial denition of Scheme.
After the
DrScheme
environment starts, it will open a window with two panels: the top
panel (called the
denition window
) is for entering programs and saving commands to les;
the bottom
interactive window
is for experimenting with Scheme commands without saving
them. (Your commands in the interactive window can use denitions in the denition
window.) You should periodically save your code in the
denition window
into a le so that
your work is not lost.
Dr. Scheme implements several variations of the Scheme language. For the purpose of our
class, you should use the language level, \Teaching Languages: Intermediate Student with
lambda." (You can choose the language level when you rst start \drscheme" or by clicking
menu item \language:choose language").
Goals of this lab:
Interact with, write and debug programs in Dr. Scheme; Understand, read, and write
simple expressions in Lisp/Scheme's prex notation; Use the operator,
dene
, to give
symbolic names to expressions; Write numeric expressions using basic operators (e.g.,
+, -, /, sqrt, and =); Understand Lisp/Scheme lists and be able to use the
cons, car,
cdr, null?
, and
pair?
operators.
You can work on the questions in small groups of 1-3 individuals each. However, you have to
write up your solution on your own
. Also, you should list the names of each individual
in your group.
Due date: January 29, 3:30pm.
At the beginning of the next recitation.
In this lab, we start with the most primitive concepts in Scheme.
1. First is the atom, syntactically, an
atom
can be any string of characters that does not
contain any parentheses or spaces, and it will be treated as a single atomic unit when
building up data structures. An atom can be a variable name, a number, or a symbol.
At the interactive window, try entering some atoms (i.e., symbols and numbers).
You may need to use
0
:::
or
(
quote :::
)
for symbols, for example,
0
programming
or
(
quote programming
)
.
Please list 5 atoms.
Solution:
for numbers:
>
5
1
pf3
pf4

Partial preview of the text

Download CS 3721 Lab 01 Solutions: Understanding Scheme Language in DrScheme and more Lab Reports Programming Languages in PDF only on Docsity!

CS 3721: Programming Languages Lab Lab #01 Solutions: Basic Scheme: learn a new language.

First, start the DrScheme programming environment. You can download various versions of DrScheme from http://www.drscheme.org or use the environment installed on the depart- ment Linux machines (by running \drscheme"). The web site, http://www.drscheme.org, has comprehensive documentation both about DrScheme and about the Scheme language itself. Various reference material for Scheme and Dr. Scheme can be found at http://download.plt-scheme.org/doc/; the Revised^5 Report on the Algorithmic Language Scheme is the ocial de nition of Scheme.

After the DrScheme environment starts, it will open a window with two panels: the top panel (called the de nition window) is for entering programs and saving commands to les; the bottom interactive window is for experimenting with Scheme commands without saving them. (Your commands in the interactive window can use de nitions in the de nition window.) You should periodically save your code in the de nition window into a le so that your work is not lost.

Dr. Scheme implements several variations of the Scheme language. For the purpose of our class, you should use the language level, \Teaching Languages: Intermediate Student with lambda." (You can choose the language level when you rst start \drscheme" or by clicking menu item \language:choose language").

Goals of this lab:

 Interact with, write and debug programs in Dr. Scheme; Understand, read, and write simple expressions in Lisp/Scheme's pre x notation; Use the operator, de ne, to give symbolic names to expressions; Write numeric expressions using basic operators (e.g., +, -, /, sqrt, and =); Understand Lisp/Scheme lists and be able to use the cons, car, cdr, null?, and pair? operators.

You can work on the questions in small groups of 1-3 individuals each. However, you have to write up your solution on your own. Also, you should list the names of each individual in your group.

Due date: January 29, 3:30pm. At the beginning of the next recitation.

In this lab, we start with the most primitive concepts in Scheme.

  1. First is the atom, syntactically, an atom can be any string of characters that does not contain any parentheses or spaces, and it will be treated as a single atomic unit when building up data structures. An atom can be a variable name, a number, or a symbol. At the interactive window, try entering some atoms (i.e., symbols and numbers). You may need to use 0 ::: or (quote :::) for symbols, for example, 0 programming or (quote programming). Please list 5 atoms. Solution: for numbers:

    5

for symbols:

'giants (quote @xy) 'a

  1. Lists are collection of atoms and/or other lists, enclosed in parentheses and seperated by spaces. A list may also be empty. At the interactive window, try to build some lists. Hint: Use 0 ::: or (quote :::) to protect the lists from being treated as expressions when they are evaluated, (for example, 0 (+ 2 3) evaluates to a list which contains the elements +, 2 and 3; whereas (+ 2 3) evaluates to the number, 5.) Note that the quote is not the part of the list. Please write an example of an empty list, a simple list of atoms, and a list containing nested list. Solution:

    '() ; empty list (quote ()) ; empty list '(a cs3721 3) ; a simple list of atoms (quote (a cs3721 3)) ; a simple list of atoms '(1 (a b) x (3 (z y) 4) c) ; nested list '(1 (a b)) ; nested list

  2. The syntax of Scheme operations (Pre x notation). Every Scheme program contains a sequence of expressions. Each expression can be a single constant value, a variable, an operation which operates on a collection of values (other expressions), or a special form. The syntax of an operation has the following format: `(' op exp1 exp2 ... ')' | that is, each expression starts with '(', followed by an operator, then followed by one or more operands, and nally ends with ')'. In the interactive window, try to evaluate the result of 1903 + 249  7 :8. Write down your Scheme expression. What is the result of the expression? You can similarly apply comparison operations such as <, >, <=, >= to numbers, and apply and, or operators to boolean values (which are either true or f alse). Write down a scheme expression to evaluate (

p 2 + 7  3) ^ (5 > 3). Solution: Scheme expression for 1903 + 249  7 :8 :

(+ 1903 (/ 249 7.8))

Scheme expression for (

p 2 + 7  3) ^ (5 > 3) :

(and (<= (sqrt (+ 2 7)) 3) (> 5 3)) true

  1. In Scheme, to declare a new variable, use operator def ine, which takes two operands, the name of the variable, and the value of the variable. In the interactive window, de ne a variable x which has the value 5. Write down your de nition. What happens if you try to rede ne the value of x? Also, de ne a variable myN ame which is your

test operators are null? and pair?. null? tests whether the given list is empty or not. pair? tests whether the given list is a non-empty list or not. First, apply these predicates on an empty list. Then, apply them on the lists list1 and list2 that you built in question 5. Write down your Scheme expressions. Solution:

(null? '()) true (pair? '()) false (null? list1) false (pair? list1) true (null? list2) false (pair? list2) true