Wipple is what's called a functional programming language. That means functions are used everywhere in Wipple code!
You probably remember from math that a function f(x) = 3x takes an input (x) and returns an output (3x). It works the same way in Wipple! You just write it a little differently:
f : x -> 3 * x
The arrow is pronounced "becomes", so the code above is read as "f is when x becomes 3 times x". The process of x becoming 3 times x is a function!
Let's create a function to add two numbers,
add : a -> b -> a + b
Notice that functions only accept a single input. If you want multiple inputs, you need multiple functions (arrows)! The above code can be rewritten like so:
add : a -> (b -> a + b)
Now how do we use functions? It's simple — just write the name of the function, followed by its inputs:
add 1 2
And of course, we need to
show the answer! (Guess what,
show is a function too!)
show (add 1 2)
If you're familiar with programming already, you might ask what happens if we only provide a single input. In Wipple, you just get back the inner function!
a is 1. What's
b? We give
b a value next:
(add 1) 2
Notice this is the same code as before —
add 1 2! Now for the super cool part — we can give
add 1 a name, and use it as its own function:
increment : add 1 show (increment 42)
You should see
43 on the screen!
If you're confused, try hovering over parts of the program, and the playground will tell you what kind of code you're hovering over. For example, hovering over
increment will show
increment :: Number -> Number, indicating that it accepts a number and returns a number. Hovering will be explained in more detail in the next section!