add cons example

This commit is contained in:
darkf 2013-10-24 01:28:14 -07:00
parent 604b2e3148
commit 330f474709
1 changed files with 24 additions and 0 deletions

24
examples/cons.lamb Normal file
View File

@ -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"]