This lesson introduces function expressions,
and &-patterns.
built-in names
But first, a list of the simple builtins.
show_boolean : Boolean -> String
show_void : True -> String
plus : Integer -> (Integer -> Integer)
minus : Integer -> (Integer -> Integer)
times : Integer -> (Integer -> Integer)
concat : String -> (String -> String)
substring : Integer -> (Integer -> (String -> String))
string_length : String -> Integer
equals : Integer -> (Integer -> Boolean)
and : Boolean -> (Boolean -> Boolean)
or : Boolean -> (Boolean -> Boolean)
not : Boolean -> Boolean
false : Boolean
true : Boolean
void : True
show_integer : Integer -> String
show_string : String -> String
In addition to the builtins names, there are integer and
string literals. 1,2,3,"a","ab","abc" etc.
put
We used trace last time because it will
display any type for debugging purposes.
put displays a string. It is better-defined
than trace, so we won't use trace in any more examples.
We can convert things to strings if necessary using
functions like show_integer .
For example,
("abc" put)
((false show_boolean) put)
((10 show_integer) put)
Note that each of the above 3 lines is a valid program.
function expressions
In many languages such as Lisp, Scheme,
Python, and Haskell these are called lambda expressions, and
Ambidexter uses the same syntax as Haskell.
((5 (\x->x 1 plus)) put)
((5 (\x->(1 x minus))) put)
If you want to name anything, you may use it as
an argument to a function.
(1 \one ->
(\x->x one plus) \increment ->
((one increment) show_integer) put)
patterns
In place of a function parameter name, you can put a pattern.
Ambidexter has several kinds of patterns. The easiest one is
the "and" pattern. Recall that in the previous lesson we didn't actually do anything useful with an "and" term.
An "and" pattern is how you get at the parts of an "and" value.
We name its parts, like this:
(& part_one & part_two & -> ...)
Use whatever names you like, or omit naming something you don't
need a name for. The following function gets the first part of
an "and" pair.
(& first &_& -> first)
There's another simple pattern, for the True type.
() (\->"OK")
It's a pattern consisting of nothing. If you prefer to see the
pattern, you can use parentheses.
() (\()->"OK")
Notice that True the type and true the value are from two
different metaphors based on truth. The type of true and
false is Boolean , unrelated to True and False .
Here's an example program that concatenates and displays the second members of two tuples.
exercises
- Demonstrate the substring function by writing a function
that takes a string and returns its last two characters.
- Use pattern matching to write a program which contains the
expression
(& 1 & 987654321 &) and which displays
Your number is 987654321.
- Which of the following can be done in the subset of
Ambidexter covered in today's lesson?
- a function which takes a long string and a short
string, and which replaces characters of the long
string with characters of the short string, beginning
with the 10th character of the long string,
returning the modified long string
- a function which takes a string and an integer n,
and which returns a string consisting of n
copies of the string concatenated together
critique
This lesson should mention the connection with proofs.
|