clean up examples

Closes #325.
This commit is contained in:
Brent Yorgey 2022-06-11 15:15:07 -05:00
parent d55c6e8bf6
commit 54e5e35bbe
8 changed files with 41 additions and 48 deletions

View File

@ -1,3 +1,7 @@
// Quickly harvesting an entire forest in parallel using breadth-first
// search, with robots spawning more robots. Fun, though not very practical
// in classic mode.
def repeat : int -> cmd () -> cmd () = \n.\c.
if (n == 0)
{}

View File

@ -1,3 +1,6 @@
// A "cat" that wanders around randomly. Shows off use of the
// 'random' command.
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

13
example/dfs.sw Normal file
View File

@ -0,0 +1,13 @@
def ifC : forall a. cmd bool -> {cmd a} -> {cmd a} -> cmd a =
\test. \thn. \els. b <- test; if b thn els end
// Recursive DFS to harvest a contiguous forest
def dfs : cmd () =
ifC (ishere "tree") {
grab;
turn west;
ifC blocked {} {move; dfs; turn east; move};
turn north;
ifC blocked {} {move; dfs; turn south; move};
} {}
end

View File

@ -1,7 +1,13 @@
let repeat : int -> cmd () -> cmd () = \n.\c.
// Defining simple recursive functions.
def repeat : int -> cmd () -> cmd () = \n.\c.
if (n == 0) {} {c ; repeat (n-1) c}
in let fact : int -> int = \n:int.
end
def fact : int -> int = \n:int.
if (n == 0)
{1}
{n * fact (n-1)}
in move; move; repeat (fact 4) move
end
def gofar = repeat (fact 4) move end

View File

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

View File

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

View File

@ -1,7 +1,12 @@
let forever : cmd () -> cmd () =
\c. c ; forever c in
forever (
b <- random 2;
turn (if (b == 0) {left} {right});
move
)
def forever : {cmd ()} -> cmd () =
\c. force c ; forever c
end
// Wander randomly forever.
def wander : cmd () =
forever {
b <- random 2;
turn (if (b == 0) {left} {right});
move
}
end

View File

@ -1,19 +0,0 @@
let rep : int -> cmd () -> cmd () = \n. \c.
if (n == 0)
{}
{ c; rep (n-1) c }
in
turn east;
rep 50 (
grab; move;
turn left;
grab; move;
turn right;
p <- whereami;
let y = snd p in
if (y >= 10) {
turn right
} {
if (y <= 0) { turn left } {}
}
)