From 330f4747099c85cb8816a131e2063069ee68e9a9 Mon Sep 17 00:00:00 2001 From: darkf Date: Thu, 24 Oct 2013 01:28:14 -0700 Subject: [PATCH] add cons example --- examples/cons.lamb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 examples/cons.lamb diff --git a/examples/cons.lamb b/examples/cons.lamb new file mode 100644 index 0000000..b3e6ad2 --- /dev/null +++ b/examples/cons.lamb @@ -0,0 +1,24 @@ +-- Cons patterns divide a list or string value into a head (the first element) and a tail (the rest of the elements). + +-- We can make an identity for lists or strings like so: +consid(hd::tl) -> hd::tl. +-- it deconstructs and then immediately reconstructs the value. +print(consid([1, 2, 3])). -- [1, 2, 3] + + +-- Cons patterns are polymorphic, depending on the value they're matching. + +-- "x"::xs will match both "x" and ["x"]. Let us demonstrate: + +f("x"::xs) -> xs. + +print(f("xy")). -- "y" +print(f(["x", "y"])). -- ["y"] + +-- the same works for chaining cons patterns, so: +-- "x"::"y"::tl will match "xy" and ["x", "y"] + +g("x"::"y"::tl) -> tl. + +print(g("xyz")). -- "z" +print(g(["x", "y", "z"])). -- ["z"] \ No newline at end of file