fun to-celsius(f): (f - 32) * (5 / 9) end for each(str from [list: "Ahoy", "world!"]): print(str) end
Pyret has Python-inspired syntax for functions, lists, and operators.
Iteration constructs are designed to be evocative of those in other languages.
Pyret makes testing a natural part of the programming process. Functions can
end in a where:
clause that holds unit tests for the
function. These assertions are checked dynamically.
fun sum(l): cases (List) l: | empty => 0 | link(first, rest) => first + sum(rest) end where: sum([list: ]) is 0 sum([list: 1, 2, 3]) is 6 end
data BinTree: | leaf | node(value, left :: BinTree, right :: BinTree) end
Pyret allows for concise, expressive, recursive data declarations. Type
annotations are optional and can be added incrementally, to serve a
variety of pedagogic styles and curricular needs.
In addition to where:
blocks, which are attached to
individual definitions (and hence usually contain unit tests), you
can also write check:
blocks at the top level, for
general program testing. Both are scope delimiters. Therefore, you
can use them to write local definitions that are useful for testing
but not relevant to the program at large.
check: fun get-status(url): request({ url: url, verb: "get", params: [list: ] }).status-code end get-status("http://google.com/") is 200 end
Some thoughts on syntax
We believe indentation is critical for readable code, but we don’t
want the whitespace of the program to determine its meaning. Rather,
the meaning of the program should determine its indentation
structure. Indentation becomes just another context-sensitive rule.
Unambiguous syntax (the reason for explicit end
delimiters)
means you can copy-and-paste code from email or the Web, and its meaning won’t
change. Your IDE can help you reindent code without worrying that doing so will
change the meaning of the program.
We haven’t yet decided on the indentation rules because we want to see
the language in use for a while before we codify these.
Real tests need to accomodate more than equality tests. Pyret supports these
generally with the satisfies
form, which can be used to check
satisfaction of an arbitrary predicate.
eps = 0.001 fun d-dx(f): doc: "Approximate the derivative of f" lam(x): (f(x + eps) - f(x)) / eps end where: