> I'm new to the group, having just learned of this project.
Welcome, and thanks for trying out Pyret!
> I took a couple of Python functions I use in one of my courses and ported
> them to Pyret. It took about half an hour to dig through the docs to figure
> it out.
What could have been better in the docs? I know there's much room for
improvement there, but I'm wondering what required digging in
particular for you.
> data Pair:
> | pair(fst :: Number, snd :: Number)
> where:
> pair(5,6).fst is 5
> pair(5,6).snd is 6
> end
> [...]
Nice!
> Is there anything I could have done better? For instance, is there built-in
> support for tuple-like constructs without having to define a type?
There isn't a Pyret-defined Tuple or Pair type right now. We deferred
this decision to see what kinds of patterns would come up in our
classes' code this semester, rather than urging pair-based patterns
from the get-go. There are clear benefits to having tuples built-in
(the standard library can know about them and provide zip- and
unzip-like functions, for instance). Even if we add them, which we
very well might, I'm conflicted on having special syntax for them:
parentheses cause an awful lot of syntactic ambiguity, and there's a
lot of competition for the rest of simple ASCII.
Often, what I do in a situation like this is create an anonymous
object, which lets me name the fields of my tuple. For example, I
might not define Pair, and write instead:
# Computes a*x + b*y = gcd(a, b) and returns (x, y)
fun egcd(a :: Number, b :: Number) -> { x :: Number, y :: Number }:
if b == 0:
{ x: 1, y: 0 }
else:
q = (a / b).truncate() # Integer division
r = a.modulo(b)
p = egcd(b, r) # (s, t) = egcd(b, r)
{ x : p.y, y: p.x - (q * p.y) } # (t, s - (q * t))
end
where:
egcd(7, 21) is { x: 1, y: 0 }
egcd(13, 40) is { x: -3, y: 1 }
end
This way, I get more descriptive names than fst and snd, or left and
right. There are obvious tradeoffs: the standard library won't be
able to unzip a list of these egcd results for me, for example.
> Is there a way to define my own operators? In SML, for example, I can define
> a function called "mod" and then turn it into a binary operator so that "5
> mod 2" is the same as mod(5,2) which in turn becomes 5.modulo(2).
Pyret has a fixed set of operators right now, and there aren't plans
to add user-defined ones, though we're open to hearing about use cases
for them. The closest thing Pyret has to programmer-defined infix is
the ^ operator. This lets you use functions in a position that
visually resembles a method call. For example:
check:
13^egcd(40) is { x: -3, y: 1 }
# v1^f(v2) is equivalent to f(v1, v2)
end
This is a slightly different use case than you might have in mind, though.
Cheers!
Joe P.