LastCalc

Show Help

Choose a section:
# Getting Started

## What is LastCalc?

LastCalc is a powerful but easy to use web-based calculator. Think of the lastCalc page like a notepad for calculations, you start on the top line and work your way down. The good news is that, unlike with a notepad and pencil, LastCalc does the hard work of figuring out the answers for you!
Another great thing about LastCalc is that you can go back and edit previous lines just by clicking on them.
## Simple math

LastCalc lets you do basic math, note that
***** means multiply and
**/** means divide, for example:
You type the part of the question before the
## Parentheses

LastCalc will multiply and divide numbers before it adds or subtracts them, but you can force it to calculate part of an answer first by putting it in parentheses:

## Conversions

LastCalc understands lots of ways to measure things, weights, distances, durations, and so on. LastCalc will let you convert between different units, some examples:
Or even currency conversions (conversion rates updated hourly from
openexchangerates.org):
# Variables and Functions

## Assigning a result to a variable

What if you would like to use the result of one calculation in a later calculation? LastCalc lets you do this:
Note how "homeValue" is now colored to indicate that it is a "variable". This means that wherever LastCalc sees "homeValue", it will replace it with the outcome of this calculation. So now, we can calculate:
You can define as many variables as you like!
## Editing a previous line

What if you assign a result to a variable, and do some subsequent calculations with it, but then you want to edit the first line so that a different result is assigned to that variable? No problem! Just go back, edit it, and all subsequent lines will be updated immediately, just like with a spreadsheet.
## Functions

What if you find yourself doing the same calculation over and over again? Wouldn't it be nice if LastCalc could learn to do that calculation for you? It can! Imagine that you are a fitness trainer and you frequently need to calculate someone's "body mass index", which is based on their weight and their height. For example, if your weight in kilograms is 90, and your height in meters is 1.8, your BMI is calculated as follows:
So how do we teach LastCalc how to do this for us? It's easy:
Let's take a closer look at this. Defining a function is similar to assigning a result to a variable, except for the
Height and
Weight - what are those?

We call these**parameters**, think of them as placeholders, into which LastCalc can slot any value (we call this "binding"). When you are defining a function, any word beginning with a Capital Letter will become a parameter. It is important to remember that we can choose any names we want for parameters, LastCalc doesn't care but it may help us remember what is what. So we could write:
This will do the
*exact same thing*, but it is less clear what it is doing.
Just don't forget that
**parameters must begin with a capital letter.**
So now, let's try using our new BMI calculator to do the same calculation we did before:
Yay, as we would expect we got the same result as before, but now it is much easier to calculate someone's BMI. In this case, the
Height parameter was "bound" to 1.8, and the
Weight parameter was "bound" to 90.
## An observation

You may have noticed that assigning a value to a variable is quite similar to creating a function. In actual fact, a variable is just a special type of function that consists of a single word and has no parameters. So from LastCalc's perspective, a variable assignment is just a boring type of function.
# Simple Lists

## Creating a list and retrieving items

People frequently deal with lists of things, and so LastCalc lets you have lists of things too, and manipulate those lists. Let's create a list of distances and assign them to a variable:
We can retrieve items from the list:
This does the same thing:
"Huh?" I hear you say? Why does "get 1" retrieve the second item from the list? The reason is that lists are "indexed" starting at 0, so to get the first item you use "get 0 from ...". But, if you use 1st, 2nd, 3rd, etc then it takes care of this for you.
You can see the trick here:
Clever eh? 3rd becomes 2 because that is what will get us the 3rd item in the list. I hope that's not too confusing.
## Math with lists

Getting things from lists isn't too exciting, but you can do math with them too:
And something a little more ambitious:
Neat eh? We can use units in list calculations.
# Basic Programming

## Pattern Matching

Here we're going to talk about one of the lowest-level features in LastCalc. This can be used as a building-block for many of the features described in the previous section. If you find this confusing please don't worry - you don't need to understand this to use 99% of LastCalc's functionality.

Consider this:
(Don't forget that
Parameters in functions
**must** be Capitalized!).
What happened here? This new structure to the left of the = in the function definition binds to a list, and more specifically it binds the parameter
First to the first item in the list, and the parameter
Rest to the remaining items in the list. Remember, the names "First" and "Rest" are unimportant, I could just as easily have called them "X" and "Y". We don't use
Rest at all, but
First is returned from our newly defined function.

You can also bind to more than just the first item in a list, here we define a function that takes a list and returns the sum of its first two items (which we assume are numbers):
And lastly, let's define a similar function that strips off the first two elements:
One last thing: for the addFirstTwo or the stripFirstTwo functions to work, the list must contain at least two items. If they don't the function won't have any effect:
This is actually quite useful as it allows us to define multiple versions of a function to deal with different situations. We'll use this in the next section.
## Booleans and Conditions

LastCalc understands boolean operations:
It also understands comparisons:
And you can use the outcome of a boolean operation to make decisions:
Now let's use this in a function, reusing the BMI function we defined earlier:
(Don't forget that
Parameters in functions
**must** be Capitalized!).
# Programming with Lists

## Applying a function to the elements of a list

Imagine you wanted to double every number in a list, how would you do that? You might hope that we had created a convenient "double list" function, but then we'd spend all of our time writing functions to cover every conceivable circumstance. Fortunately for you (and our fingers!) LastCalc provides a much more flexible solution to this problem. Check this out:
Woah, how did that happen? We created a function that takes a number and doubles it, and then we apply it to every number in the list. Note that this function is
*private* to this line, it can't be used anywhere else. Programmers call this a "first-class function", but you can call it "coolness" ;-)
This can be a bit confusing, so let's try another one - we're going to convert a bunch of distances to miles:
Ok, one more, we're going to convert a list of lists to a list containing the sizes of each sub-list:
(Don't forget that
Parameters in functions
**must** be Capitalized!).
## Using a function to filter a list

What if you wanted to filter out particular elements in a list? You can do this in a similar way to applying a function as described in the previous lesson. Here we filter out any element in a list below 2:
## "Folding" a function across a list

This is a trickier one, although it will be familiar to fans of functional programming. You now know how to apply a function to a list, and how to filter a function with a list, but both of these look at each item in the list separately, what if we want to combine them in some way?
For example, what if you wanted to multiply all of the numbers in a list together? Here's how to do it:
So what is going on here? The
**fold** function requires a function that takes two parameters. The first parameter
P is bound to the output of the same function being applied to the previous element of the list, or if it's the first element, it's given the value after the
**with**, in this case 1.
The second parameter
E is bound to the next element in the list.
Hopefully you can now see what is going on,
P maintains the running product as we cycle through all the elements in the list, multiplying each one by
E to produce the next product, and the value produced by the function being applied to the last element is the result of the
**fold** function.
Here is one last example, this is actually how the "size of" function is written within LastCalc:
# Maps

## Basics

Lists are very useful, but they can't do everything, they only allow you to look up items based on their position in the list. Maps let you store items and look them up by anything you want. In this simple example we want to store 3 friend's phone numbers so that we can look them up based on our friend's name:
And now let's look up Fred's number:
## Pattern Matching

As with lists, Maps can also be used in function definitions using pattern matching, here we define a function that retrieves Fred's number:
# Advanced Programming

## 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:
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:
**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.
## Sharing your LastCalc functions

So you've created some really useful functions and you'd like to share them with your friends? Not a problem. You can "import" pre-defined functions from any URL using the
**import** command. The URL must point to one or more function definitions in plaintext, Here is a very simple example:
http://pastebin.com/raw.php?i=kgcFCpNk. To use these functions in your worksheet just type:
And then you can use them:

- Getting Started
- Variables and Functions
- Simple Lists
- Basic Programming
- Programming with Lists
- Maps
- Advanced Programming

18 / 2 + 4 * 2

=

17

=

, and when you hit return the answer will appear after it.
18 / ( 2 + 4 ) * 2

=

6

3 weeks in days

=

21 days

15 km in miles

=

9.32056788356001 miles

$ 23 in euros

=

€18.9006

homeValue = $ 80000 + 10 %

=

US$88,000

homeValue + $ 2000

=

US$90,000

90 / ( 1.8 * 1.8 )

=

27.777777777777775

bmi Height Weight = Weight / ( Height * Height )

✓

We call these

bmi X Y = Y / ( X * X )

✓

bmi 1.8 90

=

27.777777777777775

trips = [ 2 , 30 , 200 ]

=

[ 2, 30, 200]

get 2 nd from trips

=

30

get 1 from trips

=

30

3 rd

=

2

sum of trips

=

232

size of trips

=

3

average of trips

=

77.33333333333333

average of [ 2 miles , 3000 meters , 5 km ]

=

2.3236565126328896 miles

Consider this:

first in [ First ... Rest ] = First

✓

first in [ 5 , 4 , 3 , 2 ]

=

5

You can also bind to more than just the first item in a list, here we define a function that takes a list and returns the sum of its first two items (which we assume are numbers):

addFirstTwo [ ANum , BNum ... R ] = ANum + BNum

✓

addFirstTwo [ 1 , 2 , 3 , 4 , 5 ]

=

3

stripFirstTwo [ ANum , BNum ... R ] = R

✓

stripFirstTwo [ 1 , 2 , 3 , 4 , 5 ]

=

[ 3, 4, 5]

stripFirstTwo [ 1 ]

=

stripFirstTwo [ 1]

true and true

=

true

false or ( true xor false )

=

true

not true

=

false

3 == 3

=

true

3 != 4

=

true

3 < 4

=

true

if 3 != 4 then 15 else 17

=

15

if 3 == 4 then 15 else 17

=

17

newbmi H W = if ( bmi H W > 20 ) then overweight else ok

✓

newbmi 2 70

=

ok

newbmi 2 90

=

overweight

apply ( Num = Num * 2 ) to [ 1 , 2 , 3 , 4 , 5 ]

=

[ 2, 4, 6, 8, 10]

apply ( Distance = Distance in miles ) to [ 30000 feet , 2 km ]

=

[ 5.681818181818182 miles, 1.242742384474668 miles]

apply ( List = size of List ) to [ [ 1 ] , [ 1 , 1 ] , [ 1 ] ]

=

[ 1, 2, 1]

filter [ 1 , 2 , 3 , 4 ] with ( X = X > 2 )

=

[ 3, 4]

product of List = fold ( P E = P * E ) over List with 1

✓

product of [ 1 , 2 , 3 , 4 , 5 ]

=

120

size of List = fold ( X Y = X + 1 ) over List with 0

✓

phoneBook = { "joe" : 5552727 , "fred" : 5558764 , "bill" : 5552319 }

=

{ "joe" : 5552727, "fred" : 5558764, "bill" : 5552319}

get "fred" from phoneBook

=

5558764

fredNumber { "fred" : X ... Rest } = X

✓

fredNumber phoneBook

=

5558764

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:

double [ ] = [ ]

✓

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 ]

✓

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]

import "http://pastebin.com/raw.php?i=kgcFCpNk"

✓

mouse + cat

=

28