CS374: Programming Language Principles - Functional Programming with Scheme
Activity Goals
The goals of this activity are:- To explore the functional programming paradigm using Scheme
Supplemental Reading
Feel free to visit these resources for supplemental background reading material.- The Scheme Programming Language
- Closures in Scheme
- QuickSort in Scheme
- Implementing Python as Syntax Rules for Racket
The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.Model 1: Declarative Languages - Functional Programming with Scheme
Questions
- What is a statement in Scheme?
- What shared variables exist in this program?
- What are some potential advantages of Functional Programming as a paradigm?
- How might you improve upon the implementation of the
largest
function?
Model 2: The Scheme Programming Language
Questions
- How are function parameters handled in Scheme? Are they passed by value or by reference?
- What is a function in Scheme? How is it represented?
- What does
czr
do in your own words? - Write a function to count the number of items in a list using a recursive call and a base case, using
czr
as a guide to traversing a list. - Diagram the binding of the values in the call to
plusminus
to the anonymous lambda function. - What is the result of the
map
/apply
sequence? What would happen ifmap
were applied to only a single list? - In your own words, define tail recursion. Do you see instances of tail recursion in these examples? Draw a call stack for one of these examples.
- Compare and constrast closures and objects.
- Using the
pair?
directive, which returns#t
if the parameter is a nonempty list, add a check to one of these list recursion examples to ensure thatnull
is returned if an empty list is passed. - Write a function that accepts a list and an operator as parameters, such as addition. Apply that operator to the whole list recursively; for example, if the operator is the addition operator, return the sum of the list. If it is the multiplication operator, return the product of all items in the list.
Model 3: Functional Programming in Modern Languages
Questions
- What does this code do? How does it do it?
- What are the advantages of programming this way?
Model 4: Functional Programming in Modern Languages
Questions
- Time the following program for various numbers of threads and values for
numbers
. What do you notice?
Installing Scheme
You can git clone https://github.com/BillJr99/scheme-interpreter.git
which will give you a scheme.py that you can run via python scheme.py <your scheme file
, or do one of the following:
- Cygwin (Windows): Install
guile
from the Cygwin installer - Ubuntu (Linux):
sudo apt install mit-scheme
- Mac:
brew install mit-scheme
, provided that you have installed homebrew