Recursion
How do we put pattern matching on lists to use? To do this we must rely on a concept called "recursion", which basically means a function that refers to itself. The old programmer joke is that the definition of
"recursion" is
"see recursion".
This can be a tricky subject so don't worry if you don't get it, you don't need it for 99% of what you can do with LastCalc.
Let's say we want a function to double every number in a list, and you had to write it without using the apply ... to ... function we talked about previously.
Firstly, what would such a function return if given an empty list? Obviously if there is nothing in the list to double, then it should just return an empty list:
Here we're using a variation of the list pattern matching that we haven't seen before, [] will only ever bind to an empty list. In the terminology of recursion, this is our "base case".
So great, we can now double every number in an empty list - not very useful. Here is how we define a function to double every item in a non-empty list:
double [ F ... R ] = [ F * 2 ... double R ]
✓

What's going on here? We take a list and break it into
F, the first item, and
R, the remaining items. Then on the right of the = we create a new list, with first element
F*2, and the rest of the elements are then doubled
using this very function! This is the essence of recursion.
You might then ask - "If this function calls itself, won't it keep going forever"? Fortunately not, because every time it is called it is with a shorter version of the list, until eventually it is called with an empty list, and our "base case" takes care of it. Let's try it out:
double [ 1 , 2 , 3 ]
=
[ 2, 4, 6]
Important note: While recursion is very flexible, and can be used to do something similar to the
apply,
filter, and the
fold functions mentioned previously, it isn't very efficient. It is much better to use one of these other list manipulation functions if possible.
Pulling data from the web
The Internet is full of data, but often it isn't in a very convenient format - and of course the
most convenient format would be as a value you could calculate with in LastCalc!
Well, fortunately LastCalc allows you to pull data from almost any webpage into LastCalc, and use it in subsequent calculations!
We will demonstrate how you can grab the current
Bitcoin to US Dollar exchange rate from a website, and then use it to teach LastCalc how to convert from Bitcoins to and from various other currencies.
The first stage is to find a webpage that contains the data we need,
bitcoincharts.com will do nicely, so let's grab it and store the result in a variable:
page = retrieve "http://bitcoincharts.com/"
=
<html>Bitcoin Charts ... </html>
Note that LastCalc doesn't display the entire retrieved page because it would be way too big. Next, we need to isolate the HTML elements in the page that we're interested in:
elements = select "td.right" from page
=
[ too big (1133 chars) ]
We use a
CSS selector to isolate specific elements in the page. Since more than one element matches the selector we get a list (note how LastCalc doesn't show the whole list because it is very long), so let's grab the first one:
firstElement = get 1 st from elements
=
<td class="right">126.5000</td>
Now let's grab the text of the element:
valAsText = get text from firstElement
=
"126.5000"
LastCalc is treating this text as a string, but we'd like LastCalc to interpret it so that it treats it as a number:
bitcoinInUsd = interpret valAsText
=
126.5
Now we have the value of Bitcoins in US Dollars as a number! Let's teach LastCalc how to convert to and from Bitcoins:
X Currency in btc = ( ( ( X Currency in usd ) as number ) / bitcoinInUsd ) btc
✓
(Don't forget that
Parameters in functions
must be Capitalized!).
3 euros in btc
=
0.0307BTC
It works! Let's teach LastCalc how to convert from Bitcoins now:
X btc in Currency = ( ( X * bitcoinInUsd ) usd ) in Currency
✓
It works too! Now LastCalc will let you calculate with almost any information you can find on the Internet!