Skip to content

Instantly share code, notes, and snippets.

@mflatt
Last active September 14, 2019 13:27
Show Gist options
  • Save mflatt/99f31749ad4d02af918714f7d845b895 to your computer and use it in GitHub Desktop.
Save mflatt/99f31749ad4d02af918714f7d845b895 to your computer and use it in GitHub Desktop.
colons in place of arrows
let (x = 1,
y = 2):
x+y
// I'd be tempted to write it like this, though
let (x = 1,
y = 2)
: x+y
define pi = 3.14
define fib(n):
log_error("fib called")
cond | (n = 0): 0
| (n = 1): 1
| else#: fib(n-1) + fib(n-2)
define
| fib(0): 0
| fib(1): 1
| fib(n): fib(n-1) + fib(n-2)
define fib = {
lambda (n):
cond | (n = 0): 0
| (n = 1): 1
| else#: fib(n-1) + fib(n-2)
}
define fib(n):
match n
| 0: 0
| 1: 1
| n: fib(n-1) + fib(n-2)
define fib(n):
match n
| 0:
0
| 1:
1
| n:
fib(n-1) + fib(n-2)
// This one should fail to parse, because the `+` is
// split across two groups
define fib(n):
match n
| 0
: 0
| 1
: 1
| n
: fib(n-1)
+ fib(n-2)
// Parenthesese allow an operator like `+` to continue the line
define fib(n):
match n
| 0
: 0
| 1
: 1
| n
: (fib(n-1)
+ fib(n-2))
define fib(n):
match n | 0: 0 | 1: 1
| n:
fib(n-1) + fib(n-2)
// Misleadingly indented...
define make_adder(n):
lambda (m):
printf("adding to ~a\n", m)
m + n
// Reindented
define make_adder(n):
lambda (m):
printf("adding to ~a\n", m)
m + n
// Add parens to get somethign different (correctly indented):
define make_adder(n):
(lambda (m):
printf("adding to ~a\n", m))
m + n
struct posn(x, y)
struct color_posn(col)
& extends# posn
& mutable#
struct posn(x, y)
& property# prop_equal_and_hash \
(let (hc = (lambda (a: posn, hc):
hc(a.x) + hc(a.y))):
[lambda (a: posn, b: posn, eql):
(eql(a.x, b.x)
&& eql(a.y, b.y)),
hc,
hc])
// Would it be a good idea for `define` to allow
// `=` in place of `:`, so this would work?
define fourth(n: integer):
define m = n*n
define v = m*m
printf("~a^4 = ~a\n", n, v)
v
define exp(n: integer, _base base = 2.718281828459045):
if (n = 1)
| base
| base * exp(n-1, _base base)
define positive_p(n): (if (n > 0) | #true | #false)
define go(): {
define helper(n):
list(n, n)
define more(m):
if (m == 0)
| "done"
| more(m - 1)
helper(more(9))
}
define approx(x):
match x
| something(v): printf("got it\n")
v
| nothing: 0
define approx(x):
match x
| something(v):
printf("got it\n")
v
| nothing: 0
// Nested "=>"s work out to bind more tightly than "|",
// in a sense:
define approx_thunk(x):
match x
| something(v): lambda ():
v
| nothing: lambda (): 0
// And we're happy to resort to parentheses to handle more
// general cases:
define approx_thunk(x):
match x
| something(v): (lambda
| (): v
| (n): v + n)
| nothing: (lambda
| (): 0
| (n): n)
define curried = {
lambda (x):
lambda (y):
lambda (z):
list(x, y, z)
}
define dictionary = ("foo" : 17,
"bar" : "string",
"baz" : #true)
define f(x_something,
y_something_else,
z_also_long_name):
5
define show_all(l):
for (x = in_list(l))
& print(x)
newline()
define show_zip(l, l2):
for (x = in_list(l),
x2 = in_list(l2))
& print(x)
print_string(" ")
print(y)
newline()
define show_combos_not_same(l, l2):
for (x = in_list(l))
& (x2 = in_list(l2))
& when# !is_equal(x, x2):
print(x)
print_string(" ")
print(y)
newline()
define map(f, l):
for list
& (x = in_list(l)):
f(x)
define partition(l, pred)
for fold (yes = empty,
no = empty,
result# (reverse(yes), reverse(no)))
& ( x = in_list(l)):
(if pred(x)
| (cons(x, yes), no)
| (yes, cons(x, no)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment