Pyret for Racketeers
Daniel Patterson v2025-6-4
Definitions & Whitespace & Annotations
Pyret:
fun f(y :: Number) -> Number:
doc: "adds 10"
x = 10
x + y
end # not whitespace sensitive
# Annotations are checked
# errors at y :: Number
f("hello")
ISL:
; f : Integer -> Integer
; adds 10
(define (f y)
(local [(define x 10)]
(+ x y)))
; Signatures are comments:
; Errors at (+ x y)
(f "hello")
Testing
Pyret:
# Annotations optional
fun f(y):
doc: "adds 10"
y + 10
where:
f(10) is 20
f(20) is 30
end # or standalone:
check:
f(10) is 20
end
ISL:
; f : Integer -> Integer
; adds 10
(define (f y)
(+ y 10))
(check-expect (f 10) 20)
(check-expect (f 20) 30)
Operators & Numbers
Pyret:
10 + ((2 * 3) / 4)
# Mixed ops requires parens
(0.1 + 0.2) == 0.3 # true
# Exact numbers are default
ISL:
(+ 10 (/ (* 2 3) 4))
(= (+ 0.1 0.2) 0.3) ; #t
Literal Data
Pyret:
"hello"
true
# or false
[list: 1, 2, 3]
[set: 1, 2, 3]
[string-dict: "a", 1, "b", 2]
ISL:
"hello"
#true
; or #false or #t or #f
(list 1 2 3)
; not really available
; in BSL/ISL
Conditionals
Pyret:
if 1 == 2: 10
else if 2 == 3: 20
else: 30
end
ask:
| 1 == 2 then: 10
| 2 == 3 then: 20
| otherwise: 30
end
ISL:
(if (= 1 2) 10
(if (= 2 3) 20
30))
(cond [(= 1 2) 10]
[(= 2 3) 20]
[else 30])
For Loops
Pyret:
var sum = 0
for each(x from range(1,10)):
sum := sum + x
end
print(sum)
ISL:
; requires library
; not typical
Modules & Import
Pyret:
# All identifiers available:
include charts
# Identifiers as C.foo:
import charts as C
# Only subset included:
include from charts:
bar-chart, scatter-plot
end
ISL:
(require 2htdp/image)
; not available in
; BSL/ISL
Function Values
Pyret:
lam(x :: Boolean,
y :: Number,
z :: Number):
if x: y
else: z
end
end
ISL+:
; signatures typically
; not included, as they
; aren't checked
(lambda (x y z)
(if x y z))
Data Definitions
Pyret:
data BT:
| leaf(typ :: String)
| node(
value :: Number,
left :: BT,
right :: BT)
end
t = node(0,
node(1, leaf("a"),
leaf("b")),
leaf("a"))
cases(BT) t:
| leaf => 0
| node(v, l, r) => ...
end
ISL:
(define-struct leaf [typ])
(define-struct node [value
left
right])
; A BT is one of:
; - (make-leaf String)
; - (make-node Number BT BT)
(define t
(make-node 0
(make-node 1
(make-leaf "a")
(make-leaf "b"))
(make-leaf "a")))
(cond [(leaf? t) 0]
[(node? t)
(local
[(define v (node-value t))
(define l (node-left t))
(define r (node-right t))]
...)])
Mutation
Pyret:
var z = 10
fun f():
var x = 10
y = 10
# multiple statements
# require block
block:
x := 20 # can mutate outer scope
z := 30
# y := 20 -- error: y is not mutable
x + z
end
end
f() # returns 50
z # returns 30
ISL:
; cannot
; be done
; in BSL/ISL