check in some random examples

This commit is contained in:
Brent Yorgey 2021-08-29 16:48:27 -05:00
parent 9f23dd0295
commit 6d2b678ff7
6 changed files with 71 additions and 0 deletions

21
example/cat.sw Normal file
View File

@ -0,0 +1,21 @@
let forever : cmd () -> cmd () = \c. c ; forever c in
let repeat : int -> cmd () -> cmd () =
\n. \c. if (n == 0) {} {c ; repeat (n-1) c} in
let randdir : cmd dir = {
d <- random 4;
return (
if (d == 0) north
(if (d == 1) east
(if (d == 2) south west)))
} in
forever {
n <- random 20;
repeat (10 + n) wait;
d <- randdir;
turn d;
dist <- random 10;
repeat dist move;
r <- random 5;
if (r == 0) { say "meow" } {}
}

7
example/fact.sw Normal file
View File

@ -0,0 +1,7 @@
let repeat : int -> cmd () -> cmd () = \n.\c.
if (n == 0) {} (c ; repeat (n-1) c)
in let fact : int -> int = \n:int.
if (n == 0)
1
(n * fact (n-1))
in move; move; repeat (fact 4) move

7
example/rep.sw Normal file
View File

@ -0,0 +1,7 @@
let rep : int -> cmd () -> cmd () =
\n.\c.
if (n == 0)
{}
{c ; rep (n-1) c}
in
rep 10 move

12
example/square.sw Normal file
View File

@ -0,0 +1,12 @@
let rep : int -> cmd () -> cmd () =
\n.\c.
if (n == 0)
{}
{c ; rep (n-1) c}
in
rep 4 {
rep 10 move;
turn left;
build "sq" (run("square.sw"));
return ()
}

7
example/wander.sw Normal file
View File

@ -0,0 +1,7 @@
let forever : cmd () -> cmd () =
\c. c ; forever c in
forever {
b <- random 2;
turn (if (b == 0) left right);
move
}

17
example/zigzag.sw Normal file
View File

@ -0,0 +1,17 @@
let rep : int -> cmd () -> cmd () = \n. \c.
if (n == 0)
{}
{ c; rep (n-1) c }
in
rep 50 {
harvest; move;
turn left;
harvest; move;
turn right;
y <- getY;
if (y >= 10) {
turn right
} {
if (y <= 0) { turn left } {}
}
}