This guide summarizes the essential notations that you'll need for the Pyret segment of the class. Keep in mind that Pyret is a full-fledged language and CSCI0200 will use only a small (yet powerful) portion of it. With the exception of looking in the documentation for the names of build-in functions on lists (and maybe strings), everything you need to know about Pyret will be covered in lecture.
If you are searching the documentation for some new construct or way to do things, you're almost certainly off-track! The point isn't for you to learn how to write the constructs you already know (like for-loops) in Pyret. The point is to get you to write similar programs to what you've written before with a different set of constructs. Lecture will cover those constructs.
In Java, you are used to thinking of everything in terms of classes. In functional programming, we often work with primitive data (numbers, strings, booleans) or with lists of primitive data. None of these need a notion of classes.
The simplest functional program is a single number, like
Yes, this is a complete program. Go to Pyret, type this at the prompt, and you get the value of this expression (which is, not surprisingly, 3).
Here is a function definition. Number
is the type for all numbers (there is no distinction between integer, double, float, etc). doc
is the documentation string. Functions live in your file, they don't have to be put inside anything (like a class).
Note that there is no return
annotation. In functional programming, function bodies consist of single expressions, or perhaps some statements naming intermediate values followed by the expression to compute. The result of that expression is the result of the function. Here's an example with intermediate value names.
When you want to use a function, just call it with arguments, as you are used to doing in Java.
CSCI0200 will put a lot of emphasis on writing tests. A collection of tests go into a check
block. The two expressions on either side of the is
are compared for structural equality.
Lists are built in:
Lists are actually objects in Pyret. You can write expressions over them using either function-notation or method-notation:
You can get at elements of lists using first
to get the first element and rest
to get the list without the first element.
We don't have to write these list expressions in a check
block, but we did so here to show you the result of using the list operators in a way that would run in Pyret.
Here is the Pyret documentation with the collection of built-in (functions and methods) on lists (down the left sidebar).
One hallmark of functional programming is that functions can be passed as arguments and returned as the results of functions. Here's an example from the Pyret documentation that shows how to specify criteria for sorting a list:
Sometimes, when we pass a function as an argument, we don't want to bother naming it. Then, we can use a construct called lambda
that defines a function but without giving it a name:
A datatype describes the name and fields of a class (you are used to calling fields instance variables, but functional programming talks more in terms of structure of data and doesn't emphasize mutation).
Here are two examples: a cage
is like a method-less class with two fields. boa
and dillo
are two kinds of Animal
. Here, Animal
is a type name, a role that interfaces can play in Java. boa
and dillo
name constructors that return values of type Animal
. Pyret generates constructors and getters automatically from a data
description.
Here's how to write a function over multiple variants of the same type. Cases
is like a special/custom kind of if-statement that just serves to break down data types that have fields. Within the line for each constructor, the names in parens after the constructor (like nm, len, eats
in boa
) give local names to the field values within the value passed as ani
.
Often, we make lists of datatype values:
Sometimes, you just need an if-else
as part of the logic of your program:
The value of the if-expression is the value of the answer for the first test that returns true.
Pyret has some fun features that we won't use in 200, but that you could use to practice these early coding concepts if you'd like. Check out the images library.