-
-
Notifications
You must be signed in to change notification settings - Fork 11
Tutorial 3: Mathematics
Sven Nilsen edited this page Oct 18, 2015
·
16 revisions
In the previous tutorial you learned that paths can express function bodies.
We can also use paths to express mathematical ideas.
Let us do this with the following:
Adding two even numbers always results in an even number.
First we write a function that defines what we mean about "even":
even(number) -> bool
[:] (X) -> ( X % 2 == 0 )
Then we look at the function that adds two numbers:
add(number, number) -> number
Let us try some values:
add([:] 2, [:] 3) -> [:] 5
add([:] 2, [:] 4) -> [:] 6
add([:] 2, [:] 5) -> [:] 7
add([:] 3, [:] 4) -> [:] 7
add([:] 3, [:] 5) -> [:] 8
Notice that adding two even numbers always give an even number.
How do we express this with paths?
If we use even as a path, we get the following:
add([even] bool, [even] bool) -> [even] bool
We can also use this trick more than once!
If I give you a number, and can give back [even] [:] true or [even] [:] false.
So, we can write:
add([even] [:] true, [even] [:] true) -> [even] [:] true
We have expressed a mathematical idea!