mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-06 06:48:48 +00:00
56 lines
No EOL
1.5 KiB
Text
56 lines
No EOL
1.5 KiB
Text
fun show (x) {
|
|
fun show (level, x) {
|
|
for var i; i := 0, i<level, i := i + 1 do
|
|
printf (" ")
|
|
od;
|
|
|
|
case x of
|
|
#val -> printf ("integer %d\n", x)
|
|
| #array -> printf ("array\n")
|
|
| #str -> printf ("string\n")
|
|
| #sexp -> printf ("S-expression\n")
|
|
| #fun -> printf ("closure 0x%x\n", x[0])
|
|
esac;
|
|
|
|
case x of
|
|
#val -> skip
|
|
| _ -> for var i; i := case x of #fun -> 1 | _ -> 0 esac, i < x.length, i := i + 1 do
|
|
show (level + 2, x[i])
|
|
od
|
|
esac
|
|
}
|
|
|
|
show (0, x)
|
|
}
|
|
|
|
printf ("Pattern matching is a better way to deal with the structure of a value:\n");
|
|
show (3);
|
|
show ([3]);
|
|
show ({1, 2});
|
|
show (["abc", [1, fun (x) {x+1}], {3, 4}]);
|
|
|
|
printf ("But the main designation of pattern matching is to deal with S-expressions.\n");
|
|
printf ("You can discriminate the scrutinee on the tag:\n");
|
|
|
|
case E of
|
|
A -> printf ("Nope.\n")
|
|
| B -> printf ("Nope.\n")
|
|
| C -> printf ("Nope.\n")
|
|
| D -> printf ("Nope.\n")
|
|
| E -> printf ("Yes, this is E.\n")
|
|
esac;
|
|
|
|
printf ("You can discriminate the scrutinee on the number of arguments as well:\n");
|
|
|
|
case A of
|
|
A (_) -> printf ("Nope.\n")
|
|
| A (_, _) -> printf ("Nope.\n")
|
|
| A -> printf ("This is A with no subvalues.\n")
|
|
| A (_, _, _) -> printf ("Nope.\n")
|
|
esac;
|
|
|
|
printf ("You may also use composite patterns:\n");
|
|
|
|
case A (["x", "y", "z"], {1, 2, 3}) of
|
|
A (a@[_, _, z], _ : tl) -> printf ("a=%s, z=%s, tl=%s\n", a.string, z.string, tl.string)
|
|
esac |